Title: find_context_parameter() fails to detect Context parameter in callable class instances · Issue #1974 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: find_context_parameter() fails to detect Context parameter in callable class instances · Issue #1974 · modelcontextprotocol/python-sdk
X Title: find_context_parameter() fails to detect Context parameter in callable class instances · Issue #1974 · modelcontextprotocol/python-sdk
Description: 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 cal...
Open Graph Description: 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 ...
X Description: 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 t...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/1974
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"find_context_parameter() fails to detect Context parameter in callable class instances","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the latest version of MCP Python SDK\n- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Description\n\nWhen 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.\n\nSuggested Fix\nUpdate find_context_parameter() to handle callable class instances:\n```\ndef find_context_parameter(fn: Callable[..., Any]) -> str | None:\n from mcp.server.fastmcp.server import Context\n\n # Handle callable class instances by using __call__ method\n target = fn\n if not (inspect.isfunction(fn) or inspect.ismethod(fn)):\n if callable(fn) and hasattr(fn, \"__call__\"):\n target = fn.__call__\n\n try:\n hints = typing.get_type_hints(target)\n except Exception:\n return None\n\n # ... rest of function unchanged\n```\n\n### Example Code\n\n```Python\nfrom typing import Annotated, Any\nfrom mcp.server.fastmcp import Context, FastMCP\nfrom mcp.server.session import ServerSession\nfrom starlette.requests import Request\nimport pydantic\n\nclass MyTool:\n def __init__(self, name: str):\n self.__name__ = name\n\n async def __call__(\n self,\n query: Annotated[str, pydantic.Field(description=\"Search query\")],\n ctx: Context[ServerSession, Any, Request],\n ) -> str:\n \"\"\"Performs a search.\"\"\"\n return f\"Results for: {query}\"\n\nmcp = FastMCP(name=\"Test Server\")\nmcp.add_tool(MyTool(name=\"my_search\"), name=\"my_search\", description=\"Search tool\")\n\n# When listing tools, 'ctx' appears as a required parameter in the schema\n```\n\n### Python & MCP Python SDK\n\n```Text\n1.26.0\n```","author":{"url":"https://github.com/mjahr","@type":"Person","name":"mjahr"},"datePublished":"2026-01-30T01:24:08Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/1974"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8df69407-d392-af40-f05f-cb95ca2956a8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D318:29EEB8:CC430:11B67D:6B056B9D |
| html-safe-nonce | fe52251dc2e195e41c8f75de79c7d67f70ee34803f6c489f68c9e519ceff512c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzE4OjI5RUVCODpDQzQzMDoxMUI2N0Q6NkIwNTZCOUQiLCJ2aXNpdG9yX2lkIjoiOTAyNDI2MTUzODEwNTg3OTQ1MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ff13d28ad1eebc8b75387bff64cb10badb9f241c6caf9ca926b0e7d1e687d47d |
| hovercard-subject-tag | issue:3873365288 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/1974/issue_layout |
| twitter:image | https://opengraph.githubassets.com/755a13a86d4a377b98b4d4408a9c5ed301840bcc6fb0d0825746d58c5df286fa/modelcontextprotocol/python-sdk/issues/1974 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/755a13a86d4a377b98b4d4408a9c5ed301840bcc6fb0d0825746d58c5df286fa/modelcontextprotocol/python-sdk/issues/1974 |
| og:image:alt | 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 ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mjahr |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9b4256ac5f3e310e7945ef5b71c9cb9e264357b1eb20e91d369ed6502a4c486c |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f089e689ad324655ac44bf6cdbc04d976105b374 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width