Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/Capability/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

use Mcp\Capability\Discovery\DiscoveryState;
use Mcp\Capability\Registry\PromptReference;
use Mcp\Capability\Registry\ReferenceProviderInterface;
use Mcp\Capability\Registry\ReferenceRegistryInterface;
use Mcp\Capability\Registry\ResourceReference;
use Mcp\Capability\Registry\ResourceTemplateReference;
use Mcp\Capability\Registry\ToolReference;
Expand Down Expand Up @@ -44,7 +42,7 @@
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
* @author Pavel Buchnev <butschster@gmail.com>
*/
final class Registry implements ReferenceProviderInterface, ReferenceRegistryInterface
final class Registry implements RegistryInterface
{
/**
* @var array<string, ToolReference>
Expand Down Expand Up @@ -93,6 +91,11 @@ public function getCapabilities(): ServerCapabilities
);
}

public function setServerCapabilities(ServerCapabilities $serverCapabilities): void
{
$this->serverCapabilities = $serverCapabilities;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be even cleaner to have the ServerCapabilities directly in dependency injection.

return $this->serverCapabilities ?? new ServerCapabilities(
            tools: [] !== $this->tools,
            toolsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
            resources: [] !== $this->resources || [] !== $this->resourceTemplates,
            resourcesSubscribe: false,
            resourcesListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
            prompts: [] !== $this->prompts,
            promptsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
            logging: false,
            completions: true,
        );

This would allow more flexibility in my opinion. Loaders could then prepare tools, resources etc. without having to register them at runtime. In the end, they could more easily be cache-able. I'd still keep register methods for runtime changes.

public function registerTool(Tool $tool, callable|array|string $handler, bool $isManual = false): void
{
$toolName = $tool->name;
Expand Down Expand Up @@ -456,9 +459,4 @@ private function paginateResults(array $items, int $limit, ?string $cursor = nul

return array_values(\array_slice($items, $offset, $limit));
}

public function setServerCapabilities(ServerCapabilities $serverCapabilities): void
{
$this->serverCapabilities = $serverCapabilities;
}
}
7 changes: 5 additions & 2 deletions src/Capability/Registry/ReferenceHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
23 changes: 23 additions & 0 deletions src/Capability/RegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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;

use Mcp\Capability\Registry\ReferenceProviderInterface;
use Mcp\Capability\Registry\ReferenceRegistryInterface;
use Mcp\Schema\ServerCapabilities;

interface RegistryInterface extends ReferenceRegistryInterface, ReferenceProviderInterface
{
public function getCapabilities(): ServerCapabilities;

public function setServerCapabilities(ServerCapabilities $serverCapabilities): void;
Comment on lines +20 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function getCapabilities(): ServerCapabilities;
public function setServerCapabilities(ServerCapabilities $serverCapabilities): void;

}
12 changes: 11 additions & 1 deletion src/Server/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -47,6 +48,8 @@ final class Builder
{
private ?Implementation $serverInfo = null;

private RegistryInterface $registry;

private ?LoggerInterface $logger = null;

private ?CacheInterface $discoveryCache = null;
Expand Down Expand Up @@ -245,6 +248,13 @@ public function addNotificationHandlers(iterable $handlers): self
return $this;
}

public function setRegistry(RegistryInterface $registry): self
{
$this->registry = $registry;

return $this;
}

/**
* Provides a PSR-3 logger instance. Defaults to NullLogger.
*/
Expand Down Expand Up @@ -401,7 +411,7 @@ public function build(): Server
{
$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,
Expand Down