Skip to content

Conversation

@felixweinberger
Copy link
Contributor

Summary

Change Resource URI fields from Pydantic's AnyUrl to plain str to match the MCP specification and TypeScript SDK behavior.

Motivation and Context

The Python SDK was using AnyUrl for URI fields which rejected relative paths like users/me that are valid according to the MCP spec. The spec defines uri as a plain string, and the TypeScript SDK uses z.string() with no validation.

Fixes #1574

How Has This Been Tested?

  • Added tests/issues/test_1574_resource_uri_validation.py with 9 test cases covering all affected types
  • Updated existing tests to use plain strings instead of AnyUrl() wrappers
  • All 55 resource tests pass

Breaking Changes

Minor - code that explicitly passes AnyUrl objects will still work (auto-converted to string). The ClientSession methods maintain backwards compatibility by accepting both str and AnyUrl.

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

Affected types in src/mcp/types.py:

  • Resource.uri
  • ReadResourceRequestParams.uri
  • ResourceContents.uri
  • SubscribeRequestParams.uri
  • UnsubscribeRequestParams.uri
  • ResourceUpdatedNotificationParams.uri

Migration guide added to docs/migration.md.

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
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

2 participants