Skip to content

fix(py/plugins/mcp): carry each MCP tool's output schema, title and annotations - #6903

Closed
hilariie wants to merge 3 commits into
mainfrom
fix/py-mcp-tool-output-schema
Closed

hilariie wants to merge 3 commits into
mainfrom
fix/py-mcp-tool-output-schema

Conversation

@hilariie

@hilariie hilariie commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Why

An MCP server can declare an outputSchema, a title and annotations for a tool, but the client kept only the name, description and input schema. The model never saw the output schema: to_tool_definition reads it from the action's originalOutputSchema metadata, and the MCP actions were built with genkit.tool(), which leaves it None. This applied to both <client>:tool/* selectors and the handles from get_active_tools(). The title and the behaviour hints were dropped entirely. Part of #6194

Changes

  • mcp_tool_to_action stores the server's outputSchema under the action's originalOutputSchema metadata, so both paths send it to the model. Tools without one still send none.
  • The action's metadata['mcp'] now also holds the tool's title and annotations, with hints the server left out kept absent and unknown keys kept as sent. Neither is sent to the model.
  • Gemini rejects a tool output schema with a $ref outside $defs (#/definitions/..., #/properties/...), as it already does for input schemas. That is fixed separately in the Gemini plugin.

Verification

  • Unit tests on to_tool_definition for an MCP action with and without a declared schema, and on the title and annotations it keeps.
  • generate tests through a selector and through get_active_tools() check the model request for fake_bad_output (declared schema) and fake_echo (none).
  • A stdio test checks that a selected tool and a listed handle both carry the title and annotations the fake server lists.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the mcp_tool_to_action function to propagate the server's output schema to the model by setting the ORIGINAL_OUTPUT_SCHEMA_KEY metadata key on the generated action. It also includes updates to existing tests and a new test case to verify that the output schema is correctly passed. There are no review comments, so I have no feedback to provide.

@hilariie hilariie added this to the py-1.0.0 milestone Oct 27, 2026
@hilariie hilariie changed the title fix(py/plugins/mcp): send each MCP tool's output schema to the model fix(py/plugins/mcp): carry each MCP tool's output schema, title and annotations Oct 28, 2026
@hilariie
hilariie requested a review from a team as a code owner October 29, 2026 18:45

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heads up: feat/py-mcp-host got rebased onto main today, so GitHub shows the whole genkit-mcp package as added and this conflicts. The real change is git diff d535af1cc..HEAD; that is what I reviewed. Conflicts are in _tools.py and mcp_tools_test.py.

Verified with a scripted model: last_request.tools[*].output_schema now carries the server schema on both <client>:tool/* and get_active_tools(), and the Dev UI shows originalOutputSchema and metadata.mcp. One thing I would want handled before merge is the Gemini $ref case below.

Side note, not asking for a change: JS types MCP outputs as z.any() and Go sets no output schema, so Python becomes the only runtime that hands the model the server output schema. Worth a line in the description so nobody aligns it back by accident.

Comment thread py/packages/genkit-mcp/README.md Resolved

@huangjeff5 huangjeff5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving so the stack can land on feat/py-mcp-host once it's rebased. The Gemini $ref comment still stands; fine as a follow-up on the feature branch, but I'd want it fixed before feat/py-mcp-host goes to main. When resolving the conflict, watch the strict=True zip against the base's collision skipping.

Base automatically changed from feat/py-mcp-host to main October 30, 2026 10:54
@hilariie
hilariie force-pushed the fix/py-mcp-tool-output-schema branch from f739cdb to 6606da9 Compare October 30, 2026 14:54
@github-actions github-actions Bot added the docs Improvements or additions to documentation label Oct 30, 2026
@hilariie
hilariie requested a review from huangjeff5 October 30, 2026 14:57

@huangjeff5 huangjeff5 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked on main: the threads hold (title/annotations left out when absent, no zip). Approving to land after #6969. At this head alone, a #/definitions/Item output schema fails Gemini with INVALID_ARGUMENT; with #6969 merged in, it converts.

Forwarding the server's outputSchema makes sense to me. The MCP spec intends it as a hint for clients and models, the tool output we send is structuredContent (which the spec says conforms to it), and native Python tools already send their output schema as Gemini FunctionDeclaration.response. So MCP tools now match native ones. Gemini is the only Python model plugin that reads ToolDefinition.output_schema; anthropic, compat-oai, ollama and bedrock ignore it.

For the description: the cross-SDK gap is in the Gemini plugin, not MCP. JS and Go never send a tool response to Gemini, even for native tools. Worth a line so nobody "aligns" the MCP side alone. ai.lookup_tool(...) also returns the schema now.

  • py/packages/genkit-mcp/src/genkit_mcp/_tools.py:319: nit: line 253 now stores the schema at action.metadata[ORIGINAL_OUTPUT_SCHEMA_KEY], so by_name is a second source for the same value. Reading it back from the action (as lookup_tool does) would let this drop the map.

tools = await client.get_active_tools()
for tool in tools:
print(tool.name, tool.description)
# The server's display title and behaviour hints, when it sent them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this prints the whole metadata['mcp'], so prefix, tool and _meta too. Either print .get('title') / .get('annotations') or reword the comment.

@MichaelDoyle

Copy link
Copy Markdown
Contributor

Closing here as Python SDK development has moved to https://github.com/genkit-ai/genkit-python and this PR has already been re-opened there as genkit-ai/genkit-python#99.

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

Labels

docs Improvements or additions to documentation fix python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants