Skip to content

fix: detect context on callable tool objects#2817

Open
fengjikui wants to merge 1 commit into
modelcontextprotocol:mainfrom
fengjikui:fix-callable-context-injection
Open

fix: detect context on callable tool objects#2817
fengjikui wants to merge 1 commit into
modelcontextprotocol:mainfrom
fengjikui:fix-callable-context-injection

Conversation

@fengjikui
Copy link
Copy Markdown

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 fengjikui force-pushed the fix-callable-context-injection branch from 0726f5e to ff904b0 Compare June 8, 2026 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

find_context_parameter() fails to detect Context parameter in callable class instances

1 participant