Introduce schema component

This commit is contained in:
Christopher Hertel 2025-08-01 16:41:16 +02:00
commit 6025f890f9
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3

View file

@ -0,0 +1,81 @@
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Schema\Result;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\JsonRpc\Response;
use Mcp\Schema\JsonRpc\ResultInterface;
use Mcp\Schema\Prompt;
/**
* The server's response to a prompts/list request from the client.
*
* @phpstan-import-type PromptData from Prompt
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class ListPromptsResult implements ResultInterface
{
/**
* @param array<Prompt> $prompts the list of prompt definitions
* @param string|null $nextCursor An opaque token representing the pagination position after the last returned result.
*
* If present, there may be more results available.
*/
public function __construct(
public readonly array $prompts,
public readonly ?string $nextCursor = null,
) {
}
/**
* @param array{
* prompts: array<PromptData>,
* nextCursor?: string,
* } $data
*/
public static function fromArray(array $data): self
{
if (!isset($data['prompts']) || !\is_array($data['prompts'])) {
throw new InvalidArgumentException('Missing or invalid "prompts" array in ListPromptsResult data.');
}
return new self(
array_map(fn (array $prompt) => Prompt::fromArray($prompt), $data['prompts']),
$data['nextCursor'] ?? null
);
}
public static function fromResponse(Response $response): self
{
return self::fromArray($response->result);
}
/**
* @return array{
* prompts: array<Prompt>,
* nextCursor?: string,
* }
*/
public function jsonSerialize(): array
{
$result = [
'prompts' => $this->prompts,
];
if ($this->nextCursor) {
$result['nextCursor'] = $this->nextCursor;
}
return $result;
}
}