Enhance resource template to support values with slashes (Fix #159)#197
Enhance resource template to support values with slashes (Fix #159)#197g-votte wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
|
I can't get any resource params to show up in my MCP Inspector, not sure if its related to the above. 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}" |
Thank you for working on this and the detailed description. After more time thinking about this, I think the current behaviour is desired. If we want to support paths we should go down the route of FastAPI which offers I'll close this PR, and sorry for you spending the time without it being merged. I think the detailed description made it clear how to best think about this for me. Thank you |
This PR enhances resource templates so that they can handle values containing slashes.
Motivation and Context
Motivation
This PR fixes the bug reported in #159 where the following example raises an error for paths containing slashes such as
resource://content/path/with/slashes.Currently, this raises
mcp.shared.exceptions.McpError: Unknown resource: resource://content/path/with/slashes.Screenshot from MCP inspector:

Analysis
The root cause is in
ResourceTemplate.matches()which explicitly excludes slashes from the regex matching:python-sdk/src/mcp/server/fastmcp/resources/templates.py
Line 58 in f10665d
Content of Fix
tests/server/fastmcp/resources/test_resource_template.pytests/issues/test_141_resource_templates.pywhich expected errors for slash-containing valuesHow Has This Been Tested?
tests/server/fastmcp/resources/test_resource_template.pyBreaking Changes
While this change primarily enhances functionality without breaking existing behavior, there might be edge cases where applications rely on the current error behavior for paths containing slashes. However, such cases are expected to be rare.
Types of changes
Checklist
Additional context
While #159 marks this as a "bug," there is an existing test case suggesting this might have been intentional behavior:
python-sdk/tests/issues/test_141_resource_templates.py
Lines 61 to 64 in f10665d
I would appreciate maintainers' input on the intended behavior. If rejecting slash-containing values is the desired design, please close this PR.