modelcontextprotocol / modelcontextprotocol/python-sdk
Improving how function docstring gets converted to tool's jsonschema for FastMCP
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 24.3k
- 分支
- 4k
- 平均合併
- 1 天 1 小時
- 30 天內合併 PR
- 31
描述
Is your feature request related to a problem? Please describe.
Tool descriptions are not parsed as expected from the function docstring when using FastMCP. This affects tool calling performance.
Currently, FastMCP does some function inspection to create the docstring here:
- https://github.com/modelcontextprotocol/python-sdk/blob/775f87981300660ee957b63c2a14b448ab9c3675/src/mcp/server/fastmcp/tools/base.py#L55-L59
- https://github.com/modelcontextprotocol/python-sdk/blob/775f87981300660ee957b63c2a14b448ab9c3675/src/mcp/server/fastmcp/utilities/func_metadata.py#L105-L174
From my understanding, it creates a FuncMetadata model in pydantic which then gets converted to jsonschema.
Current behaviour:
If we have a tool such as:
def add_numbers(a: float, b: float) -> float:
"""
Adds two numbers and returns the result.
Args:
a (float): The first number.
b (float): The second number.
Returns:
float: The sum of a and b.
"""
return a + b
it gets parsed into:
>>> func_arg_metadata = func_metadata(add_numbers)
>>> parameters = func_arg_metadata.arg_model.model_json_schema()
>>> parameters
{'properties': {'a': {'title': 'A', 'type': 'number'}, 'b': {'title': 'B', 'type': 'number'}}, 'required': ['a', 'b'], 'title': 'add_numbersArguments', 'type': 'object'}
>>> add_numbers.__doc__
'\nAdds two numbers and returns the result.\n\nArgs:\n a (float): The first number.\n b (float): The second number.\n\nReturns:\n float: The sum of a and b.\n'
Describe the solution you'd like
It'd be nicer to follow one of the python docstring styles and parse out the argument descriptions from the docstring.
{
"name": "add_numbers",
"description": "Adds two numbers and returns the sum.",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "number",
"description": "The first number to add."
},
"b": {
"type": "number",
"description": "The second number to add."
}
},
"required": ["a", "b"]
}
}
Describe alternatives you've considered
we used to do this in a previous python version of goose: https://github.com/block/goose/blob/eccb1b22614f39b751db4e5efd73d728d9ca40fc/packages/exchange/src/exchange/utils.py#L82-L107
here are some test examples: https://github.com/block/goose/blob/eccb1b22614f39b751db4e5efd73d728d9ca40fc/packages/exchange/tests/test_utils.py#L32-L136
Additional context
I am happy to add this in - wanted to post this first to check that you're okay with enforcing a docstring style ("google", "numpy", "sphinx") & adding griffe as a dependency.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 src/mcp/server/fastmcp/tools/base.py 和 src/mcp/server/fastmcp/utilities/func_metadata.py 開始,接著檢視連結的 Goose 工具和測試範例。首先判斷哪些 docstring 樣式和解析器相依性是可接受的。完成的標準是,受支援的引數和函式描述會出現在產生的 JSON schema 中,且測試涵蓋選定的樣式。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- api, backend-api-design
- Issue 類型
- 功能
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 52/100