-
-
Notifications
You must be signed in to change notification settings - Fork 958
feat: mcp bundle tool integration #7595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Mcp\Capability\Registry; | ||
|
|
||
| use ApiPlatform\JsonSchema\Schema; | ||
| use ApiPlatform\JsonSchema\SchemaFactory; | ||
| use ApiPlatform\JsonSchema\SchemaFactoryInterface; | ||
| use ApiPlatform\Metadata\McpResource; | ||
| use ApiPlatform\Metadata\McpTool; | ||
| use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
| use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
| use Mcp\Capability\Registry\Loader\LoaderInterface; | ||
| use Mcp\Capability\RegistryInterface; | ||
| use Mcp\Schema\Annotations; | ||
| use Mcp\Schema\Resource; | ||
| use Mcp\Schema\Tool; | ||
| use Mcp\Schema\ToolAnnotations; | ||
|
|
||
| final class Loader implements LoaderInterface | ||
| { | ||
| public const HANDLER = 'api_platform.mcp.handler'; | ||
|
|
||
| public function __construct( | ||
| private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, | ||
| private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollection, | ||
| private readonly SchemaFactoryInterface $schemaFactory, | ||
| ) { | ||
| } | ||
|
|
||
| public function load(RegistryInterface $registry): void | ||
| { | ||
| foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { | ||
| $metadata = $this->resourceMetadataCollection->create($resourceClass); | ||
|
|
||
| foreach ($metadata as $resource) { | ||
| foreach ($resource->getMcp() ?? [] as $mcp) { | ||
| if ($mcp instanceof McpTool) { | ||
| $inputClass = $mcp->getInput()['class'] ?? $mcp->getClass(); | ||
| $schema = $this->schemaFactory->buildSchema($inputClass, 'json', Schema::TYPE_INPUT, $mcp, null, [SchemaFactory::FORCE_SUBSCHEMA => true]); | ||
| $registry->registerTool( | ||
| new Tool( | ||
| name: $mcp->getName(), | ||
| inputSchema: $schema->getDefinitions()[$schema->getRootDefinitionKey()]->getArrayCopy(), | ||
| description: $mcp->getDescription(), | ||
| annotations: $mcp->getAnnotations() ? ToolAnnotations::fromArray($mcp->getAnnotations()) : null, | ||
| icons: $mcp->getIcons(), | ||
| meta: $mcp->getMeta() | ||
| ), | ||
| self::HANDLER, | ||
| true | ||
| ); | ||
| } | ||
|
|
||
| if ($mcp instanceof McpResource) { | ||
| $registry->registerResource( | ||
| new Resource( | ||
| uri: $mcp->getUri(), | ||
| name: $mcp->getName(), | ||
| description: $mcp->getDescription(), | ||
| mimeType: $mcp->getMimeType(), | ||
| annotations: $mcp->getAnnotations() ? Annotations::fromArray($mcp->getAnnotations()) : null, | ||
| size: $mcp->getSize(), | ||
| icons: $mcp->getIcons(), | ||
| meta: $mcp->getMeta() | ||
| ), | ||
| self::HANDLER, | ||
| true | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Mcp\Metadata\Operation\Factory; | ||
|
|
||
| use ApiPlatform\Metadata\McpResource; | ||
| use ApiPlatform\Metadata\McpTool; | ||
| use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface; | ||
| use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||
| use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; | ||
|
|
||
| final class OperationMetadataFactory implements OperationMetadataFactoryInterface | ||
| { | ||
| public function __construct(private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory) | ||
| { | ||
| } | ||
|
|
||
| public function create(string $operationName, array $context = []): ?\ApiPlatform\Metadata\Operation | ||
| { | ||
| foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { | ||
| foreach ($this->resourceMetadataCollectionFactory->create($resourceClass) as $resource) { | ||
| if (null === $mcp = $resource->getMcp()) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($mcp as $operation) { | ||
| if (($operation instanceof McpTool || $operation instanceof McpResource) && $operation->getName() === $operationName) { | ||
| return $operation; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As tell-dont-ask implies we expect creation of the operation, edge cases could lead to clear exceptions on how to resolve those. What about dropping nullability on the return typehint, and throw LogicException here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 and then remove the lines at: https://github.com/api-platform/core/pull/7595/files/b904a0d0b94bc53bd5e701c7a1eb456ca255ae5c |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Mcp\Routing; | ||
|
|
||
| use ApiPlatform\Metadata\IriConverterInterface; | ||
| use ApiPlatform\Metadata\McpResource; | ||
| use ApiPlatform\Metadata\McpTool; | ||
| use ApiPlatform\Metadata\Operation; | ||
|
|
||
| final class IriConverter implements IriConverterInterface | ||
| { | ||
| public function __construct(private readonly IriConverterInterface $inner) | ||
| { | ||
| } | ||
|
|
||
| public function getResourceFromIri(string $iri, array $context = [], ?Operation $operation = null): object | ||
| { | ||
| return $this->inner->getResourceFromIri($iri, $context, $operation); | ||
| } | ||
|
|
||
| public function getIriFromResource(object|string $resource, int $referenceType = 1, ?Operation $operation = null, array $context = []): ?string | ||
| { | ||
| if (($operation instanceof McpTool || $operation instanceof McpResource) && !isset($context['item_uri_template'])) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->inner->getIriFromResource($resource, $referenceType, $operation, $context); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * 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 ApiPlatform\Mcp\Server; | ||
|
|
||
| use ApiPlatform\Metadata\Exception\RuntimeException; | ||
| use ApiPlatform\Metadata\HttpOperation; | ||
| use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface; | ||
| use ApiPlatform\State\ProcessorInterface; | ||
| use ApiPlatform\State\ProviderInterface; | ||
| use Mcp\Schema\JsonRpc\Error; | ||
| use Mcp\Schema\JsonRpc\Request; | ||
| use Mcp\Schema\JsonRpc\Response; | ||
| use Mcp\Schema\Request\CallToolRequest; | ||
| use Mcp\Schema\Result\CallToolResult; | ||
| use Mcp\Server\Handler\Request\RequestHandlerInterface; | ||
| use Mcp\Server\Session\SessionInterface; | ||
| use Psr\Log\LoggerInterface; | ||
| use Psr\Log\NullLogger; | ||
| use Symfony\Component\HttpFoundation\RequestStack; | ||
|
|
||
| /** | ||
| * @implements RequestHandlerInterface<CallToolResult> | ||
| */ | ||
| final class Handler implements RequestHandlerInterface | ||
| { | ||
| public function __construct( | ||
| private readonly OperationMetadataFactoryInterface $operationMetadataFactory, | ||
| private readonly ProviderInterface $provider, | ||
| private readonly ProcessorInterface $processor, | ||
| private readonly RequestStack $requestStack, | ||
| private readonly LoggerInterface $logger = new NullLogger(), | ||
| ) { | ||
| } | ||
|
|
||
| public function supports(Request $request): bool | ||
| { | ||
| return $request instanceof CallToolRequest; | ||
| } | ||
|
|
||
| /** | ||
| * @return Response<CallToolResult>|Error | ||
| */ | ||
| public function handle(Request $request, SessionInterface $session): Response|Error | ||
| { | ||
| \assert($request instanceof CallToolRequest); | ||
|
|
||
| $toolName = $request->name; | ||
| $arguments = $request->arguments ?? []; | ||
|
|
||
| $this->logger->debug('Executing tool', ['name' => $toolName, 'arguments' => $arguments]); | ||
|
|
||
| $operation = $this->operationMetadataFactory->create($toolName); | ||
|
|
||
| if (!$operation instanceof HttpOperation) { | ||
| throw new RuntimeException(\sprintf('Operation "%s" must be an instance of HttpOperation.', $toolName)); | ||
| } | ||
|
|
||
| $uriVariables = []; | ||
| foreach ($operation->getUriVariables() ?? [] as $key => $link) { | ||
| if (isset($arguments[$key])) { | ||
| $uriVariables[$key] = $arguments[$key]; | ||
| } | ||
| } | ||
|
|
||
| $context = [ | ||
| 'request' => ($httpRequest = $this->requestStack->getCurrentRequest()), | ||
| 'mcp_request' => $request, | ||
| 'uri_variables' => $uriVariables, | ||
| 'resource_class' => $operation->getClass(), | ||
| 'mcp_data' => $arguments, | ||
| ]; | ||
|
|
||
| if (null === $operation->canValidate()) { | ||
| $operation = $operation->withValidate(false); | ||
| } | ||
|
|
||
| if (null === $operation->canRead()) { | ||
| $operation = $operation->withRead(true); | ||
| } | ||
|
|
||
| if (null === $operation->getProvider()) { | ||
| $operation = $operation->withProvider('api_platform.mcp.state.tool_provider'); | ||
| } | ||
|
|
||
| if (null === $operation->canDeserialize()) { | ||
| $operation = $operation->withDeserialize(false); | ||
| } | ||
|
|
||
| $body = $this->provider->provide($operation, $uriVariables, $context); | ||
|
|
||
| $context['previous_data'] = $httpRequest->attributes->get('previous_data'); | ||
| $context['data'] = $httpRequest->attributes->get('data'); | ||
| $context['read_data'] = $httpRequest->attributes->get('read_data'); | ||
| $context['mapped_data'] = $httpRequest->attributes->get('mapped_data'); | ||
|
|
||
| if (null === $operation->canWrite()) { | ||
| $operation = $operation->withWrite(true); | ||
| } | ||
|
|
||
| if (null === $operation->canSerialize()) { | ||
| $operation = $operation->withSerialize(false); | ||
| } | ||
|
|
||
| return $this->processor->process($body, $operation, $uriVariables, $context); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.