Skip to content

Commit ac7238d

Browse files
committed
Added possibility to set custom ServerCapabilities.
1 parent fcf605d commit ac7238d

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

examples/stdio-explicit-registration/server.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
chdir(__DIR__);
1515

1616
use Mcp\Example\StdioExplicitRegistration\SimpleHandlers;
17+
use Mcp\Schema\ServerCapabilities;
1718
use Mcp\Server;
1819
use Mcp\Server\Transport\StdioTransport;
1920

@@ -27,6 +28,17 @@
2728
->addResource([SimpleHandlers::class, 'getAppVersion'], 'app://version', 'application_version', mimeType: 'text/plain')
2829
->addPrompt([SimpleHandlers::class, 'greetingPrompt'], 'personalized_greeting')
2930
->addResourceTemplate([SimpleHandlers::class, 'getItemDetails'], 'item://{itemId}/details', 'get_item_details', mimeType: 'application/json')
31+
->setServerCapabilities(new ServerCapabilities(
32+
tools: true,
33+
toolsListChanged: false,
34+
resources: true,
35+
resourcesSubscribe: false,
36+
resourcesListChanged: false,
37+
prompts: true,
38+
promptsListChanged: false,
39+
logging: false,
40+
completions: false,
41+
))
3042
->build();
3143

3244
$transport = new StdioTransport(logger: logger());

src/Capability/Registry.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ final class Registry implements ReferenceProviderInterface, ReferenceRegistryInt
6363
*/
6464
private array $resourceTemplates = [];
6565

66+
private ServerCapabilities $serverCapabilities;
67+
6668
public function __construct(
6769
private readonly ?EventDispatcherInterface $eventDispatcher = null,
6870
private readonly LoggerInterface $logger = new NullLogger(),
@@ -74,18 +76,11 @@ public function getCapabilities(): ServerCapabilities
7476
if (!$this->hasElements()) {
7577
$this->logger->info('No capabilities registered on server.');
7678
}
79+
if (!isset($this->serverCapabilities)) {
80+
$this->setServerCapabilities(null);
81+
}
7782

78-
return new ServerCapabilities(
79-
tools: [] !== $this->tools,
80-
toolsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
81-
resources: [] !== $this->resources || [] !== $this->resourceTemplates,
82-
resourcesSubscribe: false,
83-
resourcesListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
84-
prompts: [] !== $this->prompts,
85-
promptsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
86-
logging: false,
87-
completions: true,
88-
);
83+
return $this->serverCapabilities;
8984
}
9085

9186
public function registerTool(Tool $tool, callable|array|string $handler, bool $isManual = false): void
@@ -453,4 +448,23 @@ private function paginateResults(array $items, int $limit, ?string $cursor = nul
453448

454449
return array_values(\array_slice($items, $offset, $limit));
455450
}
451+
452+
public function setServerCapabilities(?ServerCapabilities $serverCapabilities): void
453+
{
454+
if ($serverCapabilities) {
455+
$this->serverCapabilities = $serverCapabilities;
456+
} else {
457+
$this->serverCapabilities = new ServerCapabilities(
458+
tools: [] !== $this->tools,
459+
toolsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
460+
resources: [] !== $this->resources || [] !== $this->resourceTemplates,
461+
resourcesSubscribe: false,
462+
resourcesListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
463+
prompts: [] !== $this->prompts,
464+
promptsListChanged: $this->eventDispatcher instanceof EventDispatcherInterface,
465+
logging: false,
466+
completions: true,
467+
);
468+
}
469+
}
456470
}

src/Server/Builder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ final class Builder
138138
*/
139139
private array $discoveryExcludeDirs = [];
140140

141+
private ?ServerCapabilities $serverCapabilities = null;
142+
141143
/**
142144
* Sets the server's identity. Required.
143145
*/
@@ -264,6 +266,13 @@ public function setDiscovery(
264266
return $this;
265267
}
266268

269+
public function setServerCapabilities(ServerCapabilities $serverCapabilities): self
270+
{
271+
$this->serverCapabilities = $serverCapabilities;
272+
273+
return $this;
274+
}
275+
267276
/**
268277
* Manually registers a tool handler.
269278
*
@@ -348,6 +357,7 @@ public function build(): Server
348357
$registry = new Registry($this->eventDispatcher, $logger);
349358

350359
$this->registerCapabilities($registry, $logger);
360+
$registry->setServerCapabilities($this->serverCapabilities);
351361

352362
if (null !== $this->discoveryBasePath) {
353363
$this->performDiscovery($registry, $logger);

tests/Unit/Capability/Registry/RegistryTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,42 @@ public function testGetCapabilitiesWhenPopulated(): void
8080
$this->assertFalse($capabilities->logging);
8181
}
8282

83+
public function testSetCustomCapabilities(): void
84+
{
85+
$serverCapabilities = new ServerCapabilities(
86+
tools: false,
87+
toolsListChanged: true,
88+
resources: false,
89+
resourcesSubscribe: false,
90+
resourcesListChanged: false,
91+
prompts: false,
92+
promptsListChanged: false,
93+
logging: true,
94+
completions: true,
95+
);
96+
$tool = $this->createValidTool('test_tool');
97+
$resource = $this->createValidResource('test://resource');
98+
$prompt = $this->createValidPrompt('test_prompt');
99+
$template = $this->createValidResourceTemplate('test://{id}');
100+
101+
$this->registry->registerTool($tool, fn () => 'result');
102+
$this->registry->registerResource($resource, fn () => 'content');
103+
$this->registry->registerPrompt($prompt, fn () => []);
104+
$this->registry->registerResourceTemplate($template, fn () => 'template');
105+
106+
$this->registry->setServerCapabilities($serverCapabilities);
107+
108+
$capabilities = $this->registry->getCapabilities();
109+
110+
$this->assertFalse($capabilities->tools);
111+
$this->assertFalse($capabilities->resources);
112+
$this->assertFalse($capabilities->prompts);
113+
$this->assertTrue($capabilities->completions);
114+
$this->assertFalse($capabilities->resourcesSubscribe);
115+
$this->assertTrue($capabilities->logging);
116+
$this->assertTrue($capabilities->toolsListChanged);
117+
}
118+
83119
public function testRegisterToolWithManualFlag(): void
84120
{
85121
$tool = $this->createValidTool('test_tool');

0 commit comments

Comments
 (0)