fix(mcp): decouple MCP server discovery from connection to eliminate spurious OAuth redirects#2914
Draft
BnjmnZmmrmn-parafin wants to merge 1 commit intotailcallhq:mainfrom
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
MCP tool calls currently connect to every configured MCP server at chat startup — even servers whose tools aren't needed for the current request. This means invoking the GitHub MCP tool also silently attempts to connect to Notion, Datadog, and every other configured server. If any of those servers requires OAuth and has no cached credential, a browser window opens mid-session without the user asking for it.
This PR decouples discovery (which tools exist, used to build the system prompt) from connection (the live network handshake), so only the server that owns a requested tool is ever connected to.
Changes
crates/forge_services/src/mcp/lazy_client.rs(new)LazyMcpClient<I>— holds server config but defers all network I/O viaArc<OnceCell<I::Client>>until the first tool invocation. Clones share the sameOnceCellso the handshake fires exactly once across concurrent callers.crates/forge_services/src/mcp/service.rsRewrote
ForgeMcpServicewith a two-stage lifecycle:register_servers()— pure config parsing, zero network I/O. BuildsLazyMcpCliententries. Servers that declare their tools statically in config (tools: [...]field) get lightweightToolDefinitionstubs immediately so the LLM sees them in the system prompt without a connection being made.connect_server()/connect_all_pending()— fires only when a tool is actually called.pending_servers.remove()is the mutual-exclusion point: exactly one concurrent caller receives theLazyMcpClient, all others returnOk(()).insert_lazy_client()— after populatingtoolsfrom the live connection, prunes stale declared stubs for that server fromdeclared_toolssocontains_tool_in_memorydoesn't returntruefor tools that were declared in config but absent from the server's actual tool list.get_mcp_servers()— KV cache stores stubs only (never live schemas). Useshad_live_beforesnapshot for the cache-read fast path; re-readshas_live_nowafterlist()for the cache-write gate so live schemas never bleed into subsequent cold starts.crates/forge_app/src/services.rscontains_mcp_tool(&ToolName) -> anyhow::Result<bool>to theMcpServicetrait — pure in-memory check, no network activity.crates/forge_app/src/mcp_executor.rscontains_tool()now delegates tocontains_mcp_tool()instead of callingget_mcp_servers(), eliminating the second eager connection sweep that fired on every tool dispatch.crates/forge_domain/src/mcp.rstools: Vec<String>field to bothMcpStdioServerandMcpHttpServer. Skip-serialized when empty so existing configs are unaffected. Addeddeclared_tools()helper onMcpServerConfig.Behavior
Configuration (optional)
Users can pre-declare tool names in
.mcp.jsonto make them visible in the system prompt without waiting for first use:{ "mcpServers": { "github": { "url": "https://api.githubcopilot.com/mcp/", "headers": { "Authorization": "Bearer {{.env.GITHUB_TOKEN}}" }, "tools": ["get_file_contents", "create_pull_request", "list_commits"] } } }Without
tools, the server's tools are hidden from the system prompt until a tool from that server is first invoked (at which point full schemas are fetched and used for all subsequent calls in that session).Verification