php-sdk/src/Schema/JsonRpc/Response.php
Kyrian Obikwelu f1b471a219
[Server] Add Bidirectional Client Communication Support (#109)
* feat(server): add bidirectional client communication support

* fix(server): use correct session ID string conversion method

* fix: clean up docblocks for request handler methods in Builder

* fix: merge artifacts
2025-10-26 22:48:17 +01:00

86 lines
2.3 KiB
PHP

<?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;
/**
* @template TResult
*
* @phpstan-type ResponseData array{
* jsonrpc: string,
* id: string|int,
* result: array<string, mixed>,
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
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 TResult $result the value of this member is determined by the method invoked on the Server
*/
public function __construct(
public readonly string|int $id,
/** @var TResult */
public readonly mixed $result,
) {
}
public function getId(): string|int
{
return $this->id;
}
/**
* @param ResponseData $data
*
* @return self<array<string, mixed>>
*/
public static function fromArray(array $data): self
{
if (($data['jsonrpc'] ?? null) !== MessageInterface::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.');
}
if (!\is_array($data['result'])) {
throw new InvalidArgumentException('Response "result" must be an array.');
}
return new self($data['id'], $data['result']);
}
/**
* @return array{
* jsonrpc: string,
* id: string|int,
* result: mixed,
* }
*/
public function jsonSerialize(): array
{
return [
'jsonrpc' => MessageInterface::JSONRPC_VERSION,
'id' => $this->id,
'result' => $this->result,
];
}
}