Introduce Capability Registry & Discovery

This commit is contained in:
Christopher Hertel 2025-08-09 00:36:43 +02:00
commit da0d5a87a0
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3
6 changed files with 7063 additions and 8 deletions

View file

@ -0,0 +1,30 @@
<?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\Attribute;
use Mcp\Capability\Prompt\Completion\ProviderInterface;
class CompletionProviderFixture implements ProviderInterface
{
/**
* @var string[]
*/
public static array $completions = ['alpha', 'beta', 'gamma'];
public static string $lastCurrentValue = '';
public function getCompletions(string $currentValue): array
{
self::$lastCurrentValue = $currentValue;
return array_filter(self::$completions, fn ($item) => str_starts_with($item, $currentValue));
}
}

View file

@ -0,0 +1,85 @@
<?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\Attribute;
use Mcp\Capability\Attribute\CompletionProvider;
use Mcp\Tests\Fixtures\Enum\StatusEnum;
use PHPUnit\Framework\TestCase;
class CompletionProviderTest extends TestCase
{
public function testCanBeConstructedWithProviderClass(): void
{
$attribute = new CompletionProvider(provider: CompletionProviderFixture::class);
$this->assertSame(CompletionProviderFixture::class, $attribute->provider);
$this->assertNull($attribute->values);
$this->assertNull($attribute->enum);
}
public function testCanBeConstructedWithProviderInstance(): void
{
$instance = new CompletionProviderFixture();
$attribute = new CompletionProvider(provider: $instance);
$this->assertSame($instance, $attribute->provider);
$this->assertNull($attribute->values);
$this->assertNull($attribute->enum);
}
public function testCanBeConstructedWithValuesArray(): void
{
$values = ['draft', 'published', 'archived'];
$attribute = new CompletionProvider(values: $values);
$this->assertNull($attribute->provider);
$this->assertSame($values, $attribute->values);
$this->assertNull($attribute->enum);
}
public function testCanBeConstructedWithEnumClass(): void
{
$attribute = new CompletionProvider(enum: StatusEnum::class);
$this->assertNull($attribute->provider);
$this->assertNull($attribute->values);
$this->assertSame(StatusEnum::class, $attribute->enum);
}
public function testThrowsExceptionWhenNoParametersProvided(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one of provider, values, or enum can be set');
new CompletionProvider();
}
public function testThrowsExceptionWhenMultipleParametersProvided(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one of provider, values, or enum can be set');
new CompletionProvider(
provider: CompletionProviderFixture::class,
values: ['test']
);
}
public function testThrowsExceptionWhenAllParametersProvided(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Only one of provider, values, or enum can be set');
new CompletionProvider(
provider: CompletionProviderFixture::class,
values: ['test'],
enum: StatusEnum::class
);
}
}

View file

@ -0,0 +1,52 @@
<?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\Attribute;
use Mcp\Capability\Attribute\McpPrompt;
use PHPUnit\Framework\TestCase;
class McpPromptTest extends TestCase
{
public function testInstantiatesWithNameAndDescription(): void
{
// Arrange
$name = 'test-prompt-name';
$description = 'This is a test prompt description.';
// Act
$attribute = new McpPrompt(name: $name, description: $description);
// Assert
$this->assertSame($name, $attribute->name);
$this->assertSame($description, $attribute->description);
}
public function testInstantiatesWithNullValuesForNameAndDescription(): void
{
// Arrange & Act
$attribute = new McpPrompt(name: null, description: null);
// Assert
$this->assertNull($attribute->name);
$this->assertNull($attribute->description);
}
public function testInstantiatesWithMissingOptionalArguments(): void
{
// Arrange & Act
$attribute = new McpPrompt(); // Use default constructor values
// Assert
$this->assertNull($attribute->name);
$this->assertNull($attribute->description);
}
}

View file

