modelcontextprotocol / modelcontextprotocol/python-sdk
find_context_parameter() fails to detect Context parameter in callable class instances
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 24.3k
- フォーク
- 4k
- 平均マージ
- 1日 1時間
- マージ済み PR(30日)
- 31
説明
Initial Checks
- I confirm that I'm using the latest version of MCP Python SDK
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Description
When registering a callable class instance as an MCP tool via FastMCP.add_tool(), the ctx: Context parameter is incorrectly exposed as an externally visible tool parameter instead of being injected by the framework. This is because find_context_parameter() uses typing.get_type_hints() which doesn't introspect the call method of callable class instances.
Suggested Fix
Update find_context_parameter() to handle callable class instances:
def find_context_parameter(fn: Callable[..., Any]) -> str | None:
from mcp.server.fastmcp.server import Context
# Handle callable class instances by using __call__ method
target = fn
if not (inspect.isfunction(fn) or inspect.ismethod(fn)):
if callable(fn) and hasattr(fn, "__call__"):
target = fn.__call__
try:
hints = typing.get_type_hints(target)
except Exception:
return None
# ... rest of function unchanged
Example Code
from typing import Annotated, Any
from mcp.server.fastmcp import Context, FastMCP
from mcp.server.session import ServerSession
from starlette.requests import Request
import pydantic
class MyTool:
def __init__(self, name: str):
self.__name__ = name
async def __call__(
self,
query: Annotated[str, pydantic.Field(description="Search query")],
ctx: Context[ServerSession, Any, Request],
) -> str:
"""Performs a search."""
return f"Results for: {query}"
mcp = FastMCP(name="Test Server")
mcp.add_tool(MyTool(name="my_search"), name="my_search", description="Search tool")
# When listing tools, 'ctx' appears as a required parameter in the schema
Python & MCP Python SDK
1.26.0
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
FastMCP.add_tool() を通じてツールを登録する際に使用される find_context_parameter() から始めます。通常の関数と呼び出し可能なクラスインスタンスに対する処理を比較し、呼び出し可能なインスタンスを登録したときに、例で ctx がツールパラメーターとして公開されなくなっていることを確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- api, backend
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 55/100