Title: Enhance resource template to support values with slashes (Fix #159) by g-votte · Pull Request #197 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Enhance resource template to support values with slashes (Fix #159) by g-votte · Pull Request #197 · modelcontextprotocol/python-sdk
X Title: Enhance resource template to support values with slashes (Fix #159) by g-votte · Pull Request #197 · modelcontextprotocol/python-sdk
Description: 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. from mcp.server.fastmcp import FastMCP mcp = FastMCP("My App") @mcp.resource("resource://content/{path}") def get_content(path: str) -> str: return f"Path: {path}" 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 pattern = self.uri_template.replace("{", "(?P<").replace("}", ">[^/]+)") Content of Fix Modified the regex pattern to allow slashes in parameter values Added test cases in tests/server/fastmcp/resources/test_resource_template.py Removed test case in tests/issues/test_141_resource_templates.py which expected errors for slash-containing values How Has This Been Tested? Unit testing: Added a test case in tests/server/fastmcp/resources/test_resource_template.py Integration testing: Verified the example from the Motivation section works correctly in MCP inspector Breaking 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 Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed 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 with pytest.raises(ValueError, match="Unknown resource"): await mcp.read_resource( "resource://users/123/posts/456/extra" ) # Extra path component I would appreciate maintainers' input on the intended behavior. If rejecting slash-containing values is the desired design, please close this PR.
Open Graph Description: 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 a...
X Description: 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 a...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/197
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:2711e6a8-5016-b52d-9248-758139f2792f |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 95F0:296041:108B099:17B2595:6A589EBE |
| html-safe-nonce | 957b56374a0b40294a557f92a54462e5d8ccabd131acfdae983fd2ecc3fe9e18 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NUYwOjI5NjA0MToxMDhCMDk5OjE3QjI1OTU6NkE1ODlFQkUiLCJ2aXNpdG9yX2lkIjoiMzIwMTg1NTMyNTg5ODA1NTM1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1064d56b02a6e610c52d9b694d520ca7b1f3e2f2fbbdec620030348173adf2ef |
| hovercard-subject-tag | pull_request:2324326108 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/197/files |
| twitter:image | https://avatars.githubusercontent.com/u/1061922?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1061922?s=400&v=4 |
| og:image:alt | 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 a... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8aae7b8d6caacacf5c66eaeb2702d8dc88d85b4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width