@ -0,0 +1,71 @@
<?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\Attribute;
use Mcp\Capability\Attribute\McpResourceTemplate;
use PHPUnit\Framework\TestCase;
class McpResourceTemplateTest extends TestCase
{
public function testInstantiatesWithCorrectProperties(): void
{
// Arrange
$uriTemplate = 'file:///{path}/data';
$name = 'test-template-name';
$description = 'This is a test template description.';
$mimeType = 'application/json';
// Act
$attribute = new McpResourceTemplate(
uriTemplate: $uriTemplate,
name: $name,
description: $description,
mimeType: $mimeType,
);
// Assert
$this->assertSame($uriTemplate, $attribute->uriTemplate);
$this->assertSame($name, $attribute->name);
$this->assertSame($description, $attribute->description);
$this->assertSame($mimeType, $attribute->mimeType);
}
public function testInstantiatesWithNullValuesForNameAndDescription(): void
{
// Arrange & Act
$attribute = new McpResourceTemplate(
uriTemplate: 'test://{id}', // uriTemplate is required
name: null,
description: null,
mimeType: null,
);
// Assert
$this->assertSame('test://{id}', $attribute->uriTemplate);
$this->assertNull($attribute->name);
$this->assertNull($attribute->description);
$this->assertNull($attribute->mimeType);
}
public function testInstantiatesWithMissingOptionalArguments(): void
{
// Arrange & Act
$uriTemplate = 'tmpl://{key}';
$attribute = new McpResourceTemplate(uriTemplate: $uriTemplate);
// Assert
$this->assertSame($uriTemplate, $attribute->uriTemplate);
$this->assertNull($attribute->name);
$this->assertNull($attribute->description);
$this->assertNull($attribute->mimeType);
}
}

View file

@ -0,0 +1,77 @@
<?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\Attribute;
use Mcp\Capability\Attribute\McpResource;
use PHPUnit\Framework\TestCase;
class McpResourceTest extends TestCase
{
public function testInstantiatesWithCorrectProperties(): void
{
// Arrange
$uri = 'file:///test/resource';
$name = 'test-resource-name';
$description = 'This is a test resource description.';
$mimeType = 'text/plain';
$size = 1024;
// Act
$attribute = new McpResource(
uri: $uri,
name: $name,
description: $description,
mimeType: $mimeType,
size: $size,
);
// Assert
$this->assertSame($uri, $attribute->uri);
$this->assertSame($name, $attribute->name);
$this->assertSame($description, $attribute->description);
$this->assertSame($mimeType, $attribute->mimeType);
$this->assertSame($size, $attribute->size);
}
public function testInstantiatesWithNullValuesForNameAndDescription(): void
{
// Arrange & Act
$attribute = new McpResource(
uri: 'file:///test', // URI is required
name: null,
description: null,
mimeType: null,
size: null,
);
// Assert
$this->assertSame('file:///test', $attribute->uri);
$this->assertNull($attribute->name);
$this->assertNull($attribute->description);
$this->assertNull($attribute->mimeType);
$this->assertNull($attribute->size);
}
public function testInstantiatesWithMissingOptionalArguments(): void
{
// Arrange & Act
$uri = 'file:///only-uri';
$attribute = new McpResource(uri: $uri);
// Assert
$this->assertSame($uri, $attribute->uri);
$this->assertNull($attribute->name);
$this->assertNull($attribute->description);
$this->assertNull($attribute->mimeType);
$this->assertNull($attribute->size);
}
}

View file

@ -0,0 +1,52 @@
<?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\Attribute;
use Mcp\Capability\Attribute\McpTool;
use PHPUnit\Framework\TestCase;
class McpToolTest extends TestCase
{
public function testInstantiatesWithCorrectProperties(): void
{
// Arrange
$name = 'test-tool-name';
$description = 'This is a test description.';
// Act
$attribute = new McpTool(name: $name, description: $description);
// Assert
$this->assertSame($name, $attribute->name);
$this->assertSame($description, $attribute->description);
}
public function testInstantiatesWithNullValuesForNameAndDescription(): void
{
// Arrange & Act
$attribute = new McpTool(name: null, description: null);
// Assert
$this->assertNull($attribute->name);
$this->assertNull($attribute->description);
}
public function testInstantiatesWithMissingOptionalArguments(): void
{
// Arrange & Act
$attribute = new McpTool(); // Use default constructor values
// Assert
$this->assertNull($attribute->name);
$this->assertNull($attribute->description);
}
}