Skip to content

Commit 6b88994

Browse files
committed
Add _meta field
1 parent 86cad84 commit 6b88994

File tree

14 files changed

+65
-34
lines changed

14 files changed

+65
-34
lines changed

src/Capability/Attribute/McpPrompt.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ final class McpPrompt
2323
/**
2424
* @param ?string $name overrides the prompt name (defaults to method name)
2525
* @param ?string $description Optional description of the prompt. Defaults to method DocBlock summary.
26-
*/
26+
* @param ?array $_meta Optional metadata
27+
*/
2728
public function __construct(
2829
public ?string $name = null,
2930
public ?string $description = null,
31+
public ?array $_meta = null
3032
) {
3133
}
3234
}

src/Capability/Attribute/McpResourceTemplate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ final class McpResourceTemplate
2828
* @param ?string $description Optional description. Defaults to class DocBlock summary.
2929
* @param ?string $mimeType optional default MIME type for matching resources
3030
* @param ?Annotations $annotations optional annotations describing the resource template
31+
* @param ?array $_meta optional metadata
3132
*/
3233
public function __construct(
3334
public string $uriTemplate,
3435
public ?string $name = null,
3536
public ?string $description = null,
3637
public ?string $mimeType = null,
3738
public ?Annotations $annotations = null,
39+
public ?array $_meta = null
3840
) {
3941
}
4042
}

src/Capability/Attribute/McpTool.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class McpTool
2323
* @param string|null $name The name of the tool (defaults to the method name)
2424
* @param string|null $description The description of the tool (defaults to the DocBlock/inferred)
2525
* @param ToolAnnotations|null $annotations Optional annotations describing tool behavior
26+
* @param ?array $_meta Optional metadata
2627
*/
2728
public function __construct(
2829
public ?string $name = null,

src/Capability/Discovery/Discoverer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun
256256
$paramTag = $paramTags['$'.$param->getName()] ?? null;
257257
$arguments[] = new PromptArgument($param->getName(), $paramTag ? trim((string) $paramTag->getDescription()) : null, !$param->isOptional() && !$param->isDefaultValueAvailable());
258258
}
259-
$prompt = new Prompt($name, $description, $arguments);
259+
$_meta = $instance->_meta ?? null;
260+
$prompt = new Prompt($name, $description, $arguments, $_meta);
260261
$completionProviders = $this->getCompletionProviders($method);
261262
$prompts[$name] = new PromptReference($prompt, [$className, $methodName], false, $completionProviders);
262263
++$discoveredCount['prompts'];
@@ -268,7 +269,8 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun
268269
$description = $instance->description ?? $this->docBlockParser->getSummary($docBlock) ?? null;
269270
$mimeType = $instance->mimeType;
270271
$annotations = $instance->annotations;
271-
$resourceTemplate = new ResourceTemplate($instance->uriTemplate, $name, $description, $mimeType, $annotations);
272+
$_meta = $instance->_meta ?? null;
273+
$resourceTemplate = new ResourceTemplate($instance->uriTemplate, $name, $description, $mimeType, $annotations, $_meta);
272274
$completionProviders = $this->getCompletionProviders($method);
273275
$resourceTemplates[$instance->uriTemplate] = new ResourceTemplateReference($resourceTemplate, [$className, $methodName], false, $completionProviders);
274276
++$discoveredCount['resourceTemplates'];

src/Capability/Registry/Loader/ArrayLoader.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ public function load(ReferenceRegistryInterface $registry): void
173173
$uriTemplate = $data['uriTemplate'];
174174
$mimeType = $data['mimeType'];
175175
$annotations = $data['annotations'];
176+
$_meta = $data['_meta'];
176177

177-
$template = new ResourceTemplate($uriTemplate, $name, $description, $mimeType, $annotations);
178+
$template = new ResourceTemplate($uriTemplate, $name, $description, $mimeType, $annotations, $_meta);
178179
$completionProviders = $this->getCompletionProviders($reflection);
179180
$registry->registerResourceTemplate($template, $data['handler'], $completionProviders, true);
180181

@@ -225,8 +226,8 @@ public function load(ReferenceRegistryInterface $registry): void
225226
!$param->isOptional() && !$param->isDefaultValueAvailable(),
226227
);
227228
}
228-
229-
$prompt = new Prompt($name, $description, $arguments);
229+
$_meta = $data['_meta'];
230+
$prompt = new Prompt($name, $description, $arguments, $_meta);
230231
$completionProviders = $this->getCompletionProviders($reflection);
231232
$registry->registerPrompt($prompt, $data['handler'], $completionProviders, true);
232233

