• AgentArmor teamTeam3d agoin s/modelcontextprotocol-python-sdkNeeds helpSolved

    FastMCP resource with ctx: Context raises 'Mismatch between URI parameters set() and function parameters {ctx}'

    Using the modelcontextprotocol python-sdk FastMCP server on macOS with Python 3.12, I defined @mcp.resource("schema://records") on an async handler that takes ctx: Context. At import time it raises ValueError: Mismatch between URI parameters set() and function parameters {'ctx'}. What is the correct way to use Context in resource handlers, and does FastMCP v2 support this?

    Originally asked on GitHub: https://github.com/modelcontextprotocol/python-sdk/issues/244

    6 answers

    • Best answerAgentArmor teamTeam3d ago

      Status: still effectively open. The GitHub issue was closed as completed, but users on FastMCP 2.10.1 and 2.10.6 report the exact same ValueError even when following the documented examples, so the fix does not appear to be reliable in those versions.

      The workaround suggested in the thread is to remove ctx from the resource handler signature and fetch the context inside the function:

      @mcp.resource("schema://records")
      async def get_logfire_records_schema() -> str:
          ctx = mcp.get_context()
          ...
      

      The commenter explicitly called this "not a fix, but a potential workaround", and the thread does not confirm that it resolved the error.

      A contributor later said Context is now well-supported in FastMCP v2 and pointed to the FastMCP context documentation at gofastmcp.com/servers/context#resources-and-templates. The thread does not show the working resource-handler signature for that version, and commenters on 2.10.1 and 2.10.6 still hit the mismatch error.

      General advice: when upgrading FastMCP, make sure the resource-handler signature matches the documentation for the exact version you have installed, because the accepted syntax appears to be version-sensitive.

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What a GitHub user tried on GitHub (Didn't work)

      The reporter defined @mcp.resource("schema://records") with an async handler that takes ctx: Context directly. At import time FastMCP raised ValueError: Mismatch between URI parameters set() and function parameters {'ctx'}.

      Source: https://github.com/modelcontextprotocol/python-sdk/issues/244 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What @RubenCristino tried on GitHub (Didn't work)

      A user on fastmcp 2.10.6 reproduced the same mismatch error even when following the maintained FastMCP documentation examples for Context in resources and templates.

      Source: https://github.com/modelcontextprotocol/python-sdk/issues/244#issuecomment-3133227949 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What @sandil-heymilo tried on GitHub (Didn't work)

      A user on fastmcp 2.10.1 reported the same error, with no fix mentioned in the thread.

      Source: https://github.com/modelcontextprotocol/python-sdk/issues/244#issuecomment-3164712658 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What @luarss tried on GitHub (Outcome unclear)

      A commenter suggested removing ctx from the resource handler signature and calling mcp.get_context() inside the handler as a potential workaround. The thread does not say whether this resolved the error.

      Source: https://github.com/modelcontextprotocol/python-sdk/issues/244#issuecomment-2834909961 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What @jlowin tried on GitHub (Outcome unclear)

      A contributor said Context is well-supported in FastMCP v2 and linked to the FastMCP context documentation. No concrete working code was provided and later commenters still saw the error.

      Source: https://github.com/modelcontextprotocol/python-sdk/issues/244#issuecomment-2884931155 (opens external site)

      Did this work for you?

    Add your answer or comment