Adopt server examples

This commit is contained in:
Christopher Hertel 2025-08-17 02:46:01 +02:00
commit 2ef051a355
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3
14 changed files with 3639 additions and 756 deletions

View file

@ -23,6 +23,7 @@ class Implementation implements \JsonSerializable
public function __construct(
public readonly string $name = 'app',
public readonly string $version = 'dev',
public readonly ?string $description = null,
) {
}
@ -41,7 +42,7 @@ class Implementation implements \JsonSerializable
throw new InvalidArgumentException('Invalid or missing "version" in Implementation data.');
}
return new self($data['name'], $data['version']);
return new self($data['name'], $data['version'], $data['description'] ?? null);
}
/**
@ -52,9 +53,15 @@ class Implementation implements \JsonSerializable
*/
public function jsonSerialize(): array
{
return [
$data = [
'name' => $this->name,
'version' => $this->version,
];
if (null !== $this->description) {
$data['description'] = $this->description;
}
return $data;
}
}

View file

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* This file is part of the official PHP MCP SDK.
*

View file

@ -19,4 +19,5 @@ namespace Mcp\Schema\JsonRpc;
interface MessageInterface extends \JsonSerializable
{
public const JSONRPC_VERSION = '2.0';
public const PROTOCOL_VERSION = '2025-06-18';
}

View file

@ -27,7 +27,7 @@ abstract class Notification implements HasMethodInterface, MessageInterface
/**
* @var array<string, mixed>|null
*/
protected ?array $meta;
protected ?array $meta = null;
abstract public static function getMethod(): string;

View file

@ -29,7 +29,7 @@ abstract class Request implements HasMethodInterface, MessageInterface
/**
* @var array<string, mixed>|null
*/
protected ?array $meta;
protected ?array $meta = null;
abstract public static function getMethod(): string;

View file

@ -11,6 +11,7 @@
namespace Mcp\Schema\Request;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Content\SamplingMessage;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Schema\ModelPreferences;
@ -61,11 +62,11 @@ class CreateSamplingMessageRequest extends Request
protected static function fromParams(?array $params): Request
{
if (!isset($params['messages']) || !\is_array($params['messages'])) {
throw new \InvalidArgumentException('Missing or invalid "messages" parameter for sampling/createMessage.');
throw new InvalidArgumentException('Missing or invalid "messages" parameter for sampling/createMessage.');
}
if (!isset($params['maxTokens']) || !\is_int($params['maxTokens'])) {
throw new \InvalidArgumentException('Missing or invalid "maxTokens" parameter for sampling/createMessage.');
throw new InvalidArgumentException('Missing or invalid "maxTokens" parameter for sampling/createMessage.');
}
$preferences = null;

View file

@ -35,8 +35,8 @@ class EmptyResult implements ResultInterface
/**
* @return array{}
*/
public function jsonSerialize(): array
public function jsonSerialize(): object
{
return [];
return new \stdClass();
}
}

View file

@ -82,7 +82,7 @@ class InitializeResult implements ResultInterface
public function jsonSerialize(): array
{
$data = [
'protocolVersion' => MessageInterface::JSONRPC_VERSION,
'protocolVersion' => MessageInterface::PROTOCOL_VERSION,
'capabilities' => $this->capabilities,
'serverInfo' => $this->serverInfo,
];

View file

@ -64,7 +64,7 @@ class ListPromptsResult implements ResultInterface
public function jsonSerialize(): array
{
$result = [
'prompts' => $this->prompts,
'prompts' => array_values($this->prompts),
];
if ($this->nextCursor) {

View file

@ -64,7 +64,7 @@ class ListResourceTemplatesResult implements ResultInterface
public function jsonSerialize(): array
{
$result = [
'resourceTemplates' => $this->resourceTemplates,
'resourceTemplates' => array_values($this->resourceTemplates),
];
if ($this->nextCursor) {

View file

@ -64,7 +64,7 @@ class ListResourcesResult implements ResultInterface
public function jsonSerialize(): array
{
$result = [
'resources' => $this->resources,
'resources' => array_values($this->resources),
];
if (null !== $this->nextCursor) {

View file

@ -42,7 +42,7 @@ class ListRootsResult implements ResultInterface
public function jsonSerialize(): array
{
$result = [
'roots' => $this->roots,
'roots' => array_values($this->roots),
];
if (null !== $this->_meta) {

View file

@ -64,7 +64,7 @@ class ListToolsResult implements ResultInterface
public function jsonSerialize(): array
{
$result = [
'tools' => $this->tools,
'tools' => array_values($this->tools),
];
if ($this->nextCursor) {

View file

@ -13,8 +13,8 @@ namespace Mcp\Schema\Result;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Content\BlobResourceContents;
use Mcp\Schema\Content\ResourceContents;
use Mcp\Schema\Content\TextResourceContents;
use Mcp\Schema\JsonRpc\Response;
use Mcp\Schema\JsonRpc\ResultInterface;
/**
@ -30,7 +30,7 @@ class ReadResourceResult implements ResultInterface
/**
* Create a new ReadResourceResult.
*
* @param array<TextResourceContents|BlobResourceContents> $contents The contents of the resource
* @param ResourceContents[] $contents The contents of the resource
*/
public function __construct(
public readonly array $contents,