Title: `@mcp.tool()` Fails to Find Context Argument if its Type `Context` Includes Type Variables · Issue #357 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: `@mcp.tool()` Fails to Find Context Argument if its Type `Context` Includes Type Variables · Issue #357 · modelcontextprotocol/python-sdk
X Title: `@mcp.tool()` Fails to Find Context Argument if its Type `Context` Includes Type Variables · Issue #357 · modelcontextprotocol/python-sdk
Description: Describe the bug The code does not work because the type annotation for the ctx argument is not simply Context; it includes type variables ServerSessionT and AppContext. @mcp.tool() def query_db(ctx: Context[ServerSessionT, AppContext]) ...
Open Graph Description: Describe the bug The code does not work because the type annotation for the ctx argument is not simply Context; it includes type variables ServerSessionT and AppContext. @mcp.tool() def query_db(ct...
X Description: Describe the bug The code does not work because the type annotation for the ctx argument is not simply Context; it includes type variables ServerSessionT and AppContext. @mcp.tool() def query_db(ct...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/357
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`@mcp.tool()` Fails to Find Context Argument if its Type `Context` Includes Type Variables","articleBody":"**Describe the bug**\nThe code does not work because the type annotation for the `ctx` argument is not simply `Context`; it includes type variables `ServerSessionT` and `AppContext`.\n\n```python\n@mcp.tool()\ndef query_db(ctx: Context[ServerSessionT, AppContext]) -\u003e str:\n \"\"\"Tool that uses initialized resources\"\"\"\n db = ctx.request_context.lifespan_context.db\n return db.query()\n```\n\nThe MCP server assumes that `ctx` will be passed by clients, as its implementation only checks whether the annotation is exactly `Context`. \n\nYou can see this limitation in the following code:\n\nhttps://github.com/modelcontextprotocol/python-sdk/blob/9ae4df85fbab97bf476ddd160b766ca4c208cd13/src/mcp/server/fastmcp/tools/base.py#L53-L58\n\nThe necessity of adding type variables to `Context` is explained in issue #xxx.\n\n\n**To Reproduce**\n1. Apply the following patch:\n\n```patch\ndiff --git a/examples/fastmcp/simple_echo.py b/examples/fastmcp/simple_echo.py \nindex c261526..3069b7e 100644 \n--- a/examples/fastmcp/simple_echo.py\n+++ b/examples/fastmcp/simple_echo.py\n@@ -1,14 +1,15 @@\n \"\"\" \n FastMCP Echo Server \n \"\"\" \n+from typing import Any \n \n-from mcp.server.fastmcp import FastMCP \n+from mcp.server.fastmcp import FastMCP, Context \n \n # Create server \n mcp = FastMCP(\"Echo Server\") \n \n \n @mcp.tool() \n-def echo(text: str) -\u003e str: \n+def echo(ctx:Context[Any, Any], text: str) -\u003e str: \n \"\"\"Echo the input text\"\"\" \n return text \n```\n\n2. Run the following command:\n```\nuv run pytest tests/test_examples.py\n```\n\n \n3. The test will fail with the following error:\n\n```\nError executing tool echo: 1 validation error for echoArguments\nctx\n Field required [type=missing, input_value={'text': 'hello'}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.10/v/missing\n```\n\n**Expected behavior**\nType variables should be allowed in tool definitions. The MCP server should correctly handle `Context` annotations that include type variables.\n\n\n**Desktop (please complete the following information):**\n - OS: macOS 15.3.2\n - Browser N/A\n - Version N/A\n","author":{"url":"https://github.com/jkawamoto","@type":"Person","name":"jkawamoto"},"datePublished":"2025-03-24T08:49:49.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/357/python-sdk/issues/357"}
| 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:6e66f347-2fae-0318-2f96-842925195866 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A7AC:31CB57:1410602:1CA44DF:6A5A2261 |
| html-safe-nonce | 30a08b39d96a8e83eb98c7997682afc06a37c3f1e65f1e62e12394a3f25d13e3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBN0FDOjMxQ0I1NzoxNDEwNjAyOjFDQTQ0REY6NkE1QTIyNjEiLCJ2aXNpdG9yX2lkIjoiMjg3OTI0NDkzOTEzMjgwNTcyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5c48a8b7ec7013f1f25a1ca36de3c53c661527ab89f31cefee3c83464f91c295 |
| hovercard-subject-tag | issue:2942481656 |
| 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/357/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eac20548a5263b9f2d96cd9909d61aa1a3e99c5c925a4010400b69f435357481/modelcontextprotocol/python-sdk/issues/357 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eac20548a5263b9f2d96cd9909d61aa1a3e99c5c925a4010400b69f435357481/modelcontextprotocol/python-sdk/issues/357 |
| og:image:alt | Describe the bug The code does not work because the type annotation for the ctx argument is not simply Context; it includes type variables ServerSessionT and AppContext. @mcp.tool() def query_db(ct... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jkawamoto |
| hostname | github.com |
| expected-hostname | github.com |
| None | e31742e80bbd077fd5679c13ed870ce4e0c13803a2fa5beaae557b6769de2c8a |
| 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 | c9fcdbf98c619d1561ed843fa01d9766a32b5cd9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width