You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following content is AI-generated and provides a summary of the pull request:
Fix: Authenticate Remote MCP Server Handshake Per-Request
This PR fixes a bug where remote MCP servers protected by @requires authentication would never yield tools because auth headers were not sent during the tools/list handshake — only during tool invocations.
What Changed
Root Cause: The previous implementation used beforeToolCall to inject auth headers, but this callback is only invoked during _callTool(), not during initializeConnections() (where tools/list happens). This meant the discovery handshake went out unauthenticated, silently resulting in zero tools.
Fix: The MCP connection flow is redesigned using a deferred/dynamic approach:
srv/handlers/mcp-tools.js: buildMcpToolsFromConnection() no longer eagerly connects to the MCP server or fetches tools. Instead, it returns a lightweight placeholder object { _mcpDynamic: true, mcpUrl, resolveHeaders }. The MultiServerMCPClient instantiation, wrapToolsWithErrorHandling, and related code are removed.
lib/agents/middleware/remote-mcp.js(new file): A new remoteMcpMiddleware is introduced that resolves MCP tools per-request using the current user's auth headers. It:
Intercepts model calls (wrapModelCall) to replace placeholder tools with real tools fetched from the MCP server using fresh credentials
Caches resolved tools on cds.context.__mcpDynamicTools for the lifetime of the request to avoid redundant tools/list calls in multi-turn loops
Handles tool invocations (wrapToolCall) with proper error handling, returning errors as ToolMessage instead of crashing the graph
Also fixes the latent Invalid transport type: undefined error by explicitly setting transport: "http" in mcpServers config
lib/agents/middleware/index.js: Conditionally registers remoteMcpMiddleware when any tool has _mcpDynamic: true, placing it first in the middleware chain.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #48