Roll out meta properties in attributes and more schema classes (#129)

This commit is contained in:
Kaipi Yann 2025-11-08 13:58:26 +01:00 • committed by GitHub
commit d3a07912b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 178 additions and 88 deletions

View file

@ -19,7 +19,8 @@ use Mcp\Exception\InvalidArgumentException;
* @phpstan-type BlobResourceContentsData array{
* uri: string,
* mimeType?: string|null,
* blob: string
* blob: string,
* _meta?: array<string, mixed>
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
@ -27,16 +28,18 @@ use Mcp\Exception\InvalidArgumentException;
class BlobResourceContents extends ResourceContents
{
/**
* @param string $uri the URI of the resource or sub-resource
* @param string|null $mimeType the MIME type of the resource or sub-resource
* @param string $blob a base64-encoded string representing the binary data of the item
* @param string $uri the URI of the resource or sub-resource
* @param string|null $mimeType the MIME type of the resource or sub-resource
* @param string $blob a base64-encoded string representing the binary data of the item
* @param ?array<string, mixed> $meta Optional metadata
*/
public function __construct(
string $uri,
?string $mimeType,
public readonly string $blob,
?array $meta = null,
) {
parent::__construct($uri, $mimeType);
parent::__construct($uri, $mimeType, $meta);
}
/**
@ -51,25 +54,29 @@ class BlobResourceContents extends ResourceContents
throw new InvalidArgumentException('Missing or invalid "blob" for BlobResourceContents.');
}
return new self($data['uri'], $data['mimeType'] ?? null, $data['blob']);
return new self($data['uri'], $data['mimeType'] ?? null, $data['blob'], $data['_meta'] ?? null);
}
/**
* @param resource $stream
*/
public static function fromStream(string $uri, $stream, string $mimeType): self
* @param resource $stream
* @param ?array<string, mixed> $meta Optional metadata
* */
public static function fromStream(string $uri, $stream, string $mimeType, ?array $meta = null): self
{
$blob = stream_get_contents($stream);
return new self($uri, $mimeType, base64_encode($blob));
return new self($uri, $mimeType, base64_encode($blob), $meta);
}
public static function fromSplFileInfo(string $uri, \SplFileInfo $file, ?string $explicitMimeType = null): self
/**
* @param ?array<string, mixed> $meta Optional metadata
* */
public static function fromSplFileInfo(string $uri, \SplFileInfo $file, ?string $explicitMimeType = null, ?array $meta = null): self
{
$mimeType = $explicitMimeType ?? mime_content_type($file->getPathname());
$blob = file_get_contents($file->getPathname());
return new self($uri, $mimeType, base64_encode($blob));
return new self($uri, $mimeType, base64_encode($blob), $meta);
}
/**
@ -79,7 +86,7 @@ class BlobResourceContents extends ResourceContents
{
return [
'blob' => $this->blob,
...$this->jsonSerialize(),
...parent::jsonSerialize(),
];
}
}

View file

@ -16,7 +16,8 @@ namespace Mcp\Schema\Content;
*
* @phpstan-type ResourceContentsData = array{
* uri: string,
* mimeType?: string|null
* mimeType?: string|null,
* _meta?: array<string, mixed>
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
@ -24,12 +25,14 @@ namespace Mcp\Schema\Content;
abstract class ResourceContents implements \JsonSerializable
{
/**
* @param string $uri the URI of the resource or sub-resource
* @param string|null $mimeType the MIME type of the resource or sub-resource
* @param string $uri the URI of the resource or sub-resource
* @param string|null $mimeType the MIME type of the resource or sub-resource
* @param ?array<string, mixed> $meta Optional metadata
*/
public function __construct(
public readonly string $uri,
public readonly ?string $mimeType = null,
public readonly ?array $meta = null,
) {
}
@ -43,6 +46,10 @@ abstract class ResourceContents implements \JsonSerializable
$data['mimeType'] = $this->mimeType;
}
if (null !== $this->meta) {
$data['_meta'] = $this->meta;
}
return $data;
}
}

View file

@ -19,7 +19,8 @@ use Mcp\Exception\InvalidArgumentException;
* @phpstan-type TextResourceContentsData array{
* uri: string,
* mimeType?: string|null,
* text: string
* text: string,
* _meta?: array<string, mixed>
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
@ -27,16 +28,18 @@ use Mcp\Exception\InvalidArgumentException;
class TextResourceContents extends ResourceContents
{
/**
* @param string $uri the URI of the resource or sub-resource
* @param string|null $mimeType the MIME type of the resource or sub-resource
* @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).
* @param string $uri the URI of the resource or sub-resource
* @param string|null $mimeType the MIME type of the resource or sub-resource
* @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).
* @param ?array<string, mixed> $meta Optional metadata
*/
public function __construct(
string $uri,
?string $mimeType,
public readonly string $text,
?array $meta = null,
) {
parent::__construct($uri, $mimeType);
parent::__construct($uri, $mimeType, $meta);
}
/**
@ -51,7 +54,7 @@ class TextResourceContents extends ResourceContents
throw new InvalidArgumentException('Missing or invalid "text" for TextResourceContents.');
}
return new self($data['uri'], $data['mimeType'] ?? null, $data['text']);
return new self($data['uri'], $data['mimeType'] ?? null, $data['text'], $data['_meta'] ?? null);
}
/**