@@ -137,41 +137,42 @@ export namespace MCP {
137137 // Zod, blocking listTools() entirely. We accept `null` as "hint absent" by
138138 // calling `client.request()` with a permissive schema in place of the SDK's
139139 // strict one. See https://github.com/AltimateAI/altimate-code/issues/792.
140- const LenientToolAnnotationsSchema = z
141- . object ( {
142- title : z . string ( ) . optional ( ) ,
143- readOnlyHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
144- destructiveHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
145- idempotentHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
146- openWorldHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
147- } )
148- . loose ( )
149-
150- const LenientToolSchema = z
151- . object ( {
152- name : z . string ( ) ,
153- title : z . string ( ) . optional ( ) ,
154- description : z . string ( ) . optional ( ) ,
155- inputSchema : z . any ( ) ,
156- outputSchema : z . any ( ) . optional ( ) ,
157- annotations : LenientToolAnnotationsSchema . optional ( ) ,
158- _meta : z . record ( z . string ( ) , z . unknown ( ) ) . optional ( ) ,
159- } )
160- . loose ( )
161-
162- const LenientListToolsResultSchema = z
163- . object ( {
164- tools : z . array ( LenientToolSchema ) ,
165- nextCursor : z . string ( ) . optional ( ) ,
166- _meta : z . record ( z . string ( ) , z . unknown ( ) ) . optional ( ) ,
167- } )
168- . loose ( )
140+ const LenientToolAnnotationsSchema = z . looseObject ( {
141+ title : z . string ( ) . optional ( ) ,
142+ readOnlyHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
143+ destructiveHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
144+ idempotentHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
145+ openWorldHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
146+ } )
147+
148+ const LenientToolSchema = z . looseObject ( {
149+ name : z . string ( ) ,
150+ title : z . string ( ) . optional ( ) ,
151+ description : z . string ( ) . optional ( ) ,
152+ inputSchema : z . any ( ) ,
153+ outputSchema : z . any ( ) . optional ( ) ,
154+ annotations : LenientToolAnnotationsSchema . optional ( ) ,
155+ _meta : z . record ( z . string ( ) , z . unknown ( ) ) . optional ( ) ,
156+ } )
157+
158+ const LenientListToolsResultSchema = z . looseObject ( {
159+ tools : z . array ( LenientToolSchema ) ,
160+ nextCursor : z . string ( ) . optional ( ) ,
161+ _meta : z . record ( z . string ( ) , z . unknown ( ) ) . optional ( ) ,
162+ } )
169163
170164 function isSchemaError ( err : unknown ) : boolean {
171- if ( ! err ) return false
172- if ( err instanceof Error && ( err . name === "ZodError" || err . constructor ?. name === "$ZodError" ) ) return true
173- if ( typeof err === "object" && err !== null && "issues" in err ) return true
174- return false
165+ // Narrowly match Zod validation errors only. We key off the error *name*
166+ // (not `instanceof z.ZodError`) because the MCP SDK may throw a ZodError
167+ // from a different zod copy (it depends on `zod@^3.25 || ^4.0`), which
168+ // would fail a cross-realm `instanceof`. In practice the SDK's z4-mini path
169+ // names the error `$ZodError` (the primary case); the public ZodError
170+ // subclass names it `ZodError`. We require an `issues` *array* (rather than
171+ // `"issues" in err`) to avoid a throwing getter and to reject unrelated
172+ // errors that merely carry an `issues` property of some other type.
173+ if ( typeof err !== "object" || err === null ) return false
174+ const name = ( err as { name ?: string } ) . name ?? ( err as { constructor ?: { name ?: string } } ) . constructor ?. name
175+ return ( name === "ZodError" || name === "$ZodError" ) && Array . isArray ( ( err as { issues ?: unknown } ) . issues )
175176 }
176177
177178 /**
@@ -201,16 +202,20 @@ export namespace MCP {
201202 export const _testing = {
202203 LenientListToolsResultSchema,
203204 isSchemaError,
205+ listToolsLenient : ( client : {
206+ listTools : ( ) => Promise < { tools : MCPToolDef [ ] } >
207+ request : ( ...args : any [ ] ) => Promise < unknown >
208+ } ) => listToolsLenient ( client as unknown as MCPClient ) ,
204209 resolveHeadersCommand : ( spec : Record < string , string [ ] > | undefined , key = "test" ) =>
205210 resolveHeadersCommand ( spec , key ) ,
206211 hasAuthorizationHeader,
207212 }
208213 // altimate_change end
209214
210- // altimate_change start — resolve dynamic header values produced by shell
211- // commands (e.g. `az account get-access-token`). Runs via execFile (not a
212- // shell) so values aren't subject to shell injection. Re-runs on every
213- // connect so expiring bearer tokens refresh without manual config edits.
215+ // altimate_change start — resolve dynamic header values from argv commands
216+ // (e.g. `az account get-access-token`). Each value is an argv array run via
217+ // execFile (no shell) so values aren't subject to shell injection. Re-runs on
218+ // every connect so expiring bearer tokens refresh without manual config edits.
214219 // See https://github.com/AltimateAI/altimate-code/issues/791.
215220 async function resolveHeadersCommand (
216221 spec : Record < string , string [ ] > | undefined ,
@@ -223,11 +228,26 @@ export namespace MCP {
223228 throw new Error ( `headersCommand[${ name } ] must be a non-empty argv array` )
224229 }
225230 const [ cmd , ...args ] = argv
226- const { stdout } = await execFileAsync ( cmd , args , {
227- encoding : "utf-8" ,
228- maxBuffer : 1024 * 1024 ,
229- timeout : 30_000 ,
230- } )
231+ let stdout = ""
232+ try {
233+ stdout = (
234+ await execFileAsync ( cmd , args , {
235+ encoding : "utf-8" ,
236+ maxBuffer : 1024 * 1024 ,
237+ timeout : 30_000 ,
238+ } )
239+ ) . stdout
240+ } catch ( err ) {
241+ // Wrap with the header key so `mcp list` points to the exact failing
242+ // command (ENOENT, timeout, non-zero exit) rather than a bare error.
243+ // On a non-zero exit the actionable reason lives on `err.stderr` (e.g.
244+ // `az`'s "run 'az login'"), which `err.message` omits — append it.
245+ const e = err as { message ?: string ; stderr ?: string }
246+ const stderr = typeof e . stderr === "string" ? e . stderr . trim ( ) . slice ( 0 , 500 ) : ""
247+ const base = err instanceof Error ? err . message : String ( err )
248+ const message = stderr ? `${ base } : ${ stderr } ` : base
249+ throw new Error ( `headersCommand[${ name } ] failed: ${ message } ` )
250+ }
231251 const value = stdout . trim ( )
232252 if ( ! value ) {
233253 throw new Error ( `headersCommand[${ name } ] produced empty output` )
@@ -488,9 +508,9 @@ export namespace MCP {
488508 if ( mcp . type === "remote" ) {
489509 // altimate_change start — resolve dynamic headers (e.g. bearer tokens
490510 // produced by `az account get-access-token`) before constructing
491- // transports. Failure to resolve aborts the connect attempt with a
492- // clear error so the user sees `failed: headersCommand[... ] failed`
493- // in `mcp list` rather than a generic transport error .
511+ // transports. Failure to resolve aborts the connect attempt. The thrown
512+ // message already names the failing header (` headersCommand[<name> ] failed:
513+ // ...`), so the user sees exactly which command broke in `mcp list` .
494514 let dynamicHeaders : Record < string , string > = { }
495515 try {
496516 dynamicHeaders = await resolveHeadersCommand ( mcp . headersCommand , key )
@@ -499,7 +519,7 @@ export namespace MCP {
499519 log . error ( "headersCommand resolution failed" , { key, error : message } )
500520 return {
501521 mcpClient : undefined ,
502- status : { status : "failed" as const , error : `headersCommand failed: ${ message } ` } ,
522+ status : { status : "failed" as const , error : message } ,
503523 }
504524 }
505525 const mergedHeaders : Record < string , string > = { ...( mcp . headers ?? { } ) , ...dynamicHeaders }
0 commit comments