Skip to content

Commit

Permalink
fix: #129 resource template handling in FastMCP server
Browse files Browse the repository at this point in the history
  • Loading branch information
dsp-ant committed Jan 6, 2025
1 parent b66c675 commit 4770bcd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ def _setup_handlers(self) -> None:
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)
self._mcp_server.list_resource_templates()(self.list_resource_templates)

async def list_tools(self) -> list[MCPTool]:
"""List all available tools."""
Expand Down
44 changes: 44 additions & 0 deletions tests/issues/test_129_resource_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

from mcp import types
from mcp.server.fastmcp import FastMCP


@pytest.mark.anyio
async def test_resource_templates():
# Create an MCP server
mcp = FastMCP("Demo")

# 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("users://{user_id}/profile")
def get_user_profile(user_id: str) -> str:
"""Dynamic user data"""
return f"Profile data for user {user_id}"

# Get the list of resource templates using the underlying server
# Note: list_resource_templates() returns a decorator that wraps the handler
# The handler returns a ServerResult with a ListResourceTemplatesResult inside
result = await mcp._mcp_server.request_handlers[types.ListResourceTemplatesRequest](
types.ListResourceTemplatesRequest(
method="resources/templates/list", params=None, cursor=None
)
)
assert isinstance(result.root, types.ListResourceTemplatesResult)
templates = result.root.resourceTemplates

# Verify we get both templates back
assert len(templates) == 2

# Verify template details
greeting_template = next(t for t in templates if t.name == "get_greeting")
assert greeting_template.uriTemplate == "greeting://{name}"
assert greeting_template.description == "Get a personalized greeting"

profile_template = next(t for t in templates if t.name == "get_user_profile")
assert profile_template.uriTemplate == "users://{user_id}/profile"
assert profile_template.description == "Dynamic user data"

0 comments on commit 4770bcd

Please sign in to comment.