Skip to content

Commit 6b1f1c9

Browse files
committed
Merge remote-tracking branch 'origin/main' into perf/cache-tool-output-schema-validators
# Conflicts: # src/mcp/client/session.py
2 parents 2e2fcec + 11934c9 commit 6b1f1c9

142 files changed

Lines changed: 3359 additions & 1612 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/actions/conformance/expected-failures.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,24 @@
1010
# scenarios start passing and MUST be removed from this list (the runner fails
1111
# on stale entries), so the baseline burns down per milestone.
1212

13-
client: []
13+
client:
14+
# SEP-1932 (DPoP): the SDK's OAuth client does not implement DPoP proofs.
15+
# These scenarios are new in the da56f663 referee pin. The entries are
16+
# per-check (conformance #406) because both scenarios pass their
17+
# non-DPoP checks (discovery, token acquisition, request flow) live.
18+
- auth/dpop:sep-1932-client-token-request-proof
19+
- auth/dpop:sep-1932-client-dpop-auth-scheme
20+
- auth/dpop:sep-1932-client-fresh-proof
21+
- auth/dpop-nonce:sep-1932-client-token-request-proof
22+
- auth/dpop-nonce:sep-1932-client-dpop-auth-scheme
23+
- auth/dpop-nonce:sep-1932-client-fresh-proof
24+
- auth/dpop-nonce:sep-1932-client-as-nonce
25+
- auth/dpop-nonce:sep-1932-client-rs-nonce
26+
# Workload identity federation: the OAuth client does not implement the
27+
# urn:ietf:params:oauth:grant-type:jwt-bearer grant (it answers with
28+
# authorization_code). Also new in the da56f663 referee pin; per-check for
29+
# the same reason.
30+
- auth/wif-jwt-bearer:wif-grant-type
1431

1532
server:
1633
# SEP-2663 (io.modelcontextprotocol/tasks): the SDK does not implement the

.github/workflows/conformance.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ env:
1919
# Bump deliberately and reconcile both
2020
# .github/actions/conformance/expected-failures*.yml files in the same change.
2121
#
22-
# Temporarily pinned to the pkg.pr.new build of conformance main@4944b268
23-
# (0.2.0-alpha.8, which includes #372: fail checks whose prerequisite is
24-
# missing instead of skipping them) — alpha.8 is not published to npm yet.
25-
# Pinned by commit SHA so the tarball cannot move under us;
22+
# Temporarily pinned to the pkg.pr.new build of conformance main@da56f663,
23+
# which includes #403 (sep-2575 checks flipped to the post-spec-#3002 shape:
24+
# clientInfo optional on requests, serverInfo in result _meta instead of the
25+
# discover body) and #406 (per-check expected-failures granularity) — the
26+
# last published npm release (0.2.0-alpha.9) still enforces the pre-#3002
27+
# shape. Pinned by commit SHA so the tarball cannot move under us;
2628
# CONFORMANCE_PKG_SHA256 pins the bytes and the fetch-and-verify step below
2729
# downloads, checks the digest, and repoints CONFORMANCE_PKG at the
2830
# verified local copy. Repin to the next published @modelcontextprotocol/
29-
# conformance release (>=0.2.0-alpha.8) once it ships, then drop
31+
# conformance release (>=0.2.0-alpha.10) once it ships, then drop
3032
# CONFORMANCE_PKG_SHA256 and the fetch-and-verify steps.
31-
CONFORMANCE_PKG: "https://pkg.pr.new/@modelcontextprotocol/conformance@4944b268"
32-
CONFORMANCE_PKG_SHA256: "0f70c035782d319d72ab427653c5275db5c50429d59fae0241a645b33aeda1a7"
33+
CONFORMANCE_PKG: "https://pkg.pr.new/@modelcontextprotocol/conformance@da56f663"
34+
CONFORMANCE_PKG_SHA256: "bbc94678033071df4bc9851ce2054f9dff918f4501661d1d60e2d09d8c515e6d"
3335

