Skip to content

Commit 765b924

Browse files
committed
fix: replace Handler type alias with explicit PHPDoc types
Remove @phpstan-import-type Handler and replace @param Handler annotations with explicit types: - callable|array{0: class-string|object, 1: string}|string for tools - \Closure|array{0: class-string|object, 1: string}|string for others This fixes PHPStan errors where the Handler type alias didn't match the native parameter types, and resolves missingType.iterableValue errors by specifying the array structure.
1 parent d434827 commit 765b924

File tree

9 files changed

+37
-54
lines changed

9 files changed

+37
-54
lines changed

src/Capability/Registry/ElementReference.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
/**
1515
* Base class for element references with default passthrough argument preparation.
1616
*
17-
* @phpstan-type Handler \Closure|array{0: object|string, 1: string}|string|object
18-
*
1917
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2018
*/
2119
class ElementReference implements ArgumentPreparationInterface
2220
{
2321
/**
24-
* @param Handler $handler The handler can be a Closure, array method reference,
25-
* string function/class name, or a callable object (implementing __invoke)
22+
* @param object|array{0: class-string|object, 1: string}|string $handler The handler can be a Closure, array method reference,
23+
* string function/class name, or a callable object (implementing __invoke)
2624
*/
2725
public function __construct(
2826
public readonly object|array|string $handler,

src/Capability/Registry/Loader/ArrayLoader.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Mcp\Capability\Discovery\DocBlockParser;
1919
use Mcp\Capability\Discovery\HandlerResolver;
2020
use Mcp\Capability\Discovery\SchemaGenerator;
21-
use Mcp\Capability\Registry\ElementReference;
2221
use Mcp\Capability\RegistryInterface;
2322
use Mcp\Exception\ConfigurationException;
2423
use Mcp\Schema\Annotations;
@@ -35,22 +34,20 @@
3534

3635
/**
3736
* @author Antoine Bluchet <soyuka@gmail.com>
38-
*
39-
* @phpstan-import-type Handler from ElementReference
4037
*/
4138
final class ArrayLoader implements LoaderInterface
4239
{
4340
/**
4441
* @param array{
45-
* handler: Handler,
42+
* handler: callable|array{0: class-string|object, 1: string}|string,
4643
* name: ?string,
4744
* description: ?string,
4845
* annotations: ?ToolAnnotations,
4946
* icons: ?Icon[],
5047
* meta: ?array<string, mixed>
5148
* }[] $tools
5249
* @param array{
53-
* handler: Handler,
50+
* handler: \Closure|array{0: class-string|object, 1: string}|string,
5451
* uri: string,
5552
* name: ?string,
5653
* description: ?string,
@@ -61,7 +58,7 @@ final class ArrayLoader implements LoaderInterface
6158
* meta: ?array<string, mixed>
6259
* }[] $resources
6360
* @param array{
64-
* handler: Handler,
61+
* handler: \Closure|array{0: class-string|object, 1: string}|string,
6562
* uriTemplate: string,
6663
* name: ?string,
6764
* description: ?string,
@@ -70,7 +67,7 @@ final class ArrayLoader implements LoaderInterface
7067
* meta: ?array<string, mixed>
7168
* }[] $resourceTemplates
7269
* @param array{
73-
* handler: Handler,
70+
* handler: \Closure|array{0: class-string|object, 1: string}|string,
7471
* name: ?string,
7572
* description: ?string,
7673
* icons: ?Icon[],
@@ -271,7 +268,7 @@ public function load(RegistryInterface $registry): void
271268
}
272269

273270
/**
274-
* @param Handler $handler
271+
* @param \Closure|array{0: class-string|object, 1: string}|string $handler
275272
*/
276273
private function getHandlerDescription(\Closure|array|string $handler): string
277274
{

src/Capability/Registry/PromptReference.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@
1616
use Mcp\Schema\Prompt;
1717

1818
/**
19-
* @phpstan-import-type Handler from ElementReference
20-
*
2119
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2220
*/
2321
class PromptReference extends ElementReference
2422
{
2523
use ReflectionArgumentPreparationTrait;
2624

2725
/**
28-
* @param Handler $handler
29-
* @param array<string, class-string|object> $completionProviders
26+
* @param \Closure|array{0: class-string|object, 1: string}|string $handler
27+
* @param array<string, class-string|object> $completionProviders
3028
*/
3129
public function __construct(
3230
public readonly Prompt $prompt,

src/Capability/Registry/ReferenceHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public function handle(ElementReference $reference, array $arguments): mixed
6161
/**
6262
* Resolves a handler to a callable, optionally returning an instance.
6363
*
64+
* @param \Closure|array{0: class-string|object, 1: string}|string $handler
65+
*
6466
* @return array{0: callable, 1: ?object}
6567
*/
6668
private function resolveHandler(\Closure|array|string $handler): array

src/Capability/Registry/ResourceReference.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
use Mcp\Schema\Resource;
1717

1818
/**
19-
* @phpstan-import-type Handler from ElementReference
20-
*
2119
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2220
*/
2321
class ResourceReference extends ElementReference
2422
{
2523
use ReflectionArgumentPreparationTrait;
2624

2725
/**
28-
* @param Handler $handler
26+
* @param callable|array{0: class-string|object, 1: string}|string $handler
2927
*/
3028
public function __construct(
3129
public readonly Resource $schema,

src/Capability/Registry/ResourceTemplateReference.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use Mcp\Schema\ResourceTemplate;
1717

1818
/**
19-
* @phpstan-import-type Handler from ElementReference
20-
*
2119
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2220
*/
2321
class ResourceTemplateReference extends ElementReference
@@ -27,8 +25,8 @@ class ResourceTemplateReference extends ElementReference
2725
private readonly UriTemplateMatcher $uriTemplateMatcher;
2826

2927
/**
30-
* @param Handler $handler
31-
* @param array<string, class-string|object> $completionProviders
28+
* @param callable|array{0: class-string|object, 1: string}|string $handler
29+
* @param array<string, class-string|object> $completionProviders
3230
*/
3331
public function __construct(
3432
public readonly ResourceTemplate $resourceTemplate,

src/Capability/Registry/ToolReference.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
use Mcp\Schema\Tool;
1818

1919
/**
20-
* @phpstan-import-type Handler from ElementReference
21-
*
2220
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2321
*/
2422
class ToolReference extends ElementReference
2523
{
2624
use ReflectionArgumentPreparationTrait;
2725

2826
/**
29-
* @param Handler $handler
27+
* @param callable|array{0: class-string|object, 1: string}|string $handler
3028
*/
3129
public function __construct(
3230
public readonly Tool $tool,

src/Capability/RegistryInterface.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Mcp\Capability\Registry\DynamicResourceReference;
2121
use Mcp\Capability\Registry\DynamicResourceTemplateReference;
2222
use Mcp\Capability\Registry\DynamicToolReference;
23-
use Mcp\Capability\Registry\ElementReference;
2423
use Mcp\Capability\Registry\PromptReference;
2524
use Mcp\Capability\Registry\ResourceReference;
2625
use Mcp\Capability\Registry\ResourceTemplateReference;
@@ -35,8 +34,6 @@
3534
use Mcp\Schema\Tool;
3635

3736
/**
38-
* @phpstan-import-type Handler from ElementReference
39-
*
4037
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
4138
* @author Christopher Hertel <mail@christopher-hertel.de>
4239
*/
@@ -45,22 +42,22 @@ interface RegistryInterface
4542
/**
4643
* Registers a tool with its handler.
4744
*
48-
* @param Handler $handler
45+
* @param callable|array{0: class-string|object, 1: string}|string $handler
4946
*/
5047
public function registerTool(Tool $tool, callable|array|string $handler, bool $isManual = false): void;
5148

5249
/**
5350
* Registers a resource with its handler.
5451
*
55-
* @param Handler $handler
52+
* @param callable|array{0: class-string|object, 1: string}|string $handler
5653
*/
5754
public function registerResource(Resource $resource, callable|array|string $handler, bool $isManual = false): void;
5855

5956
/**
6057
* Registers a resource template with its handler and completion providers.
6158
*
62-
* @param Handler $handler
63-
* @param array<string, class-string|object> $completionProviders
59+
* @param callable|array{0: class-string|object, 1: string}|string $handler
60+
* @param array<string, class-string|object> $completionProviders
6461
*/
6562
public function registerResourceTemplate(
6663
ResourceTemplate $template,
@@ -72,8 +69,8 @@ public function registerResourceTemplate(
7269
/**
7370
* Registers a prompt with its handler and completion providers.
7471
*
75-
* @param Handler $handler
76-
* @param array<string, class-string|object> $completionProviders
72+
* @param callable|array{0: class-string|object, 1: string}|string $handler
73+
* @param array<string, class-string|object> $completionProviders
7774
*/
7875
public function registerPrompt(
7976
Prompt $prompt,

src/Server/Builder.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Mcp\Capability\Provider\DynamicToolProviderInterface;
1818
use Mcp\Capability\Registry;
1919
use Mcp\Capability\Registry\Container;
20-
use Mcp\Capability\Registry\ElementReference;
2120
use Mcp\Capability\Registry\Loader\ArrayLoader;
2221
use Mcp\Capability\Registry\Loader\DiscoveryLoader;
2322
use Mcp\Capability\Registry\Loader\LoaderInterface;
@@ -44,8 +43,6 @@
4443
use Psr\SimpleCache\CacheInterface;
4544

4645
/**
47-
* @phpstan-import-type Handler from ElementReference
48-
*
4946
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
5047
*/
5148
final class Builder
@@ -86,7 +83,7 @@ final class Builder
8683

8784
/**
8885
* @var array{
89-
* handler: Handler,
86+
* handler: callable|array{0: class-string|object, 1: string}|string,
9087
* name: ?string,
9188
* description: ?string,
9289
* annotations: ?ToolAnnotations,
@@ -98,7 +95,7 @@ final class Builder
9895

9996
/**
10097
* @var array{
101-
* handler: Handler,
98+
* handler: \Closure|array{0: class-string|object, 1: string}|string,
10299
* uri: string,
103100
* name: ?string,
104101
* description: ?string,
@@ -113,7 +110,7 @@ final class Builder
113110

114111
/**
115112
* @var array{
116-
* handler: Handler,
113+
* handler: \Closure|array{0: class-string|object, 1: string}|string,
117114
* uriTemplate: string,
118115
* name: ?string,
119116
* description: ?string,
@@ -126,7 +123,7 @@ final class Builder
126123

127124
/**
128125
* @var array{
129-
* handler: Handler,
126+
* handler: \Closure|array{0: class-string|object, 1: string}|string,
130127
* name: ?string,
131128
* description: ?string,
132129
* icons: ?Icon[],
@@ -350,10 +347,10 @@ public function setProtocolVersion(ProtocolVersion $protocolVersion): self
350347
/**
351348
* Manually registers a tool handler.
352349
*
353-
* @param Handler $handler
354-
* @param array<string, mixed>|null $inputSchema
355-
* @param ?Icon[] $icons
356-
* @param array<string, mixed>|null $meta
350+
* @param callable|array{0: class-string|object, 1: string}|string $handler
351+
* @param array<string, mixed>|null $inputSchema
352+
* @param ?Icon[] $icons
353+
* @param array<string, mixed>|null $meta
357354
*/
358355
public function addTool(
359356
callable|array|string $handler,
@@ -380,9 +377,9 @@ public function addTool(
380377
/**
381378
* Manually registers a resource handler.
382379
*
383-
* @param Handler $handler
384-
* @param ?Icon[] $icons
385-
* @param array<string, mixed>|null $meta
380+
* @param \Closure|array{0: class-string|object, 1: string}|string $handler
381+
* @param ?Icon[] $icons
382+
* @param array<string, mixed>|null $meta
386383
*/
387384
public function addResource(
388385
\Closure|array|string $handler,
@@ -413,8 +410,8 @@ public function addResource(
413410
/**
414411
* Manually registers a resource template handler.
415412
*
416-
* @param Handler $handler
417-
* @param array<string, mixed>|null $meta
413+
* @param \Closure|array{0: class-string|object, 1: string}|string $handler
414+
* @param array<string, mixed>|null $meta
418415
*/
419416
public function addResourceTemplate(
420417
\Closure|array|string $handler,
@@ -441,9 +438,9 @@ public function addResourceTemplate(
441438
/**
442439
* Manually registers a prompt handler.
443440
*
444-
* @param Handler $handler
445-
* @param ?Icon[] $icons
446-
* @param array<string, mixed>|null $meta
441+
* @param \Closure|array{0: class-string|object, 1: string}|string $handler
442+
* @param ?Icon[] $icons
443+
* @param array<string, mixed>|null $meta
447444
*/
448445
public function addPrompt(
449446
\Closure|array|string $handler,

0 commit comments

Comments
 (0)