Introduce schema component
This commit is contained in:
parent
ee0dffe369
commit
6025f890f9
1 changed files with 5419 additions and 0 deletions
79
src/Schema/JsonRpc/Response.php
Normal file
79
src/Schema/JsonRpc/Response.php
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?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\JsonRpc;
|
||||
|
||||
use Mcp\Exception\InvalidArgumentException;
|
||||
use Mcp\Schema\Constants;
|
||||
|
||||
/**
|
||||
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
|
||||
*
|
||||
* @phpstan-type ResponseData array{
|
||||
* jsonrpc: string,
|
||||
* id: string|int,
|
||||
* result: array<string, mixed>,
|
||||
* }
|
||||
*/
|
||||
class Response implements MessageInterface
|
||||
{
|
||||
/**
|
||||
* @param string|int $id this MUST be the same as the value of the id member in the Request Object
|
||||
* @param ResultInterface|array<string, mixed> $result the value of this member is determined by the method invoked on the Server
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly string|int $id,
|
||||
public readonly ResultInterface|array $result,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getId(): string|int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseData $data
|
||||
*/
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
if (($data['jsonrpc'] ?? null) !== Constants::JSONRPC_VERSION) {
|
||||
throw new InvalidArgumentException('Invalid or missing "jsonrpc" version for Response.');
|
||||
}
|
||||
if (!isset($data['id'])) {
|
||||
throw new InvalidArgumentException('Missing "id" for Response.');
|
||||
}
|
||||
if (!\is_string($data['id']) && !\is_int($data['id'])) {
|
||||
throw new InvalidArgumentException('Invalid "id" type for Response.');
|
||||
}
|
||||
if (!isset($data['result'])) {
|
||||
throw new InvalidArgumentException('Response must contain "result" field.');
|
||||
}
|
||||
|
||||
return new self($data['id'], $data['result']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* jsonrpc: string,
|
||||
* id: string|int,
|
||||
* result: ResultInterface,
|
||||
* }
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'jsonrpc' => Constants::JSONRPC_VERSION,
|
||||
'id' => $this->id,
|
||||
'result' => $this->result,
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue