|
29 | 29 | - [Resource Dependency Injection](#resource-dependency-injection) |
30 | 30 | - [Conditional Resource Registration](#conditional-resource-registration) |
31 | 31 | - [Resource Responses](#resource-responses) |
| 32 | +- [Metadata](#metadata) |
32 | 33 | - [Authentication](#authentication) |
33 | 34 | - [OAuth 2.1](#oauth) |
34 | 35 | - [Sanctum](#sanctum) |
@@ -1116,6 +1117,63 @@ To indicate an error occurred during resource retrieval, use the `error()` metho |
1116 | 1117 | return Response::error('Unable to fetch weather data for the specified location.'); |
1117 | 1118 | ``` |
1118 | 1119 |
|
| 1120 | +<a name="metadata"></a> |
| 1121 | +## Metadata |
| 1122 | + |
| 1123 | +Laravel MCP also supports the `_meta` field as defined in the [MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/basic#meta), which is required by certain MCP clients or integrations. Metadata can be applied to all MCP primitives, including tools, resources, and prompts, as well as their responses. |
| 1124 | + |
| 1125 | +You can attach metadata to individual response content using the `withMeta` method: |
| 1126 | + |
| 1127 | +```php |
| 1128 | +use Laravel\Mcp\Request; |
| 1129 | +use Laravel\Mcp\Response; |
| 1130 | + |
| 1131 | +/** |
| 1132 | + * Handle the tool request. |
| 1133 | + */ |
| 1134 | +public function handle(Request $request): Response |
| 1135 | +{ |
| 1136 | + return Response::text('The weather is sunny.') |
| 1137 | + ->withMeta(['source' => 'weather-api', 'cached' => true]); |
| 1138 | +} |
| 1139 | +``` |
| 1140 | + |
| 1141 | +For result-level metadata that applies to the entire response envelope, wrap your responses with `Response::make` and call `withMeta` on the returned response factory instance: |
| 1142 | + |
| 1143 | +```php |
| 1144 | +use Laravel\Mcp\Request; |
| 1145 | +use Laravel\Mcp\Response; |
| 1146 | +use Laravel\Mcp\ResponseFactory; |
| 1147 | + |
| 1148 | +/** |
| 1149 | + * Handle the tool request. |
| 1150 | + */ |
| 1151 | +public function handle(Request $request): ResponseFactory |
| 1152 | +{ |
| 1153 | + return Response::make( |
| 1154 | + Response::text('The weather is sunny.') |
| 1155 | + )->withMeta(['request_id' => '12345']); |
| 1156 | +} |
| 1157 | +``` |
| 1158 | + |
| 1159 | +To attach metadata to a tool, resource, or prompt itself, define a `$meta` property on the class: |
| 1160 | + |
| 1161 | +```php |
| 1162 | +use Laravel\Mcp\Server\Tool; |
| 1163 | + |
| 1164 | +class CurrentWeatherTool extends Tool |
| 1165 | +{ |
| 1166 | + protected string $description = 'Fetches the current weather forecast.'; |
| 1167 | + |
| 1168 | + protected ?array $meta = [ |
| 1169 | + 'version' => '2.0', |
| 1170 | + 'author' => 'Weather Team', |
| 1171 | + ]; |
| 1172 | + |
| 1173 | + // ... |
| 1174 | +} |
| 1175 | +``` |
| 1176 | + |
1119 | 1177 | <a name="authentication"></a> |
1120 | 1178 | ## Authentication |
1121 | 1179 |
|
|
0 commit comments