Adopt Schema component in server handlers

This commit is contained in:
Christopher Hertel 2025-08-03 22:06:46 +02:00
commit 09707b4798
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3
110 changed files with 1516 additions and 2424 deletions

View file

@ -28,7 +28,8 @@
"phpunit/phpunit": "^10.5",
"symfony/console": "^6.4 || ^7.0",
"psr/cache": "^3.0",
"php-cs-fixer/shim": "^3.84"
"php-cs-fixer/shim": "^3.84",
"nyholm/nsa": "^1.3"
},
"suggest": {
"symfony/console": "To use SymfonyConsoleTransport for STDIO",

View file

@ -22,10 +22,9 @@ $output = new SymfonyConsole\Output\ConsoleOutput($debug ? OutputInterface::VERB
$logger = new SymfonyConsole\Logger\ConsoleLogger($output);
// Configure the JsonRpcHandler and build the functionality
$jsonRpcHandler = new Mcp\Server\JsonRpcHandler(
new Mcp\Message\Factory(),
App\Builder::buildRequestHandlers(),
App\Builder::buildNotificationHandlers(),
$jsonRpcHandler = new Mcp\JsonRpc\Handler(
Mcp\JsonRpc\MessageFactory::make(),
App\Builder::buildMethodHandlers(),
$logger
);

View file

@ -14,17 +14,9 @@ namespace App;
use Mcp\Capability\PromptChain;
use Mcp\Capability\ResourceChain;
use Mcp\Capability\ToolChain;
use Mcp\Server\NotificationHandler\InitializedHandler;
use Mcp\Server\NotificationHandlerInterface;
use Mcp\Server\RequestHandler\InitializeHandler;
use Mcp\Server\RequestHandler\PingHandler;
use Mcp\Server\RequestHandler\PromptGetHandler;
use Mcp\Server\RequestHandler\PromptListHandler;
use Mcp\Server\RequestHandler\ResourceListHandler;
use Mcp\Server\RequestHandler\ResourceReadHandler;
use Mcp\Server\RequestHandler\ToolCallHandler;
use Mcp\Server\RequestHandler\ToolListHandler;
use Mcp\Server\RequestHandlerInterface;
use Mcp\Server\MethodHandlerInterface;
use Mcp\Server\NotificationHandler;
use Mcp\Server\RequestHandler;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -32,9 +24,9 @@ use Mcp\Server\RequestHandlerInterface;
class Builder
{
/**
* @return list<RequestHandlerInterface>
* @return list<MethodHandlerInterface>
*/
public static function buildRequestHandlers(): array
public static function buildMethodHandlers(): array
{
$promptManager = new PromptChain([
new ExamplePrompt(),
@ -49,24 +41,15 @@ class Builder
]);
return [
new InitializeHandler(),
new PingHandler(),
new PromptListHandler($promptManager),
new PromptGetHandler($promptManager),
new ResourceListHandler($resourceManager),
new ResourceReadHandler($resourceManager),
new ToolCallHandler($toolManager),
new ToolListHandler($toolManager),
];
}
/**
* @return list<NotificationHandlerInterface>
*/
public static function buildNotificationHandlers(): array
{
return [
new InitializedHandler(),
new NotificationHandler\InitializedHandler(),
new RequestHandler\InitializeHandler(),
new RequestHandler\PingHandler(),
new RequestHandler\ListPromptsHandler($promptManager),
new RequestHandler\GetPromptHandler($promptManager),
new RequestHandler\ListResourcesHandler($resourceManager),
new RequestHandler\ReadResourceHandler($resourceManager),
new RequestHandler\CallToolHandler($toolManager),
new RequestHandler\ListToolsHandler($toolManager),
];
}
}

View file

@ -12,26 +12,28 @@
namespace App;
use Mcp\Capability\Prompt\MetadataInterface;
use Mcp\Capability\Prompt\PromptGet;
use Mcp\Capability\Prompt\PromptGetResult;
use Mcp\Capability\Prompt\PromptGetResultMessages;
use Mcp\Capability\Prompt\PromptGetterInterface;
use Mcp\Schema\Content\PromptMessage;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Enum\Role;
use Mcp\Schema\Request\GetPromptRequest;
use Mcp\Schema\Result\GetPromptResult;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ExamplePrompt implements MetadataInterface, PromptGetterInterface
{
public function get(PromptGet $input): PromptGetResult
public function get(GetPromptRequest $request): GetPromptResult
{
$firstName = $input->arguments['first name'] ?? null;
$firstName = $request->arguments['firstName'] ?? null;
return new PromptGetResult(
return new GetPromptResult(
[new PromptMessage(
Role::User,
new TextContent(\sprintf('Hello %s', $firstName ?? 'World')),
)],
$this->getDescription(),
[new PromptGetResultMessages(
'user',
\sprintf('Hello %s', $firstName ?? 'World')
)]
);
}

View file

@ -12,21 +12,21 @@
namespace App;
use Mcp\Capability\Resource\MetadataInterface;
use Mcp\Capability\Resource\ResourceRead;
use Mcp\Capability\Resource\ResourceReaderInterface;
use Mcp\Capability\Resource\ResourceReadResult;
use Mcp\Schema\Content\TextResourceContents;
use Mcp\Schema\Request\ReadResourceRequest;
use Mcp\Schema\Result\ReadResourceResult;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ExampleResource implements MetadataInterface, ResourceReaderInterface
{
public function read(ResourceRead $input): ResourceReadResult
public function read(ReadResourceRequest $request): ReadResourceResult
{
return new ResourceReadResult(
'Content of '.$this->getName(),
$this->getUri(),
);
return new ReadResourceResult([
new TextResourceContents($this->getUri(), null, 'Content of My Resource'),
]);
}
public function getUri(): string
@ -36,7 +36,7 @@ class ExampleResource implements MetadataInterface, ResourceReaderInterface
public function getName(): string
{
return 'My resource';
return 'my-resource';
}
public function getDescription(): ?string

View file

@ -12,22 +12,25 @@
namespace App;
use Mcp\Capability\Tool\MetadataInterface;
use Mcp\Capability\Tool\ToolCall;
use Mcp\Capability\Tool\ToolCallResult;
use Mcp\Capability\Tool\ToolExecutorInterface;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Request\CallToolRequest;
use Mcp\Schema\Result\CallToolResult;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ExampleTool implements MetadataInterface, ToolExecutorInterface
{
public function call(ToolCall $input): ToolCallResult
public function call(CallToolRequest $request): CallToolResult
{
$format = $input->arguments['format'] ?? 'Y-m-d H:i:s';
$format = $request->arguments['format'] ?? 'Y-m-d H:i:s';
return new ToolCallResult(
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format)
);
return new CallToolResult([
new TextContent(
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format),
),
]);
}
public function getName(): string

View file

@ -7,3 +7,6 @@ parameters:
excludePaths:
- examples/cli/vendor/* (?)
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
message: "#^Method .*::test.*\\(\\) has no return type specified\\.$#"

View file

@ -1,28 +0,0 @@
<?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\Capability\Prompt;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class PromptGet
{
/**
* @param array<string, mixed> $arguments
*/
public function __construct(
public readonly string $id,
public readonly string $name,
public readonly array $arguments = [],
) {
}
}

View file

@ -1,27 +0,0 @@
<?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\Capability\Prompt;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class PromptGetResult
{
/**
* @param list<PromptGetResultMessages> $messages
*/
public function __construct(
public readonly string $description,
public readonly array $messages = [],
) {
}
}

View file

@ -1,30 +0,0 @@
<?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\Capability\Prompt;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class PromptGetResultMessages
{
public function __construct(
public readonly string $role,
public readonly string $result,
/**
* @var "text"|"image"|"audio"|"resource"|non-empty-string
*/
public readonly string $type = 'text',
public readonly string $mimeType = 'text/plan',
public readonly ?string $uri = null,
) {
}
}

View file

@ -13,6 +13,8 @@ namespace Mcp\Capability\Prompt;
use Mcp\Exception\PromptGetException;
use Mcp\Exception\PromptNotFoundException;
use Mcp\Schema\Request\GetPromptRequest;
use Mcp\Schema\Result\GetPromptResult;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -23,5 +25,5 @@ interface PromptGetterInterface
* @throws PromptGetException if the prompt execution fails
* @throws PromptNotFoundException if the prompt is not found
*/
public function get(PromptGet $input): PromptGetResult;
public function get(GetPromptRequest $request): GetPromptResult;
}

View file

@ -14,12 +14,12 @@ namespace Mcp\Capability;
use Mcp\Capability\Prompt\CollectionInterface;
use Mcp\Capability\Prompt\IdentifierInterface;
use Mcp\Capability\Prompt\MetadataInterface;
use Mcp\Capability\Prompt\PromptGet;
use Mcp\Capability\Prompt\PromptGetResult;
use Mcp\Capability\Prompt\PromptGetterInterface;
use Mcp\Exception\InvalidCursorException;
use Mcp\Exception\PromptGetException;
use Mcp\Exception\PromptNotFoundException;
use Mcp\Schema\Request\GetPromptRequest;
use Mcp\Schema\Result\GetPromptResult;
/**
* A collection of prompts. All prompts need to implement IdentifierInterface.
@ -60,18 +60,18 @@ class PromptChain implements PromptGetterInterface, CollectionInterface
}
}
public function get(PromptGet $input): PromptGetResult
public function get(GetPromptRequest $request): GetPromptResult
{
foreach ($this->items as $item) {
if ($item instanceof PromptGetterInterface && $input->name === $item->getName()) {
if ($item instanceof PromptGetterInterface && $request->name === $item->getName()) {
try {
return $item->get($input);
return $item->get($request);
} catch (\Throwable $e) {
throw new PromptGetException($input, $e);
throw new PromptGetException($request, $e);
}
}
}
throw new PromptNotFoundException($input);
throw new PromptNotFoundException($request);
}
}

View file

@ -13,6 +13,8 @@ namespace Mcp\Capability\Resource;
use Mcp\Exception\ResourceNotFoundException;
use Mcp\Exception\ResourceReadException;
use Mcp\Schema\Request\ReadResourceRequest;
use Mcp\Schema\Result\ReadResourceResult;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -23,5 +25,5 @@ interface ResourceReaderInterface
* @throws ResourceReadException if the resource execution fails
* @throws ResourceNotFoundException if the resource is not found
*/
public function read(ResourceRead $input): ResourceReadResult;
public function read(ReadResourceRequest $request): ReadResourceResult;
}

View file

@ -14,12 +14,12 @@ namespace Mcp\Capability;
use Mcp\Capability\Resource\CollectionInterface;
use Mcp\Capability\Resource\IdentifierInterface;
use Mcp\Capability\Resource\MetadataInterface;
use Mcp\Capability\Resource\ResourceRead;
use Mcp\Capability\Resource\ResourceReaderInterface;
use Mcp\Capability\Resource\ResourceReadResult;
use Mcp\Exception\InvalidCursorException;
use Mcp\Exception\ResourceNotFoundException;
use Mcp\Exception\ResourceReadException;
use Mcp\Schema\Request\ReadResourceRequest;
use Mcp\Schema\Result\ReadResourceResult;
/**
* A collection of resources. All resources need to implement IdentifierInterface.
@ -60,18 +60,18 @@ class ResourceChain implements CollectionInterface, ResourceReaderInterface
}
}
public function read(ResourceRead $input): ResourceReadResult
public function read(ReadResourceRequest $request): ReadResourceResult
{
foreach ($this->items as $item) {
if ($item instanceof ResourceReaderInterface && $input->uri === $item->getUri()) {
if ($item instanceof ResourceReaderInterface && $request->uri === $item->getUri()) {
try {
return $item->read($input);
return $item->read($request);
} catch (\Throwable $e) {
throw new ResourceReadException($input, $e);
throw new ResourceReadException($request, $e);
}
}
}
throw new ResourceNotFoundException($input);
throw new ResourceNotFoundException($request);
}
}

View file

@ -1,28 +0,0 @@
<?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\Capability\Tool;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class ToolCall
{
/**
* @param array<string, mixed> $arguments
*/
public function __construct(
public readonly string $id,
public readonly string $name,
public readonly array $arguments = [],
) {
}
}

View file

@ -1,30 +0,0 @@
<?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\Capability\Tool;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class ToolCallResult
{
public function __construct(
public readonly string $result,
/**
* @var "text"|"image"|"audio"|"resource"|non-empty-string
*/
public readonly string $type = 'text',
public readonly string $mimeType = 'text/plan',
public readonly bool $isError = false,
public readonly ?string $uri = null,
) {
}
}

View file

@ -13,6 +13,8 @@ namespace Mcp\Capability\Tool;
use Mcp\Exception\ToolExecutionException;
use Mcp\Exception\ToolNotFoundException;
use Mcp\Schema\Request\CallToolRequest;
use Mcp\Schema\Result\CallToolResult;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -23,5 +25,5 @@ interface ToolExecutorInterface
* @throws ToolExecutionException if the tool execution fails
* @throws ToolNotFoundException if the tool is not found
*/
public function call(ToolCall $input): ToolCallResult;
public function call(CallToolRequest $request): CallToolResult;
}

