-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Enhanced ResourceTemplate URI matching and type handling logic. #1439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…eters to resources
Improve ResourceTemplate URI matching and type handling Fixes issue modelcontextprotocol#220 - Implemented typed-aware URI matching logic within ResourceTemplate.matches() supporting {name:str}, {id:int}, {value:float},{uuid:UUID}, and {path:path} placeholders. similar to what FastAPI supports - Added automatic type conversion and validation for URI parameters. - Updated unit tests to cover typed placeholders, numeric and float parameters, and path-based URIs. Limitations and notes: - Current implementation assumes URI templates follow a strict pattern or default to str and do not support optional parameters or wildcards beyond {path:path}.
…g it on every match, removed bare exception added value error
Hello, @felixweinberger, would you be able to help find someone who could review the code? Thank you! |
related: #436 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @beaterblank thank you for this contribution - FYI there's another PR #427 that's also enhancing URI handling, though for a slightly different usecase, might be some conflicts.
This PR takes the convertor approach suggested in #220, but @Kludex raised a good point on #427 whether we should aim for more of a FastAPI-level design, which makes sense in terms of abstraction level to me.
Rather than embedding convertor syntax in the URI template ({path:path}, {id:int})
, we could specify parameter behavior in the function signature using Annotated:
from typing import Annotated
@mcp.resource("files://{path}")
def get_file(path: Annotated[str, Path(allow_slashes=True)]):
...
This would also provide a unified way to handle both path and query parameters:
@mcp.resource("search://{category}")
def search(
category: str, # Simple path param, no annotation needed
format: Annotated[str, Query(default="json")], # Query param
limit: Annotated[int, Query(default=10)]
):
...
Yes, I've also just noticed this, and this is partly done in #427 , |
try: | ||
result[name] = conv.convert(raw_value) | ||
except Exception as e: | ||
raise ValueError(f"Failed to convert '{raw_value}' for '{name}': {e}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should This be a ValueError
? Since it only happens when the converter has a bad implementation of converting.
Should we go for RuntimeError
?
Merging #427 into this. |
Merged #427 Currently working on the Annotated parameter logic implementation, and addressing comments from #427. While reviewing the merge, I noticed this line in the MCP SDK: python-sdk/src/mcp/server/fastmcp/server.py Line 1073 in 40acbc5
Should we leave this as-is and handle |
|
Feature Added:
Enhanced
ResourceTemplate
URI matching and type handling logic.Motivation and Context
This change introduces a typed-aware URI parsing mechanism similar to FastAPI’s path parameter system.
It resolves issue #220 by adding a more flexible and self-contained approach for handling resource URIs with type annotations.
How Has This Been Tested?
Verified functionality through new and existing unit tests for:
{name:str}
,{id:int}
,{value:float}
,{uuid:UUID}
,{path:path}
)Confirmed correct parameter extraction and type enforcement in all matching scenarios.
Breaking Changes
Types of changes
Checklist
Additional context
The implementation assumes URI templates follow a strict pattern and defaults to
str
when no type is provided.Optional parameters and wildcard matching beyond
{path: path}
are not supported / cannot be supported.