Add request handler for resource templates (#89)

This commit is contained in:
Christopher Hertel 2025-10-04 19:43:41 +02:00 • committed by GitHub
commit cecfa0c3c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 285 additions and 1 deletions

View file

@ -86,6 +86,7 @@ class JsonRpcHandler
new Handler\Request\GetPromptHandler($promptGetter),
new Handler\Request\ListResourcesHandler($referenceProvider, $paginationLimit),
new Handler\Request\ReadResourceHandler($resourceReader),
new Handler\Request\ListResourceTemplatesHandler($referenceProvider, $paginationLimit),
new Handler\Request\CallToolHandler($toolCaller, $logger),
new Handler\Request\ListToolsHandler($referenceProvider, $paginationLimit),
],

View file

@ -0,0 +1,53 @@
<?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\Server\Handler\Request;
use Mcp\Capability\Registry\ReferenceProviderInterface;
use Mcp\Exception\InvalidCursorException;
use Mcp\Schema\JsonRpc\HasMethodInterface;
use Mcp\Schema\JsonRpc\Response;
use Mcp\Schema\Request\ListResourceTemplatesRequest;
use Mcp\Schema\Result\ListResourceTemplatesResult;
use Mcp\Server\Handler\MethodHandlerInterface;
use Mcp\Server\Session\SessionInterface;
/**
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
final class ListResourceTemplatesHandler implements MethodHandlerInterface
{
public function __construct(
private readonly ReferenceProviderInterface $registry,
private readonly int $pageSize = 20,
) {
}
public function supports(HasMethodInterface $message): bool
{
return $message instanceof ListResourceTemplatesRequest;
}
/**
* @throws InvalidCursorException
*/
public function handle(ListResourceTemplatesRequest|HasMethodInterface $message, SessionInterface $session): Response
{
\assert($message instanceof ListResourceTemplatesRequest);
$page = $this->registry->getResourceTemplates($this->pageSize, $message->cursor);
return new Response(
$message->getId(),
new ListResourceTemplatesResult($page->references, $page->nextCursor),
);
}
}