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

fix: detect context on callable tool objects

Open fengjikui opened this issue 5 months ago • 0 comments

Fixes #1974.

Summary

find_context_parameter() previously inspected callable object instances directly with typing.get_type_hints(). For instances that implement __call__, those type hints live on the bound __call__ method, so a ctx: Context parameter was not detected. That meant callable-object tools exposed ctx as a required external tool argument instead of letting the framework inject it.

This updates context detection to inspect fn.__call__ for callable objects while preserving the existing path for regular functions and methods. It also adds regression coverage for both schema generation and runtime context injection on callable-object tools.

Validation

  • uv run --frozen pytest tests/server/mcpserver/test_tool_manager.py -q
  • uv run --frozen ruff check src/mcp/server/mcpserver/utilities/context_injection.py tests/server/mcpserver/test_tool_manager.py
  • uv run --frozen ruff format --check src/mcp/server/mcpserver/utilities/context_injection.py tests/server/mcpserver/test_tool_manager.py
  • uv run --frozen pyright
  • env -u ALL_PROXY -u HTTP_PROXY -u HTTPS_PROXY -u all_proxy -u http_proxy -u https_proxy ./scripts/test
  • git diff --check

This PR was prepared with AI assistance and manually validated with the checks above.

fengjikui avatar Jun 08 '26 04:06 fengjikui