Title: feat: RFC 6570 URI templates with operator-aware security by maxisbey · Pull Request #2356 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: feat: RFC 6570 URI templates with operator-aware security by maxisbey · Pull Request #2356 · modelcontextprotocol/python-sdk
X Title: feat: RFC 6570 URI templates with operator-aware security by maxisbey · Pull Request #2356 · modelcontextprotocol/python-sdk
Description: Replaces the regex-based resource template matcher with a linear-time RFC 6570 implementation, adds configurable path-safety validation, and exports a standalone UriTemplate usable from both client and low-level server. Motivation The existing matcher only supports RFC 6570 Level 1 ({var}). Users writing file://{+path} for multi-segment paths hit a confusing "mismatch" error because the regex-based extraction doesn't understand the + operator. The TypeScript, C#, and Go SDKs all support Level 3+. The matcher also doesn't escape template literals, so data://v1.0/{id} incorrectly matches data://v1X0/42. Separately, encoded ../ sequences in extracted parameters are passed to handlers unchecked. The 2026 spec adds a normative requirement that file:// resource servers sanitize paths. Closes #436, closes #220, closes #159, closes #378 Supersedes #197, #427, #1439, #2353 What's in here mcp.shared.uri_template.UriTemplate — RFC 6570 Levels 1-3 plus path-style explode ({/var*}, {.var*}, {;var*}). parse() / expand() / match(). Re-exported at the top level so clients can expand a template a server advertises. The matcher is a two-ended linear scan with no backtracking. Rather than handling ambiguous templates with scan-time special cases, the parser rejects them up front: two variables adjacent with no literal between them, more than one multi-segment variable, and more than one {?...} expression all raise InvalidUriTemplate at decoration time. Operators that emit their own delimiter ({+path}{.ext}, {+path}{/seg}, {+path}{;k}) still compose. Query expressions match leniently (order-agnostic, partial, extras ignored), and a handler parameter bound to one must declare a Python default — also checked at decoration time. mcp.shared.path_security — contains_path_traversal(), is_absolute_path(), safe_join(). The last resolves and verifies containment, catching symlink escapes. ResourceSecurity policy — wired into MCPServer(resource_security=...) and @mcp.resource(..., security=...). By default rejects .. escapes, absolute paths, and null bytes. A rejection is indistinguishable on the wire from a not-found resource (-32602) and halts template iteration. Per-parameter opt-out via exempt_params={"name"}. Tests Example-based suites for UriTemplate and path_security, e2e tests through Client for the decorator and policy, plus two seeded property tests over the full space the parser accepts: match(expand(v)) round-trips and match() never raises on any input. 100% coverage. Breaking changes See docs/migration.md for the full list. Headline items: Path-safety checks applied by default (opt out via ResourceSecurity) Template literals match exactly; {var} stops at structural delimiters and now matches an empty value Two adjacent variables, more than one multi-segment variable, more than one {?...}, and other malformed templates raise at decoration time A {?...}-bound handler parameter without a default raises at decoration time Docs Tested reference page at docs/advanced/uri-templates.md with runnable examples under docs_src/uri_templates/. AI Disclaimer
Open Graph Description: Replaces the regex-based resource template matcher with a linear-time RFC 6570 implementation, adds configurable path-safety validation, and exports a standalone UriTemplate usable from both client...
X Description: Replaces the regex-based resource template matcher with a linear-time RFC 6570 implementation, adds configurable path-safety validation, and exports a standalone UriTemplate usable from both client...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2356
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:c2f7b576-e0a7-a6d9-c854-4b685b9c808e |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | BFB6:46803:A93F0C:F47470:6A58D0E1 |
| html-safe-nonce | 44e69845f0e2a9131e44e27bde7a8ec053141fb26297c284bf51ab471b22cc79 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRkI2OjQ2ODAzOkE5M0YwQzpGNDc0NzA6NkE1OEQwRTEiLCJ2aXNpdG9yX2lkIjoiNDU2NDI3MDI2NDE2MzU1MzUwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cd801f61279250aba8f36303662147656c3b768f0357963bd50ce784c82a4d96 |
| hovercard-subject-tag | pull_request:3452922756 |
| 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/2356/files |
| twitter:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| og:image:alt | Replaces the regex-based resource template matcher with a linear-time RFC 6570 implementation, adds configurable path-safety validation, and exports a standalone UriTemplate usable from both client... |
| 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