Rework registry to combine provider and registry interface, move capabilities out and enable Registry injection in Builder (#150)

This commit is contained in:
Christopher Hertel 2025-11-20 19:36:57 +01:00 • committed by GitHub
commit 7cd281d0c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 567 additions and 736 deletions

View file

@ -19,7 +19,7 @@ use Mcp\Capability\Discovery\DocBlockParser;
use Mcp\Capability\Discovery\HandlerResolver;
use Mcp\Capability\Discovery\SchemaGenerator;
use Mcp\Capability\Registry\ElementReference;
use Mcp\Capability\Registry\ReferenceRegistryInterface;
use Mcp\Capability\RegistryInterface;
use Mcp\Exception\ConfigurationException;
use Mcp\Schema\Annotations;
use Mcp\Schema\Icon;
@ -86,7 +86,7 @@ final class ArrayLoader implements LoaderInterface
) {
}
public function load(ReferenceRegistryInterface $registry): void
public function load(RegistryInterface $registry): void
{
$docBlockParser = new DocBlockParser(logger: $this->logger);
$schemaGenerator = new SchemaGenerator($docBlockParser);

View file

@ -13,7 +13,7 @@ namespace Mcp\Capability\Registry\Loader;
use Mcp\Capability\Discovery\CachedDiscoverer;
use Mcp\Capability\Discovery\Discoverer;
use Mcp\Capability\Registry\ReferenceRegistryInterface;
use Mcp\Capability\RegistryInterface;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
@ -35,7 +35,7 @@ final class DiscoveryLoader implements LoaderInterface
) {
}
public function load(ReferenceRegistryInterface $registry): void
public function load(RegistryInterface $registry): void
{
// This now encapsulates the discovery process
$discoverer = new Discoverer($this->logger);

View file

@ -11,12 +11,12 @@
namespace Mcp\Capability\Registry\Loader;
use Mcp\Capability\Registry\ReferenceRegistryInterface;
use Mcp\Capability\RegistryInterface;
/**
* @author Antoine Bluchet <soyuka@gmail.com>
*/
interface LoaderInterface
{
public function load(ReferenceRegistryInterface $registry): void;
public function load(RegistryInterface $registry): void;
}

View file

@ -11,6 +11,9 @@
namespace Mcp\Capability\Registry;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Exception\RegistryException;
/**
* Interface for handling execution of MCP elements.
* Allows custom implementations of element execution logic.
@ -27,8 +30,8 @@ interface ReferenceHandlerInterface
*
* @return mixed the result of the element execution
*
* @throws \Mcp\Exception\InvalidArgumentException if the handler is invalid
* @throws \Mcp\Exception\RegistryException if execution fails
* @throws InvalidArgumentException if the handler is invalid
* @throws RegistryException if execution fails
*/
public function handle(ElementReference $reference, array $arguments): mixed;
}

View file

@ -1,79 +0,0 @@
<?php
/*
* 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 Mcp\Capability\Registry;
use Mcp\Exception\PromptNotFoundException;
use Mcp\Exception\ResourceNotFoundException;
use Mcp\Exception\ToolNotFoundException;
use Mcp\Schema\Page;
/**
* Interface for providing access to registered MCP elements.
* Separates the concern of accessing elements from registering them.
*
* @author Pavel Buchnev <butschster@gmail.com>
*/
interface ReferenceProviderInterface
{
/**
* Gets a tool reference by name.
*
* @throws ToolNotFoundException
*/
public function getTool(string $name): ToolReference;
/**
* Gets a resource reference by URI (includes template matching if enabled).
*
* @throws ResourceNotFoundException
*/
public function getResource(string $uri, bool $includeTemplates = true): ResourceReference|ResourceTemplateReference;
/**
* Gets a resource template reference by URI template.
*
* @throws ResourceNotFoundException
*/
public function getResourceTemplate(string $uriTemplate): ResourceTemplateReference;
/**
* Gets a prompt reference by name.
*
* @throws PromptNotFoundException
*/
public function getPrompt(string $name): PromptReference;
/**
* Gets all registered tools.
*/
public function getTools(?int $limit = null, ?string $cursor = null): Page;
/**
* Gets all registered resources.
*/
public function getResources(?int $limit = null, ?string $cursor = null): Page;
/**
* Gets all registered prompts.
*/
public function getPrompts(?int $limit = null, ?string $cursor = null): Page;
/**
* Gets all registered resource templates.
*/
public function getResourceTemplates(?int $limit = null, ?string $cursor = null): Page;
/**
* Checks if any elements (manual or discovered) are currently registered.
*/
public function hasElements(): bool;
}

View file

@ -1,91 +0,0 @@
<?php
/*
* 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 Mcp\Capability\Registry;
use Mcp\Capability\Discovery\DiscoveryState;
use Mcp\Schema\Prompt;
use Mcp\Schema\Resource;
use Mcp\Schema\ResourceTemplate;
use Mcp\Schema\ServerCapabilities;
use Mcp\Schema\Tool;
/**
* @phpstan-import-type Handler from ElementReference
*
* Interface for registering MCP elements.
* Separates the concern of registering elements from accessing them.
*
* @author Pavel Buchnev <butschster@gmail.com>
*/
interface ReferenceRegistryInterface
{
/**
* Gets server capabilities based on registered elements.
*/
public function getCapabilities(): ServerCapabilities;
/**
* Registers a tool with its handler.
*
* @param Handler $handler
*/
public function registerTool(Tool $tool, callable|array|string $handler, bool $isManual = false): void;
/**
* Registers a resource with its handler.
*
* @param Handler $handler
*/
public function registerResource(Resource $resource, callable|array|string $handler, bool $isManual = false): void;
/**
* Registers a resource template with its handler and completion providers.
*
* @param Handler $handler
* @param array<string, class-string|object> $completionProviders
*/
public function registerResourceTemplate(
ResourceTemplate $template,
callable|array|string $handler,
array $completionProviders = [],
bool $isManual = false,
): void;
/**
* Registers a prompt with its handler and completion providers.
*
* @param Handler $handler
* @param array<string, class-string|object> $completionProviders
*/
public function registerPrompt(
Prompt $prompt,
callable|array|string $handler,
array $completionProviders = [],
bool $isManual = false,
): void;
/**
* Clear discovered elements from registry.
*/
public function clear(): void;
/**
* Get the current discovery state (only discovered elements, not manual ones).
*/
public function getDiscoveryState(): DiscoveryState;
/**
* Set discovery state, replacing all discovered elements.
* Manual elements are preserved.
*/
public function setDiscoveryState(DiscoveryState $state): void;
}