3436
jobs:
3537
server-conformance:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
>
1919
> **v1.x is the only stable release line and remains recommended for production.** It lives on the [`v1.x` branch](https://github.com/modelcontextprotocol/python-sdk/tree/v1.x) and continues to receive critical bug fixes and security patches; see [the v1.x README](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/README.md) for its documentation. `pip` and `uv` don't select a pre-release unless you explicitly request one, so existing installs are unaffected. **If your package depends on `mcp`, add a `<2` upper bound to your version constraint (for example `mcp>=1.27,<2`) before the stable release lands.**
2020
>
21-
> v2 is a major rework of the SDK, both to support the [2026-07-28 MCP specification release](https://blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/) and to fix long-standing architectural issues. See [What's new in v2](https://py.sdk.modelcontextprotocol.io/v2/whats-new/) for the tour of what changed, and the [migration guide](https://py.sdk.modelcontextprotocol.io/v2/migration/) for every breaking change. Stable v2 is targeted for 2026-07-27, alongside the spec release. Try the pre-releases and [tell us what breaks](https://github.com/modelcontextprotocol/python-sdk/issues/new?template=v2-feedback.yaml), or discuss in [#python-sdk-dev on the MCP Contributors Discord](https://discord.gg/6CSzBmMkjX).
21+
> v2 is a major rework of the SDK, both to support the [2026-07-28 MCP specification release](https://blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/) and to fix long-standing architectural issues. See [What's new in v2](https://py.sdk.modelcontextprotocol.io/v2/whats-new/) for the tour of what changed, and the [migration guide](https://py.sdk.modelcontextprotocol.io/v2/migration/) for every breaking change. Stable v2 is targeted for 2026-07-28, alongside the spec release. Try the pre-releases and [tell us what breaks](https://github.com/modelcontextprotocol/python-sdk/issues/new?template=v2-feedback.yaml), or discuss in [#python-sdk-dev on the MCP Contributors Discord](https://discord.gg/6CSzBmMkjX).
2222
2323
## Documentation
2424

docs/advanced/extensions.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,21 @@ or veto a tool call:
133133
```
134134

135135
* `params` is the validated `CallToolRequestParams`: you get `params.name` and
136-
`params.arguments` without touching raw JSON.
137-
* `call_next(ctx)` runs the rest of the chain. Return its result unchanged (observe),
138-
return something else (replace), or raise an `MCPError` (refuse).
136+
`params.arguments` without touching raw JSON. It is also what decides which
137+
tool call runs: passing a rewritten context through `call_next` changes what
138+
the handler observes on `ctx`, not the tool invocation. Wire-level request
139+
rewriting belongs to [Middleware](middleware.md).
140+
* `call_next(ctx)` runs the rest of the chain and returns the handler's result.
141+
Return it unchanged (observe), return something else (replace), or raise an
142+
`MCPError` (refuse). Whatever you return is serialized like any handler
143+
result, including the 2026-era `serverInfo` identity stamp, so a
144+
short-circuiting interceptor never produces an anonymous or off-schema
145+
response.
139146
* With several extensions, interceptors nest in registration order: the first
140147
extension in `extensions=[...]` is outermost.
141148
* The default implementation is a pass-through, and a server whose extensions never
142-
override this hook installs **no** middleware at all. You don't pay for what
143-
you don't use.
149+
override this hook keeps the bare `tools/call` handler untouched. You don't
150+
pay for what you don't use.
144151

145152
The hook wraps `tools/call` and nothing else. For every-message concerns, use
146153
[Middleware](middleware.md). That is what it is for.

docs/advanced/low-level-server.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,13 @@ Call it and the result carries both representations:
102102
"content": [{"type": "text", "text": "Found 3 books matching 'dune'."}],
103103
"structuredContent": {"matches": 3, "query": "dune"},
104104
"isError": false,
105-
"resultType": "complete"
105+
"resultType": "complete",
106+
"_meta": {"io.modelcontextprotocol/serverInfo": {"name": "Bookshop", "version": "2.0.0"}}
106107
}
107108
```
108109

110+
The `_meta` block is the server's identity stamp: the SDK adds it to every 2026-era result, with the `version` from the constructor (a server that sets none reports an empty string). A server that must not identify itself can strip the key with a middleware, which owns the results it returns.
111+
109112
The server never compares the two fields. This SDK's `Client` does: return `structured_content` that doesn't satisfy the `output_schema` you declared and `call_tool` raises a `RuntimeError` that starts with `Invalid structured content returned by tool search_books` and goes on to quote the `jsonschema` failure. Promising a schema is cheap; keeping it is on you. The whole ladder of return types and schemas is in **[Structured Output](../servers/structured-output.md)**.
110113

111114
## `_meta`: for the application, not the model

docs/advanced/middleware.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ In increasing order of how much you should hesitate:
6363
`initialize`: the result the client gets back is built from your rewritten params, but the
6464
server commits its connection state from the original wire params. The two sides can finish
6565
the handshake disagreeing about what they negotiated.
66+
* **Answer.** Return a result without calling `call_next(ctx)` and it goes to the client as
67+
your response. `call_next` hands you the finished wire form, and the pipeline never patches
68+
what you return, so the whole envelope is yours: on a 2026-era connection that includes the
69+
`serverInfo` `_meta` stamp, which the SDK adds to handler results but not to yours.
6670

6771
!!! check
6872
`initialize` is one of the things middleware wraps, and it is the *only* hook you get

docs/client/caching.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Four calls, three fetches. The second call found a fresh entry and never reached
5151

5252
One rule sits above `"use"`: **calls carrying `meta` always reach the server.** A request with `meta` set (a progress token, tracing fields) expects a wire request, so under `cache_mode="use"` it is treated as `"refresh"`: the cache read is skipped, and the fetched result still replaces the cached entry. `"bypass"` and an explicit `"refresh"` behave as they always do.
5353

54-
To turn caching off entirely, construct with `Client(server, cache=False)`: every call is a round trip again, and `cache_mode`, while still accepted, does nothing.
54+
To turn caching off entirely, construct with `Client(server, cache=None)`: every call is a round trip again, and `cache_mode`, while still accepted, does nothing.
5555

5656
Scope is honored automatically too: `"private"` entries are keyed to the cache's *partition* (below), while `"public"` ones may opt into wider sharing. And **notifications beat TTL** for the exact entries they name: a `list_changed` notification evicts the matching cached listing, and `resources/updated` evicts the cached read stored under exactly its URI, however fresh they were. On a 2026-07-28 connection those notifications arrive on a `subscriptions/listen` stream you open with `client.listen(...)`, and eviction completes before your watcher sees the event; **[Subscriptions](subscriptions.md)** is that page.
5757

@@ -114,4 +114,4 @@ Clients on pre-2026 protocol versions never see either field; the SDK strips the
114114
* A handler that sets the fields on its result overrides the map, per field.
115115
* `"public"` is a promise that the result is identical for every caller. It is not access control.
116116
* `Client` honors the hints automatically: its response cache is on by default, serves fresh entries instead of refetching, and caches nothing for servers (or sessions) that provide no hints.
117-
* Per call, `cache_mode="refresh"` refetches and `"bypass"` skips the cache; `cache=False` at construction turns it off entirely.
117+
* Per call, `cache_mode="refresh"` refetches and `"bypass"` skips the cache; `cache=None` at construction turns it off entirely.

docs/client/callbacks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Two more. Neither declares anything.
135135

136136
`logging_callback` receives every `notifications/message` a server sends, as `LoggingMessageNotificationParams` (`level`, `logger`, `data`). Protocol logging is itself deprecated by the 2026-07-28 spec (**[Logging](../handlers/logging.md)** has what to do instead), so this callback exists for the servers that still emit it.
137137

138-
`message_handler` is the catch-all: every server notification reaches it (as well as its specific callback), and on a stream-backed transport so does every transport-level `Exception`. The one pattern worth knowing is `if isinstance(message, Exception): raise message`, so a broken connection fails loudly instead of vanishing.
138+
`message_handler` is the catch-all: every server notification the session surfaces reaches it (as well as its specific callback), and on a stream-backed transport so does every transport-level `Exception`. Two never do: `notifications/cancelled` is applied by the SDK rather than surfaced, and a subscription acknowledgment for a live `listen()` stream is consumed by that stream. Annotate the parameter with `IncomingMessage` (`ServerNotification | Exception`, exported from `mcp.client`). The one pattern worth knowing is `if isinstance(message, Exception): raise message`, so a broken connection fails loudly instead of vanishing.
139139

140140
## Recap
141141

docs/client/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Everything else on this page is identical across all three. Headers, subprocesse
3030

3131
Four read-only properties, populated the moment you enter the block:
3232

33-
* `client.server_info`: the server's identity. `server_info.name` here is `"Bookshop"`, `server_info.version` is whatever the server reports.
33+
* `client.server_info`: the server's identity, or `None` for a 2026-era server that does not report one (python-sdk servers do by default). `server_info.name` here is `"Bookshop"`, `server_info.version` is whatever the server reports.
3434
* `client.server_capabilities`: what the server can do (`tools`, `resources`, `prompts`, `completions`, ...). A capability the server doesn't have is `None`.
3535
* `client.protocol_version`: the protocol version the two sides agreed on. Here it is `"2026-07-28"`.
3636
* `client.instructions`: the server's `instructions=` string, or `None` if it didn't set one.
@@ -202,7 +202,7 @@ There is one constructor flag built for that: `Client(mcp, raise_exceptions=True
202202
## Recap
203203

204204
* `Client(x)` connects in-memory to a server object, over Streamable HTTP to a URL string, and over anything else via a transport.
205-
* `async with` is the whole lifecycle. Inside it, `server_info`, `server_capabilities`, `protocol_version` and `instructions` are already populated.
205+
* `async with` is the whole lifecycle. Inside it, `server_capabilities` and `protocol_version` are already populated; `server_info` and `instructions` are too when the server provides them.
206206
* `list_tools()` gives you each tool's `name`, `title`, `description` and `input_schema`.
207207
* `call_tool()` returns `content` for the model, `structured_content` for your code, and `is_error`. A raising tool is a result, not an exception.
208208
* `content` is a union of block types; narrow with `isinstance` before reading.

docs/client/oauth-clients.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ The first time `Client` sends a request, the server answers `401`. The provider
8383

8484
After that it is quiet. Tokens come out of storage, an expired access token is refreshed with the refresh token, and only when none of that works does it run the flow again.
8585

86-
You wrote none of it. Three keyword arguments remain (`timeout`, `client_metadata_url` and `validate_resource_url`), and this file needs none of them. `client_metadata_url` is the one worth knowing about; it gets its own section below.
86+
You wrote none of it. Two keyword arguments remain (`client_metadata_url` and `validate_resource_url`), and this file needs neither. `client_metadata_url` is the one worth knowing about; it gets its own section below.
8787

8888
### Try it
8989

@@ -112,7 +112,7 @@ A nightly job, a CI step, another service. There is no browser and nobody to cli
112112
What changed:
113113

114114
* No `OAuthClientMetadata`, no handlers. You pass `client_id` and `client_secret`; the provider builds a minimal `client_credentials` registration around them and skips dynamic registration entirely.
115-
* `scopes` is a space-separated string, the OAuth wire format.
115+
* `scope` is a space-separated string, the OAuth wire format.
116116
* Everything downstream is identical: the same `TokenStorage`, the same `httpx2.AsyncClient(auth=...)`, the same `streamable_http_client`.
117117

118118
By default the secret travels as HTTP Basic auth on the token request (`client_secret_basic`). Pass `token_endpoint_auth_method="client_secret_post"` to put it in the form body instead. Some authorization servers only accept one of the two.

0 commit comments

Comments
 (0)