Repository navigation
Description
Initial Checks
- I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Release line
2.x (current stable)
Description
This is the case left out of #3376. A tool whose return type is a RootModel with a non-object root (RootModel[list[str]], RootModel[int], ...) publishes outputSchema with "type": "array" (or a scalar) at the root. On a 2025-11-25 session ListToolsResult validation then fails on tools.0.outputSchema.type Input should be 'object', and the client gets Handler returned an invalid result for the whole listing, so every other tool on the server is gone too. 2026-07-28 sessions are fine.
_create_output_model takes any BaseModel subclass as is (func_metadata.py L536-538), so RootModel never gets the {"result": ...} wrapping that list[str] or int returns get. The PR description there said a separate issue would be opened for this, I couldn't find one so filing it.
I'd expect a RootModel whose root isn't an object to be wrapped the same way as the plain list[...] / scalar return types, while RootModel[dict[...]] or RootModel[SomeModel] keep the current behaviour. The tools/list of other tools definitely shouldn't depend on it.
Example Code
import anyio, logging
from pydantic import RootModel
from mcp import Client
from mcp.server.mcpserver import MCPServer
logging.disable(logging.CRITICAL)
class Tags(RootModel[list[str]]):
pass
mcp = MCPServer("demo")
@mcp.tool()
def tags() -> Tags:
return Tags(["a", "b"])
@mcp.tool()
def other() -> int:
return 1
async def main():
print("outputSchema:", (await mcp.list_tools())[0].output_schema)
async with Client(mcp) as c: # 2026-07-28
print("2026-07-28 tools/list:", [t.name for t in (await c.list_tools()).tools])
print("2026-07-28 structuredContent:", (await c.call_tool("tags", {})).structured_content)
try:
async with Client(mcp, mode="legacy") as c: # 2025-11-25
print("legacy tools/list:", [t.name for t in (await c.list_tools()).tools])
except BaseException as e:
while isinstance(e, BaseExceptionGroup):
e = e.exceptions[0]
print("legacy tools/list raised:", type(e).__name__, e)
anyio.run(main)Output (same on v2.3.0 and on main 77001c9)
outputSchema: {'items': {'type': 'string'}, 'title': 'Tags', 'type': 'array'}
2026-07-28 tools/list: ['tags', 'other']
2026-07-28 structuredContent: ['a', 'b']
legacy tools/list raised: MCPError Handler returned an invalid result
Python & MCP Python SDK
Python 3.12 with mcp 2.3.0 from PyPI, and Python 3.14.4 with main @ 77001c9
pydantic 2.12.5 in the main checkout
Activity
bunnysayzz commentedon Dec 5, 2026
Traced the same shape as #3376.
_create_output_modelinsrc/mcp/server/mcpserver/utilities/func_metadata.pytakes anyBaseModelsubclass as-is, so aRootModel[list[str]]return never gets the{"result": ...}wrapping that a plainlist[str]return gets, and the publishedoutputSchemahas"type": "array"at the root. On 2025-11-25 sessions that failsListToolsResultvalidation and the wholetools/listerrors out. Fix would be to route a RootModel whose root type is not an object through_create_wrapped_model, keepingRootModel[dict[...]]andRootModel[SomeModel]unwrapped. Could I take this? Happy to put up the fix once assigned.Rupithkumar commentedon Dec 5, 2026
Two regression cases worth adding alongside the list example are
RootModel[int | str]andRootModel[dict[str, int]]. With Pydantic 2.13.3 the former generates ananyOfroot with notypekey. The latter already generates an object root. Checking only fortype == "array"would miss the union; wrapping every RootModel would change the dictionary result unnecessarily.I checked four simple schemas (list, int, union, dictionary) and their serialized values with Draft202012Validator. For the first three, an object schema with a required
resultproperty accepted the corresponding wrapped value. The dictionary validated unchanged. This was an isolated schema check, not an SDK v2 session run.For an integration regression I'd list and call the tool under each negotiated version, validate the returned structured content against the schema from that same session, and include an ordinary sibling tool. A recursive root model is a useful additional case if schema wrapping moves the original root: local
#/$defs/...references still need to resolve from the published document root.AI disclosure: OpenAI Codex inspected the upstream code, ran the four local checks, and drafted this comment.
Rupithkumar commentedon Dec 5, 2026
I have a candidate patch and regression tests ready. On SDK 2.3.0, four legacy cases reproduced the discovery failure before the change. With the patch, all 12 session tests passed, including listing and calling the sibling tool. One compatibility detail for review is that modern clients would also receive non-object roots wrapped in
{"result": ...}.If the maintainers would welcome this approach, I’m happy to open a PR with the code and tests. OpenAI Codex prepared the patch, ran the tests, and drafted this update.