@@ -144,9 +144,14 @@ export class McpServerWrapper {
144144
145145 this . logger . info ( {
146146 operation : 'tools_call_request_hierarchical' ,
147+ tool_name : toolName
148+ } , `Handling hierarchical tools/call for ${ toolName } ` ) ;
149+
150+ this . logger . debug ( {
151+ operation : 'tools_call_request_hierarchical_debug' ,
147152 tool_name : toolName ,
148153 args : toolArgs
149- } , `Handling hierarchical tools/call for ${ toolName } ` ) ;
154+ } , `Hierarchical tools/call args for ${ toolName } ` ) ;
150155
151156 if ( toolName === 'discover_mcp_tools' ) {
152157 return await this . handleDiscoverTools ( toolArgs ) ;
@@ -248,9 +253,15 @@ export class McpServerWrapper {
248253 this . logger . info ( {
249254 operation : 'execute_mcp_tool' ,
250255 tool_path : toolPath ,
256+ namespaced_tool_name : namespacedToolName
257+ } , `Executing tool: ${ toolPath } (internal: ${ namespacedToolName } )` ) ;
258+
259+ this . logger . debug ( {
260+ operation : 'execute_mcp_tool_debug' ,
261+ tool_path : toolPath ,
251262 namespaced_tool_name : namespacedToolName ,
252263 arguments : toolArguments
253- } , `Executing tool: ${ toolPath } (internal: ${ namespacedToolName } ) ` ) ;
264+ } , `Tool arguments for ${ toolPath } ` ) ;
254265
255266 // Route to existing executeToolCall with namespaced format
256267 return await this . executeToolCall ( namespacedToolName , toolArguments ) ;
@@ -565,32 +576,44 @@ export class McpServerWrapper {
565576 const finalUrl = this . buildMcpServerUrl ( config . url , config . url_query_params ) ;
566577
567578 // Create transport
568- // TODO Phase 10: MCP SDK StreamableHTTPClientTransport doesn't currently support custom headers
569- // We need to either:
570- // 1. Fork/patch the SDK to accept custom headers in constructor
571- // 2. Use a custom fetch wrapper
572- // 3. Wait for SDK to add header support
573- // For now, we create the transport without headers - this will fail for OAuth-required servers
574579 const transport = new StreamableHTTPClientTransport ( new URL ( finalUrl ) ) ;
575580
576- // WORKAROUND: Monkey-patch the transport's fetch method to inject OAuth headers
581+ // WORKAROUND: Patch global fetch temporarily to inject OAuth headers
582+ // The MCP SDK doesn't currently support custom headers in StreamableHTTPClientTransport
583+ let originalGlobalFetch : typeof fetch | null = null ;
577584 if ( Object . keys ( headers ) . length > 0 ) {
578- const originalFetch = ( transport as any ) . fetch || fetch ;
579- ( transport as any ) . fetch = async ( input : any , init ?: any ) => {
585+ originalGlobalFetch = global . fetch ;
586+ global . fetch = async ( input : any , init ?: any ) => {
587+ // Properly merge headers (handle both Headers object and plain object)
588+ const mergedHeaders : Record < string , string > = { } ;
589+
590+ // Copy existing headers
591+ if ( init ?. headers ) {
592+ if ( init . headers instanceof Headers ) {
593+ init . headers . forEach ( ( value : string , key : string ) => {
594+ mergedHeaders [ key ] = value ;
595+ } ) ;
596+ } else {
597+ Object . assign ( mergedHeaders , init . headers ) ;
598+ }
599+ }
600+
601+ // Add OAuth headers (overwrite to ensure our token is used)
602+ Object . assign ( mergedHeaders , headers ) ;
603+
580604 const modifiedInit = {
581605 ...init ,
582- headers : {
583- ...( init ?. headers || { } ) ,
584- ...headers
585- }
606+ headers : mergedHeaders
586607 } ;
587- return originalFetch ( input , modifiedInit ) ;
608+
609+ return originalGlobalFetch ! ( input , modifiedInit ) ;
588610 } ;
589611
590612 this . logger . debug ( {
591- operation : 'oauth_headers_monkey_patched' ,
592- server_name : serverName
593- } , 'Monkey-patched transport fetch to inject OAuth headers' ) ;
613+ operation : 'oauth_headers_patched_global_fetch' ,
614+ server_name : serverName ,
615+ headers_to_inject : Object . keys ( headers )
616+ } , 'Patched global fetch to inject OAuth headers for tool execution' ) ;
594617 }
595618
596619 try {
@@ -660,6 +683,15 @@ export class McpServerWrapper {
660683 throw new Error ( `HTTP MCP server error: ${ errorMessage } ` ) ;
661684
662685 } finally {
686+ // Restore global fetch if we patched it
687+ if ( originalGlobalFetch ) {
688+ global . fetch = originalGlobalFetch ;
689+ this . logger . debug ( {
690+ operation : 'oauth_headers_restored_global_fetch' ,
691+ server_name : serverName
692+ } , 'Restored global fetch after tool execution' ) ;
693+ }
694+
663695 try {
664696 await client . close ( ) ;
665697 this . logger . debug ( {
0 commit comments