Skip to content

fix: allow URIs without double slash in Resource and ResourceTemplate (RFC 3986)#297

Open
blackwell-systems wants to merge 1 commit intomodelcontextprotocol:mainfrom
blackwell-systems:fix/uri-pattern-rfc3986
Open

fix: allow URIs without double slash in Resource and ResourceTemplate (RFC 3986)#297
blackwell-systems wants to merge 1 commit intomodelcontextprotocol:mainfrom
blackwell-systems:fix/uri-pattern-rfc3986

Conversation

@blackwell-systems
Copy link
Copy Markdown

Summary

The URI regex in Resource and ResourceTemplate requires :// after the scheme, rejecting valid URIs where the hier-part does not start with //. Per RFC 3986, the authority component (and its // prefix) is optional.

What breaks

// All of these throw InvalidArgumentException:
new Resource(uri: 'urn:isbn:0451450523', name: 'book');
new Resource(uri: 'mailto:user@example.com', name: 'contact');
new Resource(uri: 'data:text/plain;base64,SGVsbG8=', name: 'data');
new Resource(uri: 'config:myapp/settings', name: 'settings');

new ResourceTemplate(uriTemplate: 'config:{key}', name: 'config');

The MCP spec allows custom URI schemes following RFC 3986.

Fix

Changed :// to : in both regex patterns:

- '/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^\s]*$/'
+ '/^[a-zA-Z][a-zA-Z0-9+.-]*:[^\s]*$/'

- '/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/.*{[^{}]+}.*/'
+ '/^[a-zA-Z][a-zA-Z0-9+.-]*:.*{[^{}]+}.*/'

URIs with :// (like file://path) continue to match.

Test plan

  • Added data-provider tests for urn:, mailto:, data:, and custom schemes without //
  • Added data-provider tests for resource templates without //
  • All 20 tests pass (phpunit tests/Unit/Schema/)
  • Existing tests unchanged

Fixes #293

… (RFC 3986)

The URI regex required "://" after the scheme, rejecting valid URIs
like urn:isbn:123, mailto:user@example.com, config:key, and data:
URIs. RFC 3986 only requires ":" after the scheme; the "//" is part
of the authority component which is optional.

Changed both Resource.URI_PATTERN and ResourceTemplate.URI_TEMPLATE_PATTERN
to require ":" instead of "://".

Adds data-provider tests for URIs without double slash in both classes.

Fixes modelcontextprotocol#293
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.

[Schema] URI Regex doesn't seem to be correct

1 participant