Skip to content

Commit 7f205d2

Browse files
feat: use callable instead of Closure|array|string for handler type
1 parent 3fd8bb3 commit 7f205d2

File tree

9 files changed

+32
-30
lines changed

9 files changed

+32
-30
lines changed

src/Elements/RegisteredElement.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,17 @@
1818

1919
class RegisteredElement implements JsonSerializable
2020
{
21+
/** @var callable|array|string */
22+
public readonly mixed $handler;
23+
public readonly bool $isManual;
24+
2125
public function __construct(
22-
public readonly \Closure|array|string $handler,
23-
public readonly bool $isManual = false,
24-
) {}
26+
callable|array|string $handler,
27+
bool $isManual = false,
28+
) {
29+
$this->handler = $handler;
30+
$this->isManual = $isManual;
31+
}
2532

2633
public function handle(ContainerInterface $container, array $arguments): mixed
2734
{

src/Elements/RegisteredPrompt.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class RegisteredPrompt extends RegisteredElement
2424
{
2525
public function __construct(
2626
public readonly Prompt $schema,
27-
\Closure|array|string $handler,
27+
callable|array|string $handler,
2828
bool $isManual = false,
2929
public readonly array $completionProviders = []
3030
) {
3131
parent::__construct($handler, $isManual);
3232
}
3333

34-
public static function make(Prompt $schema, \Closure|array|string $handler, bool $isManual = false, array $completionProviders = []): self
34+
public static function make(Prompt $schema, callable|array|string $handler, bool $isManual = false, array $completionProviders = []): self
3535
{
3636
return new self($schema, $handler, $isManual, $completionProviders);
3737
}

src/Elements/RegisteredResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class RegisteredResource extends RegisteredElement
1616
{
1717
public function __construct(
1818
public readonly Resource $schema,
19-
\Closure|array|string $handler,
19+
callable|array|string $handler,
2020
bool $isManual = false,
2121
) {
2222
parent::__construct($handler, $isManual);
2323
}
2424

25-
public static function make(Resource $schema, \Closure|array|string $handler, bool $isManual = false): self
25+
public static function make(Resource $schema, callable|array|string $handler, bool $isManual = false): self
2626
{
2727
return new self($schema, $handler, $isManual);
2828
}

src/Elements/RegisteredResourceTemplate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RegisteredResourceTemplate extends RegisteredElement
2222

2323
public function __construct(
2424
public readonly ResourceTemplate $schema,
25-
\Closure|array|string $handler,
25+
callable|array|string $handler,
2626
bool $isManual = false,
2727
public readonly array $completionProviders = []
2828
) {
@@ -31,7 +31,7 @@ public function __construct(
3131
$this->compileTemplate();
3232
}
3333

34-
public static function make(ResourceTemplate $schema, \Closure|array|string $handler, bool $isManual = false, array $completionProviders = []): self
34+
public static function make(ResourceTemplate $schema, callable|array|string $handler, bool $isManual = false, array $completionProviders = []): self
3535
{
3636
return new self($schema, $handler, $isManual, $completionProviders);
3737
}

src/Elements/RegisteredTool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class RegisteredTool extends RegisteredElement
1414
{
1515
public function __construct(
1616
public readonly Tool $schema,
17-
\Closure|array|string $handler,
17+
callable|array|string $handler,
1818
bool $isManual = false,
1919
) {
2020
parent::__construct($handler, $isManual);
2121
}
2222

23-
public static function make(Tool $schema, \Closure|array|string $handler, bool $isManual = false): self
23+
public static function make(Tool $schema, callable|array|string $handler, bool $isManual = false): self
2424
{
2525
return new self($schema, $handler, $isManual);
2626
}

src/Registry.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function load(): void
181181
}
182182
}
183183

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

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

216216
public function registerResourceTemplate(
217217
ResourceTemplate $template,
218-
\Closure|array|string $handler,
218+
callable|array|string $handler,
219219
array $completionProviders = [],
220220
bool $isManual = false,
221221
): void {
@@ -235,7 +235,7 @@ public function registerResourceTemplate(
235235

236236
public function registerPrompt(
237237
Prompt $prompt,
238-
\Closure|array|string $handler,
238+
callable|array|string $handler,
239239
array $completionProviders = [],
240240
bool $isManual = false,
241241
): void {

src/ServerBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function withLoop(LoopInterface $loop): self
215215
/**
216216
* Manually registers a tool handler.
217217
*/
218-
public function withTool(\Closure|array|string $handler, ?string $name = null, ?string $description = null, ?ToolAnnotations $annotations = null, ?array $inputSchema = null): self
218+
public function withTool(callable|array|string $handler, ?string $name = null, ?string $description = null, ?ToolAnnotations $annotations = null, ?array $inputSchema = null): self
219219
{
220220
$this->manualTools[] = compact('handler', 'name', 'description', 'annotations', 'inputSchema');
221221

@@ -225,7 +225,7 @@ public function withTool(\Closure|array|string $handler, ?string $name = null, ?
225225
/**
226226
* Manually registers a resource handler.
227227
*/
228-
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
228+
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
229229
{
230230
$this->manualResources[] = compact('handler', 'uri', 'name', 'description', 'mimeType', 'size', 'annotations');
231231

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

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

tests/Unit/Attributes/CompletionProviderTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
use PhpMcp\Server\Tests\Fixtures\General\CompletionProviderFixture;
99
use PhpMcp\Server\Defaults\ListCompletionProvider;
1010
use PhpMcp\Server\Defaults\EnumCompletionProvider;
11-
12-
enum TestEnum: string
13-
{
14-
case DRAFT = 'draft';
15-
case PUBLISHED = 'published';
16-
case ARCHIVED = 'archived';
17-
}
11+
use PhpMcp\Server\Tests\Fixtures\Enums\StatusEnum;
1812

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

4539
it('can be constructed with enum class', function () {
46-
$attribute = new CompletionProvider(enum: TestEnum::class);
40+
$attribute = new CompletionProvider(enum: StatusEnum::class);
4741

4842
expect($attribute->provider)->toBeNull();
4943
expect($attribute->values)->toBeNull();
50-
expect($attribute->enum)->toBe(TestEnum::class);
44+
expect($attribute->enum)->toBe(StatusEnum::class);
5145
});
5246

5347
it('throws exception when no parameters provided', function () {
@@ -65,6 +59,6 @@ enum TestEnum: string
6559
new CompletionProvider(
6660
provider: CompletionProviderFixture::class,
6761
values: ['test'],
68-
enum: TestEnum::class
62+
enum: StatusEnum::class
6963
);
7064
})->throws(\InvalidArgumentException::class, 'Only one of provider, values, or enum can be set');

tests/Unit/Elements/RegisteredPromptTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMcp\Schema\Content\ImageContent;
1313
use PhpMcp\Schema\Content\AudioContent;
1414
use PhpMcp\Schema\Content\EmbeddedResource;
15+
use PhpMcp\Server\Tests\Fixtures\Enums\StatusEnum;
1516
use PhpMcp\Server\Tests\Fixtures\General\PromptHandlerFixture;
1617
use PhpMcp\Server\Tests\Fixtures\General\CompletionProviderFixture;
1718
use PhpMcp\Server\Tests\Unit\Attributes\TestEnum;
@@ -261,7 +262,7 @@
261262
[PromptArgument::make('priority')]
262263
);
263264

264-
$enumProvider = new \PhpMcp\Server\Defaults\EnumCompletionProvider(TestEnum::class);
265+
$enumProvider = new \PhpMcp\Server\Defaults\EnumCompletionProvider(StatusEnum::class);
265266
$providers = ['priority' => $enumProvider];
266267

267268
$original = RegisteredPrompt::make(

0 commit comments

Comments
 (0)