fastmcp
fastmcp copied to clipboard
`@mcp.resource` with dynamic parameters example don't work
In the README, there is an example:
@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}"
This don't work
I can't see any resources in the mcp inspector
Same for me, I copied the example server from README.md
from 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}!"
if __name__ == "__main__":
mcp.run()
Then started the inspector with:
uv run fastmcp dev fastmcp.py
I can see the add tool but I get errors for the resource in Error output from MCP server
[12/10/24 17:30:03] INFO Processing request of type __init__.py:431 ListResourcesRequest
(btw it's a bit weird that the log is labelled as INFO in the error output, not sure if this is a bug or a feature :) )
In history I have:
resources/listโผ
Request:
{
"method": "resources/list",
"params": {}
}
Response:
{
"resources": []
}
@jlowin
Anyone got a solution to this problem? Thanks.
It appears to be an issue when there is a parameter for a resource. Resources without parameters appear to work fine.
This is related to #10, the current version of fastmcp simply doesn't support resource templates listing
hi all @micpst @gmr @mcp2everything @vemonet @CNSeniorious000 - ICYMI
https://github.com/jlowin/fastmcp?tab=readme-ov-file#-fastmcp-has-been-added-to-the-official-mcp-sdk-
@zzstoatzz thanks my bad, seems to be fixed but not released yet
I'm having a similar issue on the official python-sdk. I can't get any resource params to show up in my MCP Inspector, might have to switch to native.
This works fine.
@mcp.resource("config://app")
def get_config() -> str:
"""Static configuration data"""
return "App configuration here"
Once I add an input param it does not show up as a listed resource
@mcp.resource("users://{hello}/message")
def get_config(hello: str) -> str:
"""dynamic user message"""
return "Message here {hello}"
I have the same issue. Anthropic suggests that for dynamic resources should be registered with a template however FastMCP says that dynamic resources are automatically handled as templates. FastMCP has list_templates function implemented too. Not sure what is happening.
On top of the problem, the mcp.resource doesn't work in case when function has a ctx parameter:
mcp = FastMCP("Resonanz")
@mcp.resource("system://status")
async def get_system_status(ctx: Context) -> dict:
"""Checks system status and logs information."""
ctx.log(ctx.request_context)
return {
"status": "ok",
"message": "System is running",
"request_context": ctx.request_context,
}
raises:
94 โ โ # Validate that URI params match function params โญโโโโโโโโโโโโโโ locals โโโโโโโโโโโโโโโฎ โ
โ 95 โ โ uri_params = set(re.findall(r"{(\w+)}", uri_template)) โ description = None โ โ
โ 96 โ โ if not uri_params: โ func_name = 'get_system_status' โ โ
โ โฑ 97 โ โ โ raise ValueError("URI template must contain at least one parameter") โ mime_type = None โ โ
โ 98 โ โ โ name = None โ โ
โ 99 โ โ func_params = set(inspect.signature(fn).parameters.keys()) โ tags = None โ โ
โ 100 โ uri_params = set() โ โ
โ โ uri_template = 'system://status' โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Only tools support context injection (see documentation https://gofastmcp.com/servers/context). There is an open issue for supplying context to resources and prompts (#134) if you'd like to support it.