Skip to content

fix(mcp): resolve tools with cold server mapping - #44498

Closed
ycwhencpp wants to merge 4 commits into
BerriAI:mainfrom
ycwhencpp:fix/mcp-cold-tool-mapping
Closed

ycwhencpp wants to merge 4 commits into
BerriAI:mainfrom
ycwhencpp:fix/mcp-cold-tool-mapping

Conversation

@ycwhencpp

Copy link
Copy Markdown

TLDR

Problem

DB-backed MCP servers can start with a cold tool_name_to_mcp_server_name_mapping. In this state, a direct tools/call can fail with Tool <name> not found even though the MCP server exposes the requested tool.

Fix

  • Identify the candidate MCP server from the requested tool call.
  • Lazily load that server's tool mapping when its mapping is cold.
  • Continue using the existing tool ownership/resolution checks after discovery.
  • Preserve rejection of tools that are not exposed by the requested server.

User Flow

Before

  1. A DB-backed MCP server is loaded after proxy startup/restart.
  2. Its tool mapping is initially empty.
  3. The client calls tools/call directly for a valid tool.
  4. The proxy cannot resolve the tool and returns Tool <name> not found.
  5. Calling tools/list first warms the mapping, after which tools/call succeeds.

After

  1. A DB-backed MCP server is loaded after proxy startup/restart.
  2. The client calls tools/call directly for a valid tool.
  3. The proxy detects the cold mapping and loads the tools for that server.
  4. Existing resolution and ownership checks run against the populated mapping.
  5. The tool call succeeds without requiring a prior tools/list.

Relevant issues

Fixes #44373

Tests

Added coverage for:

  • warming a cold MCP server mapping
  • skipping discovery when the mapping is already warm
  • integrating mapping warm-up into call_tool()
  • preserving existing failure behavior for unknown and foreign tools

Targeted test result:

12 passed

Proof of Fix

DB-backed MCP server

Verified registration through the management API with:

  • is_config: false
  • server: e2e_math
  • transport: HTTP
  • deterministic upstream MCP tools: add, multiply

E2E cold-mapping verification

[Add the actual result here after restarting the proxy and calling tools/call directly without tools/list.]

Expected behavior:

  • add(10, 20) succeeds immediately after restart.
  • No prior tools/list request is made.
  • An unknown tool continues to fail.

Type

🐛 Bug Fix

Caveats

Low

A cold server mapping requires one upstream tools/list request before the first tool call can be resolved.

@ycwhencpp
ycwhencpp requested a review from a team October 4, 2026 11:19
@greptile-apps

greptile-apps Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 3/5

[Medium risk] Adds tool mapping warm-up logic to the MCP server manager.

This PR is not safe to merge until direct cold calls use the caller’s discovery credentials and warming checks the requested tool.

Findings

  1. P1 Discovery drops caller credentials ▶
  2. P1 One tool masks cold mappings ▶
  3. P2 Concurrent calls duplicate discovery ▶
  4. P2 Async test marker misspelled ▶

Summary

The PR attempts to populate a cold MCP tool mapping before resolving a direct tool call and adds focused unit tests. The new discovery omits caller credentials, treats any server mapping as complete, and can duplicate discovery under concurrent first calls.

Reviews (1) · Last reviewed commit: "fix(mcp): resolve tools with cold server..."

if self._has_mapped_tools(server):
return

await self._get_tools_from_server(server)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Discovery drops caller credentials

On a direct first call to a cold server that requires a caller credential, this fetches tools/list without the BYOK or OAuth credentials and identity already available to call_tool(). Discovery also runs before the BYOK check. A valid call can fail with an upstream authentication error instead of resolving its tool, and a caller missing a BYOK credential can receive that error instead of the intended gateway challenge.

Knowledge Base Used: Proxy endpoints and request processing

)

async def _ensure_tool_mapping_for_server(self, server: MCPServer) -> None:
if self._has_mapped_tools(server):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 One tool masks cold mappings

