Skip to content

Commit 657e7a6

Browse files
author
klapaudius
committed
#21 Add support for MCP resources in server capabilities
Implemented the `supportsResources` feature in `ServerCapabilities` to enable resource registration and exposure to clients. Updated related methods, serialization logic, and tests to account for resources configuration. Extended `MCPServer` to integrate resources functionality seamlessly.
1 parent 656f39f commit 657e7a6

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/Server/MCPServer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function registerToolRepository(ToolRepository $toolRepository): self
122122
{
123123
$this->registerRequestHandler(new ToolsListHandler($toolRepository));
124124
$this->registerRequestHandler(new ToolsCallHandler($toolRepository));
125-
$this->capabilities = (new ServerCapabilities)->withTools($toolRepository->getToolSchemas());
125+
$this->capabilities->withTools($toolRepository->getToolSchemas());
126126

127127
return $this;
128128
}
@@ -131,6 +131,7 @@ public function registerResourceRepository(ResourceRepository $resourceRepositor
131131
{
132132
$this->registerRequestHandler(new ResourcesListHandler($resourceRepository));
133133
$this->registerRequestHandler(new ResourcesReadHandler($resourceRepository));
134+
$this->capabilities->withResources($resourceRepository->getResourceSchemas());
134135

135136
return $this;
136137
}

src/Server/ServerCapabilities.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ final class ServerCapabilities implements ServerCapabilitiesInterface
2020
*/
2121
private bool $supportsTools = false;
2222

23+
/**
24+
* Indicates whether the server supports the MCP resources feature.
25+
* If true, the server can register and expose resources to the client.
26+
*
27+
* @see https://modelcontextprotocol.io/docs/concepts/resources
28+
*/
29+
private bool $supportsResources = false;
30+
2331
/**
2432
* Optional configuration specific to the tools capability.
2533
* This structure can be defined by the specific server implementation
@@ -28,6 +36,12 @@ final class ServerCapabilities implements ServerCapabilitiesInterface
2836
*/
2937
private ?array $toolsConfig = null;
3038

39+
/**
40+
* Optional configuration specific to the resources capability.
41+
* This structure can be defined by the specific server implementation
42+
*/
43+
private ?array $resourcesConfig = null;
44+
3145
/**
3246
* Enables the tools capability for the server instance.
3347
* Allows specifying optional configuration details for the tools feature.
@@ -46,6 +60,24 @@ public function withTools(?array $config = []): self
4660
return $this;
4761
}
4862

63+
/**
64+
* Enables the tools capability for the server instance.
65+
* Allows specifying optional configuration details for the tools feature.
66+
*
67+
* @param array|null $config Optional configuration data specific to the tools capability.
68+
* Defaults to an empty array if not provided.
69+
* @return self Returns the instance for method chaining.
70+
*
71+
* @see https://modelcontextprotocol.io/docs/concepts/tools
72+
*/
73+
public function withResources(?array $config = []): self
74+
{
75+
$this->supportsResources = true;
76+
$this->resourcesConfig = $config;
77+
78+
return $this;
79+
}
80+
4981
/**
5082
* Converts the server capabilities configuration into an array format suitable for JSON serialization.
5183
* Only includes capabilities that are actively enabled.
@@ -57,7 +89,7 @@ public function toArray(): array
5789
{
5890
$capabilities = [
5991
'prompts' => new stdClass,
60-
'resources' => new stdClass,
92+
'resources' => $this->supportsResources ? $this->resourcesConfig : new stdClass,
6193
];
6294

6395
if ($this->supportsTools) {
@@ -83,6 +115,9 @@ public function toInitializeMessage(): array
83115
if ($this->supportsTools) {
84116
$capabilities['tools']->listChanged = true;
85117
}
118+
if ($this->supportsResources) {
119+
$capabilities['resources']->listChanged = true;
120+
}
86121

87122
return $capabilities;
88123
}

tests/Server/MCPServerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use KLP\KlpMcpServer\Server\MCPServer;
1313
use KLP\KlpMcpServer\Server\Request\ToolsCallHandler;
1414
use KLP\KlpMcpServer\Server\Request\ToolsListHandler;
15+
use KLP\KlpMcpServer\Server\ServerCapabilities;
1516
use KLP\KlpMcpServer\Server\ServerCapabilitiesInterface;
1617
use KLP\KlpMcpServer\Services\ToolService\ToolRepository;
1718
use PHPUnit\Framework\Attributes\Small;
@@ -41,6 +42,7 @@ public function test_register_tool_repository(): void
4142

4243
$server = new ReflectionClass(MCPServer::class);
4344
$instance = $server->newInstanceWithoutConstructor();
45+
$server->getProperty('capabilities')->setValue($instance, new ServerCapabilities);
4446
$server->getProperty('protocol')->setValue($instance, $mockProtocol);
4547

4648
// Act
@@ -57,6 +59,7 @@ public function test_register_tool_repository_returns_instance(): void
5759

5860
$server = new ReflectionClass(MCPServer::class);
5961
$instance = $server->newInstanceWithoutConstructor();
62+
$server->getProperty('capabilities')->setValue($instance, new ServerCapabilities);
6063
$server->getProperty('protocol')->setValue($instance, $mockProtocol);
6164

6265
// Act

0 commit comments

Comments
 (0)