-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Support routable transport headers for gateways (SEP-2243) #4622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Routable transport headers (SEP-2243) survive a FastMCP HTTP round trip. | ||
|
|
||
| The MCP Python SDK emits the routing headers on the client (`ClientSession`) and | ||
| validates them on the modern streamable-HTTP server transport. These tests are | ||
| FastMCP's regression guard: they prove FastMCP's HTTP layer neither strips nor | ||
| blocks the headers, so a gateway sitting in front of a FastMCP server can route | ||
| on them. The tool echoes back the raw request headers it received, letting the | ||
| test assert on exactly what reached the server. | ||
| """ | ||
|
|
||
| from typing import Annotated | ||
|
|
||
| from pydantic import Field | ||
|
|
||
| from fastmcp.server.dependencies import get_http_request | ||
| from fastmcp.server.server import FastMCP | ||
| from fastmcp.utilities.tests import asgi_server | ||
|
|
||
|
|
||
| def _echo_server() -> FastMCP: | ||
| server = FastMCP() | ||
|
|
||
| @server.tool | ||
| def echo_headers( | ||
| tenant: Annotated[ | ||
| str, Field(json_schema_extra={"x-mcp-header": "Tenant"}) | ||
| ] = "acme", | ||
| ) -> dict[str, str]: | ||
| """Return the raw HTTP headers the server received for this request.""" | ||
| return dict(get_http_request().headers) | ||
|
|
||
| return server | ||
|
|
||
|
|
||
| async def test_mcp_method_and_name_headers_reach_server(): | ||
| """`Mcp-Method` and `Mcp-Name` set by the SDK client arrive at the server.""" | ||
| async with asgi_server(_echo_server(), transport="http") as running_server: | ||
| async with running_server.client() as client: | ||
| result = await client.call_tool("echo_headers") | ||
|
|
||
| headers = result.data | ||
| assert headers["mcp-method"] == "tools/call" | ||
| assert headers["mcp-name"] == "echo_headers" | ||
|
|
||
|
|
||
| async def test_mcp_param_header_reaches_server(): | ||
| """An `x-mcp-header` annotated parameter is mirrored into `Mcp-Param-*`. | ||
|
|
||
| The SDK client only emits `Mcp-Param-*` once it has seen the tool's input | ||
| schema, so the test lists tools before calling. | ||
| """ | ||
| async with asgi_server(_echo_server(), transport="http") as running_server: | ||
| async with running_server.client() as client: | ||
| await client.list_tools() | ||
| result = await client.call_tool("echo_headers", {"tenant": "beta-corp"}) | ||
|
|
||
| headers = result.data | ||
| assert headers["mcp-param-tenant"] == "beta-corp" | ||
|
|
||
|
|
||
| async def test_routing_headers_survive_host_origin_protection(): | ||
| """The Host/Origin request guard does not strip the routing headers.""" | ||
| async with asgi_server( | ||
| _echo_server(), | ||
| transport="http", | ||
| host_origin_protection=True, | ||
| allowed_hosts=["*"], | ||
| allowed_origins=["*"], | ||
| ) as running_server: | ||
| async with running_server.client() as client: | ||
| await client.list_tools() | ||
| result = await client.call_tool("echo_headers", {"tenant": "gamma"}) | ||
|
|
||
| headers = result.data | ||
| assert headers["mcp-method"] == "tools/call" | ||
| assert headers["mcp-name"] == "echo_headers" | ||
| assert headers["mcp-param-tenant"] == "gamma" | ||
|
|
||
|
|
||
| async def test_x_mcp_header_annotation_survives_schema_generation(): | ||
| """FastMCP preserves `x-mcp-header` in a tool's advertised input schema. | ||
|
|
||
| This is the annotation the SDK client reads to decide which arguments to | ||
| mirror into `Mcp-Param-*` headers, so it must reach the wire unchanged. | ||
| """ | ||
| server = _echo_server() | ||
| tools = await server._list_tools() | ||
| (tool,) = [t for t in tools if t.name == "echo_headers"] | ||
| assert tool.parameters["properties"]["tenant"]["x-mcp-header"] == "Tenant" |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With a Streamable HTTP backend, this documented proxy scenario fails rather than routing successfully:
ProxyTool.run()creates a fresh backend client and callsclient.session.call_tool()without first listing tools, so the SDK has not cached the advertisedx-mcp-headerannotation and omitsMcp-Param-Tenant; the modern backend then rejects the call withHEADER_MISMATCHbecause the body containstenantwithout its required header. The added proxy test uses an in-process backend and only checks the re-advertised schema, so it never exercises the validating HTTP hop; add an HTTP round-trip test and ensure the backend session absorbs or lists the tool schema before calling it.AGENTS.md reference: AGENTS.md:L146-L152
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed and fixed in 5be0c22.
You're right — this reproduces over a real HTTP hop. A modern Streamable-HTTP backend rejects the proxied call with
HEADER_MISMATCH("Mcp-Param-Tenant header is missing but the request body's 'tenant' argument is present"), becauseProxyTool.run()callsclient.session.call_tool()directly and the SDK only emitsMcp-Param-*for tools it has cached vialist_tools. My earlier proxy test used an in-process backend and only checked the re-advertised schema, so it never exercised the validating hop.Note the failure is gated on a modern backend:
ProxyClientdefaults tomode="legacy", which neither emits nor validates these headers, so the default path was unaffected — butmode="auto"against a modern backend fails.The fix seeds the backend session's header map from the tool's already-mirrored input schema before the call, using the SDK's own
x_mcp_header_map, so no extralist_toolsround-trip is added and an unannotated tool produces no headers:New test
test_proxy_forwards_mcp_param_header_to_modern_http_backendputs a FastMCP proxy (mode="auto") in front of a real modern Streamable-HTTP backend with anx-mcp-header-annotated param and asserts the call routes through successfully — it reproduces the failure without the fix and passes with it.