@@ -133,41 +133,42 @@ export namespace MCP {
133133 // Zod, blocking listTools() entirely. We accept `null` as "hint absent" by
134134 // calling `client.request()` with a permissive schema in place of the SDK's
135135 // strict one. See https://github.com/AltimateAI/altimate-code/issues/792.
136- const LenientToolAnnotationsSchema = z
137- . object ( {
138- title : z . string ( ) . optional ( ) ,
139- readOnlyHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
140- destructiveHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
141- idempotentHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
142- openWorldHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
143- } )
144- . loose ( )
145-
146- const LenientToolSchema = z
147- . object ( {
148- name : z . string ( ) ,
149- title : z . string ( ) . optional ( ) ,
150- description : z . string ( ) . optional ( ) ,
151- inputSchema : z . any ( ) ,
152- outputSchema : z . any ( ) . optional ( ) ,
153- annotations : LenientToolAnnotationsSchema . optional ( ) ,
154- _meta : z . record ( z . string ( ) , z . unknown ( ) ) . optional ( ) ,
155- } )
156- . loose ( )
157-
158- const LenientListToolsResultSchema = z
159- . object ( {
160- tools : z . array ( LenientToolSchema ) ,
161- nextCursor : z . string ( ) . optional ( ) ,
162- _meta : z . record ( z . string ( ) , z . unknown ( ) ) . optional ( ) ,
163- } )
164- . loose ( )
136+ const LenientToolAnnotationsSchema = z . looseObject ( {
137+ title : z . string ( ) . optional ( ) ,
138+ readOnlyHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
139+ destructiveHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
140+ idempotentHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
141+ openWorldHint : z . boolean ( ) . nullable ( ) . optional ( ) ,
142+ } )
143+
144+ const LenientToolSchema = z . looseObject ( {
145+ name : z . string ( ) ,
146+ title : z . string ( ) . optional ( ) ,
147+ description : z . string ( ) . optional ( ) ,
148+ inputSchema : z . any ( ) ,
149+ outputSchema : z . any ( ) . optional ( ) ,
150+ annotations : LenientToolAnnotationsSchema . optional ( ) ,
151+ _meta : z . record ( z . string ( ) , z . unknown ( ) ) . optional ( ) ,
152+ } )
153+
154+ const LenientListToolsResultSchema = z . looseObject ( {
155+ tools : z . array ( LenientToolSchema ) ,
156+ nextCursor : z . string ( ) . optional ( ) ,
157+ _meta : z . record ( z . string ( ) , z . unknown ( ) ) . optional ( ) ,
158+ } )
165159
166160 function isSchemaError ( err : unknown ) : boolean {
167- if ( ! err ) return false
168- if ( err instanceof Error && ( err . name === "ZodError" || err . constructor ?. name === "$ZodError" ) ) return true
169- if ( typeof err === "object" && err !== null && "issues" in err ) return true
170- return false
161+ // Narrowly match Zod validation errors only. We key off the error *name*
162+ // (not `instanceof z.ZodError`) because the MCP SDK may throw a ZodError
163+ // from a different zod copy (it depends on `zod@^3.25 || ^4.0`), which
164+ // would fail a cross-realm `instanceof`. In practice the SDK's z4-mini path
165+ // names the error `$ZodError` (the primary case); the public ZodError
166+ // subclass names it `ZodError`. We require an `issues` *array* (rather than
167+ // `"issues" in err`) to avoid a throwing getter and to reject unrelated
168+ // errors that merely carry an `issues` property of some other type.
169+ if ( typeof err !== "object" || err === null ) return false
170+ const name = ( err as { name ?: string } ) . name ?? ( err as { constructor ?: { name ?: string } } ) . constructor ?. name
171+ return ( name === "ZodError" || name === "$ZodError" ) && Array . isArray ( ( err as { issues ?: unknown } ) . issues )
171172 }
172173
173174 /**
@@ -197,16 +198,20 @@ export namespace MCP {
197198 export const _testing = {
198199 LenientListToolsResultSchema,
199200 isSchemaError,
201+ listToolsLenient : ( client : {
202+ listTools : ( ) => Promise < { tools : MCPToolDef [ ] } >
203+ request : ( ...args : any [ ] ) => Promise < unknown >
204+ } ) => listToolsLenient ( client as unknown as MCPClient ) ,
200205 resolveHeadersCommand : ( spec : Record < string , string [ ] > | undefined , key = "test" ) =>
201206 resolveHeadersCommand ( spec , key ) ,
202207 hasAuthorizationHeader,
203208 }
204209 // altimate_change end
205210
206- // altimate_change start — resolve dynamic header values produced by shell
207- // commands (e.g. `az account get-access-token`). Runs via execFile (not a
208- // shell) so values aren't subject to shell injection. Re-runs on every
209- // connect so expiring bearer tokens refresh without manual config edits.
211+ // altimate_change start — resolve dynamic header values from argv commands
212+ // (e.g. `az account get-access-token`). Each value is an argv array run via
213+ // execFile (no shell) so values aren't subject to shell injection. Re-runs on
214+ // every connect so expiring bearer tokens refresh without manual config edits.
210215 // See https://github.com/AltimateAI/altimate-code/issues/791.
211216 async function resolveHeadersCommand (
212217 spec : Record < string , string [ ] > | undefined ,
@@ -219,11 +224,26 @@ export namespace MCP {
219224 throw new Error ( `headersCommand[${ name } ] must be a non-empty argv array` )
220225 }
221226 const [ cmd , ...args ] = argv
222- const { stdout } = await execFileAsync ( cmd , args , {
223- encoding : "utf-8" ,
224- maxBuffer : 1024 * 1024 ,
225- timeout : 30_000 ,
226- } )
227+ let stdout = ""
228+ try {
229+ stdout = (
230+ await execFileAsync ( cmd , args , {
231+ encoding : "utf-8" ,
232+ maxBuffer : 1024 * 1024 ,
233+ timeout : 30_000 ,
234+ } )
235+ ) . stdout
236+ } catch ( err ) {
237+ // Wrap with the header key so `mcp list` points to the exact failing
238+ // command (ENOENT, timeout, non-zero exit) rather than a bare error.
239+ // On a non-zero exit the actionable reason lives on `err.stderr` (e.g.
240+ // `az`'s "run 'az login'"), which `err.message` omits — append it.
241+ const e = err as { message ?: string ; stderr ?: string }
242+ const stderr = typeof e . stderr === "string" ? e . stderr . trim ( ) . slice ( 0 , 500 ) : ""
243+ const base = err instanceof Error ? err . message : String ( err )
244+ const message = stderr ? `${ base } : ${ stderr } ` : base
245+ throw new Error ( `headersCommand[${ name } ] failed: ${ message } ` )
246+ }
227247 const value = stdout . trim ( )
228248 if ( ! value ) {
229249 throw new Error ( `headersCommand[${ name } ] produced empty output` )
@@ -484,9 +504,9 @@ export namespace MCP {
484504 if ( mcp . type === "remote" ) {
485505 // altimate_change start — resolve dynamic headers (e.g. bearer tokens
486506 // produced by `az account get-access-token`) before constructing
487- // transports. Failure to resolve aborts the connect attempt with a
488- // clear error so the user sees `failed: headersCommand[... ] failed`
489- // in `mcp list` rather than a generic transport error .
507+ // transports. Failure to resolve aborts the connect attempt. The thrown
508+ // message already names the failing header (` headersCommand[<name> ] failed:
509+ // ...`), so the user sees exactly which command broke in `mcp list` .
490510 let dynamicHeaders : Record < string , string > = { }
491511 try {
492512 dynamicHeaders = await resolveHeadersCommand ( mcp . headersCommand , key )
@@ -495,7 +515,7 @@ export namespace MCP {
495515 log . error ( "headersCommand resolution failed" , { key, error : message } )
496516 return {
497517 mcpClient : undefined ,
498- status : { status : "failed" as const , error : `headersCommand failed: ${ message } ` } ,
518+ status : { status : "failed" as const , error : message } ,
499519 }
500520 }
501521 const mergedHeaders : Record < string , string > = { ...( mcp . headers ?? { } ) , ...dynamicHeaders }
0 commit comments