Enhance resource template to support values with slashes (Fix #159) #197
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.