Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/mcp/shared/jsonrpc_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,6 @@ async def send_raw_request(
if on_progress is not None:
# The request id doubles as the progress token, so `_pending[token]` finds `on_progress` directly.
out_meta["progressToken"] = request_id
out_params["_meta"] = out_meta

# buffer=1: a close signal can arrive before the waiter parks in receive();
# a WouldBlock later just means the waiter already has its one outcome.
Expand All @@ -386,8 +385,16 @@ async def send_raw_request(
kind=SpanKind.CLIENT,
attributes={"mcp.method.name": method, "jsonrpc.request.id": str(request_id)},
):
# SEP-414: inject W3C trace context; `_meta` stays on the wire even with a no-op tracer.
# SEP-414: inject W3C trace context into `_meta`; the field only
# goes on the wire when it carries something (progress token,
# caller keys, or trace context) — an empty `_meta:{}` is
# rejected as invalid params by strict servers (e.g. Meta's
# hosted Ads MCP).
inject_trace_context(out_meta)
if out_meta:
out_params["_meta"] = out_meta
elif "_meta" in out_params:
del out_params["_meta"]
msg = JSONRPCRequest(jsonrpc="2.0", id=request_id, method=method, params=out_params)
# Surface a pre-existing cancellation while the request provably
# never started; past this point a cancelled write counts as issued.
Expand Down
14 changes: 9 additions & 5 deletions tests/shared/test_jsonrpc_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,9 +1501,10 @@ async def on_notify(ctx: DCtx, method: str, params: Mapping[str, Any] | None) ->


@pytest.mark.anyio
async def test_send_raw_request_always_carries_meta_on_the_wire():
"""Outbound requests always carry `params._meta` (otel injection per SEP-414); caller-supplied
keys are preserved and the progress token is merged in."""
async def test_send_raw_request_carries_meta_only_when_non_empty():
"""Outbound requests omit `params._meta` when it would be empty (strict servers
reject `_meta:{}` as invalid params); caller-supplied keys are preserved and
the progress token is merged in."""
seen: list[Mapping[str, Any] | None] = []

async def server_on_request(ctx: DCtx, method: str, params: Mapping[str, Any] | None) -> dict[str, Any]:
Expand All @@ -1518,13 +1519,16 @@ async def noop_progress(progress: float, total: float | None, message: str | Non
with anyio.fail_after(5):
await client.send_raw_request("a", None)
await client.send_raw_request("b", {"x": 1, "_meta": {"k": "v"}}, opts)
await client.send_raw_request("c", {"x": 1, "_meta": {}})
# `_meta` contents depend on the active otel tracer, so pin only what sits beyond the W3C keys.
w3c = {"traceparent", "tracestate"}
assert seen[0] is not None and seen[0].keys() == {"_meta"}
assert set(seen[0]["_meta"].keys()) <= w3c
# No progress token, no caller keys, no-op tracer in tests: `_meta` is absent entirely.
assert seen[0] is not None and "_meta" not in seen[0]
assert seen[1] is not None and seen[1]["x"] == 1
assert set(seen[1]["_meta"].keys()) - w3c == {"k", "progressToken"}
assert seen[1]["_meta"]["k"] == "v"
# A caller-supplied empty `_meta` must not go on the wire either.
assert seen[2] is not None and "_meta" not in seen[2]


@pytest.mark.anyio
Expand Down
Loading