python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Field alias is published in the tool schema but forwarded as an invalid Python keyword

Open destire-mio opened this issue 4 months ago • 7 comments

Initial Checks

  • [x] I confirm that I'm using the latest version of MCP Python SDK.
  • [x] I confirm that I searched the issue tracker before opening this issue.

Description

For a function parameter annotated with Field(alias="externalX"), the generated tool schema correctly requires externalX. At runtime, however, that wire name is forwarded directly to the original function, so the call fails with:

ToolError: Error executing tool f: f() got an unexpected keyword argument 'externalX'

I expected the schema-valid input to be mapped back to the Python parameter x before invocation.

Example Code

import asyncio
from typing import Annotated

try:
    from mcp.server.mcpserver.tools.base import Tool
except ImportError:  # MCP Python SDK 1.x
    from mcp.server.fastmcp.tools.base import Tool

from pydantic import Field


AliasInt = Annotated[int, Field(alias="externalX", ge=1)]


def f(x: AliasInt) -> int:
    return x


async def main() -> None:
    tool = Tool.from_function(f)
    print(tool.parameters)
    print(await tool.run({"externalX": 1}, None))


asyncio.run(main())

The printed schema requires externalX, then the call fails with the error above.

Python & MCP Python SDK

Stable reproduction:
- Python 3.13.13
- mcp 1.28.1
- pydantic 2.13.4

Current main reproduction:
- modelcontextprotocol/python-sdk commit 2713b53b127afc094dc97d6067df9f69b647661c
- mcp 2.0.0b2
- mcp-types 2.0.0b2
- Python 3.13.13

destire-mio avatar Jul 16 '26 02:07 destire-mio

Hi, I'd like to work on this. I'll submit a PR shortly.

nightcityblade avatar Jul 16 '26 03:07 nightcityblade

This is another tool-schema contract issue: the published JSON Schema advertises a field alias, but the Python call path still forwards the alias as if it were a valid keyword argument — so the tool “exists” in the agent’s schema while the runtime rejects or mis-binds the name.

Isolation that usually resolves it in one pass:

  1. Dump tool schema (what the agent sees) vs actual function signature / pydantic model field names.
  2. Call the tool outside the agent with both the alias and the real keyword — which one succeeds?
  3. Redacted failing run: schema snippet + TypeError/validation error + whether a non-agent call works.

That separates “model chose wrong field” from “schema lied about the callable surface.”

IgorGanapolsky avatar Jul 16 '26 14:07 IgorGanapolsky

I reviewed the contribution guidelines and will wait for maintainer feedback or assignment before starting implementation.

nightcityblade avatar Jul 17 '26 03:07 nightcityblade

I'd like to work on this — I've root-caused it (model_dump_one_level() conflates the SDK's internal reserved-name alias workaround with a user-supplied Field(alias=...), using the alias as the call-time keyword in both cases) and have a fix + regression test ready to submit. Could a maintainer assign this to me or apply ready for work?

zerafachris avatar Jul 17 '26 17:07 zerafachris

Just flagging that a fix for this already exists in #3119 (opened Jul 17, CI green). It is still marked draft because of the repo's open-PR limit for non-write contributors, not because it is unfinished. Reuse or ignore it as suits whoever picks this up.

jayzuccarelli avatar Jul 20 '26 13:07 jayzuccarelli

Could a maintainer assign this one? #3119 has the fix, open since Jul 17 with CI green. It predates the intake gate so it was never assignment-checked. I'll rebase onto main and mark it ready as soon as it's assigned.

Several people have offered here, so no objection if you'd rather assign someone else. Worth knowing the fix already exists either way.

jayzuccarelli avatar Aug 30 '26 21:08 jayzuccarelli

Confirmed on latest stable 2.3.0 (2118f14f8a19bc158d8a1cf90af58d85d187f849) and current main 754254c08fe1c3eda18b69da3d336106fe8d72d6, with Python 3.14.7 / Pydantic 2.12.5.

A standalone in-memory Client(server) test verifies that Field(alias="wire") appears in the published schema, then calls the tool with {"wire": "ok"}. The result has is_error=True; the server traceback ends in echo() got an unexpected keyword argument 'wire'. The intended output is seen:ok.

Test: standalone failing reproduction, branch repro/tool-input-alias (diff).

From that branch, after installing its development dependencies:

uv run --frozen pytest tests/server/mcpserver/tools/test_base.py::test_tool_field_alias_has_a_standalone_wire_to_python_reproduction -q

Both source versions report 1 intentional failure and 2 passing existing controls. This adds a pinned public-client round-trip reproduction to the existing report; no production changes are included.

AI-assisted investigation and test reproduction, with a human in the loop.

inanna-malick avatar Oct 14 '26 22:10 inanna-malick