Adopt server examples
This commit is contained in:
parent
da0d5a87a0
commit
2ef051a355
6 changed files with 3639 additions and 756 deletions
|
|
@ -11,13 +11,16 @@
|
|||
|
||||
namespace Mcp\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Tool\ToolExecutorInterface;
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Exception\ExceptionInterface;
|
||||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Mcp\Schema\JsonRpc\HasMethodInterface;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\CallToolRequest;
|
||||
use Mcp\Schema\Result\CallToolResult;
|
||||
use Mcp\Server\MethodHandlerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* @author Christopher Hertel <mail@christopher-hertel.de>
|
||||
|
|
@ -26,7 +29,8 @@ use Mcp\Server\MethodHandlerInterface;
|
|||
final class CallToolHandler implements MethodHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ToolExecutorInterface $toolExecutor,
|
||||
private readonly Registry $registry,
|
||||
private readonly LoggerInterface $logger = new NullLogger(),
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -40,11 +44,16 @@ final class CallToolHandler implements MethodHandlerInterface
|
|||
\assert($message instanceof CallToolRequest);
|
||||
|
||||
try {
|
||||
$result = $this->toolExecutor->call($message);
|
||||
} catch (ExceptionInterface) {
|
||||
$content = $this->registry->handleCallTool($message->name, $message->arguments);
|
||||
} catch (ExceptionInterface $exception) {
|
||||
$this->logger->error(\sprintf('Error while executing tool "%s": "%s".', $message->name, $exception->getMessage()), [
|
||||
'tool' => $message->name,
|
||||
'arguments' => $message->arguments,
|
||||
]);
|
||||
|
||||
return Error::forInternalError('Error while executing tool', $message->getId());
|
||||
}
|
||||
|
||||
return new Response($message->getId(), $result);
|
||||
return new Response($message->getId(), new CallToolResult($content));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,12 +11,13 @@
|
|||
|
||||
namespace Mcp\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Prompt\PromptGetterInterface;
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Exception\ExceptionInterface;
|
||||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Mcp\Schema\JsonRpc\HasMethodInterface;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\GetPromptRequest;
|
||||
use Mcp\Schema\Result\GetPromptResult;
|
||||
use Mcp\Server\MethodHandlerInterface;
|
||||
|
||||
/**
|
||||
|
|
@ -25,7 +26,7 @@ use Mcp\Server\MethodHandlerInterface;
|
|||
final class GetPromptHandler implements MethodHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PromptGetterInterface $getter,
|
||||
private readonly Registry $registry,
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -39,11 +40,11 @@ final class GetPromptHandler implements MethodHandlerInterface
|
|||
\assert($message instanceof GetPromptRequest);
|
||||
|
||||
try {
|
||||
$result = $this->getter->get($message);
|
||||
$messages = $this->registry->handleGetPrompt($message->name, $message->arguments);
|
||||
} catch (ExceptionInterface) {
|
||||
return Error::forInternalError('Error while handling prompt', $message->getId());
|
||||
}
|
||||
|
||||
return new Response($message->getId(), $result);
|
||||
return new Response($message->getId(), new GetPromptResult($messages));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,9 @@
|
|||
|
||||
namespace Mcp\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Prompt\CollectionInterface;
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Schema\JsonRpc\HasMethodInterface;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Prompt;
|
||||
use Mcp\Schema\PromptArgument;
|
||||
use Mcp\Schema\Request\ListPromptsRequest;
|
||||
use Mcp\Schema\Result\ListPromptsResult;
|
||||
use Mcp\Server\MethodHandlerInterface;
|
||||
|
|
@ -26,7 +24,7 @@ use Mcp\Server\MethodHandlerInterface;
|
|||
final class ListPromptsHandler implements MethodHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CollectionInterface $collection,
|
||||
private readonly Registry $registry,
|
||||
private readonly int $pageSize = 20,
|
||||
) {
|
||||
}
|
||||
|
|
@ -41,19 +39,7 @@ final class ListPromptsHandler implements MethodHandlerInterface
|
|||
\assert($message instanceof ListPromptsRequest);
|
||||
|
||||
$cursor = null;
|
||||
$prompts = [];
|
||||
|
||||
$metadataList = $this->collection->getMetadata($this->pageSize, $message->cursor);
|
||||
|
||||
foreach ($metadataList as $metadata) {
|
||||
$cursor = $metadata->getName();
|
||||
$prompts[] = new Prompt(
|
||||
$metadata->getName(),
|
||||
$metadata->getDescription(),
|
||||
array_map(fn (array $data) => PromptArgument::fromArray($data), $metadata->getArguments()),
|
||||
);
|
||||
}
|
||||
|
||||
$prompts = $this->registry->getPrompts($this->pageSize, $message->cursor);
|
||||
$nextCursor = (null !== $cursor && \count($prompts) === $this->pageSize) ? $cursor : null;
|
||||
|
||||
return new Response(
|
||||
|
|
|
|||
|
|
@ -11,11 +11,10 @@
|
|||
|
||||
namespace Mcp\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Resource\CollectionInterface;
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Schema\JsonRpc\HasMethodInterface;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\ListResourcesRequest;
|
||||
use Mcp\Schema\Resource;
|
||||
use Mcp\Schema\Result\ListResourcesResult;
|
||||
use Mcp\Server\MethodHandlerInterface;
|
||||
|
||||
|
|
@ -25,7 +24,7 @@ use Mcp\Server\MethodHandlerInterface;
|
|||
final class ListResourcesHandler implements MethodHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CollectionInterface $collection,
|
||||
private readonly Registry $registry,
|
||||
private readonly int $pageSize = 20,
|
||||
) {
|
||||
}
|
||||
|
|
@ -40,22 +39,7 @@ final class ListResourcesHandler implements MethodHandlerInterface
|
|||
\assert($message instanceof ListResourcesRequest);
|
||||
|
||||
$cursor = null;
|
||||
$resources = [];
|
||||
|
||||
$metadataList = $this->collection->getMetadata($this->pageSize, $message->cursor);
|
||||
|
||||
foreach ($metadataList as $metadata) {
|
||||
$cursor = $metadata->getUri();
|
||||
$resources[] = new Resource(
|
||||
$metadata->getUri(),
|
||||
$metadata->getName(),
|
||||
$metadata->getDescription(),
|
||||
$metadata->getMimeType(),
|
||||
null,
|
||||
$metadata->getSize(),
|
||||
);
|
||||
}
|
||||
|
||||
$resources = $this->registry->getResources($this->pageSize, $message->cursor);
|
||||
$nextCursor = (null !== $cursor && \count($resources) === $this->pageSize) ? $cursor : null;
|
||||
|
||||
return new Response(
|
||||
|
|
|
|||
|
|
@ -11,12 +11,11 @@
|
|||
|
||||
namespace Mcp\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Tool\CollectionInterface;
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Schema\JsonRpc\HasMethodInterface;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\ListToolsRequest;
|
||||
use Mcp\Schema\Result\ListToolsResult;
|
||||
use Mcp\Schema\Tool;
|
||||
use Mcp\Server\MethodHandlerInterface;
|
||||
|
||||
/**
|
||||
|
|
@ -26,7 +25,7 @@ use Mcp\Server\MethodHandlerInterface;
|
|||
final class ListToolsHandler implements MethodHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CollectionInterface $collection,
|
||||
private readonly Registry $registry,
|
||||
private readonly int $pageSize = 20,
|
||||
) {
|
||||
}
|
||||
|
|
@ -41,24 +40,7 @@ final class ListToolsHandler implements MethodHandlerInterface
|
|||
\assert($message instanceof ListToolsRequest);
|
||||
|
||||
$cursor = null;
|
||||
$tools = [];
|
||||
|
||||
$metadataList = $this->collection->getMetadata($this->pageSize, $message->cursor);
|
||||
|
||||
foreach ($metadataList as $tool) {
|
||||
$cursor = $tool->getName();
|
||||
$inputSchema = $tool->getInputSchema();
|
||||
$tools[] = new Tool(
|
||||
$tool->getName(),
|
||||
[] === $inputSchema ? [
|
||||
'type' => 'object',
|
||||
'$schema' => 'http://json-schema.org/draft-07/schema#',
|
||||
] : $inputSchema,
|
||||
$tool->getDescription(),
|
||||
null,
|
||||
);
|
||||
}
|
||||
|
||||
$tools = $this->registry->getTools($this->pageSize, $message->cursor);
|
||||
$nextCursor = (null !== $cursor && \count($tools) === $this->pageSize) ? $cursor : null;
|
||||
|
||||
return new Response(
|
||||
|
|
|
|||
|
|
@ -11,13 +11,14 @@
|
|||
|
||||
namespace Mcp\Server\RequestHandler;
|
||||
|
||||
use Mcp\Capability\Resource\ResourceReaderInterface;
|
||||
use Mcp\Capability\Registry;
|
||||
use Mcp\Exception\ExceptionInterface;
|
||||
use Mcp\Exception\ResourceNotFoundException;
|
||||
use Mcp\Schema\JsonRpc\Error;
|
||||
use Mcp\Schema\JsonRpc\HasMethodInterface;
|
||||
use Mcp\Schema\JsonRpc\Response;
|
||||
use Mcp\Schema\Request\ReadResourceRequest;
|
||||
use Mcp\Schema\Result\ReadResourceResult;
|
||||
use Mcp\Server\MethodHandlerInterface;
|
||||
|
||||
/**
|
||||
|
|
@ -26,7 +27,7 @@ use Mcp\Server\MethodHandlerInterface;
|
|||
final class ReadResourceHandler implements MethodHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ResourceReaderInterface $reader,
|
||||
private readonly Registry $registry,
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -40,13 +41,13 @@ final class ReadResourceHandler implements MethodHandlerInterface
|
|||
\assert($message instanceof ReadResourceRequest);
|
||||
|
||||
try {
|
||||
$result = $this->reader->read($message);
|
||||
$contents = $this->registry->handleReadResource($message->uri);
|
||||
} catch (ResourceNotFoundException $e) {
|
||||
return new Error($message->getId(), Error::RESOURCE_NOT_FOUND, $e->getMessage());
|
||||
} catch (ExceptionInterface) {
|
||||
return Error::forInternalError('Error while reading resource', $message->getId());
|
||||
}
|
||||
|
||||
return new Response($message->getId(), $result);
|
||||
return new Response($message->getId(), new ReadResourceResult($contents));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue