* refactor: Separate Registry concerns following SOLID principles [WIP] - Extract ReferenceProvider and ReferenceRegistryInterface interfaces - Create DefaultToolExecutor with ReferenceHandlerInterface - Remove execution responsibility from Registry class - Enable custom handler and executor implementations * refactor: implement SOLID principles with separated execution concerns * Create DefaultResourceReader, DefaultPromptGetter * Refactor JsonRpc Handler and RequestHandlers to use dedicated executors * Update ServerBuilder to support custom executors via dependency injection * chore: remove extra comments * feat: introduce DispatchableRegistry for enhanced tool and resource management * refactor: use proper interfaces * refactor: implement HandlerInterface for improved abstraction and flexibility * test: add unit tests for DispatchableRegistry and Registry classes * refactor: cover with unit tests Resource reader, Prompt getter and Tool executor * cs fix * phpstan fix * test: add unit tests for CallToolHandler, GetPromptHandler, PingHandler, and ReadResourceHandler * refactor: revert Handler * chore: remove style guide for tests * refactor: remove "Default" prefix from classes * refactor: remove DispatchableRegistry * refactor: use package specific exception classes * refactor: remove DispatchableRegistry * refactor: add logger support to PromptGetter and ResourceReader classes * Update src/Capability/Prompt/PromptGetter.php Co-authored-by: Christopher Hertel <mail@christopher-hertel.de> * refactor: Use FQN for Resource class to avoid cs-fixer misinterpreting it as `resource` * refactor: rename ToolExecutor to ToolCaller and related classes for clarity and consistency * refactor: add missed docblock * refactor: rename ToolExecutor references to ToolCaller * refactor: rename ToolCallerTest property and variable references * cs fix * ignore some phpstan errors --------- Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
313 lines
12 KiB
PHP
313 lines
12 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the official PHP MCP SDK.
|
|
*
|
|
* A collaboration between Symfony and the PHP Foundation.
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Mcp\Tests\Capability\Registry;
|
|
|
|
use Mcp\Capability\Registry;
|
|
use Mcp\Capability\Registry\PromptReference;
|
|
use Mcp\Capability\Registry\ResourceReference;
|
|
use Mcp\Capability\Registry\ResourceTemplateReference;
|
|
use Mcp\Capability\Registry\ToolReference;
|
|
use Mcp\Schema\Prompt;
|
|
use Mcp\Schema\Resource;
|
|
use Mcp\Schema\ResourceTemplate;
|
|
use Mcp\Schema\Tool;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class RegistryProviderTest extends TestCase
|
|
{
|
|
private Registry $registry;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->registry = new Registry();
|
|
}
|
|
|
|
public function testGetToolReturnsRegisteredTool(): void
|
|
{
|
|
$tool = $this->createValidTool('test_tool');
|
|
$handler = fn () => 'result';
|
|
|
|
$this->registry->registerTool($tool, $handler);
|
|
|
|
$toolRef = $this->registry->getTool('test_tool');
|
|
$this->assertInstanceOf(ToolReference::class, $toolRef);
|
|
$this->assertEquals($tool->name, $toolRef->tool->name);
|
|
$this->assertEquals($handler, $toolRef->handler);
|
|
$this->assertFalse($toolRef->isManual);
|
|
}
|
|
|
|
public function testGetToolReturnsNullForUnregisteredTool(): void
|
|
{
|
|
$toolRef = $this->registry->getTool('non_existent_tool');
|
|
$this->assertNull($toolRef);
|
|
}
|
|
|
|
public function testGetResourceReturnsRegisteredResource(): void
|
|
{
|
|
$resource = $this->createValidResource('test://resource');
|
|
$handler = fn () => 'content';
|
|
|
|
$this->registry->registerResource($resource, $handler);
|
|
|
|
$resourceRef = $this->registry->getResource('test://resource');
|
|
$this->assertInstanceOf(ResourceReference::class, $resourceRef);
|
|
$this->assertEquals($resource->uri, $resourceRef->schema->uri);
|
|
$this->assertEquals($handler, $resourceRef->handler);
|
|
$this->assertFalse($resourceRef->isManual);
|
|
}
|
|
|
|
public function testGetResourceReturnsNullForUnregisteredResource(): void
|
|
{
|
|
$resourceRef = $this->registry->getResource('test://non_existent');
|
|
$this->assertNull($resourceRef);
|
|
}
|
|
|
|
public function testGetResourceMatchesResourceTemplate(): void
|
|
{
|
|
$template = $this->createValidResourceTemplate('test://{id}');
|
|
$handler = fn (string $id) => "content for {$id}";
|
|
|
|
$this->registry->registerResourceTemplate($template, $handler);
|
|
|
|
$resourceRef = $this->registry->getResource('test://123');
|
|
$this->assertInstanceOf(ResourceTemplateReference::class, $resourceRef);
|
|
$this->assertEquals($template->uriTemplate, $resourceRef->resourceTemplate->uriTemplate);
|
|
$this->assertEquals($handler, $resourceRef->handler);
|
|
}
|
|
|
|
public function testGetResourceWithIncludeTemplatesFalse(): void
|
|
{
|
|
$template = $this->createValidResourceTemplate('test://{id}');
|
|
$handler = fn (string $id) => "content for {$id}";
|
|
|
|
$this->registry->registerResourceTemplate($template, $handler);
|
|
|
|
$resourceRef = $this->registry->getResource('test://123', false);
|
|
$this->assertNull($resourceRef);
|
|
}
|
|
|
|
public function testGetResourcePrefersDirectResourceOverTemplate(): void
|
|
{
|
|
$resource = $this->createValidResource('test://123');
|
|
$resourceHandler = fn () => 'direct resource';
|
|
|
|
$template = $this->createValidResourceTemplate('test://{id}');
|
|
$templateHandler = fn (string $id) => "template for {$id}";
|
|
|
|
$this->registry->registerResource($resource, $resourceHandler);
|
|
$this->registry->registerResourceTemplate($template, $templateHandler);
|
|
|
|
$resourceRef = $this->registry->getResource('test://123');
|
|
$this->assertInstanceOf(ResourceReference::class, $resourceRef);
|
|
$this->assertEquals($resource->uri, $resourceRef->schema->uri);
|
|
}
|
|
|
|
public function testGetResourceTemplateReturnsRegisteredTemplate(): void
|
|
{
|
|
$template = $this->createValidResourceTemplate('test://{id}');
|
|
$handler = fn (string $id) => "content for {$id}";
|
|
|
|
$this->registry->registerResourceTemplate($template, $handler);
|
|
|
|
$templateRef = $this->registry->getResourceTemplate('test://{id}');
|
|
$this->assertInstanceOf(ResourceTemplateReference::class, $templateRef);
|
|
$this->assertEquals($template->uriTemplate, $templateRef->resourceTemplate->uriTemplate);
|
|
$this->assertEquals($handler, $templateRef->handler);
|
|
$this->assertFalse($templateRef->isManual);
|
|
}
|
|
|
|
public function testGetResourceTemplateReturnsNullForUnregisteredTemplate(): void
|
|
{
|
|
$templateRef = $this->registry->getResourceTemplate('test://{non_existent}');
|
|
$this->assertNull($templateRef);
|
|
}
|
|
|
|
public function testGetPromptReturnsRegisteredPrompt(): void
|
|
{
|
|
$prompt = $this->createValidPrompt('test_prompt');
|
|
$handler = fn () => ['role' => 'user', 'content' => 'test message'];
|
|
|
|
$this->registry->registerPrompt($prompt, $handler);
|
|
|
|
$promptRef = $this->registry->getPrompt('test_prompt');
|
|
$this->assertInstanceOf(PromptReference::class, $promptRef);
|
|
$this->assertEquals($prompt->name, $promptRef->prompt->name);
|
|
$this->assertEquals($handler, $promptRef->handler);
|
|
$this->assertFalse($promptRef->isManual);
|
|
}
|
|
|
|
public function testGetPromptReturnsNullForUnregisteredPrompt(): void
|
|
{
|
|
$promptRef = $this->registry->getPrompt('non_existent_prompt');
|
|
$this->assertNull($promptRef);
|
|
}
|
|
|
|
public function testGetToolsReturnsAllRegisteredTools(): void
|
|
{
|
|
$tool1 = $this->createValidTool('tool1');
|
|
$tool2 = $this->createValidTool('tool2');
|
|
|
|
$this->registry->registerTool($tool1, fn () => 'result1');
|
|
$this->registry->registerTool($tool2, fn () => 'result2');
|
|
|
|
$tools = $this->registry->getTools();
|
|
$this->assertCount(2, $tools);
|
|
$this->assertArrayHasKey('tool1', $tools);
|
|
$this->assertArrayHasKey('tool2', $tools);
|
|
$this->assertInstanceOf(Tool::class, $tools['tool1']);
|
|
$this->assertInstanceOf(Tool::class, $tools['tool2']);
|
|
}
|
|
|
|
public function testGetResourcesReturnsAllRegisteredResources(): void
|
|
{
|
|
$resource1 = $this->createValidResource('test://resource1');
|
|
$resource2 = $this->createValidResource('test://resource2');
|
|
|
|
$this->registry->registerResource($resource1, fn () => 'content1');
|
|
$this->registry->registerResource($resource2, fn () => 'content2');
|
|
|
|
$resources = $this->registry->getResources();
|
|
$this->assertCount(2, $resources);
|
|
$this->assertArrayHasKey('test://resource1', $resources);
|
|
$this->assertArrayHasKey('test://resource2', $resources);
|
|
$this->assertInstanceOf(Resource::class, $resources['test://resource1']);
|
|
$this->assertInstanceOf(Resource::class, $resources['test://resource2']);
|
|
}
|
|
|
|
public function testGetPromptsReturnsAllRegisteredPrompts(): void
|
|
{
|
|
$prompt1 = $this->createValidPrompt('prompt1');
|
|
$prompt2 = $this->createValidPrompt('prompt2');
|
|
|
|
$this->registry->registerPrompt($prompt1, fn () => []);
|
|
$this->registry->registerPrompt($prompt2, fn () => []);
|
|
|
|
$prompts = $this->registry->getPrompts();
|
|
$this->assertCount(2, $prompts);
|
|
$this->assertArrayHasKey('prompt1', $prompts);
|
|
$this->assertArrayHasKey('prompt2', $prompts);
|
|
$this->assertInstanceOf(Prompt::class, $prompts['prompt1']);
|
|
$this->assertInstanceOf(Prompt::class, $prompts['prompt2']);
|
|
}
|
|
|
|
public function testGetResourceTemplatesReturnsAllRegisteredTemplates(): void
|
|
{
|
|
$template1 = $this->createValidResourceTemplate('test1://{id}');
|
|
$template2 = $this->createValidResourceTemplate('test2://{category}');
|
|
|
|
$this->registry->registerResourceTemplate($template1, fn () => 'content1');
|
|
$this->registry->registerResourceTemplate($template2, fn () => 'content2');
|
|
|
|
$templates = $this->registry->getResourceTemplates();
|
|
$this->assertCount(2, $templates);
|
|
$this->assertArrayHasKey('test1://{id}', $templates);
|
|
$this->assertArrayHasKey('test2://{category}', $templates);
|
|
$this->assertInstanceOf(ResourceTemplate::class, $templates['test1://{id}']);
|
|
$this->assertInstanceOf(ResourceTemplate::class, $templates['test2://{category}']);
|
|
}
|
|
|
|
public function testHasElementsReturnsFalseForEmptyRegistry(): void
|
|
{
|
|
$this->assertFalse($this->registry->hasElements());
|
|
}
|
|
|
|
public function testHasElementsReturnsTrueWhenToolIsRegistered(): void
|
|
{
|
|
$tool = $this->createValidTool('test_tool');
|
|
$this->registry->registerTool($tool, fn () => 'result');
|
|
|
|
$this->assertTrue($this->registry->hasElements());
|
|
}
|
|
|
|
public function testHasElementsReturnsTrueWhenResourceIsRegistered(): void
|
|
{
|
|
$resource = $this->createValidResource('test://resource');
|
|
$this->registry->registerResource($resource, fn () => 'content');
|
|
|
|
$this->assertTrue($this->registry->hasElements());
|
|
}
|
|
|
|
public function testHasElementsReturnsTrueWhenPromptIsRegistered(): void
|
|
{
|
|
$prompt = $this->createValidPrompt('test_prompt');
|
|
$this->registry->registerPrompt($prompt, fn () => []);
|
|
|
|
$this->assertTrue($this->registry->hasElements());
|
|
}
|
|
|
|
public function testHasElementsReturnsTrueWhenResourceTemplateIsRegistered(): void
|
|
{
|
|
$template = $this->createValidResourceTemplate('test://{id}');
|
|
$this->registry->registerResourceTemplate($template, fn () => 'content');
|
|
|
|
$this->assertTrue($this->registry->hasElements());
|
|
}
|
|
|
|
public function testResourceTemplateMatchingPrefersMoreSpecificMatches(): void
|
|
{
|
|
$specificTemplate = $this->createValidResourceTemplate('test://users/{userId}/profile');
|
|
$genericTemplate = $this->createValidResourceTemplate('test://users/{userId}');
|
|
|
|
$this->registry->registerResourceTemplate($genericTemplate, fn () => 'generic');
|
|
$this->registry->registerResourceTemplate($specificTemplate, fn () => 'specific');
|
|
|
|
// Should match the more specific template first
|
|
$resourceRef = $this->registry->getResource('test://users/123/profile');
|
|
$this->assertInstanceOf(ResourceTemplateReference::class, $resourceRef);
|
|
$this->assertEquals('test://users/{userId}/profile', $resourceRef->resourceTemplate->uriTemplate);
|
|
}
|
|
|
|
private function createValidTool(string $name): Tool
|
|
{
|
|
return new Tool(
|
|
name: $name,
|
|
inputSchema: [
|
|
'type' => 'object',
|
|
'properties' => [
|
|
'param' => ['type' => 'string'],
|
|
],
|
|
'required' => null,
|
|
],
|
|
description: "Test tool: {$name}",
|
|
annotations: null
|
|
);
|
|
}
|
|
|
|
private function createValidResource(string $uri): Resource
|
|
{
|
|
return new Resource(
|
|
uri: $uri,
|
|
name: 'test_resource',
|
|
description: 'Test resource',
|
|
mimeType: 'text/plain'
|
|
);
|
|
}
|
|
|
|
private function createValidResourceTemplate(string $uriTemplate): ResourceTemplate
|
|
{
|
|
return new ResourceTemplate(
|
|
uriTemplate: $uriTemplate,
|
|
name: 'test_template',
|
|
description: 'Test resource template',
|
|
mimeType: 'text/plain'
|
|
);
|
|
}
|
|
|
|
private function createValidPrompt(string $name): Prompt
|
|
{
|
|
return new Prompt(
|
|
name: $name,
|
|
description: "Test prompt: {$name}",
|
|
arguments: []
|
|
);
|
|
}
|
|
}
|