Skip to content

Fall back to unstructured output when output model selection fails (#3573) - #3587

Closed
Rainmemery wants to merge 1 commit into
modelcontextprotocol:mainfrom
Rainmemery:fix/iterator-return-fallback
Closed

Rainmemery wants to merge 1 commit into
modelcontextprotocol:mainfrom
Rainmemery:fix/iterator-return-fallback

Conversation

@Rainmemery

Copy link
Copy Markdown

Fixes #3573

Disclosure: this fix was developed with AI assistance; I have reviewed the diff, run the tests locally, and can explain the change.

Summary

func_metadata raises an uncaught pydantic.errors.PydanticSchemaGenerationError when a tool's return annotation is Iterator[...] / AsyncIterator[...] (either the typing or the collections.abc spelling), so a correctly annotated generator tool cannot register at all. The fallback machinery for unserializable return types already exists — the call to _create_output_model() simply sits outside the try that routes those failures to it.

This wraps that call in the same failure path: on PydanticSchemaGenerationError the tool degrades to unstructured output (output_schema=None), and with structured_output=True the existing flow raises InvalidSignature. Generator[...] is untouched — pydantic treats it as a sequence and its structured array schema keeps working.

Motivation and Context

-> Iterator[str] is the PEP 484 spelling for generator functions, but registering one currently crashes at decoration time instead of taking the documented unstructured fallback (#3573). Fixing it at the model-selection step reuses the existing fallback semantics in one place rather than special-casing iterator types.

How Has This Been Tested?

  • Reproduced on main (f1b6589) for all four spellings (typing/collections.abc × Iterator/AsyncIterator) with the default and structured_output=True.
  • tests/server/mcpserver/test_func_metadata.py: 60/60 pass, including two new regression tests (the four spellings fall back to unstructured / raise InvalidSignature; Generator[str, None, None] keeps its structured array schema).
  • Adjacent suites (tests/server/mcpserver/tools, tests/interaction/mcpserver/test_tools.py): 85/85 pass. ruff check and ruff format --check clean.

Breaking Changes

None. Registrations that previously crashed now succeed as unstructured tools (or raise InvalidSignature under structured_output=True, matching the documented contract for unserializable return types).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I am assigned to the linked issue (or it is labeled help wanted, or I'm a maintainer)
  • I have disclosed any AI assistance and can explain the change in my own words
  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Picking the output model builds throwaway pydantic models for the return
type, which raises PydanticSchemaGenerationError for annotations pydantic
cannot model, such as Iterator[str] or AsyncIterator[str]. That call sat
outside the try/except that routes expected schema failures to the
unstructured fallback, so the error escaped func_metadata and a properly
annotated generator tool could not register at all. With
structured_output=True the existing flow raises InvalidSignature instead.

Fixes modelcontextprotocol#3573
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Sep 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #3573.

If a maintainer assigns you to #3573, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take.

You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way.

CONTRIBUTING.md has the full reasoning, but in short:

  • We're a small team with very little capacity to review community PRs right now.
  • Many recent PRs are AI-generated with little human review, and reviewing one carefully still costs a maintainer as much time as it ever did. A well-described issue is usually more useful to us than the code.

Maintainers: reopen, remove missing-issue-link, or add bypass-issue-check to override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

func_metadata raises uncaught PydanticSchemaGenerationError for Iterator/AsyncIterator tool return annotations instead of the unstructured fallback

1 participant