-
-
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
Open
soyuka
wants to merge
1
commit into
api-platform:main
Choose a base branch
from
soyuka:mcp-bundle-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,510
−24
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?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]); | ||
| $outputSchema = $this->schemaFactory->buildSchema($inputClass, 'json', Schema::TYPE_OUTPUT, $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(), | ||
| outputSchema: $outputSchema->getDefinitions()[$outputSchema->getRootDefinitionKey()]->getArrayCopy(), | ||
| ), | ||
| 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 | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
src/Mcp/Metadata/Operation/Factory/OperationMetadataFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?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\Exception\RuntimeException; | ||
| use ApiPlatform\Metadata\HttpOperation; | ||
| use ApiPlatform\Metadata\McpResource; | ||
| use ApiPlatform\Metadata\McpTool; | ||
| use ApiPlatform\Metadata\Operation; | ||
| 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, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @throws RuntimeException | ||
| * | ||
| * @return HttpOperation | ||
| */ | ||
| public function create(string $operationName, array $context = []): 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)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($operation->getName() === $operationName) { | ||
| return $operation; | ||
| } | ||
|
|
||
| if ($operation instanceof McpResource && $operation->getUri() === $operationName) { | ||
| return $operation; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw new RuntimeException(\sprintf('MCP operation "%s" not found.', $operationName)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?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; | ||
| use ApiPlatform\Metadata\UrlGeneratorInterface; | ||
|
|
||
| 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 = UrlGeneratorInterface::ABS_PATH, ?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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?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\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\Request\ReadResourceRequest; | ||
| use Mcp\Schema\Result\CallToolResult; | ||
| use Mcp\Schema\Result\ReadResourceResult; | ||
| 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|ReadResourceResult> | ||
| */ | ||
| 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 || $request instanceof ReadResourceRequest; | ||
| } | ||
|
|
||
| /** | ||
| * @return Response<CallToolResult|ReadResourceResult>|Error | ||
| */ | ||
| public function handle(Request $request, SessionInterface $session): Response|Error | ||
| { | ||
| $isResource = $request instanceof ReadResourceRequest; | ||
|
|
||
| if ($isResource) { | ||
| $operationNameOrUri = $request->uri; | ||
| $arguments = []; | ||
| $this->logger->debug('Reading resource', ['uri' => $operationNameOrUri]); | ||
| } else { | ||
| \assert($request instanceof CallToolRequest); | ||
| $operationNameOrUri = $request->name; | ||
| $arguments = $request->arguments ?? []; | ||
| $this->logger->debug('Executing tool', ['name' => $operationNameOrUri, 'arguments' => $arguments]); | ||
| } | ||
|
|
||
| /** @var HttpOperation $operation */ | ||
| $operation = $this->operationMetadataFactory->create($operationNameOrUri); | ||
|
|
||
| $uriVariables = []; | ||
| if (!$isResource) { | ||
| 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(), | ||
| ]; | ||
|
|
||
| if (!$isResource) { | ||
| $context['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); | ||
|
|
||
| if (!$isResource) { | ||
| $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); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.