This treats the server as warm when any of its tools is mapped, even if the requested tool is not. If one tool has been mapped but another exposed tool is still unmapped, discovery is skipped and the direct call can still return Tool <name> not found. Check whether the requested tool is mapped before skipping discovery.

Knowledge Base Used: Proxy endpoints and request processing

Comment on lines +2860 to +2864
async def _ensure_tool_mapping_for_server(self, server: MCPServer) -> None:
if self._has_mapped_tools(server):
return

await self._get_tools_from_server(server)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Concurrent calls duplicate discovery

Concurrent first calls can all see the shared mapping as cold before any fetch finishes. Each then makes its own upstream tools/list request, creating avoidable connection and catalog work during a cold start. Coalescing in-flight discovery per server would prevent that burst.


assert resolved is server

@pytest.mark.asyncioc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Async test marker misspelled

asyncioc is not a declared pytest marker. Automatic asyncio mode still runs this test today, but the marker has no effect and collecting the file with strict marker validation would fail. Use asyncio, as the adjacent tests do.

Suggested change
@pytest.mark.asyncioc
@pytest.mark.asyncio

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@codecov

codecov Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!


def _get_server_for_tool_call(self, server_name: str, name: str) -> MCPServer | None:
prefixed_tool_name = add_server_prefix_to_name(name, server_name)
return self.server_owning_tool_name_prefix(prefixed_tool_name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: Cross-server authorization bypass

This lookup matches the longest prefix of the concatenated server/tool name, rather than the explicit server: if public (ID public-id) exposes admin-read, and a cold private server with configured ID public-admin exposes both read and admin-read, calling {"server_id":"public-id","name":"admin-read","arguments":{}} warms the private server and overwrites the public-admin-read mapping. The subsequent resolver dispatches admin-read to that private server after operations._execute_mcp_tool has already authorized public; the later tool-permission check does not repeat server authorization, so a key granted only public can execute the private tool.

Resolve the warmup target from the exact server identifier rather than the concatenated tool name, and keep discovery and dispatch bound to the authorized server.

@veria-ai

veria-ai Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

This PR updates MCP tool resolution to handle cold server mappings, warming server tool catalogs when an explicitly requested tool is not yet mapped.

Two security issues remain open, with none addressed so far. Ambiguous server/tool name matching can cause a request authorized for a public server to dispatch to a private server instead. The warmup path can also forward the caller’s LiteLLM admission key to a malicious upstream, exposing that credential even when tool resolution ultimately fails.

Open issues (2)

Fixed/addressed: 0 · PR risk: 8/10

@codspeed

codspeed Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing ycwhencpp:fix/mcp-cold-tool-mapping (7ae2599) with main (b4fcc5c)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (a2bf67a) during the generation of this report, so b4fcc5c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@CLAassistant

CLAassistant commented Oct 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ ycwhencpp
❌ Anurag


Anurag seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

server=mcp_server,
tool_name=name,
mcp_auth_header=mcp_auth_header,
extra_headers=oauth2_headers,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High: LiteLLM admission key forwarded upstream

For oauth_delegate/true_passthrough REST calls, oauth2_headers contains the incoming Authorization, even when it was used as the LiteLLM admission credential. A malicious upstream can return an empty catalog for a request with an explicit server_id and an unmapped tool, then receive the caller's key on this warmup request before tool resolution fails; unlike normal discovery and execution, this path never applies _should_strip_caller_authorization.

Prepare warmup credentials through the existing per-server header preparation and admission-token filtering instead of passing oauth2_headers unchanged.

@mateo-berri

Copy link
Copy Markdown
Contributor

Main lists a cold server with the caller's credentials before its first tool call since #42072, so closing this. Reopen if something is missing

@mateo-berri mateo-berri closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: MCP tools/call returns 404 'Tool not found' after proxy restart for DB-managed servers (cold tool-name mapping)

3 participants