Skip to content

Conversation

@pushpak1300
Copy link
Member

@pushpak1300 pushpak1300 commented Nov 24, 2025

Resolves #69

This PR introduces support for ResourceTemplate using an interface based approach. The earlier implementation required extending a dedicated ResourceTemplate class, which added extra structure that was not really necessary. With this update, URI template support becomes an optional capability that any resource can opt into by implementing a small interface.

Usage

use Laravel\Mcp\Server\Contracts\SupportsURITemplate;
use Laravel\Mcp\Server\Resource;
use Laravel\Mcp\Support\UriTemplate;

class UserFileResource extends Resource implements SupportsURITemplate
{
    protected string $mimeType = 'text/plain';

    protected string $description = 'Access user files by ID';

    public function uriTemplate(): UriTemplate
    {
        return new UriTemplate('file://users/{userId}/files/{fileId}');
    }

    public function handle(Request $request): Response
    { 
        $userId = $request->get('userId');
        $fileId = $request->get('fileId');
        $uri = $request->uri();

        return Response::text("User $userId, File $fileId");
    }
}

URI Template

Why Simplified

The MCP spec references RFC 6570 for URI template behavior, but full RFC support is more than what MCP resources typically need. This intentionally implements a simpler subset. The approach mirrors what the official PHP MCP SDK docs here, and we followed the same interpretation.

Supported

  • Simple placeholders like {variable}
  • Multiple variables in a path such as {var1}/{var2}
  • Variables match a single path segment
  • Alphanumeric variable names
new UriTemplate('file://users/{userId}');
new UriTemplate('file://users/{userId}/files/{fileId}');
new UriTemplate('https://api.example.com/{version}/{resource}/{id}');

Not Supported

  • {+var}
  • {#var}
  • {.var}
  • {/var}
  • {?q,limit}
  • {&page}
  • {var*}

Important

Variables cannot span across multiple path segments.

// Valid
$template = new UriTemplate('file://files/{filename}');
$template->match('file://files/document.txt'); // returns ['filename' => 'document.txt']
// Not valid
$template->match('file://files/folder/document.txt'); // returns null

@pushpak1300 pushpak1300 changed the title Add Resource Template Add Support For Resource Template Nov 24, 2025
pushpak1300 and others added 5 commits November 25, 2025 00:43
* Add meta and security scheme to tools

* Fix phpstan definition

* WIP 🚧

* WIP

* Update the meta $property addition

* Introduce `ResponseFactory` for handling responses with result-level metadata.

* Replace `UnexpectedValueException` with `InvalidArgumentException` in `ResponseFactory` and improve type validation logic.

* Formatting

* Fix test and change the API

* Refactor

* Refactor

* Update method signatures

* Update Test

* Improve testing

---------

Co-authored-by: zacksmash <zachary.warren@me.com>
# Conflicts:
#	tests/Unit/Methods/ReadResourceTest.php
* Remove non-spec fields from resource content responses

* Update the minimum code coverage threshold to 91.7%
# Conflicts:
#	tests/Unit/Methods/ReadResourceTest.php
@pushpak1300 pushpak1300 changed the title Add Support For Resource Template Add Support For Resource Templates Nov 24, 2025
@pushpak1300 pushpak1300 marked this pull request as ready for review November 26, 2025 14:01
Copy link
Collaborator

@joetannenbaum joetannenbaum left a comment

Choose a reason for hiding this comment

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

Looks good! Some minor changes, thanks Pushpak.

@taylorotwell
Copy link
Member

Renamed interface to HasUriTemplate. Got rid of separate console command as it feels like if anything it should just be a flag of existing make:resource command.

@taylorotwell taylorotwell merged commit 742837a into main Nov 28, 2025
2 of 20 checks passed
@taylorotwell taylorotwell deleted the add_support_for_resorce_templatees branch November 28, 2025 20:39
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.

Support resource templates

4 participants