Skip to content

Commit 50f6ec3

Browse files
committed
Add _meta to Ressource
1 parent 55f8608 commit 50f6ec3

File tree

8 files changed

+32
-16
lines changed

8 files changed

+32
-16
lines changed

src/Capability/Attribute/McpResource.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ final class McpResource
2929
* @param ?string $mimeType the MIME type, if known and constant for this resource
3030
* @param ?int $size the size in bytes, if known and constant
3131
* @param Annotations|null $annotations optional annotations describing the resource
32+
* @param ?array $_meta optional metadata
3233
*/
34+
3335
public function __construct(
3436
public string $uri,
3537
public ?string $name = null,
3638
public ?string $description = null,
3739
public ?string $mimeType = null,
3840
public ?int $size = null,
3941
public ?Annotations $annotations = null,
42+
public ?array $_meta = null
4043
) {
4144
}
4245
}

src/Capability/Discovery/Discoverer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,10 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun
234234
$mimeType = $instance->mimeType;
235235
$size = $instance->size;
236236
$annotations = $instance->annotations;
237-
$resource = new Resource($instance->uri, $name, $description, $mimeType, $annotations, $size);
237+
$_meta = $instance->_meta;
238+
$resource = new Resource($instance->uri, $name, $description, $mimeType, $annotations, $size, $_meta);
238239
$resources[$instance->uri] = new ResourceReference($resource, [$className, $methodName], false);
240+
239241
++$discoveredCount['resources'];
240242
break;
241243

src/Capability/Registry/Loader/ArrayLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ public function load(ReferenceRegistryInterface $registry): void
137137
$mimeType = $data['mimeType'];
138138
$size = $data['size'];
139139
$annotations = $data['annotations'];
140+
$_meta = $data['_meta'];
140141

141-
$resource = new Resource($uri, $name, $description, $mimeType, $annotations, $size);
142+
$resource = new Resource($uri, $name, $description, $mimeType, $annotations, $size, $_meta);
142143
$registry->registerResource($resource, $data['handler'], true);
143144

144145
$handlerDesc = $this->getHandlerDescription($data['handler']);

src/Capability/Registry/ResourceReference.php

