|
| 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\Source\ElementSourceInterface; |
| 15 | +use Mcp\Capability\RegistryInterface; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | +use Psr\Log\NullLogger; |
| 18 | + |
| 19 | +/** |
| 20 | + * Writes the elements of a source into a registry. |
| 21 | + * |
| 22 | + * @author Christopher Hertel <mail@christopher-hertel.de> |
| 23 | + */ |
| 24 | +final class SourceLoader implements LoaderInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @param bool $onlyMissing keep what the registry already holds under the same name or URI |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + private readonly ElementSourceInterface $source, |
| 31 | + private readonly bool $onlyMissing = false, |
| 32 | + private readonly LoggerInterface $logger = new NullLogger(), |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public function load(RegistryInterface $registry): void |
| 37 | + { |
| 38 | + $collection = $this->source->provide(); |
| 39 | + |
| 40 | + foreach ($collection->getTools() as $reference) { |
| 41 | + if ($this->mayWrite('tool', $reference->tool->name, $registry->hasTool(...))) { |
| 42 | + $registry->registerTool($reference->tool, $reference->handler); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + foreach ($collection->getResources() as $reference) { |
| 47 | + if ($this->mayWrite('resource', $reference->resource->uri, $registry->hasResource(...))) { |
| 48 | + $registry->registerResource($reference->resource, $reference->handler); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + foreach ($collection->getResourceTemplates() as $reference) { |
| 53 | + if ($this->mayWrite('resource template', $reference->resourceTemplate->uriTemplate, $registry->hasResourceTemplate(...))) { |
| 54 | + $registry->registerResourceTemplate($reference->resourceTemplate, $reference->handler, $reference->completionProviders); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + foreach ($collection->getPrompts() as $reference) { |
| 59 | + if ($this->mayWrite('prompt', $reference->prompt->name, $registry->hasPrompt(...))) { |
| 60 | + $registry->registerPrompt($reference->prompt, $reference->handler, $reference->completionProviders); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param callable(string): bool $exists |
| 67 | + */ |
| 68 | + private function mayWrite(string $kind, string $key, callable $exists): bool |
| 69 | + { |
| 70 | + if (!$this->onlyMissing || !$exists($key)) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + $this->logger->debug(\sprintf('Ignoring %s "%s" from an element source: the registry already holds it.', $kind, $key)); |
| 75 | + |
| 76 | + return false; |
| 77 | + } |
| 78 | +} |
0 commit comments