Skip to content

Commit 86e74b6

Browse files
committed
refactor: registry loader
1 parent 54a5d14 commit 86e74b6

File tree

4 files changed

+383
-262
lines changed

4 files changed

+383
-262
lines changed
Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
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 Mcp\Capability\Registry\Loader;
13+
14+
use Mcp\Capability\Attribute\CompletionProvider;
15+
use Mcp\Capability\Completion\EnumCompletionProvider;
16+
use Mcp\Capability\Completion\ListCompletionProvider;
17+
use Mcp\Capability\Completion\ProviderInterface;
18+
use Mcp\Capability\Discovery\DocBlockParser;
19+
use Mcp\Capability\Discovery\HandlerResolver;
20+
use Mcp\Capability\Discovery\SchemaGenerator;
21+
use Mcp\Capability\Registry\ElementReference;
22+
use Mcp\Capability\Registry\ReferenceRegistryInterface;
23+
use Mcp\Exception\ConfigurationException;
24+
use Mcp\Schema\Annotations;
25+
use Mcp\Schema\Prompt;
26+
use Mcp\Schema\PromptArgument;
27+
use Mcp\Schema\Resource;
28+
use Mcp\Schema\ResourceTemplate;
29+
use Mcp\Schema\Tool;
30+
use Mcp\Schema\ToolAnnotations;
31+
use Mcp\Server\Handler;
32+
use Psr\Log\LoggerInterface;
33+
use Psr\Log\NullLogger;
34+
35+
/**
36+
* @author Antoine Bluchet <[email protected]>
37+
* @phpstan-import-type Handler from ElementReference
38+
*/
39+
final class ArrayLoader implements LoaderInterface
40+
{
41+
/**
42+
* @param array{
43+
* handler: Handler,
44+
* name: ?string,
45+
* description: ?string,
46+
* annotations: ?ToolAnnotations,
47+
* }[] $tools
48+
* @param array{
49+
* handler: Handler,
50+
* uri: string,
51+
* name: ?string,
52+
* description: ?string,
53+
* mimeType: ?string,
54+
* size: int|null,
55+
* annotations: ?Annotations,
56+
* }[] $resources
57+
* @param array{
58+
* handler: Handler,
59+
* uriTemplate: string,
60+
* name: ?string,
61+
* description: ?string,
62+
* mimeType: ?string,
63+
* annotations: ?Annotations,
64+
* }[] $resourceTemplates
65+
* @param array{
66+
* handler: Handler,
67+
* name: ?string,
68+
* description: ?string,
69+
* }[] $prompts
70+
*/
71+
public function __construct(
72+
private array $tools = [],
73+
private array $resources = [],
74+
private array $resourceTemplates = [],
75+
private array $prompts = [],
76+
private LoggerInterface $logger = new NullLogger,
77+
) {
78+
}
79+
80+
public function load(ReferenceRegistryInterface $registry): void
81+
{
82+
$docBlockParser = new DocBlockParser(logger: $this->logger);
83+
$schemaGenerator = new SchemaGenerator($docBlockParser);
84+
85+
// Register Tools
86+
foreach ($this->tools as $data) {
87+
try {
88+
$reflection = HandlerResolver::resolve($data['handler']);
89+
90+
if ($reflection instanceof \ReflectionFunction) {
91+
$name = $data['name'] ?? 'closure_tool_'.spl_object_id($data['handler']);
92+
$description = $data['description'] ?? null;
93+
} else {
94+
$classShortName = $reflection->getDeclaringClass()->getShortName();
95+
$methodName = $reflection->getName();
96+
$docBlock = $docBlockParser->parseDocBlock($reflection->getDocComment() ?? null);
97+
98+
$name = $data['name'] ?? ('__invoke' === $methodName ? $classShortName : $methodName);
99+
$description = $data['description'] ?? $docBlockParser->getSummary($docBlock) ?? null;
100+
}
101+
102+
$inputSchema = $data['inputSchema'] ?? $schemaGenerator->generate($reflection);
103+
104+
$tool = new Tool($name, $inputSchema, $description, $data['annotations']);
105+
$registry->registerTool($tool, $data['handler'], true);
106+
107+
$handlerDesc = $this->getHandlerDescription($data['handler']);
108+
$this->logger->debug("Registered manual tool {$name} from handler {$handlerDesc}");
109+
} catch (\Throwable $e) {
110+
$this->logger->error(
111+
'Failed to register manual tool',
112+
['handler' => $data['handler'], 'name' => $data['name'], 'exception' => $e],
113+
);
114+
throw new ConfigurationException("Error registering manual tool '{$data['name']}': {$e->getMessage()}", 0, $e);
115+
}
116+
}
117+
118+
// Register Resources
119+
foreach ($this->resources as $data) {
120+
try {
121+
$reflection = HandlerResolver::resolve($data['handler']);
122+
123+
if ($reflection instanceof \ReflectionFunction) {
124+
$name = $data['name'] ?? 'closure_resource_'.spl_object_id($data['handler']);
125+
$description = $data['description'] ?? null;
126+
} else {
127+
$classShortName = $reflection->getDeclaringClass()->getShortName();
128+
$methodName = $reflection->getName();
129+
$docBlock = $docBlockParser->parseDocBlock($reflection->getDocComment() ?? null);
130+
131+
$name = $data['name'] ?? ('__invoke' === $methodName ? $classShortName : $methodName);
132+
$description = $data['description'] ?? $docBlockParser->getSummary($docBlock) ?? null;
133+
}
134+
135+
$uri = $data['uri'];
136+
$mimeType = $data['mimeType'];
137+
$size = $data['size'];
138+
$annotations = $data['annotations'];
139+
140+
$resource = new Resource($uri, $name, $description, $mimeType, $annotations, $size);
141+
$registry->registerResource($resource, $data['handler'], true);
142+
143+
$handlerDesc = $this->getHandlerDescription($data['handler']);
144+
$this->logger->debug("Registered manual resource {$name} from handler {$handlerDesc}");
145+
} catch (\Throwable $e) {
146+
$this->logger->error(
147+
'Failed to register manual resource',
148+
['handler' => $data['handler'], 'uri' => $data['uri'], 'exception' => $e],
149+
);
150+
throw new ConfigurationException("Error registering manual resource '{$data['uri']}': {$e->getMessage()}", 0, $e);
151+
}
152+
}
153+
154+
// Register Templates
155+
foreach ($this->resourceTemplates as $data) {
156+
try {
157+
$reflection = HandlerResolver::resolve($data['handler']);
158+
159+
if ($reflection instanceof \ReflectionFunction) {
160+
$name = $data['name'] ?? 'closure_template_'.spl_object_id($data['handler']);
161+
$description = $data['description'] ?? null;
162+
} else {
163+
$classShortName = $reflection->getDeclaringClass()->getShortName();
164+
$methodName = $reflection->getName();
165+
$docBlock = $docBlockParser->parseDocBlock($reflection->getDocComment() ?? null);
166+
167+
$name = $data['name'] ?? ('__invoke' === $methodName ? $classShortName : $methodName);
168+
$description = $data['description'] ?? $docBlockParser->getSummary($docBlock) ?? null;
169+
}
170+
171+
$uriTemplate = $data['uriTemplate'];
172+
$mimeType = $data['mimeType'];
173+
$annotations = $data['annotations'];
174+
175+
$template = new ResourceTemplate($uriTemplate, $name, $description, $mimeType, $annotations);
176+
$completionProviders = $this->getCompletionProviders($reflection);
177+
$registry->registerResourceTemplate($template, $data['handler'], $completionProviders, true);
178+
179+
$handlerDesc = $this->getHandlerDescription($data['handler']);
180+
$this->logger->debug("Registered manual template {$name} from handler {$handlerDesc}");
181+
} catch (\Throwable $e) {
182+
$this->logger->error(
183+
'Failed to register manual template',
184+
['handler' => $data['handler'], 'uriTemplate' => $data['uriTemplate'], 'exception' => $e],
185+
);
186+
throw new ConfigurationException("Error registering manual resource template '{$data['uriTemplate']}': {$e->getMessage()}", 0, $e);
187+
}
188+
}
189+
190+
// Register Prompts
191+
foreach ($this->prompts as $data) {
192+
try {
193+
$reflection = HandlerResolver::resolve($data['handler']);
194+
195+
if ($reflection instanceof \ReflectionFunction) {
196+
$name = $data['name'] ?? 'closure_prompt_'.spl_object_id($data['handler']);
197+
$description = $data['description'] ?? null;
198+
} else {
199+
$classShortName = $reflection->getDeclaringClass()->getShortName();
200+
$methodName = $reflection->getName();
201+
$docBlock = $docBlockParser->parseDocBlock($reflection->getDocComment() ?? null);
202+
203+
$name = $data['name'] ?? ('__invoke' === $methodName ? $classShortName : $methodName);
204+
$description = $data['description'] ?? $docBlockParser->getSummary($docBlock) ?? null;
205+
}
206+
207+
$arguments = [];
208+
$paramTags = $reflection instanceof \ReflectionMethod ? $docBlockParser->getParamTags(
209+
$docBlockParser->parseDocBlock($reflection->getDocComment() ?? null),
210+
) : [];
211+
foreach ($reflection->getParameters() as $param) {
212+
$reflectionType = $param->getType();
213+
214+
// Basic DI check (heuristic)
215+
if ($reflectionType instanceof \ReflectionNamedType && !$reflectionType->isBuiltin()) {
216+
continue;
217+
}
218+
219+
$paramTag = $paramTags['$'.$param->getName()] ?? null;
220+
$arguments[] = new PromptArgument(
221+
$param->getName(),
222+
$paramTag ? trim((string) $paramTag->getDescription()) : null,
223+
!$param->isOptional() && !$param->isDefaultValueAvailable(),
224+
);
225+
}
226+
227+
$prompt = new Prompt($name, $description, $arguments);
228+
$completionProviders = $this->getCompletionProviders($reflection);
229+
$registry->registerPrompt($prompt, $data['handler'], $completionProviders, true);
230+
231+
$handlerDesc = $this->getHandlerDescription($data['handler']);
232+
$this->logger->debug("Registered manual prompt {$name} from handler {$handlerDesc}");
233+
} catch (\Throwable $e) {
234+
$this->logger->error(
235+
'Failed to register manual prompt',
236+
['handler' => $data['handler'], 'name' => $data['name'], 'exception' => $e],
237+
);
238+
throw new ConfigurationException("Error registering manual prompt '{$data['name']}': {$e->getMessage()}", 0, $e);
239+
}
240+
}
241+
242+
$this->logger->debug('Manual element registration complete.');
243+
}
244+
245+
/**
246+
* @param Handler $handler
247+
*/
248+
private function getHandlerDescription(\Closure|array|string $handler): string
249+
{
250+
if ($handler instanceof \Closure) {
251+
return 'Closure';
252+
}
253+
254+
if (\is_array($handler)) {
255+
return \sprintf(
256+
'%s::%s',
257+
\is_object($handler[0]) ? $handler[0]::class : $handler[0],
258+
$handler[1],
259+
);
260+
}
261+
262+
return (string) $handler;
263+
}
264+
265+
/**
266+
* @return array<string, ProviderInterface>
267+
*/
268+
private function getCompletionProviders(\ReflectionMethod|\ReflectionFunction $reflection): array
269+
{
270+
$completionProviders = [];
271+
foreach ($reflection->getParameters() as $param) {
272+
$reflectionType = $param->getType();
273+
if ($reflectionType instanceof \ReflectionNamedType && !$reflectionType->isBuiltin()) {
274+
continue;
275+
}
276+
277+
$completionAttributes = $param->getAttributes(
278+
CompletionProvider::class,
279+
\ReflectionAttribute::IS_INSTANCEOF,
280+
);
281+
if (!empty($completionAttributes)) {
282+
$attributeInstance = $completionAttributes[0]->newInstance();
283+
284+
if ($attributeInstance->provider) {
285+
$completionProviders[$param->getName()] = $attributeInstance->provider;
286+
} elseif ($attributeInstance->providerClass) {
287+
$completionProviders[$param->getName()] = $attributeInstance->providerClass;
288+
} elseif ($attributeInstance->values) {
289+
$completionProviders[$param->getName()] = new ListCompletionProvider($attributeInstance->values);
290+
} elseif ($attributeInstance->enum) {
291+
$completionProviders[$param->getName()] = new EnumCompletionProvider($attributeInstance->enum);
292+
}
293+
}
294+
}
295+
296+
return $completionProviders;
297+
}
298+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Mcp\Capability\Registry\Loader;
13+
14+
use Mcp\Capability\Discovery\CachedDiscoverer;
15+
use Mcp\Capability\Discovery\Discoverer;
16+
use Mcp\Capability\Registry\ReferenceRegistryInterface;
17+
use Psr\Log\LoggerInterface;
18+
use Psr\SimpleCache\CacheInterface;
19+
20+
/**
21+
* @author Antoine Bluchet <[email protected]>
22+
*/
23+
final class DiscoveryLoader implements LoaderInterface
24+
{
25+
/**
26+
* @param string[] $scanDirs
27+
* @param array|string[] $excludeDirs
28+
*/
29+
public function __construct(
30+
private string $basePath,
31+
private array $scanDirs,
32+
private array $excludeDirs,
33+
private LoggerInterface $logger,
34+
private ?CacheInterface $cache = null,
35+
) {
36+
}
37+
38+
public function load(ReferenceRegistryInterface $registry): void
39+
{
40+
// This now encapsulates the discovery process
41+
$discoverer = new Discoverer($registry, $this->logger);
42+
43+
$cachedDiscoverer = $this->cache
44+
? new CachedDiscoverer($discoverer, $this->cache, $this->logger)
45+
: $discoverer;
46+
47+
$cachedDiscoverer->discover($this->basePath, $this->scanDirs, $this->excludeDirs);
48+
}
49+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 Mcp\Capability\Registry\Loader;
13+
14+
use Mcp\Capability\Registry\ReferenceRegistryInterface;
15+
16+
/**
17+
* @author Antoine Bluchet <[email protected]>
18+
*/
19+
interface LoaderInterface
20+
{
21+
public function load(ReferenceRegistryInterface $registry): void;
22+
}

0 commit comments

Comments
 (0)