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
5 changed files with 1516 additions and 2424 deletions

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