Skip to content

Commit 4a03926

Browse files
[12.x] Add Resource Templates section to documentation (#10938)
* Add Resource Templates section to MCP documentation * Update mcp.md --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent db1e0d5 commit 4a03926

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

mcp.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [Prompt Responses](#prompt-responses)
2626
- [Resources](#resources)
2727
- [Creating Resources](#creating-resources)
28+
- [Resource Templates](#resource-templates)
2829
- [Resource URI and MIME Type](#resource-uri-and-mime-type)
2930
- [Resource Request](#resource-request)
3031
- [Resource Dependency Injection](#resource-dependency-injection)
@@ -985,6 +986,115 @@ class WeatherGuidelinesResource extends Resource
985986
> [!NOTE]
986987
> The description is a critical part of the resource's metadata, as it helps AI models understand when and how to use the resource effectively.
987988
989+
<a name="resource-templates"></a>
990+
### Resource Templates
991+
992+
[Resource templates](https://modelcontextprotocol.io/specification/2025-06-18/server/resources#resource-templates) enable your server to expose dynamic resources that match URI patterns with variables. Instead of defining a static URI for each resource, you can create a single resource that handles multiple URIs based on a template pattern.
993+
994+
<a name="creating-resource-templates"></a>
995+
#### Creating Resource Templates
996+
997+
To create a resource template, implement the `HasUriTemplate` interface on your resource class and define a `uriTemplate` method that returns a `UriTemplate` instance:
998+
999+
```php
1000+
<?php
1001+
1002+
namespace App\Mcp\Resources;
1003+
1004+
use Laravel\Mcp\Request;
1005+
use Laravel\Mcp\Response;
1006+
use Laravel\Mcp\Server\Contracts\HasUriTemplate;
1007+
use Laravel\Mcp\Server\Resource;
1008+
use Laravel\Mcp\Support\UriTemplate;
1009+
1010+
class UserFileResource extends Resource implements HasUriTemplate
1011+
{
1012+
/**
1013+
* The resource's description.
1014+
*/
1015+
protected string $description = 'Access user files by ID';
1016+
1017+
/**
1018+
* The resource's MIME type.
1019+
*/
1020+
protected string $mimeType = 'text/plain';
1021+
1022+
/**
1023+
* Get the URI template for this resource.
1024+
*/
1025+
public function uriTemplate(): UriTemplate
1026+
{
1027+
return new UriTemplate('file://users/{userId}/files/{fileId}');
1028+
}
1029+
1030+
/**
1031+
* Handle the resource request.
1032+
*/
1033+
public function handle(Request $request): Response
1034+
{
1035+
$userId = $request->get('userId');
1036+
$fileId = $request->get('fileId');
1037+
1038+
// Fetch and return the file content...
1039+
1040+
return Response::text($content);
1041+
}
1042+
}
1043+
```
1044+
1045+
When a resource implements the `HasUriTemplate` interface, it will be registered as a resource template rather than a static resource. AI clients can then request resources using URIs that match the template pattern, and the variables from the URI will be automatically extracted and made available in your resource's `handle` method.
1046+
1047+
<a name="uri-template-syntax"></a>
1048+
#### URI Template Syntax
1049+
1050+
URI templates use placeholders enclosed in curly braces to define variable segments in the URI:
1051+
1052+
```php
1053+
new UriTemplate('file://users/{userId}');
1054+
new UriTemplate('file://users/{userId}/files/{fileId}');
1055+
new UriTemplate('https://api.example.com/{version}/{resource}/{id}');
1056+
```
1057+
1058+
<a name="accessing-template-variables"></a>
1059+
#### Accessing Template Variables
1060+
1061+
When a URI matches your resource template, the extracted variables are automatically merged into the request and can be accessed using the `get` method:
1062+
1063+
```php
1064+
<?php
1065+
1066+
namespace App\Mcp\Resources;
1067+
1068+
use Laravel\Mcp\Request;
1069+
use Laravel\Mcp\Response;
1070+
use Laravel\Mcp\Server\Contracts\HasUriTemplate;
1071+
use Laravel\Mcp\Server\Resource;
1072+
use Laravel\Mcp\Support\UriTemplate;
1073+
1074+
class UserProfileResource extends Resource implements HasUriTemplate
1075+
{
1076+
public function uriTemplate(): UriTemplate
1077+
{
1078+
return new UriTemplate('file://users/{userId}/profile');
1079+
}
1080+
1081+
public function handle(Request $request): Response
1082+
{
1083+
// Access the extracted variable
1084+
$userId = $request->get('userId');
1085+
1086+
// Access the full URI if needed
1087+
$uri = $request->uri();
1088+
1089+
// Fetch user profile...
1090+
1091+
return Response::text("Profile for user {$userId}");
1092+
}
1093+
}
1094+
```
1095+
1096+
The `Request` object provides both the extracted variables and the original URI that was requested, giving you full context for processing the resource request.
1097+
9881098
<a name="resource-uri-and-mime-type"></a>
9891099
### Resource URI and MIME Type
9901100

0 commit comments

Comments
 (0)