Skip to content

Conversation

@felixweinberger
Copy link
Contributor

Summary

The Python SDK was incorrectly using Pydantic's AnyUrl for URI fields on resource types. This rejected relative paths like users/me that are valid according to the MCP specification and accepted by the TypeScript SDK.

Motivation and Context

The MCP specification defines URI fields as plain strings with no JSON Schema format validation. This intentionally allows:

  • Relative paths: users/me
  • Custom schemes: custom://resource
  • File paths: file:///path
  • HTTP URLs: https://example.com

The Python SDK was stricter than the spec, causing interoperability issues when Python clients received resources from servers using relative URIs.

Fixes #1574

How Has This Been Tested?

  • Added new regression tests for relative path URIs in tests/issues/test_1574_resource_uri_validation.py
  • Updated existing tests to use plain strings instead of AnyUrl
  • Full test suite passes (743 passed, 94 skipped)

Breaking Changes

This is a breaking change for users who were passing AnyUrl objects to URI fields. They will need to convert to strings. The client session methods (read_resource, subscribe_resource, unsubscribe_resource) accept both str | AnyUrl for backwards compatibility.

Updated docs/migration.md with migration guidance.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • 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

Types updated:

  • Resource.uri
  • ReadResourceRequestParams.uri
  • ResourceContents.uri
  • SubscribeRequestParams.uri
  • UnsubscribeRequestParams.uri
  • ResourceUpdatedNotificationParams.uri
  • FastMCP Resource.uri base class

@felixweinberger felixweinberger marked this pull request as draft January 15, 2026 16:05
@felixweinberger felixweinberger force-pushed the fweinberger/fix-resource-uri-validation branch 5 times, most recently from 511541a to d399ce4 Compare January 16, 2026 08:19
Github-Issue: #1574
The Python SDK was incorrectly using Pydantic's AnyUrl for URI fields on
resource types. This rejected relative paths like 'users/me' that are
valid according to the MCP specification, which defines URIs as plain
strings without format validation.

This change updates the following types to use str instead of AnyUrl:
- Resource.uri
- ReadResourceRequestParams.uri
- ResourceContents.uri
- SubscribeRequestParams.uri
- UnsubscribeRequestParams.uri
- ResourceUpdatedNotificationParams.uri
- FastMCP Resource.uri (base class)

Client and server session methods now accept str | AnyUrl for backwards
compatibility, converting to string internally.

Updates migration.md with documentation of this breaking change.

Github-Issue: #1574
Replace type-instantiation tests with meaningful tests that verify:
- Relative URIs survive the full server-client JSON-RPC roundtrip
- Custom scheme URIs work end-to-end
- URIs are preserved exactly through JSON serialization

These tests catch regressions more reliably - if AnyUrl is reintroduced,
the integration tests will fail during serialization or URI transformation.

Github-Issue: #1574
@felixweinberger felixweinberger force-pushed the fweinberger/fix-resource-uri-validation branch from d399ce4 to 9f417f3 Compare January 16, 2026 08:24
With URI fields changed from AnyUrl to str, URIs are no longer
normalized (e.g., trailing slashes not added). Update test assertions
to match exact URIs being sent.
@felixweinberger felixweinberger force-pushed the fweinberger/fix-resource-uri-validation branch from a1e1c46 to 3a2584e Compare January 16, 2026 08:39
@felixweinberger felixweinberger marked this pull request as ready for review January 16, 2026 08:54
@Kludex Kludex merged commit cfb2909 into main Jan 16, 2026
25 checks passed
@Kludex Kludex deleted the fweinberger/fix-resource-uri-validation branch January 16, 2026 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python SDK incorrectly validates Resource URIs as strict URLs, breaking relative paths

3 participants