-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expose the middleware chain on MCPServer and stop sending unrequested change notifications #3201
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 all commits
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 |
|---|---|---|
|
|
@@ -5,12 +5,14 @@ | |
| You write it as `async (ctx, call_next)` and append it to `server.middleware`. That is the whole API. | ||
|
|
||
| !!! warning | ||
| `Server.middleware` is marked **provisional** in the source. The signature and semantics are | ||
| expected to change before v2 is final. Use it to *observe*: timing, logging, tracing. | ||
| Do not make it the foundation your server stands on. | ||
| The middleware list is marked **provisional** in the source. The signature and semantics are | ||
| expected to change before v2 is final. Use it to *observe* (timing, logging, tracing) and to | ||
| *refuse* messages; do not make it the foundation your server stands on. | ||
|
|
||
| This is a **low-level `Server`** feature. `MCPServer` does not expose a middleware list. | ||
| If `Server(name, on_call_tool=...)` is new to you, read **[The low-level Server](low-level-server.md)** first. | ||
| `MCPServer` takes the list at construction (`MCPServer(name, middleware=[...])`) and exposes it as | ||
| `mcp.middleware`; the low-level `Server` exposes the same list as `server.middleware`. The example | ||
| below uses the low-level `Server`; if `Server(name, on_call_tool=...)` is new to you, read | ||
| **[The low-level Server](low-level-server.md)** first. | ||
|
|
||
| ## A timing middleware | ||
|
|
||
|
|
@@ -57,7 +59,10 @@ | |
|
|
||
| * **Observe.** Time it, count it, log it. The example above. | ||
| * **Refuse.** Raise an `MCPError` *instead of* calling `call_next(ctx)` and that one message is | ||
| answered with a JSON-RPC error. The connection stays up; the next message goes through. | ||
| answered with a JSON-RPC error. The connection stays up; the next message goes through. This is | ||
| how a server gates `subscriptions/listen` per caller: | ||
| **[Deciding who may watch](../handlers/subscriptions.md#deciding-who-may-watch)** on the | ||
| Subscriptions page walks through it. | ||
| * **Rewrite.** `ctx` is a dataclass: `await call_next(dataclasses.replace(ctx, params=...))` | ||
| hands the rest of the chain different params than the client sent. Never do this to | ||
| `initialize`: the result the client gets back is built from your rewritten params, but the | ||
|
|
@@ -98,8 +103,8 @@ | |
|
|
||
| ## Recap | ||
|
|
||
| * A middleware is `async (ctx, call_next) -> result`, appended to `server.middleware` on the | ||
| low-level `Server`. | ||
| * A middleware is `async (ctx, call_next) -> result`, passed as `MCPServer(middleware=[...])` (or | ||
| appended to `mcp.middleware`), and appended to `server.middleware` on the low-level `Server`. | ||
|
Check warning on line 107 in docs/advanced/middleware.md
|
||
|
Comment on lines
+106
to
+107
Contributor
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. 🟡 Four sentences missed in this PR's docs sweep now contradict the middleware surface the PR itself exposes: (1) this page's closing Recap bullet still says "Observe with it; don't build on it", contradicting the rewritten top warning (observe and refuse) and the new subscriptions/listen gating pitch; (2) docs/migration.md:1603 likewise still says "use middleware to observe (log, time, trace) rather than as a foundation"; (3) the "one middleware that ships on by default" section says "The SDK ships exactly one middleware", but mcp.middleware carries two SDK built-ins on MCPServer (OpenTelemetry plus RequestStateBoundary); (4) docs/run/opentelemetry.md:86-88 still edits the chain via the private mcp._lowlevel_server.middleware reach-through that the new public mcp.middleware supersedes. One small doc sweep fixes all four. Extended reasoning...What's stale. This PR rescopes docs/advanced/middleware.md to cover 1. The closing Recap bullet on this page ( 2. 3. "The one middleware that ships on by default" ( Step-by-step proof for (3): (a) 4. Fix. One doc sweep: reword the middleware.md recap bullet (e.g. "Observe and refuse with it; don't build your server's core on it."), align migration.md:1603 ("...to observe (log, time, trace) and to refuse messages rather than as a foundation"), reword the "exactly one" section (the low-level Severity. Docs-only; nothing breaks at runtime, so nit. Distinct from the already-posted comments (the |
||
| * It wraps **every** inbound message (`server/discover`, `initialize`, requests, notifications, | ||
| unknown methods) and runs outermost-first. | ||
| * `ctx.request_id is None` is how you tell a notification from a request. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ Duplicate events waiting to be consumed collapse into one, and refetching still | |
|
|
||
| Two more properties of the handle: | ||
|
|
||
| * `sub.honored` is the filter the server acknowledged: a `SubscriptionFilter` with the fields you passed, read as attributes (`sub.honored.prompts_list_changed`). `MCPServer` honors every kind you ask for, so it echoes your request back. A server that narrows the filter (see the [filter warning](../handlers/subscriptions.md#only-what-was-asked-for) on the server page) acknowledges less, and an honored kind may still never fire. | ||
| * `sub.honored` is the filter the server acknowledged: a `SubscriptionFilter` with the fields you passed, read as attributes (`sub.honored.prompts_list_changed`). `MCPServer` honors every kind you ask for, so it echoes your request back. A server that supports fewer kinds acknowledges less, and an honored kind may still never fire. A server may also refuse the whole request rather than acknowledge it (see [Deciding who may watch](../handlers/subscriptions.md#deciding-who-may-watch) on the server page), which surfaces as the request's error. | ||
|
Contributor
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. 🟡 The closing recap on this page (line 86, "Publishing these events, narrowing the filter, and scaling past one process are the server's story") still points readers to a filter-narrowing story that this PR deliberately removed from the linked server page in favor of whole-request refusal. Consider rewording it to match the new design, e.g. "Publishing these events, deciding who may watch, and scaling past one process are the server's story." Extended reasoning...What the bug is. The closing recap paragraph of Why it contradicts this PR's own design. The replacement section, Deciding who may watch, takes the opposite stance: a middleware refuses the whole Why this is an oversight introduced by this PR, not pre-existing staleness. The PR clearly intended to sweep this page: the parallel narrowing reference in the Concrete reader walk-through. (1) A reader finishes the client subscriptions page and hits the recap: "narrowing the filter... [is] the server's story: Subscriptions." (2) They follow the link expecting guidance on how a server narrows an acknowledged filter. (3) The server page now says Impact and fix. Docs-only; nothing breaks at runtime, but the page is internally inconsistent with the change this PR makes (line 23 describes refusal, line 86 describes narrowing) and dangles a pointer to removed content. One-sentence reword fixes it, e.g.: "Publishing these events, deciding who may watch, and scaling past one process are the server's story: Subscriptions." Severity. All three verifiers confirmed and none refuted. Merging as-is causes no failure a user hits — this is a wording inconsistency in prose — so it does not meet the bar for blocking. Nit. (Distinct from the |
||
| * `sub.subscription_id` is the listen request's id, the one stamped on every frame of this stream. Several subscriptions can be open at once, each demultiplexed by its own id. | ||
|
|
||
| ## Watching without blocking | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -742,7 +742,7 @@ This parameter was redundant because the SSE transport already handles sub-path | |||||
|
|
||||||
| ### Transport-specific parameters moved from MCPServer constructor to run()/app methods | ||||||
|
|
||||||
| Transport-specific parameters have been moved off the `MCPServer` constructor and onto `run()`, `sse_app()`, and `streamable_http_app()`, so transport configuration is passed when starting or building the server. The rest of the constructor is unchanged: identity (`name`, `instructions`, `website_url`, `icons`, plus the newly added positional `title`, `description`, and `version` covered [above](#mcpserver-constructor-title-description-and-version-added-to-the-positional-parameters)), authentication (`auth`, `token_verifier`, `auth_server_provider`), `lifespan`, `dependencies`, `tools`, `debug`, `log_level`, and the `warn_on_duplicate_*` flags; the new keyword-only parameters (`resources`, `extensions`, `resource_security`, `request_state_security`, `cache_hints`, `subscriptions`) are additive. | ||||||
| Transport-specific parameters have been moved off the `MCPServer` constructor and onto `run()`, `sse_app()`, and `streamable_http_app()`, so transport configuration is passed when starting or building the server. The rest of the constructor is unchanged: identity (`name`, `instructions`, `website_url`, `icons`, plus the newly added positional `title`, `description`, and `version` covered [above](#mcpserver-constructor-title-description-and-version-added-to-the-positional-parameters)), authentication (`auth`, `token_verifier`, `auth_server_provider`), `lifespan`, `dependencies`, `tools`, `debug`, `log_level`, and the `warn_on_duplicate_*` flags; the new keyword-only parameters (`resources`, `extensions`, `resource_security`, `request_state_security`, `cache_hints`, `subscriptions`, `middleware`) are additive. | ||||||
|
|
||||||
| **Parameters moved:** | ||||||
|
|
||||||
|
|
@@ -1718,7 +1718,7 @@ The helpers keep their v1 signatures, so calls through `ctx.session` are source- | |||||
| Behavior changes: | ||||||
|
|
||||||
| - **A new `ServerSession` proxy is built for every inbound message.** In v1 one `ServerSession` lived for the whole connection, and servers commonly keyed per-client state on `ctx.session` identity (a `WeakKeyDictionary[ServerSession, ...]`, `id(ctx.session)`, a set of captured sessions to notify later). In v2 each request and notification gets a fresh proxy over the same connection, so those idioms silently misbehave: a session-keyed dict never finds an earlier key, and a broadcast set grows by one entry per request, sending duplicates. Key on something connection-stable instead — on stateful streamable HTTP the `mcp-session-id` request header names the transport session (read it via `ctx.headers` on `MCPServer` or `ctx.request.headers` in a lowlevel handler); on stdio there is one connection per process. The per-connection object the proxies share is `mcp.server.connection.Connection` (`state`, `session_id`, `exit_stack`), which is not currently reachable from `ctx`. | ||||||
| - **A captured `ctx.session` stays usable after the handler returns.** The proxy holds the connection, not the request, so a background task can keep calling `send_resource_updated()` / `send_tool_list_changed()` on it while the client stays connected; with `related_request_id` omitted these ride the standalone stream as in v1. A request-scoped send is only meaningful while that request is in flight — once the handler returns, that stream is closed and the message is dropped with a debug log. | ||||||
| - **A captured `ctx.session` stays usable after the handler returns.** The proxy holds the connection, not the request, so a background task can keep calling `send_resource_updated()` / `send_tool_list_changed()` on it while the client stays connected; with `related_request_id` omitted these ride the standalone stream as in v1 — except on a 2026-era connection, where change notifications are dropped and belong on the subscription bus instead ([change notifications travel only on `subscriptions/listen` streams](#change-notifications-travel-only-on-subscriptionslisten-streams)). A request-scoped send is only meaningful while that request is in flight — once the handler returns, that stream is closed and the message is dropped with a debug log. | ||||||
| - **Notifications after the connection has closed are dropped instead of raising.** In v1 the notification helpers raised `anyio.ClosedResourceError`/`anyio.BrokenResourceError` on a dead connection, and broadcast loops used that exception to prune sessions. In v2 the send returns normally (the drop is debug-logged), so probe with a request instead: `await session.send_ping()` raises `MCPError` once the connection has closed. On a 2026-07-28 connection, though, every server-initiated request raises `NoBackChannelError` (an `MCPError`) regardless, so a ping is a liveness probe only on connections negotiated at 2025-11-25 or earlier. | ||||||
|
|
||||||
| `ServerSession` is normally constructed for you by `Server.run()` and reached via `ctx.session` in handlers, so beyond the behavior changes above, most servers are unaffected. If you were constructing or subclassing it directly: | ||||||
|
|
@@ -2804,7 +2804,7 @@ rest of this guide stays focused on the v1-to-v2 upgrade itself. | |||||
|
|
||||||
| The 2026-07-28 protocol has no server-initiated requests, so a handler that reaches back to the client mid-request — `ctx.elicit()`, `ctx.elicit_url()`, `ctx.session.create_message()`, `ctx.session.list_roots()`, or any other `ServerSession` request helper — raises `NoBackChannelError` on such a connection instead of sending. An in-process `Client(server)` negotiates 2026-07-28 by default (see [`Client` defaults to `mode='auto'`](#client-defaults-to-modeauto)), so the first smoke test of an unchanged v1 sampling or elicitation tool fails, and setting `sampling_callback=` / `elicitation_callback=` on the client changes nothing because no request ever reaches the client. | ||||||
|
|
||||||
| `NoBackChannelError` lives in `mcp.shared.exceptions` and subclasses `MCPError` (code `-32600`, message `Cannot send '<method>': this transport context has no back-channel for server-initiated requests.`). Raised inside an `@mcp.tool()` it reaches the client as a top-level JSON-RPC error, not `CallToolResult(is_error=True)` — see [`MCPError` raised from an `@mcp.tool()` handler now surfaces as a JSON-RPC error](#mcperror-raised-from-an-mcptool-handler-now-surfaces-as-a-json-rpc-error) — and the [Troubleshooting](troubleshooting.md) page walks through the client-side traceback. The same exception is raised on a legacy session against a `stateless_http=True` server, and on the request-scoped channel of a stateful legacy session against a `json_response=True` server (a JSON body carries exactly one response, so a mid-request `ctx.elicit()` cannot ride it; the session's standalone `GET` stream still carries unrelated messages) — both places v1 dropped the message and stalled ([`Server.run()` no longer takes a `stateless` flag](#serverrun-no-longer-takes-a-stateless-flag)). Notifications never raise it: `send_log_message()`, `send_tool_list_changed()`, and the other notification helpers are dropped with a debug log where no channel exists, and `UrlElicitationRequiredError` from a tool is unaffected (it is an error response, not a request). | ||||||
| `NoBackChannelError` lives in `mcp.shared.exceptions` and subclasses `MCPError` (code `-32600`, message `Cannot send '<method>': this transport context has no back-channel for server-initiated requests.`). Raised inside an `@mcp.tool()` it reaches the client as a top-level JSON-RPC error, not `CallToolResult(is_error=True)` — see [`MCPError` raised from an `@mcp.tool()` handler now surfaces as a JSON-RPC error](#mcperror-raised-from-an-mcptool-handler-now-surfaces-as-a-json-rpc-error) — and the [Troubleshooting](troubleshooting.md) page walks through the client-side traceback. The same exception is raised on a legacy session against a `stateless_http=True` server, and on the request-scoped channel of a stateful legacy session against a `json_response=True` server (a JSON body carries exactly one response, so a mid-request `ctx.elicit()` cannot ride it; the session's standalone `GET` stream still carries unrelated messages) — both places v1 dropped the message and stalled ([`Server.run()` no longer takes a `stateless` flag](#serverrun-no-longer-takes-a-stateless-flag)). Notifications never raise it: `send_log_message()`, `send_tool_list_changed()`, and the other notification helpers are dropped with a debug log where no channel exists (and the change-notification helpers are dropped on every 2026-era connection, channel or not — see [change notifications travel only on `subscriptions/listen` streams](#change-notifications-travel-only-on-subscriptionslisten-streams)), and `UrlElicitationRequiredError` from a tool is unaffected (it is an error response, not a request). | ||||||
|
|
||||||
| Two ways to migrate: | ||||||
|
|
||||||
|
|
@@ -2856,6 +2856,12 @@ async with Client(server, logging_callback=on_log, log_level="info") as client: | |||||
|
|
||||||
| `log_level=None` (the default) means no opt-in — a `logging_callback` alone is not one — and a single request can override the client-wide default by supplying the key in its own `meta=` (e.g. `meta={LOG_LEVEL_META_KEY: "debug"}` from `mcp_types`). The opt-in is what the spec calls for on 2026-era servers generally, not just this SDK's. Because 2026 log delivery is request-scoped by construction, `related_request_id` on `send_log_message` no longer selects the standalone stream there: whatever is delivered rides the requesting stream. | ||||||
|
|
||||||
| ### Change notifications travel only on `subscriptions/listen` streams | ||||||
|
|
||||||
| On a 2026-07-28 connection, `notifications/tools/list_changed`, `notifications/prompts/list_changed`, `notifications/resources/list_changed`, and `notifications/resources/updated` reach a client only through a `subscriptions/listen` stream it opened — the spec forbids sending a notification type a subscription did not request. The v1-style session helpers (`ctx.session.send_tool_list_changed()`, `send_prompt_list_changed()`, `send_resource_list_changed()`, `send_resource_updated(uri)`) push a bare copy onto the connection's standalone channel instead, so on such a connection they are dropped with a debug log: silently on streamable HTTP (there is no standalone channel), and on stdio, where earlier v2 releases wrote the bare notification to the shared pipe, it is now dropped too. On pre-2026 connections the helpers behave as in v1. | ||||||
|
|
||||||
| Migrate to publishing on the subscription bus, which stamps and filters per stream: `await ctx.notify_tools_changed()`, `notify_prompts_changed()`, `notify_resources_changed()`, and `notify_resource_updated(uri)` on `MCPServer`'s `Context`, or `await bus.publish(...)` on a low-level `Server`'s own `SubscriptionBus` — see [Subscriptions](handlers/subscriptions.md). A stream only ever receives the kinds and URIs the server acknowledged for it; to gate per caller which subscriptions may be opened, refuse `subscriptions/listen` in a middleware (`MCPServer(middleware=[...])`), covered on the same page. | ||||||
|
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. P2: Migrated prompt/resource notifications will create unawaited coroutines rather than publish events if readers follow the listed calls. Show Prompt for AI agents
Suggested change
|
||||||
|
|
||||||
| ### Servers validate `Mcp-Param-*` headers against the request body ([SEP-2243](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2243)) | ||||||
|
|
||||||
| On the 2026-07-28 Streamable HTTP path, a `tools/call` whose tool declares `x-mcp-header` annotations is validated before dispatch — each annotated argument and its mirroring `Mcp-Param-*` header must be present together and agree (after base64-sentinel decoding; integers compare numerically), or absent together. A violation is rejected with HTTP 400 and JSON-RPC error `-32020` (`HeaderMismatch`), as the spec requires. A client that sends an annotated argument *without* its header — for example one that never listed the tool — is therefore rejected instead of silently served; the spec's recovery is to re-list and retry. On the client side, `ClientSession.call_tool` emits these headers automatically for annotated arguments of any tool it has listed; list the tool first, and note that pre-2026 connections and non-HTTP transports never emit them. | ||||||
|
|
||||||
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.
🟡 docs/advanced/index.md (lines 10-11, not touched by this diff) still says Pagination and Middleware are "two things you can only do on the low-level
Server" — this PR makes that false for middleware, contradicting the rewritten claim on this page and the new tutorial006, which gatessubscriptions/listenentirely viaMCPServer(middleware=[...]). Reword the index bullet so only pagination keeps the low-level-only claim.Extended reasoning...
What's stale. The Advanced section's index page,
docs/advanced/index.mdlines 10-11, reads:Before this PR that sentence was accurate —
docs/advanced/middleware.mditself said "This is a low-levelServerfeature.MCPServerdoes not expose a middleware list." This PR deliberately removes that limitation:MCPServernow acceptsmiddleware=[...]at construction and exposes the live chain asmcp.middleware(src/mcp/server/mcpserver/server.py), and the diff rewrites the claim ondocs/advanced/middleware.mdaccordingly ("MCPServertakes the list at construction ... and exposes it asmcp.middleware"). The index bullet was missed.Why it matters. The index bullet directly contradicts the very page it links to. A reader who lands on the Advanced index — the natural entry point when deciding whether they need the low-level
Server— concludes that gatingsubscriptions/listenper caller requires dropping down toServer(name, on_call_tool=...). That is exactly the misconception this PR set out to remove: the newdocs/handlers/subscriptions.md"Deciding who may watch" section anddocs_src/subscriptions/tutorial006.pydo the whole gate withMCPServer("Reports", middleware=[gate_subscriptions]), never touching the low-levelServer.Concrete walk-through.
files://payroll.csvand opens the docs' Advanced index.Server, so they start rewriting theirMCPServerapp ontoServer(name, on_list_tools=..., on_call_tool=..., ...)with hand-written handlers.middleware.md(updated by this PR) orhandlers/subscriptions.md(added by this PR), they'd seeMCPServer(name, middleware=[...])does it in one keyword argument. The two pages now disagree, and the index is the wrong one.Why nothing else catches it. The docs tests (
tests/docs_src/test_subscriptions.py) exercise the tutorial code, not the prose on the index page, and the file isn't in this diff, so no reviewer of the changed hunks sees the contradiction.AGENTS.mdasks that docs pages affected by a public-API change be updated in the same PR — this bullet is affected (the PR touched five other docs pages for exactly this reason) and was the one spot missed.Fix. Reword the bullet so only pagination keeps the exclusivity claim, e.g.:
(or any phrasing that stops claiming middleware is low-level-only).
Severity. Nit: it's a one-line documentation inconsistency introduced by this PR — nothing breaks at runtime, but it steers readers toward unnecessary low-level rewrites and contradicts the page it links to, so it's worth fixing in the same PR.