Title: Client: expose an outbound request seam (the send_raw_request TODO) so observers can see a request before it is written · Issue #3715 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Client: expose an outbound request seam (the send_raw_request TODO) so observers can see a request before it is written · Issue #3715 · modelcontextprotocol/python-sdk
X Title: Client: expose an outbound request seam (the send_raw_request TODO) so observers can see a request before it is written · Issue #3715 · modelcontextprotocol/python-sdk
Description: Summary There is no supported way for code outside the SDK to observe or wrap an outgoing client request at the point where it is built and written. The dispatcher already says as much in a TODO: # mcp/shared/jsonrpc_dispatcher.py, JSONR...
Open Graph Description: Summary There is no supported way for code outside the SDK to observe or wrap an outgoing client request at the point where it is built and written. The dispatcher already says as much in a TODO: #...
X Description: Summary There is no supported way for code outside the SDK to observe or wrap an outgoing client request at the point where it is built and written. The dispatcher already says as much in a TODO: #...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3715
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Client: expose an outbound request seam (the send_raw_request TODO) so observers can see a request before it is written","articleBody":"### Summary\n\nThere is no supported way for code outside the SDK to observe or wrap an outgoing client request at the point where it is built and written. The dispatcher already says as much in a TODO:\n\n```python\n# mcp/shared/jsonrpc_dispatcher.py, JSONRPCDispatcher.send_raw_request\n# TODO(maxisbey): move the otel span + inject into an outbound\n# middleware once that seam exists; the dispatcher should not own otel.\nwith otel_span(span_name, kind=SpanKind.CLIENT, ...):\n inject_trace_context(out_meta)\n msg = JSONRPCRequest(jsonrpc=\"2.0\", id=request_id, method=method, params=out_params)\n ...\n await self._write(msg, plan.metadata)\n```\n\nThis issue asks for that seam on the client side, as the counterpart of the existing lowlevel `Server.middleware`. It is related to #1233 (middleware support), which so far discusses the server side only.\n\nVersion checked: `mcp` 2.2.0.\n\n### Use case\n\nInstrumentation and policy tooling that needs to act on a `tools/call` **before** it leaves the process, for example:\n\n- emitting a \"tool call started\" event with the tool name and arguments to an observability pipeline or a policy engine, so a decision can be made while the request is still local;\n- attaching or rewriting `_meta` (correlation ids, session ids) in addition to the trace context;\n- refusing a request locally.\n\nThe event has to be tied to the same trace context that goes on the wire, so it must be emitted from inside the span the dispatcher opens (`MCP send tools/call \u003ctool>`), where `inject_trace_context` runs. That is the only place where the method, the tool name, the arguments and the outgoing `traceparent` are all known together.\n\n### What we do today, and why it is fragile\n\nBecause there is no seam, the only option is to wrap the module-level function `mcp.shared.jsonrpc_dispatcher.inject_trace_context`, which is called inside that span, and use it as the \"request is about to be written\" signal. That depends on:\n\n- a private import name in `jsonrpc_dispatcher` (a rename or a move into `_otel` only would silently stop it working);\n- `inject_trace_context` being called exactly once per request, inside the span, before the write;\n- the arguments being passed through a side channel (a contextvar set by a wrapper around `ClientSession.send_request`), because the hook only receives the `_meta` dict.\n\nWrapping `ClientSession.send_request` instead does not work for this purpose: it runs outside the dispatcher's span, so anything emitted there carries a different span id than the `traceparent` that is sent.\n\n### Proposal\n\nSomething minimal and symmetrical to the server middleware, for example a keyword-only `middleware` on the client session/dispatcher, called once per outgoing request with an object exposing at least:\n\n- `method`, `params` (read-only view of what will be sent) and a mutable `meta` (the `_meta` that will be sent);\n- a way to call the next stage.\n\nand run **inside** the OpenTelemetry span, so that observers see the same span context that `inject_trace_context` uses. The built-in OTel injection could then become the first middleware, which resolves the TODO.\n\nOpen questions:\n\n1. Is this the seam you had in mind in the TODO, or is something different planned for the client?\n2. Should it also cover notifications, or requests only?\n3. Position relative to the OTel span: inside it (needed for the use case above), or outside it?\n4. Would you prefer this as part of #1233, or as a separate piece of work?\n\n### Alternatives considered\n\n- Wrapping `inject_trace_context` (what we do now): works, but depends on private structure as above.\n- Wrapping `ClientSession.send_request`: wrong span, see above.\n- Using the HTTP client hooks (httpx event hooks): they see the already-encoded HTTP request, not the method and arguments, and only exist on HTTP transports, not stdio.\n\nHappy to help with a PR if the shape is agreed.\n\n---\n\nI would like to fix this myself once the shape is agreed (and a maintainer is fine with that). AI disclosure: this issue was drafted with AI assistance (Claude Code); I reviewed the text and checked the quoted code against `mcp` 2.2.0.\n","author":{"url":"https://github.com/rafaelsilva29","@type":"Person","name":"rafaelsilva29"},"datePublished":"2026-10-15T13:13:34Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3715"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d2549c22-7c47-cfd1-0e94-769ab80c8eb5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A2F8:1B13E8:2DBA41E:3BE62B6:6B056B95 |
| html-safe-nonce | 53ba0ca25d5c39ff1e3ec05ec4b6ecd2b8c57b1af17e1a3618422de113c15d2a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMkY4OjFCMTNFODoyREJBNDFFOjNCRTYyQjY6NkIwNTZCOTUiLCJ2aXNpdG9yX2lkIjoiNzI3MTg1NzgyNjk5MjMxOTM4MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 58983899306c5ff1b47892fc5400015e54eb17827ab94f6c853e61432ef2d0a9 |
| hovercard-subject-tag | issue:5866812174 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3715/issue_layout |
| twitter:image | https://opengraph.githubassets.com/297ec7f1a410c1a6c29b5ad44c688476e16a9ce3728a8e49c0c32a4f1f3fc745/modelcontextprotocol/python-sdk/issues/3715 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/297ec7f1a410c1a6c29b5ad44c688476e16a9ce3728a8e49c0c32a4f1f3fc745/modelcontextprotocol/python-sdk/issues/3715 |
| og:image:alt | Summary There is no supported way for code outside the SDK to observe or wrap an outgoing client request at the point where it is built and written. The dispatcher already says as much in a TODO: #... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rafaelsilva29 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9b4256ac5f3e310e7945ef5b71c9cb9e264357b1eb20e91d369ed6502a4c486c |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f089e689ad324655ac44bf6cdbc04d976105b374 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width