@@ -4,13 +4,14 @@ import path from "node:path"
44import { pathToFileURL } from "node:url"
55import {
66 Client ,
7+ ProtocolError ,
78 SdkError ,
89 SdkErrorCode ,
910 SdkHttpError ,
1011 StreamableHTTPClientTransport ,
1112 UnauthorizedError ,
1213 UnsupportedProtocolVersionError ,
13- isJSONRPCResultResponse ,
14+ type CallToolRequestOptions ,
1415 type CallToolResult as SdkCallToolResult ,
1516 type ElicitRequestFormParams ,
1617 type ElicitRequestParams ,
@@ -23,20 +24,22 @@ import {
2324 type ReadResourceResult ,
2425 type Resource ,
2526 type ResourceTemplateType ,
26- type Tool ,
2727 type Transport ,
2828 type VersionNegotiationOptions ,
2929} from "@modelcontextprotocol/client"
3030import { Cause , Clock , Duration , Effect , Exit , Schedule , Schema } from "effect"
3131import { ConfigMCP } from "@opencode/schema/config/mcp"
3232import type { Session } from "@opencode/schema/session"
33+ import { isRecord } from "@opencode/util/record"
3334import { McpStdio } from "./stdio.js"
3435
3536const DEFAULT_STARTUP_TIMEOUT = 30_000
3637const DEFAULT_CATALOG_TIMEOUT = 30_000
3738const DEFAULT_EXECUTION_TIMEOUT = 12 * 60 * 60 * 1_000 // 12 hours
3839const ATTACHED_EXECUTION_TIMEOUT = 10 * 60 * 1_000 // 10 minutes
3940const TERMINATE_TIMEOUT = 1_000
41+ // SEP-2243 HeaderMismatch; the SDK does not export the constant.
42+ const HEADER_MISMATCH = - 32020
4043const toError = ( error : unknown ) => ( error instanceof Error ? error : new Error ( String ( error ) ) )
4144
4245// HTTP statuses and network error codes live on error properties and are lost once flattened to a message.
@@ -59,15 +62,24 @@ const isTransient = (error: unknown) =>
5962 ? error . status === 408 || error . status === 429 || ( error . status >= 500 && error . status !== 501 )
6063 : error instanceof TypeError && "code" in error
6164
62- export type { GetPromptResult , Prompt , ReadResourceResult , Resource , Tool }
65+ export type { GetPromptResult , Prompt , ReadResourceResult , Resource }
6366export type ResourceTemplate = ResourceTemplateType
6467
65- // The era codec is protected; it checks a tools/list result against the exact schema the SDK applies
66- // for the negotiated protocol era, so a tool is skipped only when the SDK itself would reject it.
67- class ToolCheckingClient extends Client {
68- checkToolsList ( result : unknown ) {
69- return this . _wireCodec ( ) . validateResult ( "tools/list" , result )
68+ /** A listed tool, keeping the server's fields except `icons`, each only when it has the right type. */
69+ export interface Tool {
70+ readonly name : string
71+ readonly title ?: string
72+ readonly description ?: string
73+ readonly inputSchema : {
74+ readonly type : "object"
75+ readonly properties ?: Record < string , unknown >
76+ readonly required ?: string [ ]
77+ readonly [ key : string ] : unknown
7078 }
79+ readonly outputSchema ?: Record < string , unknown >
80+ readonly annotations ?: Record < string , unknown >
81+ readonly execution ?: Record < string , unknown >
82+ readonly _meta ?: Record < string , unknown >
7183}
7284
7385export class NeedsAuthError extends Schema . TaggedError < NeedsAuthError > ( ) ( "MCP.NeedsAuthError" , {
@@ -209,7 +221,7 @@ export const connect = Effect.fnUntraced(function* (
209221 const context = yield * Effect . context ( )
210222 const runFork = Effect . runForkWith ( context )
211223 const runPromise = Effect . runPromiseWith ( context )
212- const client = new ToolCheckingClient ( clientInfo , {
224+ const client = new Client ( clientInfo , {
213225 capabilities : {
214226 ...( elicitation ? { elicitation : { form : { } , url : { } } } : { } ) ,
215227 // Legacy era only: roots are deprecated as of 2026-07-28 and modern servers cannot request them.
@@ -239,32 +251,6 @@ export const connect = Effect.fnUntraced(function* (
239251 )
240252 }
241253
242- // The SDK validates tools/list as a whole, so one malformed tool would fail every tool on the server.
243- // The SDK runs a handler set on the transport before connect ahead of its own, on the same message,
244- // so tools it would reject are dropped there and it still caches the rest. A page that fails for
245- // another reason is left for the SDK to report.
246- const onmessage = transport . onmessage
247- transport . onmessage = ( message , extra ) => {
248- if (
249- isJSONRPCResultResponse ( message ) &&
250- Array . isArray ( message . result . tools ) &&
251- client . checkToolsList ( { ...message . result , tools : [ ] } ) . ok
252- )
253- message . result . tools = message . result . tools . filter ( ( tool : unknown ) => {
254- const outcome = client . checkToolsList ( { ...message . result , tools : [ tool ] } )
255- if ( outcome . ok ) return true
256- runFork (
257- Effect . logWarning ( "skipping invalid MCP tool" , {
258- server,
259- tool : typeof tool === "object" && tool !== null && "name" in tool ? tool . name : undefined ,
260- error : outcome . reason === "invalid" ? outcome . message : outcome . reason ,
261- } ) ,
262- )
263- return false
264- } )
265- onmessage ?.( message , extra )
266- }
267-
268254 yield * Effect . tryPromise ( {
269255 try : ( signal ) => client . connect ( transport , { timeout : timeout ?. startup ?? DEFAULT_STARTUP_TIMEOUT , signal } ) ,
270256 catch : ( error ) => error ,
@@ -280,6 +266,8 @@ export const connect = Effect.fnUntraced(function* (
280266 reported : false ,
281267 }
282268 const failure = ( error : unknown ) => {
269+ // A tool call that relists first can fail with the list's already mapped expiry.
270+ if ( error instanceof SessionExpiredError ) return error
283271 if ( ! ( error instanceof SdkHttpError ) || session . transport ?. sessionId === undefined ) return new Error ( explain ( error ) )
284272 const expired =
285273 error . status === 404 ||
@@ -367,6 +355,7 @@ export const connect = Effect.fnUntraced(function* (
367355 } ) ,
368356 )
369357 const catalog = { timeout : timeout ?. catalog ?? DEFAULT_CATALOG_TIMEOUT }
358+ const listed = new Map < string , Tool > ( )
370359 const execution = timeout ?. execution ?? DEFAULT_EXECUTION_TIMEOUT
371360 const request = < A > ( what : string , run : ( signal : AbortSignal ) => Promise < A > ) =>
372361 Effect . tryPromise ( { try : run , catch : failure } ) . pipe (
@@ -389,10 +378,61 @@ export const connect = Effect.fnUntraced(function* (
389378 modern : client . getProtocolEra ( ) === "modern" ,
390379 title : client . getServerVersion ( ) ?. title ?. trim ( ) || undefined ,
391380 instructions : client . getInstructions ( ) ?. trim ( ) || undefined ,
381+ // Tool lists skip the SDK response cache: it re-stringifies every list, and on the first tools/call
382+ // re-parses it and compiles an output validator for every listed tool. Each call passes its own
383+ // checked definition instead, so only that tool's outputSchema is compiled and modern Mcp-Param-*
384+ // header mirroring still sees the schema. Tools are relisted on connect, list_changed and a stale
385+ // header declaration; the SDK cache never relisted an expired list before a call either.
392386 tools : ( ) =>
393387 advertised ( "tools" )
394- ? list ( "list MCP tools" , ( signal ) => client . listTools ( undefined , { ...catalog , signal } ) ) . pipe (
395- Effect . map ( ( r ) => r . tools ) ,
388+ ? list ( "list MCP tools" , ( signal ) =>
389+ client . listTools ( undefined , { ...catalog , cacheMode : "bypass" , signal } ) ,
390+ ) . pipe (
391+ // Our SDK patch leaves the listed tools unchecked, so one malformed tool doesn't fail the
392+ // whole list; each is checked here and copied, dropping fields of the wrong type.
393+ Effect . flatMap ( ( result ) => {
394+ const skipped : Array < { tool : unknown ; reason : string } > = [ ]
395+ const tools = result . tools . flatMap ( ( tool : unknown ) : Tool [ ] => {
396+ const skip = ( reason : string ) => {
397+ skipped . push ( { tool : isRecord ( tool ) ? tool . name : undefined , reason } )
398+ return [ ]
399+ }
400+ if ( ! isRecord ( tool ) ) return skip ( "not an object" )
401+ if ( typeof tool . name !== "string" || tool . name === "" ) return skip ( "name is not a non-empty string" )
402+ if ( ! isRecord ( tool . inputSchema ) ) return skip ( "inputSchema is not an object" )
403+ if ( tool . inputSchema . type !== undefined && tool . inputSchema . type !== "object" )
404+ return skip ( 'inputSchema type is not "object"' )
405+ const { properties, required, ...inputSchema } = tool . inputSchema
406+ return [
407+ {
408+ name : tool . name ,
409+ title : typeof tool . title === "string" ? tool . title : undefined ,
410+ description : typeof tool . description === "string" ? tool . description : undefined ,
411+ // Providers that check tool parameters against the JSON Schema metaschema reject the
412+ // whole request over a malformed top level. Nested property schemas are left as sent.
413+ inputSchema : {
414+ ...inputSchema ,
415+ type : "object" ,
416+ ...( isRecord ( properties ) ? { properties } : { } ) ,
417+ ...( Array . isArray ( required ) && required . every ( ( key ) : key is string => typeof key === "string" )
418+ ? { required }
419+ : { } ) ,
420+ } ,
421+ outputSchema : isRecord ( tool . outputSchema ) ? tool . outputSchema : undefined ,
422+ annotations : isRecord ( tool . annotations ) ? tool . annotations : undefined ,
423+ execution : isRecord ( tool . execution ) ? tool . execution : undefined ,
424+ _meta : isRecord ( tool . _meta ) ? tool . _meta : undefined ,
425+ } ,
426+ ]
427+ } )
428+ listed . clear ( )
429+ for ( const tool of tools ) listed . set ( tool . name , tool )
430+ return Effect . forEach (
431+ skipped ,
432+ ( entry ) => Effect . logWarning ( "skipping invalid MCP tool" , { server, ...entry } ) ,
433+ { discard : true } ,
434+ ) . pipe ( Effect . as ( tools ) )
435+ } ) ,
396436 )
397437 : Effect . succeed ( [ ] ) ,
398438 prompts : ( ) =>
@@ -423,18 +463,44 @@ export const connect = Effect.fnUntraced(function* (
423463 request ( "get MCP prompt" , ( signal ) =>
424464 client . getPrompt ( { name : input . name , arguments : input . args ?? { } } , { signal, timeout : execution } ) ,
425465 ) ,
426- callTool : ( input ) =>
427- request ( "call MCP tool" , ( signal ) =>
428- client . callTool (
429- {
430- name : input . name ,
431- arguments : input . args ?? { } ,
432- ...( input . sessionID === undefined ? { } : { _meta : { "ai.opencode/sessionID" : input . sessionID } } ) ,
433- } ,
434- // Requesting progress keeps long calls alive under the SDK's timeout; execution is the hard wall.
435- { signal, timeout : execution , onprogress : ( ) => { } } ,
466+ callTool : ( input ) => {
467+ const call = Effect . tryPromise ( {
468+ try : ( signal ) =>
469+ client . callTool (
470+ {
471+ name : input . name ,
472+ arguments : input . args ?? { } ,
473+ ...( input . sessionID === undefined ? { } : { _meta : { "ai.opencode/sessionID" : input . sessionID } } ) ,
474+ } ,
475+ {
476+ signal,
477+ timeout : execution ,
478+ // Requesting progress keeps long calls alive under the SDK's timeout; execution is the hard wall.
479+ onprogress : ( ) => { } ,
480+ // Always passed, even for a tool we haven't listed, so the SDK never falls back to listing
481+ // and caching the raw tools itself. Its types are narrower than what it reads (JSON values,
482+ // a string `$schema`); ours keep the server's values.
483+ toolDefinition : ( listed . get ( input . name ) ?? {
484+ name : input . name ,
485+ inputSchema : { type : "object" } ,
486+ } ) as CallToolRequestOptions [ "toolDefinition" ] ,
487+ } ,
488+ ) ,
489+ catch : ( error ) => error ,
490+ } )
491+ // A modern server rejects headers mirrored from a stale definition before running the tool,
492+ // so relist and retry once with the fresh definition.
493+ return call . pipe (
494+ Effect . catchIf (
495+ ( error ) =>
496+ client . getProtocolEra ( ) === "modern" && error instanceof ProtocolError && error . code === HEADER_MISMATCH ,
497+ ( ) => connection . tools ( ) . pipe ( Effect . andThen ( call ) ) ,
436498 ) ,
437- ) . pipe ( Effect . map ( toCallToolResult ) ) ,
499+ Effect . mapError ( failure ) ,
500+ Effect . tapError ( ( error ) => Effect . logWarning ( "failed to call MCP tool" , { server, error : error . message } ) ) ,
501+ Effect . map ( toCallToolResult ) ,
502+ )
503+ } ,
438504 onClose : ( callback ) => {
439505 client . onclose = ( ) => callback ( lastError ? `Connection closed: ${ lastError } ` : "Connection closed" )
440506 } ,
0 commit comments