Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Elements/RegisteredElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@

class RegisteredElement implements JsonSerializable
{
/** @var callable|array|string */
public readonly mixed $handler;
public readonly bool $isManual;

public function __construct(
public readonly \Closure|array|string $handler,
public readonly bool $isManual = false,
) {}
callable|array|string $handler,
bool $isManual = false,
) {
$this->handler = $handler;
$this->isManual = $isManual;
}

public function handle(ContainerInterface $container, array $arguments): mixed
{
Expand Down
4 changes: 2 additions & 2 deletions src/Elements/RegisteredPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class RegisteredPrompt extends RegisteredElement
{
public function __construct(
public readonly Prompt $schema,
\Closure|array|string $handler,
callable|array|string $handler,
bool $isManual = false,
public readonly array $completionProviders = []
) {
parent::__construct($handler, $isManual);
}

public static function make(Prompt $schema, \Closure|array|string $handler, bool $isManual = false, array $completionProviders = []): self
public static function make(Prompt $schema, callable|array|string $handler, bool $isManual = false, array $completionProviders = []): self
{
return new self($schema, $handler, $isManual, $completionProviders);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Elements/RegisteredResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class RegisteredResource extends RegisteredElement
{
public function __construct(
public readonly Resource $schema,
\Closure|array|string $handler,
callable|array|string $handler,
bool $isManual = false,
) {
parent::__construct($handler, $isManual);
}

public static function make(Resource $schema, \Closure|array|string $handler, bool $isManual = false): self
public static function make(Resource $schema, callable|array|string $handler, bool $isManual = false): self
{
return new self($schema, $handler, $isManual);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Elements/RegisteredResourceTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RegisteredResourceTemplate extends RegisteredElement

public function __construct(
public readonly ResourceTemplate $schema,
\Closure|array|string $handler,
callable|array|string $handler,
bool $isManual = false,
public readonly array $completionProviders = []
) {
Expand All @@ -31,7 +31,7 @@ public function __construct(
$this->compileTemplate();
}

public static function make(ResourceTemplate $schema, \Closure|array|string $handler, bool $isManual = false, array $completionProviders = []): self
public static function make(ResourceTemplate $schema, callable|array|string $handler, bool $isManual = false, array $completionProviders = []): self
{
return new self($schema, $handler, $isManual, $completionProviders);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Elements/RegisteredTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class RegisteredTool extends RegisteredElement
{
public function __construct(
public readonly Tool $schema,
\Closure|array|string $handler,
callable|array|string $handler,
bool $isManual = false,
) {
parent::__construct($handler, $isManual);
}

public static function make(Tool $schema, \Closure|array|string $handler, bool $isManual = false): self
public static function make(Tool $schema, callable|array|string $handler, bool $isManual = false): self
{
return new self($schema, $handler, $isManual);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function load(): void
}
}

public function registerTool(Tool $tool, \Closure|array|string $handler, bool $isManual = false): void
public function registerTool(Tool $tool, callable|array|string $handler, bool $isManual = false): void
{
$toolName = $tool->name;
$existing = $this->tools[$toolName] ?? null;
Expand All @@ -197,7 +197,7 @@ public function registerTool(Tool $tool, \Closure|array|string $handler, bool $i
$this->checkAndEmitChange('tools', $this->tools);
}

public function registerResource(Resource $resource, \Closure|array|string $handler, bool $isManual = false): void
public function registerResource(Resource $resource, callable|array|string $handler, bool $isManual = false): void
{
$uri = $resource->uri;
$existing = $this->resources[$uri] ?? null;
Expand All @@ -215,7 +215,7 @@ public function registerResource(Resource $resource, \Closure|array|string $hand

public function registerResourceTemplate(
ResourceTemplate $template,
\Closure|array|string $handler,
callable|array|string $handler,
array $completionProviders = [],
bool $isManual = false,
): void {
Expand All @@ -235,7 +235,7 @@ public function registerResourceTemplate(

public function registerPrompt(
Prompt $prompt,
\Closure|array|string $handler,
callable|array|string $handler,
array $completionProviders = [],
bool $isManual = false,
): void {
Expand Down
8 changes: 4 additions & 4 deletions src/ServerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function withLoop(LoopInterface $loop): self
/**
* Manually registers a tool handler.
*/
public function withTool(\Closure|array|string $handler, ?string $name = null, ?string $description = null, ?ToolAnnotations $annotations = null, ?array $inputSchema = null): self
public function withTool(callable|array|string $handler, ?string $name = null, ?string $description = null, ?ToolAnnotations $annotations = null, ?array $inputSchema = null): self
{
$this->manualTools[] = compact('handler', 'name', 'description', 'annotations', 'inputSchema');

Expand All @@ -225,7 +225,7 @@ public function withTool(\Closure|array|string $handler, ?string $name = null, ?
/**
* Manually registers a resource handler.
*/
public function withResource(\Closure|array|string $handler, string $uri, ?string $name = null, ?string $description = null, ?string $mimeType = null, ?int $size = null, ?Annotations $annotations = null): self
public function withResource(callable|array|string $handler, string $uri, ?string $name = null, ?string $description = null, ?string $mimeType = null, ?int $size = null, ?Annotations $annotations = null): self
{
$this->manualResources[] = compact('handler', 'uri', 'name', 'description', 'mimeType', 'size', 'annotations');

Expand All @@ -235,7 +235,7 @@ public function withResource(\Closure|array|string $handler, string $uri, ?strin
/**
* Manually registers a resource template handler.
*/
public function withResourceTemplate(\Closure|array|string $handler, string $uriTemplate, ?string $name = null, ?string $description = null, ?string $mimeType = null, ?Annotations $annotations = null): self
public function withResourceTemplate(callable|array|string $handler, string $uriTemplate, ?string $name = null, ?string $description = null, ?string $mimeType = null, ?Annotations $annotations = null): self
{
$this->manualResourceTemplates[] = compact('handler', 'uriTemplate', 'name', 'description', 'mimeType', 'annotations');

Expand All @@ -245,7 +245,7 @@ public function withResourceTemplate(\Closure|array|string $handler, string $uri
/**
* Manually registers a prompt handler.
*/
public function withPrompt(\Closure|array|string $handler, ?string $name = null, ?string $description = null): self
public function withPrompt(callable|array|string $handler, ?string $name = null, ?string $description = null): self
{
$this->manualPrompts[] = compact('handler', 'name', 'description');

Expand Down
14 changes: 4 additions & 10 deletions tests/Unit/Attributes/CompletionProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
use PhpMcp\Server\Tests\Fixtures\General\CompletionProviderFixture;
use PhpMcp\Server\Defaults\ListCompletionProvider;
use PhpMcp\Server\Defaults\EnumCompletionProvider;

enum TestEnum: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}
use PhpMcp\Server\Tests\Fixtures\Enums\StatusEnum;

it('can be constructed with provider class', function () {
$attribute = new CompletionProvider(provider: CompletionProviderFixture::class);
Expand Down Expand Up @@ -43,11 +37,11 @@ enum TestEnum: string
});

it('can be constructed with enum class', function () {
$attribute = new CompletionProvider(enum: TestEnum::class);
$attribute = new CompletionProvider(enum: StatusEnum::class);

expect($attribute->provider)->toBeNull();
expect($attribute->values)->toBeNull();
expect($attribute->enum)->toBe(TestEnum::class);
expect($attribute->enum)->toBe(StatusEnum::class);
});

it('throws exception when no parameters provided', function () {
Expand All @@ -65,6 +59,6 @@ enum TestEnum: string
new CompletionProvider(
provider: CompletionProviderFixture::class,
values: ['test'],
enum: TestEnum::class
enum: StatusEnum::class
);
})->throws(\InvalidArgumentException::class, 'Only one of provider, values, or enum can be set');
4 changes: 2 additions & 2 deletions tests/Unit/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
use PhpMcp\Schema\Request\ListResourceTemplatesRequest;
use PhpMcp\Schema\ResourceReference;
use PhpMcp\Server\Protocol;
use PhpMcp\Server\Tests\Fixtures\Enums\StatusEnum;
use React\EventLoop\Loop;
use PhpMcp\Server\Tests\Unit\Attributes\TestEnum;

const DISPATCHER_SESSION_ID = 'dispatcher-session-xyz';
const DISPATCHER_PAGINATION_LIMIT = 3;
Expand Down Expand Up @@ -528,7 +528,7 @@
$currentValue = 'a';
$expectedCompletions = ['archived'];

$enumProvider = new \PhpMcp\Server\Defaults\EnumCompletionProvider(TestEnum::class);
$enumProvider = new \PhpMcp\Server\Defaults\EnumCompletionProvider(StatusEnum::class);

$promptSchema = PromptSchema::make($promptName, '', [PromptArgument::make($argName)]);
$registeredPrompt = new RegisteredPrompt(
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Elements/RegisteredPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpMcp\Schema\Content\ImageContent;
use PhpMcp\Schema\Content\AudioContent;
use PhpMcp\Schema\Content\EmbeddedResource;
use PhpMcp\Server\Tests\Fixtures\Enums\StatusEnum;
use PhpMcp\Server\Tests\Fixtures\General\PromptHandlerFixture;
use PhpMcp\Server\Tests\Fixtures\General\CompletionProviderFixture;
use PhpMcp\Server\Tests\Unit\Attributes\TestEnum;
Expand Down Expand Up @@ -261,7 +262,7 @@
[PromptArgument::make('priority')]
);

$enumProvider = new \PhpMcp\Server\Defaults\EnumCompletionProvider(TestEnum::class);
$enumProvider = new \PhpMcp\Server\Defaults\EnumCompletionProvider(StatusEnum::class);
$providers = ['priority' => $enumProvider];

$original = RegisteredPrompt::make(
Expand Down