@@ -2712,7 +2712,104 @@ describe("OpenCode2 adapter", () => {
27122712 } ) . pipe ( Effect . scoped ) ,
27132713 ) ;
27142714
2715- it . effect ( "adds the thread's shared MCP servers next to T3's and removes them with it" , ( ) =>
2715+ it . effect (
2716+ "adds the thread's shared MCP servers next to T3's, retries a refused one, and removes them with it" ,
2717+ ( ) =>
2718+ Effect . gen ( function * ( ) {
2719+ McpProviderSession . setMcpProviderSession ( {
2720+ environmentId : EnvironmentId . make ( "environment:opencode2-adapter" ) ,
2721+ threadId,
2722+ providerSessionId : "mcp:opencode2-adapter" ,
2723+ providerInstanceId : instanceId ,
2724+ endpoint : "http://127.0.0.1:3773/mcp" ,
2725+ authorizationHeader : "Bearer thread-credential" ,
2726+ browserToolsAvailable : false ,
2727+ sharedServers : [
2728+ {
2729+ name : "gateway" ,
2730+ url : "http://127.0.0.1:3050/mcp" ,
2731+ enabled : true ,
2732+ headers : { Authorization : "Bearer gateway-token" } ,
2733+ } ,
2734+ { name : "broken" , url : "http://127.0.0.1:1/mcp" , enabled : true , headers : { } } ,
2735+ ] ,
2736+ } ) ;
2737+ yield * Effect . addFinalizer ( ( ) =>
2738+ Effect . sync ( ( ) => McpProviderSession . clearMcpProviderSession ( threadId ) ) ,
2739+ ) ;
2740+ const server = "t3-code-thread_opencode2-adapter" ;
2741+ const gateway = "t3-shared-thread_opencode2-adapter_gateway" ;
2742+ const broken = "t3-shared-thread_opencode2-adapter_broken" ;
2743+ const { runtime, thread } = yield * resumed ( [
2744+ out ( "mcp.add" , { server, "location[directory]" : WORK , config : "<any>" } ) ,
2745+ reply ( "mcp.add" , null ) ,
2746+ out ( "mcp.add" , {
2747+ server : gateway ,
2748+ "location[directory]" : WORK ,
2749+ config : {
2750+ type : "remote" ,
2751+ url : "http://127.0.0.1:3050/mcp" ,
2752+ headers : { Authorization : "Bearer gateway-token" } ,
2753+ oauth : false ,
2754+ } ,
2755+ } ) ,
2756+ reply ( "mcp.add" , null ) ,
2757+ // A shared server OpenCode refuses is skipped; the turn still runs.
2758+ out ( "mcp.add" , {
2759+ server : broken ,
2760+ "location[directory]" : WORK ,
2761+ config : { type : "remote" , url : "http://127.0.0.1:1/mcp" , oauth : false } ,
2762+ } ) ,
2763+ reply ( "mcp.add" , { status : 500 , body : { _tag : "UnknownError" , message : "add failed" } } ) ,
2764+ out ( "session.prompt" , { sessionID : SESSION , text : "<any>" } ) ,
2765+ promptAccepted ,
2766+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
2767+ // The next turn retries the one OpenCode refused, as it would T3's own.
2768+ out ( "mcp.add" , {
2769+ server : broken ,
2770+ "location[directory]" : WORK ,
2771+ config : { type : "remote" , url : "http://127.0.0.1:1/mcp" , oauth : false } ,
2772+ } ) ,
2773+ reply ( "mcp.add" , null ) ,
2774+ out ( "session.prompt" , { sessionID : SESSION , text : "<any>" } ) ,
2775+ promptAccepted ,
2776+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
2777+ out ( "mcp.remove" , { server, "location[directory]" : WORK } ) ,
2778+ reply ( "mcp.remove" , null ) ,
2779+ out ( "mcp.remove" , { server : gateway , "location[directory]" : WORK } ) ,
2780+ reply ( "mcp.remove" , null ) ,
2781+ out ( "mcp.remove" , { server : broken , "location[directory]" : WORK } ) ,
2782+ reply ( "mcp.remove" , null ) ,
2783+ ] ) ;
2784+ const firstEnded = yield * Deferred . make < void > ( ) ;
2785+ const ended = yield * runtime . events . pipe (
2786+ Stream . filter (
2787+ ( event ) : event is Extract < ProviderAdapterV2Event , { type : "turn.terminal" } > =>
2788+ event . type === "turn.terminal" ,
2789+ ) ,
2790+ Stream . tap ( ( ) => Deferred . succeed ( firstEnded , undefined ) ) ,
2791+ Stream . take ( 2 ) ,
2792+ Stream . runCollect ,
2793+ Effect . forkScoped ,
2794+ ) ;
2795+ yield * runtime . startTurn ( turnInput ( thread ) ) ;
2796+ yield * Deferred . await ( firstEnded ) ;
2797+ yield * runtime . startTurn ( {
2798+ ...turnInput ( thread ) ,
2799+ runId : RunId . make ( "run:opencode2-adapter:2" ) ,
2800+ runOrdinal : 2 ,
2801+ providerTurnOrdinal : 2 ,
2802+ attemptId : RunAttemptId . make ( "attempt:opencode2-adapter:2" ) ,
2803+ } ) ;
2804+ assert . deepEqual (
2805+ [ ...( yield * Fiber . join ( ended ) ) ] . map ( ( terminal ) => terminal . status ) ,
2806+ [ "completed" , "completed" ] ,
2807+ ) ;
2808+ yield * runtime . unloadThread ! ( { providerThread : thread } ) ;
2809+ } ) . pipe ( Effect . scoped ) ,
2810+ ) ;
2811+
2812+ it . effect ( "adds shared MCP servers on an external server, which skips T3's own" , ( ) =>
27162813 Effect . gen ( function * ( ) {
27172814 McpProviderSession . setMcpProviderSession ( {
27182815 environmentId : EnvironmentId . make ( "environment:opencode2-adapter" ) ,
@@ -2723,57 +2820,37 @@ describe("OpenCode2 adapter", () => {
27232820 authorizationHeader : "Bearer thread-credential" ,
27242821 browserToolsAvailable : false ,
27252822 sharedServers : [
2726- {
2727- name : "gateway" ,
2728- url : "http://127.0.0.1:3050/mcp" ,
2729- enabled : true ,
2730- headers : { Authorization : "Bearer gateway-token" } ,
2731- } ,
2732- { name : "broken" , url : "http://127.0.0.1:1/mcp" , enabled : true , headers : { } } ,
2823+ { name : "gateway" , url : "https://mcp.example.com/mcp" , enabled : true , headers : { } } ,
27332824 ] ,
27342825 } ) ;
27352826 yield * Effect . addFinalizer ( ( ) =>
27362827 Effect . sync ( ( ) => McpProviderSession . clearMcpProviderSession ( threadId ) ) ,
27372828 ) ;
2738- const server = "t3-code-thread_opencode2-adapter" ;
27392829 const gateway = "t3-shared-thread_opencode2-adapter_gateway" ;
2740- const { runtime, thread } = yield * resumed ( [
2741- out ( "mcp.add" , { server, "location[directory]" : WORK , config : "<any>" } ) ,
2742- reply ( "mcp.add" , null ) ,
2743- out ( "mcp.add" , {
2744- server : gateway ,
2745- "location[directory]" : WORK ,
2746- config : {
2747- type : "remote" ,
2748- url : "http://127.0.0.1:3050/mcp" ,
2749- headers : { Authorization : "Bearer gateway-token" } ,
2750- oauth : false ,
2751- } ,
2752- } ) ,
2753- reply ( "mcp.add" , null ) ,
2754- // A shared server OpenCode refuses is skipped; the turn still runs.
2755- out ( "mcp.add" , {
2756- server : "t3-shared-thread_opencode2-adapter_broken" ,
2757- "location[directory]" : WORK ,
2758- config : { type : "remote" , url : "http://127.0.0.1:1/mcp" , oauth : false } ,
2759- } ) ,
2760- reply ( "mcp.add" , { status : 500 , body : { _tag : "UnknownError" , message : "add failed" } } ) ,
2761- out ( "session.prompt" , { sessionID : SESSION , text : "<any>" } ) ,
2762- promptAccepted ,
2763- event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
2764- out ( "mcp.remove" , { server, "location[directory]" : WORK } ) ,
2765- reply ( "mcp.remove" , null ) ,
2766- out ( "mcp.remove" , { server : gateway , "location[directory]" : WORK } ) ,
2767- reply ( "mcp.remove" , null ) ,
2768- ] ) ;
2830+ const { runtime, thread } = yield * resumed (
2831+ [
2832+ out ( "mcp.add" , {
2833+ server : gateway ,
2834+ "location[directory]" : WORK ,
2835+ config : { type : "remote" , url : "https://mcp.example.com/mcp" , oauth : false } ,
2836+ } ) ,
2837+ reply ( "mcp.add" , null ) ,
2838+ out ( "session.prompt" , { sessionID : SESSION , text : "<any>" } ) ,
2839+ promptAccepted ,
2840+ event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
2841+ out ( "mcp.remove" , { server : gateway , "location[directory]" : WORK } ) ,
2842+ reply ( "mcp.remove" , null ) ,
2843+ ] ,
2844+ { external : true } ,
2845+ ) ;
27692846 const terminal = yield * terminalOf ( runtime ) . pipe ( Effect . forkScoped ) ;
27702847 yield * runtime . startTurn ( turnInput ( thread ) ) ;
27712848 assert . equal ( ( yield * Fiber . join ( terminal ) ) ?. status , "completed" ) ;
27722849 yield * runtime . unloadThread ! ( { providerThread : thread } ) ;
27732850 } ) . pipe ( Effect . scoped ) ,
27742851 ) ;
27752852
2776- it . effect ( "adds shared MCP servers on an external server, which skips T3's own " , ( ) =>
2853+ it . effect ( "keeps an external server's shared MCP registrations through a reconnect " , ( ) =>
27772854 Effect . gen ( function * ( ) {
27782855 McpProviderSession . setMcpProviderSession ( {
27792856 environmentId : EnvironmentId . make ( "environment:opencode2-adapter" ) ,
@@ -2784,7 +2861,12 @@ describe("OpenCode2 adapter", () => {
27842861 authorizationHeader : "Bearer thread-credential" ,
27852862 browserToolsAvailable : false ,
27862863 sharedServers : [
2787- { name : "gateway" , url : "https://mcp.example.com/mcp" , enabled : true , headers : { } } ,
2864+ {
2865+ name : "gateway" ,
2866+ url : "https://mcp.example.com/mcp" ,
2867+ enabled : true ,
2868+ headers : { Authorization : "Bearer gateway-token" } ,
2869+ } ,
27882870 ] ,
27892871 } ) ;
27902872 yield * Effect . addFinalizer ( ( ) =>
@@ -2793,15 +2875,15 @@ describe("OpenCode2 adapter", () => {
27932875 const gateway = "t3-shared-thread_opencode2-adapter_gateway" ;
27942876 const { runtime, thread } = yield * resumed (
27952877 [
2796- out ( "mcp.add" , {
2797- server : gateway ,
2798- "location[directory]" : WORK ,
2799- config : { type : "remote" , url : "https://mcp.example.com/mcp" , oauth : false } ,
2800- } ) ,
2878+ out ( "mcp.add" , { server : gateway , "location[directory]" : WORK , config : "<any>" } ) ,
28012879 reply ( "mcp.add" , null ) ,
2802- out ( "session.prompt" , { sessionID : SESSION , text : "<any>" } ) ,
2880+ out ( "session.prompt" , { sessionID : SESSION , id : PROMPT_ID , text : "<any>" } ) ,
28032881 promptAccepted ,
2882+ // The event stream drops mid-turn; the external server is the same one after.
2883+ ...reconnected ( { [ SESSION ] : { type : "running" } } ) ,
2884+ ...openRequests ( [ ] ) ,
28042885 event ( "session.execution.succeeded" , { sessionID : SESSION } ) ,
2886+ // Its registration is still known, so unloading still removes it, headers and all.
28052887 out ( "mcp.remove" , { server : gateway , "location[directory]" : WORK } ) ,
28062888 reply ( "mcp.remove" , null ) ,
28072889 ] ,
0 commit comments