Repository navigation
Mirror x-mcp-header tool arguments into Mcp-Param-* request headers (SEP-2243) #2990
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
Changes from 1 commit
cff6319
c895823
b8a88af
9a84462
33c0d76
0c1c904
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,13 +42,16 @@ | |
| "InboundModernRoute", | ||
| "MCP_METHOD_HEADER", | ||
| "MCP_NAME_HEADER", | ||
| "MCP_PARAM_HEADER_PREFIX", | ||
| "MCP_PROTOCOL_VERSION_HEADER", | ||
| "NAME_BEARING_METHODS", | ||
| "X_MCP_HEADER_KEY", | ||
| "classify_inbound_request", | ||
| "decode_header_value", | ||
| "encode_header_value", | ||
| "find_invalid_x_mcp_header", | ||
| "mcp_param_headers", | ||
| "x_mcp_header_map", | ||
| ] | ||
|
|
||
| MCP_PROTOCOL_VERSION_HEADER: Final = "mcp-protocol-version" | ||
|
|
@@ -206,6 +209,51 @@ def find_invalid_x_mcp_header(input_schema: Any) -> str | None: | |
| return None | ||
|
|
||
|
|
||
| MCP_PARAM_HEADER_PREFIX: Final = "Mcp-Param-" | ||
| """Prefix the `x-mcp-header` token is joined to, forming the per-parameter HTTP header name.""" | ||
|
|
||
|
|
||
| def x_mcp_header_map(input_schema: Any) -> dict[tuple[str, ...], str]: | ||
| """Map each property carrying a valid `x-mcp-header` to its annotation token, keyed by property path. | ||
|
|
||
| The key is the chain of `properties` keys from the schema root to the | ||
| annotated property; a top-level property has a one-element path, a nested | ||
| one a longer path. Call only on a schema that | ||
| :func:`find_invalid_x_mcp_header` accepts; an invalid schema yields an | ||
| undefined subset. | ||
| """ | ||
| mapping: dict[tuple[str, ...], str] = {} | ||
| for path, schema in _walk_schema_positions(input_schema): | ||
| if path and isinstance(header := schema.get(X_MCP_HEADER_KEY), str): | ||
| mapping[path] = header | ||
| return mapping | ||
|
|
||
|
|
||
| def mcp_param_headers(header_map: Mapping[tuple[str, ...], str], arguments: Mapping[str, Any]) -> dict[str, str]: | ||
| """Build the `Mcp-Param-*` headers a `tools/call` mirrors from its arguments. | ||
|
|
||
| For each `(path, token)` in `header_map`, read the value at that property | ||
| path in `arguments` and, when it is present and not `None`, emit | ||
| `Mcp-Param-<token>` carrying it: `bool` as `true`/`false`, other scalars via | ||
| `str`, each passed through :func:`encode_header_value` so a non-token value | ||
| is base64-wrapped. A path that hits a missing key or a non-mapping node is | ||
| skipped, matching the spec's "omit the header when no value is present". | ||
| """ | ||
| headers: dict[str, str] = {} | ||
| for path, token in header_map.items(): | ||
| node: Any = arguments | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn't be Any here!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved the JSON traversal into a small |
||
| for key in path: | ||
| if not isinstance(node, Mapping): | ||
| node = None | ||
| break | ||
| node = cast("Mapping[str, Any]", node).get(key) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cast is not necessary
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the inline |
||
| if node is None: | ||
| continue | ||
| rendered = ("true" if node else "false") if isinstance(node, bool) else str(node) | ||
| headers[f"{MCP_PARAM_HEADER_PREFIX}{token}"] = encode_header_value(rendered) | ||
| return headers | ||
|
|
||
|
|
||
| # INTERNAL_ERROR is deliberately unmapped (→ HTTP 200): the spec assigns no status to | ||
| # -32603, and whether handler-origin errors get 5xx is an open S4 question — see TODO(L66). | ||
| ERROR_CODE_HTTP_STATUS: Final[Mapping[int, int]] = MappingProxyType( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.