@@ -36,6 +36,7 @@ import {
3636 type ProviderInstanceId ,
3737 type RunId ,
3838 type RuntimeRequestId ,
39+ type ThreadId ,
3940} from "@t3tools/contracts" ;
4041import type * as Cause from "effect/Cause" ;
4142import * as DateTime from "effect/DateTime" ;
@@ -3089,16 +3090,29 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
30893090 * session manager before `openSession`, so the config is normally ready.
30903091 */
30913092 const registeredMcp = new Map <
3092- string ,
3093+ ThreadId ,
30933094 { readonly name : string ; readonly directory : string ; readonly credential : string }
30943095 > ( ) ;
3095- const mcpSession = yield * mcpSessions . read ( input . threadId ) ;
3096- if ( mcpSession !== undefined && ! connection . external ) {
3097- const name = yield * mcpServerNameFor ( input . threadId ) ;
3096+ /**
3097+ * Registers the thread's MCP server in a specific directory, before that
3098+ * thread's native session exists. OpenCode MCP registrations are
3099+ * directory-scoped, and a session's tool catalog is built from the
3100+ * directory the session is created in, so `directory` must be the same
3101+ * runtime directory the session will be created or forked in — not the
3102+ * server launch directory.
3103+ */
3104+ const preRegisterMcp = Effect . fn ( "OpenCode2Adapter.preRegisterMcp" ) ( function * (
3105+ threadId : ThreadId ,
3106+ directory : string ,
3107+ ) {
3108+ if ( connection . external || registeredMcp . has ( threadId ) ) return ;
3109+ const mcpSession = yield * mcpSessions . read ( threadId ) ;
3110+ if ( mcpSession === undefined ) return ;
3111+ const name = yield * mcpServerNameFor ( threadId ) ;
30983112 const added = yield * client . mcp
30993113 . add ( {
31003114 server : name ,
3101- location : { directory : host . paths . cwd } ,
3115+ location : { directory } ,
31023116 config : new Mcp . RemoteConfig ( {
31033117 type : "remote" ,
31043118 url : mcpSession . endpoint ,
@@ -3116,13 +3130,18 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
31163130 ) ,
31173131 ) ;
31183132 if ( added ) {
3119- registeredMcp . set ( input . threadId , {
3133+ registeredMcp . set ( threadId , {
31203134 name,
3121- directory : host . paths . cwd ,
3135+ directory,
31223136 credential : mcpSession . authorizationHeader ,
31233137 } ) ;
31243138 }
3125- }
3139+ } ) ;
3140+
3141+ // The session's first thread is prepared by the session manager with this
3142+ // same runtime policy before `openSession`, so register in the directory
3143+ // its native session will be created in.
3144+ yield * preRegisterMcp ( input . threadId , input . runtimePolicy . cwd ?? host . paths . cwd ) ;
31263145
31273146 yield * Effect . addFinalizer ( ( ) =>
31283147 Effect . forEach (
@@ -3150,7 +3169,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
31503169 : { name, directory, credential : mcpSession . authorizationHeader } ;
31513170 // A server registered before the thread's first turn lives in
31523171 // `registeredMcp`: no ThreadState existed yet. Removal decisions must see
3153- // it, and once this turn records it on the state, the map copy goes away.
3172+ // it, and ownership moves to the state only once this turn's add
3173+ // succeeds — a failed or timed-out re-add leaves the map entry in place
3174+ // so the finalizer can still remove the live server.
31543175 const known = state . mcp ?? registeredMcp . get ( turnInput . threadId ) ;
31553176 if (
31563177 known !== undefined &&
@@ -3160,8 +3181,10 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
31603181 ) {
31613182 yield * removeMcp ( known ) ;
31623183 if ( state . mcp === known ) state . mcp = undefined ;
3184+ if ( registeredMcp . get ( turnInput . threadId ) === known ) {
3185+ registeredMcp . delete ( turnInput . threadId ) ;
3186+ }
31633187 }
3164- registeredMcp . delete ( turnInput . threadId ) ;
31653188 // Idle Location eviction drops runtime MCP servers without clearing T3's cache.
31663189 // Re-adding an unchanged config keeps its existing connection.
31673190 let mcpAvailable = false ;
@@ -3186,7 +3209,11 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
31863209 ) ,
31873210 ) ,
31883211 ) ;
3189- if ( mcpAvailable ) state . mcp = wanted ;
3212+ if ( mcpAvailable ) {
3213+ state . mcp = wanted ;
3214+ // Ownership of any pre-registration moved onto the state.
3215+ registeredMcp . delete ( turnInput . threadId ) ;
3216+ }
31903217 }
31913218 const instructions = [
31923219 buildRuntimeInstructions ( { harness : "OpenCode" , model : turnInput . modelSelection . model } ) ,
@@ -3528,6 +3555,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
35283555 threadInput . threadId ,
35293556 ) ;
35303557 const title = threadInput . title ?. trim ( ) || undefined ;
3558+ // The catalog is built from this directory at `session.create`, so
3559+ // the thread's own MCP server must already be registered there.
3560+ yield * preRegisterMcp ( threadInput . threadId , directory ) ;
35313561 // A session created with a title is never titled by OpenCode's own model.
35323562 const created = yield * client . session . create ( {
35333563 location : Location . PublicRef . make ( { directory : AbsolutePath . make ( directory ) } ) ,
@@ -4125,6 +4155,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
41254155 yield * userMessages ( sourceId ) ,
41264156 ) ;
41274157 // A fork copies the source's history, model, location and rules; its
4158+ // catalog is built at `session.fork`, so the target's own MCP server
4159+ // must already be registered in the directory the forked session
4160+ // starts in — a fork's inherited source server is denied by the
4161+ // target thread's rules.
4162+ yield * preRegisterMcp (
4163+ forkInput . targetThreadId ,
4164+ forkInput . runtimePolicy ?. cwd ?? ownerOf ( sourceId ) ?. directory ?? host . paths . cwd ,
4165+ ) ;
41284166 const forked = yield * client . session . fork ( {
41294167 sessionID : Session . ID . make ( sourceId ) ,
41304168 ...( before === null ? { } : { before : SessionMessage . ID . make ( before ) } ) ,
0 commit comments