Skip to content

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

Merged
chr-hertel merged 2 commits intomodelcontextprotocol:mainfrom
blackwell-systems:fix/uri-pattern-rfc3986
May 8, 2026
Merged

[Schema] fix: allow URIs without double slash in Resource and ResourceTemplate (RFC 3986)#297
chr-hertel merged 2 commits intomodelcontextprotocol:mainfrom
blackwell-systems:fix/uri-pattern-rfc3986

Conversation

@blackwell-systems
Copy link
Copy Markdown
Contributor

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
Comment thread tests/Unit/Schema/ResourceTemplateTest.php Outdated
Comment thread tests/Unit/Schema/ResourceTest.php Outdated
@chr-hertel chr-hertel changed the title fix: allow URIs without double slash in Resource and ResourceTemplate (RFC 3986) [Schema] fix: allow URIs without double slash in Resource and ResourceTemplate (RFC 3986) May 8, 2026
@chr-hertel chr-hertel added Schema Issues & PRs related to the Schema component P2 Moderate issues affecting some users, edge cases, potentially valuable feature bug Something isn't working labels May 8, 2026
Copy link
Copy Markdown
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @blackwell-systems - briefly renamed some test methods while merging, JFYI.

@chr-hertel chr-hertel merged commit 7974727 into modelcontextprotocol:main May 8, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working P2 Moderate issues affecting some users, edge cases, potentially valuable feature Schema Issues & PRs related to the Schema component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants