Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace ApiPlatform\Mcp\Metadata\Operation\Factory;

use ApiPlatform\Metadata\Exception\RuntimeException;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\McpResource;
use ApiPlatform\Metadata\McpTool;
Expand All @@ -32,10 +31,7 @@ public function __construct(
) {
}

/**
* @throws RuntimeException
*/
public function create(string $operationName, array $context = []): HttpOperation
public function create(string $operationName, array $context = []): ?HttpOperation
{
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
foreach ($this->resourceMetadataCollectionFactory->create($resourceClass) as $resource) {
Expand All @@ -55,6 +51,6 @@ public function create(string $operationName, array $context = []): HttpOperatio
}
}

throw new RuntimeException(\sprintf('MCP operation "%s" not found.', $operationName));
return null;
}
}
23 changes: 20 additions & 3 deletions src/Mcp/Server/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ public function __construct(

public function supports(Request $request): bool
{
return $request instanceof CallToolRequest || $request instanceof ReadResourceRequest;
if ($request instanceof CallToolRequest) {
return null !== $this->operationMetadataFactory->create($request->name);
}

if ($request instanceof ReadResourceRequest) {
return null !== $this->operationMetadataFactory->create($request->uri);
}

return false;
}

/**
Expand All @@ -70,9 +78,13 @@ public function handle(Request $request, SessionInterface $session): Response|Er
$this->logger->debug('Executing tool', ['name' => $operationNameOrUri, 'arguments' => $arguments]);
}

/** @var HttpOperation $operation */
/** @var HttpOperation|null $operation */
$operation = $this->operationMetadataFactory->create($operationNameOrUri);

if (null === $operation) {
return Error::forMethodNotFound(\sprintf('MCP operation "%s" not found.', $operationNameOrUri), $request->getId());
}

$uriVariables = [];
if (!$isResource) {
foreach ($operation->getUriVariables() ?? [] as $key => $link) {
Expand All @@ -83,7 +95,7 @@ public function handle(Request $request, SessionInterface $session): Response|Er
}

$context = [
'request' => ($httpRequest = $this->requestStack->getCurrentRequest()),
'request' => $this->requestStack->getCurrentRequest(),
'mcp_request' => $request,
'uri_variables' => $uriVariables,
'resource_class' => $operation->getClass(),
Expand All @@ -93,6 +105,10 @@ public function handle(Request $request, SessionInterface $session): Response|Er
$context['mcp_data'] = $arguments;
}

$operation = $operation->withExtraProperties(
array_merge($operation->getExtraProperties(), ['_api_disable_swagger_provider' => true])
);

if (null === $operation->canValidate()) {
$operation = $operation->withValidate(false);
}
Expand All @@ -112,6 +128,7 @@ public function handle(Request $request, SessionInterface $session): Response|Er
$body = $this->provider->provide($operation, $uriVariables, $context);

if (!$isResource) {
$httpRequest = $context['request'];
$context['previous_data'] = $httpRequest->attributes->get('previous_data');
$context['data'] = $httpRequest->attributes->get('data');
$context['read_data'] = $httpRequest->attributes->get('read_data');
Expand Down
Loading