8000 resourceTemplate do not - but do actually work in Python SDK v1.2.0 · Issue #129 · modelcontextprotocol/python-sdk · GitHub
[go: up one dir, main page]

Skip to content
resourceTemplate do not - but do actually work in Python SDK v1.2.0 #129
Closed
@wanderingnature

Description

@wanderingnature

Describe the bug
resourceTemplates do not work, but actually it seems they do work....

To Reproduce
Steps to reproduce the behavior:
Follow the example at: https://github.com/modelcontextprotocol/python-sdk
Create the Demo server in latest release:

# server.py
from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Demo")

# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b

# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """Get a personalized greeting"""
    return f"Hello, {name}!"

@mcp.resource("config://app")
def get_config() -> str:
    """Static configuration data"""
    return "App configuration here"

@mcp.resource("users://{user_id}/profile")
def get_user_profile(user_id: str) -> str:
    """Dynamic user data"""
    return f"Profile data for user {user_id}"

Expected behavior
Using inspector I should be able to 'List Resources' (that works) and 'List Templates(throws error).
Click List Templates in inspector throws:
Error
MCP error -32601: Method not found

I should see the two resourceTemplates in my Demo server.py
There are open issues in this repository related to same behavior with resourceTemplates and ./.venv/lib/python3.12/site-packages/mcp/server/fastmcp/server.py there is:

    def _setup_handlers(self) -> None:
        """Set up core MCP protocol handlers."""
        self._mcp_server.list_tools()(self.list_tools)
        self._mcp_server.call_tool()(self.call_tool)
        self._mcp_server.list_resources()(self.list_resources)
        self._mcp_server.read_resource()(self.read_resource)
        self._mcp_server.list_prompts()(self.list_prompts)
        self._mcp_server.get_prompt()(self.get_prompt)
        # TODO: This has not been added to MCP yet, see https://github.com/jlowin/fastmcp/issues/10
        # self._mcp_server.list_resource_templates()(self.list_resource_templates)

I removed the comment

    def _setup_handlers(self) -> None:
        """Set up core MCP protocol handlers."""
        self._mcp_server.list_tools()(self.list_tools)
        self._mcp_server.call_tool()(self.call_tool)
        self._mcp_server.list_resources()(self.list_resources)
        self._mcp_server.read_resource()(self.read_resource)
        self._mcp_server.list_prompts()(self.list_prompts)
        self._mcp_server.get_prompt()(self.get_prompt)
        # TODO: This has not been added to MCP yet, see https://github.com/jlowin/fastmcp/issues/10
        self._mcp_server.list_resource_templates()(self.list_resource_templates)

Apparently resourceTemplates are added to MCP (and I use them myself prior to FastMCP, just with python SDK)
Now I get the expected result.

REQUEST:

{"method":"resources/templates/list","params":{}}

RESPONSE:

{"resourceTemplates":[{"uriTemplate":"greeting://{name}","name":"get_greeting","description":"Get a personalized greeting"},{"uriTemplate":"users://{user_id}/profile","name":"get_user_profile","description":"Dynamic user data"}]}

Desktop (please complete the following information):
OS: Monterey 12.7.6
Browser Chrome

Additional context
just need to remove the '#' and enable the method it seems...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0