Add structured output to the Python weather server and client and update to v2#164
Draft
olaservo wants to merge 1 commit into
Draft
Conversation
olaservo
force-pushed
the
structured-output-2026-07-28/python
branch
4 times, most recently
from
July 26, 2026 03:34
0eac5ce to
704cea2
Compare
olaservo
commented
Jul 26, 2026
|
|
||
| `Client(transport, mode="auto")` probes `server/discover` and falls back to the `2025-11-25` handshake; `client.protocol_version` reports which era you got. See [Protocol versions](https://py.sdk.modelcontextprotocol.io/v2/protocol-versions/). | ||
|
|
||
| Requires the `mcp` 2.0 prereleases, so `pyproject.toml` sets `[tool.uv] prerelease = "allow"`. |
Member
Author
There was a problem hiding this comment.
Will remove this before un-drafting.
olaservo
commented
Jul 26, 2026
| # The 2026-07-28 protocol revision only ships in the mcp 2.0 prereleases, so | ||
| # allow them without every command needing --prerelease=allow. | ||
| [tool.uv] | ||
| prerelease = "allow" |
Member
Author
There was a problem hiding this comment.
Will remove this before un-drafting.
olaservo
commented
Jul 26, 2026
|
|
||
| Note that an array-rooted schema requires a `2026-07-28` client. This SDK does not project it down for older ones — it raises instead. | ||
|
|
||
| Requires the `mcp` 2.0 prereleases, so `pyproject.toml` sets `[tool.uv] prerelease = "allow"`. |
Member
Author
There was a problem hiding this comment.
Will remove this before un-drafting.
Both tools declare an output schema - the return type annotation - and return
structured_content alongside the text.
get_alerts answers with a top-level JSON array rather than an array nested in
an object, which protocol revision 2026-07-28 is the first to allow. "No
alerts" is simply []. get_forecast returns an object, for contrast.
The detail worth reading the source for: Alerts is a RootModel[list[Alert]],
not a plain list[Alert]. A list is not a JSON object, so the SDK wraps it as
{"result": [...]} and advertises an object-rooted schema to match, with no
opt-out. A RootModel is a BaseModel, so it is taken as the schema exactly as
written, and a RootModel over a list serializes as the bare list.
Error paths raise: a tool declaring an output schema MUST return conforming
structured content, so a path with no data has to fail.
The client moves to the 2.0 Client API with mode="auto" and prints the
negotiated version on connect. call_tool already revalidates every non-error
result against the declared schema, so the client-side SHOULD needs no code.
Each channel goes to its stated reader: content is forwarded to the model,
structured_content is used as data, reporting how many items came back.
This clears the staleness modelcontextprotocol#3124 flags against this
repository - weather.py imported mcp.server.fastmcp, client.py held the
ClientSession version - and matches that PR's idioms: the short
"from mcp.server import MCPServer" spelling, httpx2 with httpx dropped from
the dependencies since httpx2>=2.5.0 is a hard dependency of mcp,
result.content narrowed to TextContent, and input() on a worker thread.
Requires mcp 2.0.0b2; pyproject.toml sets [tool.uv] prerelease = "allow" so
plain uv run and uv sync work without flags. Model identifier moves to
claude-sonnet-5.
Note that an array-rooted schema only works on a 2026-07-28 connection. This
SDK does not project it down for older clients; it raises server-side instead.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
olaservo
force-pushed
the
structured-output-2026-07-28/python
branch
from
July 26, 2026 04:24
704cea2 to
c76c52e
Compare
This was referenced Jul 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Brings the Python examples onto MCP SDK 2.0 and protocol revision
2026-07-28, and gives both tools a declaredoutputSchemawith matchingstructuredContent.This also clears the staleness that modelcontextprotocol/modelcontextprotocol#3124 flags against this repository. That PR modernises the Python in the tutorials and notes that the "complete code" they link to here had fallen behind:
weather.pyimportedmcp.server.fastmcp, which does not exist in v2, andclient.pystill held theClientSessionversion.get_alertsanswers with a top-level JSON array:[ { "event": "Flood Warning", "area": "La Salle County", ... }, { "event": "Heat Advisory", "area": "Wheeler County", ... } ]Other changes
Error paths raise. Once a tool declares an output schema it MUST return conforming structured content, so a path with no data to return has to fail rather than answer with a bare text result.
The client negotiates. It moves to the 2.0
ClientAPI and passesmode="auto"— oneserver/discoverprobe, falling back to the2025-11-25handshake — and prints the negotiated version on connect.Matched to modelcontextprotocol/modelcontextprotocol#3124 so the tutorials and this code agree: the short
from mcp.server import MCPServerspelling;httpx2instead ofhttpx, withhttpxdropped from the dependencies becausehttpx2>=2.5.0is a hard dependency ofmcpand listing it installed a second HTTP stack;result.contentnarrowed toTextContentbefore being forwarded to the model API; andinput()moved onto a worker thread so it does not block the event loop.Verification
Captured off the raw wire — a real
get_alerts("TX")call against live NWS data, with no SDK on the client side, so this is literally what the server sends:structuredContentis a JSON array at the top level, not an object wrapping one, and the tool's single content block is the serialised JSON — the backwards-compatibility fallback the tools specification recommends for structured results.Driven through the client with a real API key, the model calls the tool and answers from the structured result:
Draft status
Two separate gates:
1. #163 has to merge first. The smoke test on
mainuses an MCP SDK v1 helper, which negotiates2025-11-25.2. The revision has to ship.
2026-07-28support is only in themcp2.0 prereleases, so this pinsmcp[cli]>=2.0.0b2and sets[tool.uv] prerelease = "allow"inpyproject.tomlso plainuv runanduv syncwork without extra flags. Ideally this PR waits formcp2.0 stable and the pin is the only line that changes.I will rebase and mark ready once both are cleared.
Related
One of a set bringing the examples onto
2026-07-28, one PR per language so the four can be compared: #164 (Python), #165 (TypeScript), #166 (Go), #167 (Rust), with #163 as the shared prerequisite. The Ruby examples are deliberately not in the set: themcpgem caps at protocol2025-11-25.🤖 Generated with Claude Code