René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:6e66f347-2fae-0318-2f96-842925195866
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA7AC:31CB57:1410602:1CA44DF:6A5A2261
html-safe-nonce30a08b39d96a8e83eb98c7997682afc06a37c3f1e65f1e62e12394a3f25d13e3
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBN0FDOjMxQ0I1NzoxNDEwNjAyOjFDQTQ0REY6NkE1QTIyNjEiLCJ2aXNpdG9yX2lkIjoiMjg3OTI0NDkzOTEzMjgwNTcyOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac5c48a8b7ec7013f1f25a1ca36de3c53c661527ab89f31cefee3c83464f91c295
hovercard-subject-tagissue:2942481656
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/357/issue_layout
twitter:imagehttps://opengraph.githubassets.com/eac20548a5263b9f2d96cd9909d61aa1a3e99c5c925a4010400b69f435357481/modelcontextprotocol/python-sdk/issues/357
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/eac20548a5263b9f2d96cd9909d61aa1a3e99c5c925a4010400b69f435357481/modelcontextprotocol/python-sdk/issues/357
og:image:altDescribe 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamejkawamoto
hostnamegithub.com
expected-hostnamegithub.com
Nonee31742e80bbd077fd5679c13ed870ce4e0c13803a2fa5beaae557b6769de2c8a
turbo-cache-controlno-preview
go-importgithub.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git
octolytics-dimension-user_id182288589
octolytics-dimension-user_loginmodelcontextprotocol
octolytics-dimension-repository_id862584018
octolytics-dimension-repository_nwomodelcontextprotocol/python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id862584018
octolytics-dimension-repository_network_root_nwomodelcontextprotocol/python-sdk
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasec9fcdbf98c619d1561ed843fa01d9766a32b5cd9
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/modelcontextprotocol/python-sdk/issues/357#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F357
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F357
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/357
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/357
Reloadhttps://github.com/modelcontextprotocol/python-sdk/issues/357
Please reload this pagehttps://github.com/modelcontextprotocol/python-sdk/issues/357
modelcontextprotocol https://github.com/modelcontextprotocol
python-sdkhttps://github.com/modelcontextprotocol/python-sdk
Notifications https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Fork 3.7k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Star 23.6k https://github.com/login?return_to=%2Fmodelcontextprotocol%2Fpython-sdk
Code https://github.com/modelcontextprotocol/python-sdk
Issues 255 https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests 303 https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality 6 https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
Code https://github.com/modelcontextprotocol/python-sdk
Issues https://github.com/modelcontextprotocol/python-sdk/issues
Pull requests https://github.com/modelcontextprotocol/python-sdk/pulls
Actions https://github.com/modelcontextprotocol/python-sdk/actions
Projects https://github.com/modelcontextprotocol/python-sdk/projects
Models https://github.com/modelcontextprotocol/python-sdk/models
Security and quality https://github.com/modelcontextprotocol/python-sdk/security
Insights https://github.com/modelcontextprotocol/python-sdk/pulse
@mcp.tool() Fails to Find Context Argument if its Type Context Includes Type Variableshttps://github.com/modelcontextprotocol/python-sdk/issues/357#top
https://github.com/jkawamoto
jkawamotohttps://github.com/jkawamoto
on Mar 24, 2025https://github.com/modelcontextprotocol/python-sdk/issues/357#issue-2942481656
python-sdk/src/mcp/server/fastmcp/tools/base.pyhttps://github.com/modelcontextprotocol/python-sdk/blob/9ae4df85fbab97bf476ddd160b766ca4c208cd13/src/mcp/server/fastmcp/tools/base.py#L53-L58
9ae4df8https://github.com/modelcontextprotocol/python-sdk/commit/9ae4df85fbab97bf476ddd160b766ca4c208cd13
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.