Skip to content

[Server] Reject a placeholder-less resource template URI at registration - #477

Open
wachterjohannes wants to merge 1 commit into
modelcontextprotocol:mainfrom
wachterjohannes:fix/resource-template-validation-at-registration
Open

[Server] Reject a placeholder-less resource template URI at registration#477
wachterjohannes wants to merge 1 commit into
modelcontextprotocol:mainfrom
wachterjohannes:fix/resource-template-validation-at-registration

Conversation

@wachterjohannes

Copy link
Copy Markdown
Contributor

Fixes #476.

A URI template without a placeholder was accepted by Builder::addResourceTemplate() and only rejected when the registry was loaded. Loading is lazy by default, so that happened while a request was being served, aborted the whole load, and left every other element unreachable: one malformed template answered tools/list and tools/call with -32602 and the template's message.

The builder now refuses it where it is written, naming the handler and pointing a URI that addresses a single resource at addResource(). Builder::add() already validated eagerly, since it takes a constructed ResourceTemplate, so this brings the two registration entry points in line.

Adds ResourceTemplate::isValidUriTemplate() so the check lives with the pattern it belongs to.

Not addressed here, both worth a separate look: a ConfigurationException reaching the client as -32602 (Invalid params) for a server-side configuration error, and a single failing element aborting the whole registry load.

A URI template without a placeholder was accepted by
Builder::addResourceTemplate() and only rejected when the registry was
loaded. Loading is lazy by default, so that happened while a request was
being served, aborted the whole load and left every other element
unreachable: one malformed template answered tools/list and tools/call
with -32602 and the template's message.

The builder now refuses it where it is written, naming the handler and
pointing a URI that addresses a single resource at addResource().

Fixes modelcontextprotocol#476
@chr-hertel chr-hertel added the Server Issues & PRs related to the Server component label Aug 23, 2026
Comment thread src/Server/Builder.php
): self {
if (!ResourceTemplate::isValidUriTemplate($uriTemplate)) {
throw new InvalidArgumentException(\sprintf('Invalid URI template "%s" for resource template handler %s: a template needs a scheme and at least one placeholder, e.g. "user://{userId}/profile". Use addResource() for a URI that addresses a single resource.', $uriTemplate, $this->describeHandler($handler)));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

bringing the validation logic into Builder is not a real option to me. even tho you tried to mitigate with that new isValidUriTemplate method, this still is a bit leaky and wouldn't scale nice - think of bringing all those validation+exception paths to the Builder ... 😬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Server Issues & PRs related to the Server component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

One placeholder-less resource template makes the whole server unserviceable

2 participants