fix: change Resource URI fields from AnyUrl to str#1863
Merged
Conversation
511541a to
d399ce4
Compare
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
d399ce4 to
9f417f3
Compare
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.
a1e1c46 to
3a2584e
Compare
Kludex
approved these changes
Jan 16, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Python SDK was incorrectly using Pydantic's
AnyUrlfor URI fields on resource types. This rejected relative paths likeusers/methat 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:
users/mecustom://resourcefile:///pathhttps://example.comThe 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?
tests/issues/test_1574_resource_uri_validation.pyAnyUrlBreaking Changes
This is a breaking change for users who were passing
AnyUrlobjects to URI fields. They will need to convert to strings. The client session methods (read_resource,subscribe_resource,unsubscribe_resource) accept bothstr | AnyUrlfor backwards compatibility.Updated
docs/migration.mdwith migration guidance.Types of changes
Checklist
Additional context
Types updated:
Resource.uriReadResourceRequestParams.uriResourceContents.uriSubscribeRequestParams.uriUnsubscribeRequestParams.uriResourceUpdatedNotificationParams.uriResource.uribase class