View file

@ -14,12 +14,12 @@ namespace Mcp\Capability;
use Mcp\Capability\Tool\CollectionInterface;
use Mcp\Capability\Tool\IdentifierInterface;
use Mcp\Capability\Tool\MetadataInterface;
use Mcp\Capability\Tool\ToolCall;
use Mcp\Capability\Tool\ToolCallResult;
use Mcp\Capability\Tool\ToolExecutorInterface;
use Mcp\Exception\InvalidCursorException;
use Mcp\Exception\ToolExecutionException;
use Mcp\Exception\ToolNotFoundException;
use Mcp\Schema\Request\CallToolRequest;
use Mcp\Schema\Result\CallToolResult;
/**
* A collection of tools. All tools need to implement IdentifierInterface.
@ -60,18 +60,18 @@ class ToolChain implements ToolExecutorInterface, CollectionInterface
}
}
public function call(ToolCall $input): ToolCallResult
public function call(CallToolRequest $request): CallToolResult
{
foreach ($this->items as $item) {
if ($item instanceof ToolExecutorInterface && $input->name === $item->getName()) {
if ($item instanceof ToolExecutorInterface && $request->name === $item->getName()) {
try {
return $item->call($input);
return $item->call($request);
} catch (\Throwable $e) {
throw new ToolExecutionException($input, $e);
throw new ToolExecutionException($request, $e);
}
}
}
throw new ToolNotFoundException($input);
throw new ToolNotFoundException($request);
}
}

View file

@ -0,0 +1,19 @@
<?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\Exception;
/**
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
final class LogicException extends \LogicException implements ExceptionInterface
{
}

View file

@ -11,7 +11,7 @@
namespace Mcp\Exception;
use Mcp\Capability\Prompt\PromptGet;
use Mcp\Schema\Request\GetPromptRequest;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@ -19,9 +19,9 @@ use Mcp\Capability\Prompt\PromptGet;
final class PromptGetException extends \RuntimeException implements ExceptionInterface
{
public function __construct(
public readonly PromptGet $promptGet,
public readonly GetPromptRequest $request,
?\Throwable $previous = null,
) {
parent::__construct(\sprintf('Handling prompt "%s" failed with error: %s', $promptGet->name, $previous->getMessage()), previous: $previous);
parent::__construct(\sprintf('Handling prompt "%s" failed with error: "%s".', $request->name, $previous->getMessage()), previous: $previous);
}
}

Some files were not shown because too many files have changed in this diff Show more