|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace ApiPlatform\Mcp\Server; |
| 13 | + |
| 14 | +use ApiPlatform\Mcp\State\ToolProvider; |
| 15 | +use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface; |
| 16 | +use ApiPlatform\State\ProcessorInterface; |
| 17 | +use ApiPlatform\State\ProviderInterface; |
| 18 | +use Mcp\Capability\Registry\ReferenceHandlerInterface; |
| 19 | +use Mcp\Capability\RegistryInterface; |
| 20 | +use Mcp\Exception\ToolCallException; |
| 21 | +use Mcp\Exception\ToolNotFoundException; |
| 22 | +use Mcp\Schema\Content\TextContent; |
| 23 | +use Mcp\Schema\JsonRpc\Error; |
| 24 | +use Mcp\Schema\JsonRpc\Request; |
| 25 | +use Mcp\Schema\JsonRpc\Response; |
| 26 | +use Mcp\Schema\Request\CallToolRequest; |
| 27 | +use Mcp\Schema\Result\CallToolResult; |
| 28 | +use Mcp\Server\Handler\Request\RequestHandlerInterface; |
| 29 | +use Mcp\Server\Session\SessionInterface; |
| 30 | +use Psr\Log\LoggerInterface; |
| 31 | +use Psr\Log\NullLogger; |
| 32 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 33 | + |
| 34 | +/** |
| 35 | + * @implements RequestHandlerInterface<CallToolResult> |
| 36 | + */ |
| 37 | +final class Handler implements RequestHandlerInterface |
| 38 | +{ |
| 39 | + public function __construct( |
| 40 | + private readonly OperationMetadataFactoryInterface $operationMetadataFactory, |
| 41 | + private readonly ProviderInterface $provider, |
| 42 | + private readonly ProcessorInterface $processor, |
| 43 | + private readonly RequestStack $requestStack, |
| 44 | + private readonly LoggerInterface $logger = new NullLogger(), |
| 45 | + ) { |
| 46 | + } |
| 47 | + |
| 48 | + public function supports(Request $request): bool |
| 49 | + { |
| 50 | + return $request instanceof CallToolRequest; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return Response<CallToolResult>|Error |
| 55 | + */ |
| 56 | + public function handle(Request $request, SessionInterface $session): Response|Error |
| 57 | + { |
| 58 | + \assert($request instanceof CallToolRequest); |
| 59 | + |
| 60 | + $toolName = $request->name; |
| 61 | + $arguments = $request->arguments ?? []; |
| 62 | + |
| 63 | + $this->logger->debug('Executing tool', ['name' => $toolName, 'arguments' => $arguments]); |
| 64 | + |
| 65 | + $operation = $this->operationMetadataFactory->create($toolName); |
| 66 | + |
| 67 | + $uriVariables = []; |
| 68 | + foreach ($operation->getUriVariables() ?? [] as $key => $link) { |
| 69 | + if (isset($arguments[$key])) { |
| 70 | + $uriVariables[$key] = $arguments[$key]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + $context = [ |
| 75 | + 'request' => ($httpRequest = $this->requestStack->getCurrentRequest()), |
| 76 | + 'mcp_request' => $request, |
| 77 | + 'uri_variables' => $uriVariables, |
| 78 | + 'resource_class' => $operation->getClass(), |
| 79 | + 'mcp_data' => $arguments, |
| 80 | + ]; |
| 81 | + |
| 82 | + if (null === $operation->canValidate()) { |
| 83 | + $operation = $operation->withValidate(false); |
| 84 | + } |
| 85 | + |
| 86 | + if (null === $operation->canRead()) { |
| 87 | + $operation = $operation->withRead(true); |
| 88 | + } |
| 89 | + |
| 90 | + if (null === $operation->getProvider()) { |
| 91 | + $operation = $operation->withProvider('api_platform.mcp.state.tool_provider'); |
| 92 | + } |
| 93 | + |
| 94 | + if (null === $operation->canDeserialize()) { |
| 95 | + $operation = $operation->withDeserialize(false); |
| 96 | + } |
| 97 | + |
| 98 | + $body = $this->provider->provide($operation, $uriVariables, $context); |
| 99 | + |
| 100 | + $context['previous_data'] = $httpRequest->attributes->get('previous_data'); |
| 101 | + $context['data'] = $httpRequest->attributes->get('data'); |
| 102 | + $context['read_data'] = $httpRequest->attributes->get('read_data'); |
| 103 | + $context['mapped_data'] = $httpRequest->attributes->get('mapped_data'); |
| 104 | + |
| 105 | + if (null === $operation->canWrite()) { |
| 106 | + $operation = $operation->withWrite(true); |
| 107 | + } |
| 108 | + |
| 109 | + if (null === $operation->canSerialize()) { |
| 110 | + $operation = $operation->withSerialize(false); |
| 111 | + } |
| 112 | + |
| 113 | + return $this->processor->process($body, $operation, $uriVariables, $context); |
| 114 | + } |
| 115 | +} |
0 commit comments