src/Capability/Registry/ResourceReference.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(
5858
* - array: Converted to JSON if MIME type is application/json or contains 'json'
5959
* For other MIME types, will try to convert to JSON with a warning
6060
*/
61-
public function formatResult(mixed $readResult, string $uri, ?string $mimeType = null, ?array $_meta = null): array
61+
public function formatResult(mixed $readResult, string $uri, ?string $mimeType = null): array
6262
{
6363
if ($readResult instanceof ResourceContents) {
6464
return [$readResult];
@@ -68,9 +68,11 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
6868
return [$readResult->resource];
6969
}
7070

71+
$_meta = $this->schema->_meta;
72+
7173
if (\is_array($readResult)) {
7274
if (empty($readResult)) {
73-
return [new TextResourceContents($uri, 'application/json', '[]')];
75+
return [new TextResourceContents($uri, 'application/json', '[]', $_meta)];
7476
}
7577

7678
$allAreResourceContents = true;
@@ -125,7 +127,8 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
125127
$result = BlobResourceContents::fromStream(
126128
$uri,
127129
$readResult,
128-
$mimeType ?? 'application/octet-stream'
130+
$mimeType ?? 'application/octet-stream',
131+
$_meta
129132
);
130133

131134
@fclose($readResult);
@@ -136,7 +139,7 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
136139
if (\is_array($readResult) && isset($readResult['blob']) && \is_string($readResult['blob'])) {
137140
$mimeType = $readResult['mimeType'] ?? $mimeType ?? 'application/octet-stream';
138141

139-
return [new BlobResourceContents($uri, $mimeType, $readResult['blob'])];
142+
return [new BlobResourceContents($uri, $mimeType, $readResult['blob'], $_meta)];
140143
}
141144

142145
if (\is_array($readResult) && isset($readResult['text']) && \is_string($readResult['text'])) {
@@ -150,7 +153,7 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
150153
return [new TextResourceContents($uri, $mimeType, file_get_contents($readResult->getPathname()), $_meta)];
151154
}
152155

153-
return [BlobResourceContents::fromSplFileInfo($uri, $readResult, $mimeType)];
156+
return [BlobResourceContents::fromSplFileInfo($uri, $readResult, $mimeType, $_meta)];
154157
}
155158

156159
if (\is_array($readResult)) {

src/Capability/Registry/ResourceTemplateReference.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
101101
return [$readResult->resource];
102102
}
103103

104+
$_meta = $this->resourceTemplate->_meta;
105+
104106
if (\is_array($readResult)) {
105107
if (empty($readResult)) {
106-
return [new TextResourceContents($uri, 'application/json', '[]')];
108+
return [new TextResourceContents($uri, 'application/json', '[]', $_meta)];
107109
}
108110

109111
$allAreResourceContents = true;
@@ -151,14 +153,15 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
151153
if (\is_string($readResult)) {
152154
$mimeType = $mimeType ?? $this->guessMimeTypeFromString($readResult);
153155

154-
return [new TextResourceContents($uri, $mimeType, $readResult)];
156+
return [new TextResourceContents($uri, $mimeType, $readResult, $_meta)];
155157
}
156158

157159
if (\is_resource($readResult) && 'stream' === get_resource_type($readResult)) {
158160
$result = BlobResourceContents::fromStream(
159161
$uri,
160162
$readResult,
161-
$mimeType ?? 'application/octet-stream'
163+
$mimeType ?? 'application/octet-stream',
164+
$_meta
162165
);
163166

164167
@fclose($readResult);
@@ -169,21 +172,21 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
169172
if (\is_array($readResult) && isset($readResult['blob']) && \is_string($readResult['blob'])) {
170173
$mimeType = $readResult['mimeType'] ?? $mimeType ?? 'application/octet-stream';
171174

172-
return [new BlobResourceContents($uri, $mimeType, $readResult['blob'])];
175+
return [new BlobResourceContents($uri, $mimeType, $readResult['blob'], $_meta)];
173176
}
174177

175178
if (\is_array($readResult) && isset($readResult['text']) && \is_string($readResult['text'])) {
176179
$mimeType = $readResult['mimeType'] ?? $mimeType ?? 'text/plain';
177180

178-
return [new TextResourceContents($uri, $mimeType, $readResult['text'])];
181+
return [new TextResourceContents($uri, $mimeType, $readResult['text'], $_meta)];
179182
}
180183

181184
if ($readResult instanceof \SplFileInfo && $readResult->isFile() && $readResult->isReadable()) {
182185
if ($mimeType && str_contains(strtolower($mimeType), 'text')) {
183-
return [new TextResourceContents($uri, $mimeType, file_get_contents($readResult->getPathname()))];
186+
return [new TextResourceContents($uri, $mimeType, file_get_contents($readResult->getPathname()), $_meta)];
184187
}
185188

186-
return [BlobResourceContents::fromSplFileInfo($uri, $readResult, $mimeType)];
189+
return [BlobResourceContents::fromSplFileInfo($uri, $readResult, $mimeType, $_meta)];
187190
}
188191

189192
if (\is_array($readResult)) {
@@ -192,7 +195,7 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
192195
try {
193196
$jsonString = json_encode($readResult, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
194197

195-
return [new TextResourceContents($uri, $mimeType, $jsonString)];
198+
return [new TextResourceContents($uri, $mimeType, $jsonString, $_meta)];
196199
} catch (\JsonException $e) {
197200
throw new RuntimeException("Failed to encode array as JSON for URI '{$uri}': {$e->getMessage()}");
198201
}
@@ -202,7 +205,7 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
202205
$jsonString = json_encode($readResult, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
203206
$mimeType = $mimeType ?? 'application/json';
204207

205-
return [new TextResourceContents($uri, $mimeType, $jsonString)];
208+
return [new TextResourceContents($uri, $mimeType, $jsonString, $_meta)];
206209
} catch (\JsonException $e) {
207210
throw new RuntimeException("Failed to encode array as JSON for URI '{$uri}': {$e->getMessage()}");
208211
}

src/Schema/Content/BlobResourceContents.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ class BlobResourceContents extends ResourceContents
3030
* @param string $uri the URI of the resource or sub-resource
3131
* @param string|null $mimeType the MIME type of the resource or sub-resource
3232
* @param string $blob a base64-encoded string representing the binary data of the item
33+
* @param ?array $_meta Optional metadata
3334
*/
3435
public function __construct(
3536
string $uri,
3637
?string $mimeType,
3738
public readonly string $blob,
39+
?array $_meta
3840
) {
39-
parent::__construct($uri, $mimeType);
41+
parent::__construct($uri, $mimeType, $_meta);
4042
}
4143

4244
/**
@@ -51,25 +53,25 @@ public static function fromArray(array $data): self
5153
throw new InvalidArgumentException('Missing or invalid "blob" for BlobResourceContents.');
5254
}
5355

54-
return new self($data['uri'], $data['mimeType'] ?? null, $data['blob']);
56+
return new self($data['uri'], $data['mimeType'] ?? null, $data['blob'], $data['_meta'] ?? null);
5557
}
5658

5759
/**
5860
* @param resource $stream
5961
*/
60-
public static function fromStream(string $uri, $stream, string $mimeType): self
62+
public static function fromStream(string $uri, $stream, string $mimeType, ?array $_meta = null): self
6163
{
6264
$blob = stream_get_contents($stream);
6365

64-
return new self($uri, $mimeType, base64_encode($blob));
66+
return new self($uri, $mimeType, base64_encode($blob), $_meta);
6567
}
6668

67-
public static function fromSplFileInfo(string $uri, \SplFileInfo $file, ?string $explicitMimeType = null): self
69+
public static function fromSplFileInfo(string $uri, \SplFileInfo $file, ?string $explicitMimeType = null, ?array $_meta = null): self
6870
{
6971
$mimeType = $explicitMimeType ?? mime_content_type($file->getPathname());
7072
$blob = file_get_contents($file->getPathname());
7173

72-
return new self($uri, $mimeType, base64_encode($blob));
74+
return new self($uri, $mimeType, base64_encode($blob), $_meta);
7375
}
7476

7577
/**

src/Schema/Content/ResourceContents.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ abstract class ResourceContents implements \JsonSerializable
2626
/**
2727
* @param string $uri the URI of the resource or sub-resource
2828
* @param string|null $mimeType the MIME type of the resource or sub-resource
29-
*/
29+
* @param ?array $_meta Optional metadata
30+
*/
3031
public function __construct(
3132
public readonly string $uri,
3233
public readonly ?string $mimeType = null,

src/Schema/Content/TextResourceContents.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class TextResourceContents extends ResourceContents
3030
* @param string $uri the URI of the resource or sub-resource
3131
* @param string|null $mimeType the MIME type of the resource or sub-resource
3232
* @param string $text The text of the item. This must only be set if the item can actually be represented as text (not binary data).
33-
*/
33+
* @param ?array $_meta Optional metadata
34+
*/
3435
public function __construct(
3536
string $uri,
3637
?string $mimeType,

0 commit comments

Comments
 (0)