modelcontextprotocol / modelcontextprotocol/python-sdk

Tool input schemas carry a pydantic-derived title on every property

未关闭
#3,391 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

v1 v2
主要语言
Python
星标
24.3k
派生
4k
平均合并
1 天 1 小时
30 天内合并 PR
31

描述

Description

Every tool's inputSchema carries a title on every property, derived by pydantic from the field name. A parameter named exercise_id gets "title": "Exercise Id" — a restatement of the key it already sits under. Tool schemas are re-sent to the model on every request, so this is paid for in context on every turn.

I ran into this running a local 27B model against the wger MCP server, where context is genuinely scarce. Measuring its live tools/list:

bytes share
whole payload, 49 tools 43,710 100%
tool descriptions (prose) 10,904 25%
auto-derived title keys (297 of them) 8,333 19%
anyOf null-wrapping on optionals 1,869 4%

My agent is granted 43 of those tools, which is ~11,100 tokens of schema against a 32k window — 42% of the context gone before the first message, once the system prompt is counted. Roughly 2,000 of those tokens are titles.

Reproduction — any tool at all:

from mcp.server.mcpserver import MCPServer

mcp = MCPServer("demo")

@mcp.tool()
def log_set(exercise_id: str, reps: int) -> str:
    """Log a set."""
    return "ok"

inputSchema.properties is:

{
  "exercise_id": {"title": "Exercise Id", "type": "string"},
  "reps": {"title": "Reps", "type": "integer"}
}

I'd expect the titles not to be there, since they add nothing a model can act on that the property name doesn't already say.

GenerateJsonSchema has a hook for exactly this — field_title_should_be_set — and Tool.from_function already passes a custom generator elsewhere in the file, so it's a small change. Suppressing the automatic titles leaves an explicit Field(title=...) intact, which seems like the right line to draw: an explicit title is the author's choice, an auto-derived one is a default nobody asked for.

Two things I'd want a maintainer's call on before this is worth doing:

  1. Default or opt-in. Changing the default updates 12 test expectations in this repo (mostly snapshot(...) in tests/docs_src/), so it's visible. An opt-in flag on MCPServer(...) avoids that but adds public API.
  2. Scope. Output schemas, prompts and resource templates generate titles the same way. Output schemas alone are another 61 titles in the payload I measured. Worth doing together, or separately?

Happy to open a PR if it's useful — I have the change and the test updates working locally against main, full suite green. Equally happy to leave it if you'd rather write it yourselves.

Disclosure: I used an AI agent to take the measurements and draft the change. The problem is one I actually hit, and I've read and can explain the result.

References

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 Tool.from_function 及其现有的自定义生成器开始,然后检查 Pydantic 的 GenerateJsonSchema.field_title_should_be_set hook。查看 tests/docs_src/ 中的快照预期,并与 maintainer 一起决定此更改是否只涵盖输入 schema,还是也涵盖其他生成的 schema。完成的标准是移除自动生成的属性标题,同时保留显式的 Field 标题,并更新受影响的测试且使其通过。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
api, backend
Issue 类型
功能
难度
3/5
预计耗时
1-2 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
52/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。