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

View file

@ -18,6 +18,7 @@ use Mcp\Capability\Registry\Loader\ArrayLoader;
use Mcp\Capability\Registry\Loader\DiscoveryLoader;
use Mcp\Capability\Registry\Loader\LoaderInterface;
use Mcp\Capability\Registry\ReferenceHandler;
use Mcp\Capability\RegistryInterface;
use Mcp\JsonRpc\MessageFactory;
use Mcp\Schema\Annotations;
use Mcp\Schema\Enum\ProtocolVersion;
@ -47,6 +48,8 @@ final class Builder
{
private ?Implementation $serverInfo = null;
private RegistryInterface $registry;
private ?LoggerInterface $logger = null;
private ?CacheInterface $discoveryCache = null;
@ -248,6 +251,13 @@ final class Builder
return $this;
}
public function setRegistry(RegistryInterface $registry): self
{
$this->registry = $registry;
return $this;
}
/**
* Provides a PSR-3 logger instance. Defaults to NullLogger.
*/
@ -440,7 +450,7 @@ final class Builder
{
$logger = $this->logger ?? new NullLogger();
$container = $this->container ?? new Container();
$registry = new Registry($this->eventDispatcher, $logger);
$registry = $this->registry ?? new Registry($this->eventDispatcher, $logger);
$loaders = [
...$this->loaders,
@ -455,16 +465,22 @@ final class Builder
$loader->load($registry);
}
if ($this->serverCapabilities) {
$registry->setServerCapabilities($this->serverCapabilities);
}
$sessionTtl = $this->sessionTtl ?? 3600;
$sessionFactory = $this->sessionFactory ?? new SessionFactory();
$sessionStore = $this->sessionStore ?? new InMemorySessionStore($sessionTtl);
$messageFactory = MessageFactory::make();
$capabilities = $registry->getCapabilities();
$capabilities = $this->serverCapabilities ?? new ServerCapabilities(
tools: $registry->hasTools(),
toolsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
resources: $registry->hasResources() || $registry->hasResourceTemplates(),
resourcesSubscribe: false,
resourcesListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
prompts: $registry->hasPrompts(),
promptsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
logging: false,
completions: true,
);
$configuration = new Configuration($this->serverInfo, $capabilities, $this->paginationLimit, $this->instructions, $this->protocolVersion);
$referenceHandler = new ReferenceHandler($container);