FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Enhance resource template to support values with slashes (Fix #159) by g-votte · Pull Request #197 · modelcontextprotocol/python-sdk · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion src/mcp/server/fastmcp/resources/templates.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def from_function(
def matches(self, uri: str) -> dict[str, Any] | None:
"""Check if URI matches template and extract parameters."""
# Convert template to regex pattern
pattern = self.uri_template.replace("{", "(?P<").replace("}", ">[^/]+)")
pattern = self.uri_template.replace("{", "(?P<").replace("}", ">.+)")
match = re.match(f"^{pattern}$", uri)
if match:
return match.groupdict()
Expand Down
5 changes: 0 additions & 5 deletions tests/issues/test_141_resource_templates.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ def get_user_profile_missing(user_id: str) -> str:
with pytest.raises(ValueError, match="Unknown resource"):
await mcp.read_resource("resource://users/123/posts") # Missing post_id

with pytest.raises(ValueError, match="Unknown resource"):
await mcp.read_resource(
"resource://users/123/posts/456/extra"
) # Extra path component


@pytest.mark.anyio
async def test_resource_template_client_interaction():
Expand Down
19 changes: 19 additions & 0 deletions tests/server/fastmcp/resources/test_resource_template.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ def my_func(key: str, value: int) -> dict:
assert template.matches("test://foo") is None
assert template.matches("other://foo/123") is None

def test_template_matches_slashes_in_value(self):
"""Test matching URIs against a template, the value containing slashes."""

def my_func(path: str) -> dict:
return {"path": path}

template = ResourceTemplate.from_function(
fn=my_func,
uri_template="test://content/{path}",
name="test",
)

# Valid match
params = template.matches("test://content/path/with/slashes")
assert params == {"path": "path/with/slashes"}

# No match
assert template.matches("test://content/") is None

@pytest.mark.anyio
async def test_create_resource(self):
"""Test creating a resource from a template."""
Expand Down

Back | FazBrowse Home | New Git URL