Skip to content

Commit 46648bd

Browse files
committed
[Capability] Split RegistryInterface by role
Refs #495
1 parent 5701e69 commit 46648bd

16 files changed

Lines changed: 231 additions & 153 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
All notable changes to `mcp/sdk` will be documented in this file.
44

5+
0.11.0
6+
------
7+
8+
* Split `RegistryInterface` by role into `ElementCatalogInterface`, `ElementResolverInterface` and `ElementWriterInterface`, which it extends. Request handlers depend on the role they use, so a handler can run against a catalog or resolver alone.
9+
510
0.10.0
611
------
712

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Server\Capability;
13+
14+
use Mcp\Schema\Page;
15+
use Mcp\Schema\Prompt;
16+
use Mcp\Schema\ResourceDefinition;
17+
use Mcp\Schema\ResourceTemplate;
18+
use Mcp\Schema\Tool;
19+
20+
/**
21+
* Lists the elements a server offers.
22+
*
23+
* @author Christopher Hertel <mail@christopher-hertel.de>
24+
*/
25+
interface ElementCatalogInterface
26+
{
27+
/**
28+
* Gets all registered tools.
29+
*
30+
* @return Page<Tool>
31+
*/
32+
public function getTools(?int $limit = null, ?string $cursor = null): Page;
33+
34+
/**
35+
* Gets all registered resources.
36+
*
37+
* @return Page<ResourceDefinition>
38+
*/
39+
public function getResources(?int $limit = null, ?string $cursor = null): Page;
40+
41+
/**
42+
* Gets all registered resource templates.
43+
*
44+
* @return Page<ResourceTemplate>
45+
*/
46+
public function getResourceTemplates(?int $limit = null, ?string $cursor = null): Page;
47+
48+
/**
49+
* Gets all registered prompts.
50+
*
51+
* @return Page<Prompt>
52+
*/
53+
public function getPrompts(?int $limit = null, ?string $cursor = null): Page;
54+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Server\Capability;
13+
14+
use Mcp\Server\Capability\Registry\PromptReference;
15+
use Mcp\Server\Capability\Registry\ResourceReference;
16+
use Mcp\Server\Capability\Registry\ResourceTemplateReference;
17+
use Mcp\Server\Capability\Registry\ToolReference;
18+
use Mcp\Server\Exception\ConfigurationException;
19+
use Mcp\Server\Exception\PromptNotFoundException;
20+
use Mcp\Server\Exception\ResourceNotFoundException;
21+
use Mcp\Server\Exception\ToolNotFoundException;
22+
23+
/**
24+
* Finds a single element by its name or URI, without needing the whole catalog.
25+
*
26+
* @author Christopher Hertel <mail@christopher-hertel.de>
27+
*/
28+
interface ElementResolverInterface
29+
{
30+
/**
31+
* Gets a tool reference by name.
32+
*
33+
* @throws ToolNotFoundException
34+
* @throws ConfigurationException when the elements fail to load
35+
*/
36+
public function getTool(string $name): ToolReference;
37+
38+
/**
39+
* Gets a resource reference by URI (includes template matching if enabled).
40+
*
41+
* @throws ResourceNotFoundException
42+
*/
43+
public function getResource(string $uri, bool $includeTemplates = true): ResourceReference|ResourceTemplateReference;
44+
45+
/**
46+
* Gets a prompt reference by name.
47+
*
48+
* @throws PromptNotFoundException
49+
*/
50+
public function getPrompt(string $name): PromptReference;
51+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Server\Capability;
13+
14+
use Mcp\Schema\Prompt;
15+
use Mcp\Schema\ResourceDefinition;
16+
use Mcp\Schema\ResourceTemplate;
17+
use Mcp\Schema\Tool;
18+
use Mcp\Server\Capability\Registry\ElementReference;
19+
use Mcp\Server\Capability\Registry\PromptReference;
20+
use Mcp\Server\Capability\Registry\ResourceReference;
21+
use Mcp\Server\Capability\Registry\ResourceTemplateReference;
22+
use Mcp\Server\Capability\Registry\ToolReference;
23+
24+
/**
25+
* Adds and removes elements.
26+
*
27+
* @phpstan-import-type Handler from ElementReference
28+
*
29+
* @author Christopher Hertel <mail@christopher-hertel.de>
30+
*/
31+
interface ElementWriterInterface
32+
{
33+
/**
34+
* Registers a tool with its handler. Overwrites any prior registration of the same name.
35+
* Returns the stored reference, whose identity callers may track to detect later overwrites.
36+
*
37+
* @param Handler $handler
38+
*/
39+
public function registerTool(Tool $tool, callable|array|string $handler): ToolReference;
40+
41+
/**
42+
* Registers a resource with its handler. Overwrites any prior registration of the same URI.
43+
* Returns the stored reference, whose identity callers may track to detect later overwrites.
44+
*
45+
* @param Handler $handler
46+
*/
47+
public function registerResource(ResourceDefinition $resource, callable|array|string $handler): ResourceReference;
48+
49+
/**
50+
* Registers a resource template with its handler and completion providers.
51+
* Overwrites any prior registration of the same URI template.
52+
* Returns the stored reference, whose identity callers may track to detect later overwrites.
53+
*
54+
* @param Handler $handler
55+
* @param array<string, class-string|object> $completionProviders
56+
*/
57+
public function registerResourceTemplate(
58+
ResourceTemplate $template,
59+
callable|array|string $handler,
60+
array $completionProviders = [],
61+
): ResourceTemplateReference;
62+
63+
/**
64+
* Registers a prompt with its handler and completion providers.
65+
* Overwrites any prior registration of the same name.
66+
* Returns the stored reference, whose identity callers may track to detect later overwrites.
67+
*
68+
* @param Handler $handler
69+
* @param array<string, class-string|object> $completionProviders
70+
*/
71+
public function registerPrompt(
72+
Prompt $prompt,
73+
callable|array|string $handler,
74+
array $completionProviders = [],
75+
): PromptReference;
76+
77+
/**
78+
* Removes a tool by name. No-op if absent.
79+
*/
80+
public function unregisterTool(string $name): void;
81+
82+
/**
83+
* Removes a resource by URI. No-op if absent.
84+
*/
85+
public function unregisterResource(string $uri): void;
86+
87+
/**
88+
* Removes a resource template by URI template. No-op if absent.
89+
*/
90+
public function unregisterResourceTemplate(string $uriTemplate): void;
91+
92+
/**
93+
* Removes a prompt by name. No-op if absent.
94+
*/
95+
public function unregisterPrompt(string $name): void;
96+
}

‎src/Server/Capability/RegistryInterface.php‎

Lines changed: 3 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -11,94 +11,16 @@
1111

1212
namespace Mcp\Server\Capability;
1313

14-
use Mcp\Schema\Page;
15-
use Mcp\Schema\Prompt;
16-
use Mcp\Schema\ResourceDefinition;
17-
use Mcp\Schema\ResourceTemplate;
18-
use Mcp\Schema\Tool;
19-
use Mcp\Server\Capability\Registry\ElementReference;
20-
use Mcp\Server\Capability\Registry\PromptReference;
21-
use Mcp\Server\Capability\Registry\ResourceReference;
2214
use Mcp\Server\Capability\Registry\ResourceTemplateReference;
23-
use Mcp\Server\Capability\Registry\ToolReference;
24-
use Mcp\Server\Exception\ConfigurationException;
25-
use Mcp\Server\Exception\PromptNotFoundException;
2615
use Mcp\Server\Exception\ResourceNotFoundException;
27-
use Mcp\Server\Exception\ToolNotFoundException;
2816
use Mcp\Server\Subscription\PublishingEventDispatcher;
2917

3018
/**
31-
* @phpstan-import-type Handler from ElementReference
32-
*
3319
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
3420
* @author Christopher Hertel <mail@christopher-hertel.de>
3521
*/
36-
interface RegistryInterface
22+
interface RegistryInterface extends ElementCatalogInterface, ElementResolverInterface, ElementWriterInterface
3723
{
38-
/**
39-
* Registers a tool with its handler. Overwrites any prior registration of the same name.
40-
* Returns the stored reference, whose identity callers may track to detect later overwrites.
41-
*
42-
* @param Handler $handler
43-
*/
44-
public function registerTool(Tool $tool, callable|array|string $handler): ToolReference;
45-
46-
/**
47-
* Registers a resource with its handler. Overwrites any prior registration of the same URI.
48-
* Returns the stored reference, whose identity callers may track to detect later overwrites.
49-
*
50-
* @param Handler $handler
51-
*/
52-
public function registerResource(ResourceDefinition $resource, callable|array|string $handler): ResourceReference;
53-
54-
/**
55-
* Registers a resource template with its handler and completion providers.
56-
* Overwrites any prior registration of the same URI template.
57-
* Returns the stored reference, whose identity callers may track to detect later overwrites.
58-
*
59-
* @param Handler $handler
60-
* @param array<string, class-string|object> $completionProviders
61-
*/
62-
public function registerResourceTemplate(
63-
ResourceTemplate $template,
64-
callable|array|string $handler,
65-
array $completionProviders = [],
66-
): ResourceTemplateReference;
67-
68-
/**
69-
* Registers a prompt with its handler and completion providers.
70-
* Overwrites any prior registration of the same name.
71-
* Returns the stored reference, whose identity callers may track to detect later overwrites.
72-
*
73-
* @param Handler $handler
74-
* @param array<string, class-string|object> $completionProviders
75-
*/
76-
public function registerPrompt(
77-
Prompt $prompt,
78-
callable|array|string $handler,
79-
array $completionProviders = [],
80-
): PromptReference;
81-
82-
/**
83-
* Removes a tool by name. No-op if absent.
84-
*/
85-
public function unregisterTool(string $name): void;
86-
87-
/**
88-
* Removes a resource by URI. No-op if absent.
89-
*/
90-
public function unregisterResource(string $uri): void;
91-
92-
/**
93-
* Removes a resource template by URI template. No-op if absent.
94-
*/
95-
public function unregisterResourceTemplate(string $uriTemplate): void;
96-
97-
/**
98-
* Removes a prompt by name. No-op if absent.
99-
*/
100-
public function unregisterPrompt(string $name): void;
101-
10224
/**
10325
* Whether list changes reach a notification bus, e.g. through a {@see PublishingEventDispatcher}.
10426
* A decorator forwards the answer of the registry it wraps.
@@ -118,75 +40,25 @@ public function hasPrompt(string $name): bool;
11840
*/
11941
public function hasTools(): bool;
12042

121-
/**
122-
* Gets all registered tools.
123-
*
124-
* @return Page<Tool>
125-
*/
126-
public function getTools(?int $limit = null, ?string $cursor = null): Page;
127-
128-
/**
129-
* Gets a tool reference by name.
130-
*
131-
* @throws ToolNotFoundException
132-
* @throws ConfigurationException when the elements fail to load
133-
*/
134-
public function getTool(string $name): ToolReference;
135-
13643
/**
13744
* @return bool true if any resources are registered
13845
*/
13946
public function hasResources(): bool;
14047

141-
/**
142-
* Gets all registered resources.
143-
*
144-
* @return Page<ResourceDefinition>
145-
*/
146-
public function getResources(?int $limit = null, ?string $cursor = null): Page;
147-
148-
/**
149-
* Gets a resource reference by URI (includes template matching if enabled).
150-
*
151-
* @throws ResourceNotFoundException
152-
*/
153-
public function getResource(string $uri, bool $includeTemplates = true): ResourceReference|ResourceTemplateReference;
154-
15548
/**
15649
* @return bool true if any resource templates are registered
15750
*/
15851
public function hasResourceTemplates(): bool;
15952

16053
/**
161-
* Gets all registered resource templates.
162-
*
163-
* @return Page<ResourceTemplate>
54+
* @return bool true if any prompts are registered
16455
*/
165-
public function getResourceTemplates(?int $limit = null, ?string $cursor = null): Page;
56+
public function hasPrompts(): bool;
16657

16758
/**
16859
* Gets a resource template reference by URI template.
16960
*
17061
* @throws ResourceNotFoundException
17162
*/
17263
public function getResourceTemplate(string $uriTemplate): ResourceTemplateReference;
173-
174-
/**
175-
* @return bool true if any prompts are registered
176-
*/
177-
public function hasPrompts(): bool;
178-
179-
/**
180-
* Gets all registered prompts.
181-
*
182-
* @return Page<Prompt>
183-
*/
184-
public function getPrompts(?int $limit = null, ?string $cursor = null): Page;
185-
186-
/**
187-
* Gets a prompt reference by name.
188-
*
189-
* @throws PromptNotFoundException
190-
*/
191-
public function getPrompt(string $name): PromptReference;
19264
}

0 commit comments

Comments
 (0)