1313
1414use Mcp \Capability \Discovery \DiscoveryState ;
1515use Mcp \Capability \Registry \PromptReference ;
16- use Mcp \Capability \Registry \ReferenceProviderInterface ;
17- use Mcp \Capability \Registry \ReferenceRegistryInterface ;
1816use Mcp \Capability \Registry \ResourceReference ;
1917use Mcp \Capability \Registry \ResourceTemplateReference ;
2018use Mcp \Capability \Registry \ToolReference ;
3129use Mcp \Schema \Prompt ;
3230use Mcp \Schema \Resource ;
3331use Mcp \Schema \ResourceTemplate ;
34- use Mcp \Schema \ServerCapabilities ;
3532use Mcp \Schema \Tool ;
3633use Psr \EventDispatcher \EventDispatcherInterface ;
3734use Psr \Log \LoggerInterface ;
3835use Psr \Log \NullLogger ;
3936
4037/**
4138 * Registry implementation that manages MCP element registration and access.
42- * Implements both ReferenceProvider (for access) and ReferenceRegistry (for registration)
43- * following the Interface Segregation Principle.
4439 *
4540 * @author Kyrian Obikwelu <koshnawaza@gmail.com>
46- * @author Pavel Buchnev <butschster@gmail.com>
4741 */
48- final class Registry implements ReferenceProviderInterface, ReferenceRegistryInterface
42+ final class Registry implements RegistryInterface
4943{
5044 /**
5145 * @var array<string, ToolReference>
@@ -67,34 +61,13 @@ final class Registry implements ReferenceProviderInterface, ReferenceRegistryInt
6761 */
6862 private array $ resourceTemplates = [];
6963
70- private ServerCapabilities $ serverCapabilities ;
71-
7264 public function __construct (
7365 private readonly ?EventDispatcherInterface $ eventDispatcher = null ,
7466 private readonly LoggerInterface $ logger = new NullLogger (),
7567 private readonly NameValidator $ nameValidator = new NameValidator (),
7668 ) {
7769 }
7870
79- public function getCapabilities (): ServerCapabilities
80- {
81- if (!$ this ->hasElements ()) {
82- $ this ->logger ->info ('No capabilities registered on server. ' );
83- }
84-
85- return $ this ->serverCapabilities ?? new ServerCapabilities (
86- tools: [] !== $ this ->tools ,
87- toolsListChanged: $ this ->eventDispatcher instanceof EventDispatcherInterface,
88- resources: [] !== $ this ->resources || [] !== $ this ->resourceTemplates ,
89- resourcesSubscribe: false ,
90- resourcesListChanged: $ this ->eventDispatcher instanceof EventDispatcherInterface,
91- prompts: [] !== $ this ->prompts ,
92- promptsListChanged: $ this ->eventDispatcher instanceof EventDispatcherInterface,
93- logging: false ,
94- completions: true ,
95- );
96- }
97-
9871 public function registerTool (Tool $ tool , callable |array |string $ handler , bool $ isManual = false ): void
9972 {
10073 $ toolName = $ tool ->name ;
@@ -220,41 +193,9 @@ public function clear(): void
220193 }
221194 }
222195
223- public function getTool (string $ name ): ToolReference
224- {
225- return $ this ->tools [$ name ] ?? throw new ToolNotFoundException ($ name );
226- }
227-
228- public function getResource (
229- string $ uri ,
230- bool $ includeTemplates = true ,
231- ): ResourceReference |ResourceTemplateReference {
232- $ registration = $ this ->resources [$ uri ] ?? null ;
233- if ($ registration ) {
234- return $ registration ;
235- }
236-
237- if ($ includeTemplates ) {
238- foreach ($ this ->resourceTemplates as $ template ) {
239- if ($ template ->matches ($ uri )) {
240- return $ template ;
241- }
242- }
243- }
244-
245- $ this ->logger ->debug ('No resource matched URI. ' , ['uri ' => $ uri ]);
246-
247- throw new ResourceNotFoundException ($ uri );
248- }
249-
250- public function getResourceTemplate (string $ uriTemplate ): ResourceTemplateReference
196+ public function hasTools (): bool
251197 {
252- return $ this ->resourceTemplates [$ uriTemplate ] ?? throw new ResourceNotFoundException ($ uriTemplate );
253- }
254-
255- public function getPrompt (string $ name ): PromptReference
256- {
257- return $ this ->prompts [$ name ] ?? throw new PromptNotFoundException ($ name );
198+ return [] !== $ this ->tools ;
258199 }
259200
260201 public function getTools (?int $ limit = null , ?string $ cursor = null ): Page
@@ -279,6 +220,16 @@ public function getTools(?int $limit = null, ?string $cursor = null): Page
279220 return new Page ($ paginatedTools , $ nextCursor );
280221 }
281222
223+ public function getTool (string $ name ): ToolReference
224+ {
225+ return $ this ->tools [$ name ] ?? throw new ToolNotFoundException ($ name );
226+ }
227+
228+ public function hasResources (): bool
229+ {
230+ return [] !== $ this ->resources ;
231+ }
232+
282233 public function getResources (?int $ limit = null , ?string $ cursor = null ): Page
283234 {
284235 $ resources = [];
@@ -301,26 +252,31 @@ public function getResources(?int $limit = null, ?string $cursor = null): Page
301252 return new Page ($ paginatedResources , $ nextCursor );
302253 }
303254
304- public function getPrompts (?int $ limit = null , ?string $ cursor = null ): Page
305- {
306- $ prompts = [];
307- foreach ($ this ->prompts as $ promptReference ) {
308- $ prompts [$ promptReference ->prompt ->name ] = $ promptReference ->prompt ;
255+ public function getResource (
256+ string $ uri ,
257+ bool $ includeTemplates = true ,
258+ ): ResourceReference |ResourceTemplateReference {
259+ $ registration = $ this ->resources [$ uri ] ?? null ;
260+ if ($ registration ) {
261+ return $ registration ;
309262 }
310263
311- if (null === $ limit ) {
312- return new Page ($ prompts , null );
264+ if ($ includeTemplates ) {
265+ foreach ($ this ->resourceTemplates as $ template ) {
266+ if ($ template ->matches ($ uri )) {
267+ return $ template ;
268+ }
269+ }
313270 }
314271
315- $ paginatedPrompts = $ this ->paginateResults ( $ prompts , $ limit , $ cursor );
272+ $ this ->logger -> debug ( ' No resource matched URI. ' , [ ' uri ' => $ uri ] );
316273
317- $ nextCursor = $ this ->calculateNextCursor (
318- \count ($ prompts ),
319- $ cursor ,
320- $ limit
321- );
274+ throw new ResourceNotFoundException ($ uri );
275+ }
322276
323- return new Page ($ paginatedPrompts , $ nextCursor );
277+ public function hasResourceTemplates (): bool
278+ {
279+ return [] !== $ this ->resourceTemplates ;
324280 }
325281
326282 public function getResourceTemplates (?int $ limit = null , ?string $ cursor = null ): Page
@@ -345,12 +301,41 @@ public function getResourceTemplates(?int $limit = null, ?string $cursor = null)
345301 return new Page ($ paginatedTemplates , $ nextCursor );
346302 }
347303
348- public function hasElements ( ): bool
304+ public function getResourceTemplate ( string $ uriTemplate ): ResourceTemplateReference
349305 {
350- return !empty ($ this ->tools )
351- || !empty ($ this ->resources )
352- || !empty ($ this ->prompts )
353- || !empty ($ this ->resourceTemplates );
306+ return $ this ->resourceTemplates [$ uriTemplate ] ?? throw new ResourceNotFoundException ($ uriTemplate );
307+ }
308+
309+ public function hasPrompts (): bool
310+ {
311+ return [] !== $ this ->prompts ;
312+ }
313+
314+ public function getPrompts (?int $ limit = null , ?string $ cursor = null ): Page
315+ {
316+ $ prompts = [];
317+ foreach ($ this ->prompts as $ promptReference ) {
318+ $ prompts [$ promptReference ->prompt ->name ] = $ promptReference ->prompt ;
319+ }
320+
321+ if (null === $ limit ) {
322+ return new Page ($ prompts , null );
323+ }
324+
325+ $ paginatedPrompts = $ this ->paginateResults ($ prompts , $ limit , $ cursor );
326+
327+ $ nextCursor = $ this ->calculateNextCursor (
328+ \count ($ prompts ),
329+ $ cursor ,
330+ $ limit
331+ );
332+
333+ return new Page ($ paginatedPrompts , $ nextCursor );
334+ }
335+
336+ public function getPrompt (string $ name ): PromptReference
337+ {
338+ return $ this ->prompts [$ name ] ?? throw new PromptNotFoundException ($ name );
354339 }
355340
356341 /**
@@ -464,9 +449,4 @@ private function paginateResults(array $items, int $limit, ?string $cursor = nul
464449
465450 return array_values (\array_slice ($ items , $ offset , $ limit ));
466451 }
467-
468- public function setServerCapabilities (ServerCapabilities $ serverCapabilities ): void
469- {
470- $ this ->serverCapabilities = $ serverCapabilities ;
471- }
472452}
0 commit comments