Add possibility to set custom ServerCapabilities. (#95)
This commit is contained in:
parent
2dbd411194
commit
709a815cde
4 changed files with 68 additions and 1 deletions
|
|
@ -14,6 +14,7 @@ require_once dirname(__DIR__).'/bootstrap.php';
|
|||
chdir(__DIR__);
|
||||
|
||||
use Mcp\Example\StdioExplicitRegistration\SimpleHandlers;
|
||||
use Mcp\Schema\ServerCapabilities;
|
||||
use Mcp\Server;
|
||||
use Mcp\Server\Transport\StdioTransport;
|
||||
|
||||
|
|
@ -27,6 +28,17 @@ $server = Server::builder()
|
|||
->addResource([SimpleHandlers::class, 'getAppVersion'], 'app://version', 'application_version', mimeType: 'text/plain')
|
||||
->addPrompt([SimpleHandlers::class, 'greetingPrompt'], 'personalized_greeting')
|
||||
->addResourceTemplate([SimpleHandlers::class, 'getItemDetails'], 'item://{itemId}/details', 'get_item_details', mimeType: 'application/json')
|
||||
->setServerCapabilities(new ServerCapabilities(
|
||||
tools: true,
|
||||
toolsListChanged: false,
|
||||
resources: true,
|
||||
resourcesSubscribe: false,
|
||||
resourcesListChanged: false,
|
||||
prompts: true,
|
||||
promptsListChanged: false,
|
||||
logging: false,
|
||||
completions: false,
|
||||
))
|
||||
->build();
|
||||
|
||||
$transport = new StdioTransport(logger: logger());
|
||||
|
|
|
|||
|
|
@ -63,6 +63,8 @@ final class Registry implements ReferenceProviderInterface, ReferenceRegistryInt
|
|||
*/
|
||||
private array $resourceTemplates = [];
|
||||
|
||||
private ServerCapabilities $serverCapabilities;
|
||||
|
||||
public function __construct(
|
||||
private readonly ?EventDispatcherInterface $eventDispatcher = null,
|
||||
private readonly LoggerInterface $logger = new NullLogger(),
|
||||
|
|
@ -75,7 +77,7 @@ final class Registry implements ReferenceProviderInterface, ReferenceRegistryInt
|
|||
$this->logger->info('No capabilities registered on server.');
|
||||
}
|
||||
|
||||
return new ServerCapabilities(
|
||||
return $this->serverCapabilities ?? new ServerCapabilities(
|
||||
tools: [] !== $this->tools,
|
||||
toolsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
|
||||
resources: [] !== $this->resources || [] !== $this->resourceTemplates,
|
||||
|
|
@ -453,4 +455,9 @@ final class Registry implements ReferenceProviderInterface, ReferenceRegistryInt
|
|||
|
||||
return array_values(\array_slice($items, $offset, $limit));
|
||||
}
|
||||
|
||||
public function setServerCapabilities(ServerCapabilities $serverCapabilities): void
|
||||
{
|
||||
$this->serverCapabilities = $serverCapabilities;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -138,6 +138,8 @@ final class Builder
|
|||
*/
|
||||
private array $discoveryExcludeDirs = [];
|
||||
|
||||
private ?ServerCapabilities $serverCapabilities = null;
|
||||
|
||||
/**
|
||||
* Sets the server's identity. Required.
|
||||
*/
|
||||
|
|
@ -264,6 +266,13 @@ final class Builder
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function setServerCapabilities(ServerCapabilities $serverCapabilities): self
|
||||
{
|
||||
$this->serverCapabilities = $serverCapabilities;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually registers a tool handler.
|
||||
*
|
||||
|
|
@ -348,6 +357,9 @@ final class Builder
|
|||
$registry = new Registry($this->eventDispatcher, $logger);
|
||||
|
||||
$this->registerCapabilities($registry, $logger);
|
||||
if ($this->serverCapabilities) {
|
||||
$registry->setServerCapabilities($this->serverCapabilities);
|
||||
}
|
||||
|
||||
if (null !== $this->discoveryBasePath) {
|
||||
$this->performDiscovery($registry, $logger);
|
||||
|
|
|
|||
|
|
@ -80,6 +80,42 @@ class RegistryTest extends TestCase
|
|||
$this->assertFalse($capabilities->logging);
|
||||
}
|
||||
|
||||
public function testSetCustomCapabilities(): void
|
||||
{
|
||||
$serverCapabilities = new ServerCapabilities(
|
||||
tools: false,
|
||||
toolsListChanged: true,
|
||||
resources: false,
|
||||
resourcesSubscribe: false,
|
||||
resourcesListChanged: false,
|
||||
prompts: false,
|
||||
promptsListChanged: false,
|
||||
logging: true,
|
||||
completions: true,
|
||||
);
|
||||
$tool = $this->createValidTool('test_tool');
|
||||
$resource = $this->createValidResource('test://resource');
|
||||
$prompt = $this->createValidPrompt('test_prompt');
|
||||
$template = $this->createValidResourceTemplate('test://{id}');
|
||||
|
||||
$this->registry->registerTool($tool, fn () => 'result');
|
||||
$this->registry->registerResource($resource, fn () => 'content');
|
||||
$this->registry->registerPrompt($prompt, fn () => []);
|
||||
$this->registry->registerResourceTemplate($template, fn () => 'template');
|
||||
|
||||
$this->registry->setServerCapabilities($serverCapabilities);
|
||||
|
||||
$capabilities = $this->registry->getCapabilities();
|
||||
|
||||
$this->assertFalse($capabilities->tools);
|
||||
$this->assertFalse($capabilities->resources);
|
||||
$this->assertFalse($capabilities->prompts);
|
||||
$this->assertTrue($capabilities->completions);
|
||||
$this->assertFalse($capabilities->resourcesSubscribe);
|
||||
$this->assertTrue($capabilities->logging);
|
||||
$this->assertTrue($capabilities->toolsListChanged);
|
||||
}
|
||||
|
||||
public function testRegisterToolWithManualFlag(): void
|
||||
{
|
||||
$tool = $this->createValidTool('test_tool');
|
||||
|
|
|
|||
Loading…
Reference in a new issue