Lines changed: 6 additions & 6 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
61+
public function formatResult(mixed $readResult, string $uri, ?string $mimeType = null, ?array $_meta = null): array
6262
{
6363
if ($readResult instanceof ResourceContents) {
6464
return [$readResult];
@@ -118,7 +118,7 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
118118
if (\is_string($readResult)) {
119119
$mimeType = $mimeType ?? $this->guessMimeTypeFromString($readResult);
120120

121-
return [new TextResourceContents($uri, $mimeType, $readResult)];
121+
return [new TextResourceContents($uri, $mimeType, $readResult, $_meta)];
122122
}
123123

124124
if (\is_resource($readResult) && 'stream' === get_resource_type($readResult)) {
@@ -142,12 +142,12 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
142142
if (\is_array($readResult) && isset($readResult['text']) && \is_string($readResult['text'])) {
143143
$mimeType = $readResult['mimeType'] ?? $mimeType ?? 'text/plain';
144144

145-
return [new TextResourceContents($uri, $mimeType, $readResult['text'])];
145+
return [new TextResourceContents($uri, $mimeType, $readResult['text'], $_meta)];
146146
}
147147

148148
if ($readResult instanceof \SplFileInfo && $readResult->isFile() && $readResult->isReadable()) {
149149
if ($mimeType && str_contains(strtolower($mimeType), 'text')) {
150-
return [new TextResourceContents($uri, $mimeType, file_get_contents($readResult->getPathname()))];
150+
return [new TextResourceContents($uri, $mimeType, file_get_contents($readResult->getPathname()), $_meta)];
151151
}
152152

153153
return [BlobResourceContents::fromSplFileInfo($uri, $readResult, $mimeType)];
@@ -159,7 +159,7 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
159159
try {
160160
$jsonString = json_encode($readResult, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
161161

162-
return [new TextResourceContents($uri, $mimeType, $jsonString)];
162+
return [new TextResourceContents($uri, $mimeType, $jsonString, $_meta)];
163163
} catch (\JsonException $e) {
164164
throw new RuntimeException(\sprintf('Failed to encode array as JSON for URI "%s": %s', $uri, $e->getMessage()));
165165
}
@@ -169,7 +169,7 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
169169
$jsonString = json_encode($readResult, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
170170
$mimeType = $mimeType ?? 'application/json';
171171

172-
return [new TextResourceContents($uri, $mimeType, $jsonString)];
172+
return [new TextResourceContents($uri, $mimeType, $jsonString, $_meta)];
173173
} catch (\JsonException $e) {
174174
throw new RuntimeException(\sprintf('Failed to encode array as JSON for URI "%s": %s', $uri, $e->getMessage()));
175175
}

src/Schema/Content/ResourceContents.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ abstract class ResourceContents implements \JsonSerializable
3030
public function __construct(
3131
public readonly string $uri,
3232
public readonly ?string $mimeType = null,
33+
public readonly ?array $_meta = null,
3334
) {
3435
}
3536

@@ -43,6 +44,10 @@ public function jsonSerialize(): array
4344
$data['mimeType'] = $this->mimeType;
4445
}
4546

47+
if (null !== $this->_meta) {
48+
$data['_meta'] = $this->_meta;
49+
}
50+
4651
return $data;
4752
}
4853
}

src/Schema/Content/TextResourceContents.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public function __construct(
3535
string $uri,
3636
?string $mimeType,
3737
public readonly string $text,
38+
?array $_meta
3839
) {
39-
parent::__construct($uri, $mimeType);
40+
parent::__construct($uri, $mimeType, $_meta);
4041
}
4142

4243
/**
@@ -51,7 +52,7 @@ public static function fromArray(array $data): self
5152
throw new InvalidArgumentException('Missing or invalid "text" for TextResourceContents.');
5253
}
5354

54-
return new self($data['uri'], $data['mimeType'] ?? null, $data['text']);
55+
return new self($data['uri'], $data['mimeType'] ?? null, $data['text'], $data['_meta'] ?? null);
5556
}
5657

5758
/**

src/Schema/Resource.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Resource implements \JsonSerializable
4949
* @param string|null $mimeType the MIME type of this resource, if known
5050
* @param Annotations|null $annotations optional annotations for the client
5151
* @param int|null $size The size of the raw resource content, in bytes (i.e., before base64 encoding or any tokenization), if known.
52+
* @param array|null $_meta optional for additional metadata
5253
*
5354
* This can be used by Hosts to display file sizes and estimate context window usage.
5455
*/
@@ -59,6 +60,7 @@ public function __construct(
5960
public readonly ?string $mimeType = null,
6061
public readonly ?Annotations $annotations = null,
6162
public readonly ?int $size = null,
63+
public readonly ?array $_meta = null
6264
) {
6365
if (!preg_match(self::RESOURCE_NAME_PATTERN, $name)) {
6466
throw new InvalidArgumentException('Invalid resource name: must contain only alphanumeric characters, underscores, and hyphens.');
@@ -86,7 +88,8 @@ public static function fromArray(array $data): self
8688
description: $data['description'] ?? null,
8789
mimeType: $data['mimeType'] ?? null,
8890
annotations: isset($data['annotations']) ? Annotations::fromArray($data['annotations']) : null,
89-
size: isset($data['size']) ? (int) $data['size'] : null
91+
size: isset($data['size']) ? (int) $data['size'] : null,
92+
_meta: isset($data['_meta']) ? (int) $data['_meta'] : null
9093
);
9194
}
9295

@@ -118,6 +121,9 @@ public function jsonSerialize(): array
118121
if (null !== $this->size) {
119122
$data['size'] = $this->size;
120123
}
124+
if (null !== $this->_meta) {
125+
$data['_meta'] = $this->_meta;
126+
}
121127

122128
return $data;
123129
}

src/Server/Handler/Request/ReadResourceHandler.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ public function handle(Request $request, SessionInterface $session): Response|Er
5151
\assert($request instanceof ReadResourceRequest);
5252

5353
$uri = $request->uri;
54-
55-
$this->logger->debug('Reading resource', ['uri' => $uri]);
56-
54+
5755
try {
5856
$reference = $this->referenceProvider->getResource($uri);
5957
if (null === $reference) {
@@ -64,7 +62,7 @@ public function handle(Request $request, SessionInterface $session): Response|Er
6462
'uri' => $uri,
6563
'_session' => $session,
6664
];
67-
65+
$this->logger->debug(var_export($reference, true));
6866
if ($reference instanceof ResourceTemplateReference) {
6967
$variables = $reference->extractVariables($uri);
7068
$arguments = array_merge($arguments, $variables);
@@ -73,7 +71,7 @@ public function handle(Request $request, SessionInterface $session): Response|Er
7371
$formatted = $reference->formatResult($result, $uri, $reference->resourceTemplate->mimeType);
7472
} else {
7573
$result = $this->referenceHandler->handle($reference, $arguments);
76-
$formatted = $reference->formatResult($result, $uri, $reference->schema->mimeType);
74+
$formatted = $reference->formatResult($result, $uri, $reference->schema->mimeType, $reference->schema->_meta);
7775
}
7876

7977
return new Response($request->getId(), new ReadResourceResult($formatted));

0 commit comments

Comments
 (0)