-
-
Notifications
You must be signed in to change notification settings - Fork 963
Expand file tree
/
Copy pathHandler.php
More file actions
148 lines (123 loc) · 4.91 KB
/
Handler.php
File metadata and controls
148 lines (123 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Server;
use ApiPlatform\Mcp\State\ToolProvider;
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;
/**
* @experimental
*
* @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
{
if ($request instanceof CallToolRequest) {
return null !== $this->operationMetadataFactory->create($request->name);
}
if ($request instanceof ReadResourceRequest) {
return null !== $this->operationMetadataFactory->create($request->uri);
}
return false;
}
/**
* @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|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) {
if (isset($arguments[$key])) {
$uriVariables[$key] = $arguments[$key];
}
}
}
$context = [
'request' => $this->requestStack->getCurrentRequest(),
'mcp_request' => $request,
'uri_variables' => $uriVariables,
'resource_class' => $operation->getClass(),
];
if (!$isResource) {
$context['mcp_data'] = $arguments;
}
$operation = $operation->withExtraProperties(
array_merge($operation->getExtraProperties(), ['_api_disable_swagger_provider' => true])
);
if (null === $operation->canValidate()) {
$operation = $operation->withValidate(false);
}
if (null === $operation->canRead()) {
$operation = $operation->withRead(true);
}
if (null === $operation->getProvider()) {
$operation = $operation->withProvider(ToolProvider::class);
}
if (null === $operation->canDeserialize()) {
$operation = $operation->withDeserialize(false);
}
$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');
$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);
}
}