Repository navigation
Expand file tree
/
Copy pathSchemaGeneratorFixture.php
More file actions
672 lines (614 loc) · 21.8 KB
/
Copy pathSchemaGeneratorFixture.php
File metadata and controls
672 lines (614 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
<?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\Unit\Server\Capability\Discovery;
use Mcp\Server\Capability\Attribute\McpTool;
use Mcp\Server\Capability\Attribute\Schema;
use Mcp\Server\ClientGateway;
use Mcp\Server\RequestContext;
use Mcp\Tests\Unit\Fixtures\Enum\BackedIntEnum;
use Mcp\Tests\Unit\Fixtures\Enum\BackedStringEnum;
use Mcp\Tests\Unit\Fixtures\Enum\UnitEnum;
/**
* Comprehensive fixture for testing SchemaGenerator with various scenarios.
*/
class SchemaGeneratorFixture
{
// ===== BASIC SCENARIOS =====
public function noParams(): void
{
}
/**
* Type hints only - no Schema attributes.
*/
public function typeHintsOnly(string $name, int $age, bool $active, array $tags, ?\stdClass $config = null): void
{
}
public function iterableParameters(iterable $items, ?iterable $optional = null): void
{
}
/**
* DocBlock types only - no PHP type hints, no Schema attributes.
*
* @param string $username The username
* @param int $count Number of items
* @param bool $enabled Whether enabled
* @param array $data Some data
*/
public function docBlockOnly($username, $count, $enabled, $data): void
{
}
/**
* Type hints with DocBlock descriptions.
*
* @param string $email User email address
* @param int $score User score
* @param bool $verified Whether user is verified
*/
public function typeHintsWithDocBlock(string $email, int $score, bool $verified): void
{
}
// ===== METHOD-LEVEL SCHEMA SCENARIOS =====
/**
* Method-level Schema with complete definition.
*/
#[Schema(definition: [
'type' => 'object',
'description' => 'Creates a custom filter with complete definition',
'properties' => [
'field' => ['type' => 'string', 'enum' => ['name', 'date', 'status']],
'operator' => ['type' => 'string', 'enum' => ['eq', 'gt', 'lt', 'contains']],
'value' => ['description' => 'Value to filter by, type depends on field and operator'],
],
'required' => ['field', 'operator', 'value'],
'if' => [
'properties' => ['field' => ['const' => 'date']],
],
'then' => [
'properties' => ['value' => ['type' => 'string', 'format' => 'date']],
],
])]
public function methodLevelCompleteDefinition(string $field, string $operator, mixed $value): array
{
return compact('field', 'operator', 'value');
}
/**
* Method-level Schema defining properties.
*/
#[Schema(
description: 'Creates a new user with detailed information.',
properties: [
'username' => ['type' => 'string', 'minLength' => 3, 'pattern' => '^[a-zA-Z0-9_]+$'],
'email' => ['type' => 'string', 'format' => 'email'],
'age' => ['type' => 'integer', 'minimum' => 18, 'description' => 'Age in years.'],
'isActive' => ['type' => 'boolean', 'default' => true],
],
required: ['username', 'email']
)]
public function methodLevelWithProperties(string $username, string $email, int $age, bool $isActive = true): array
{
return compact('username', 'email', 'age', 'isActive');
}
/**
* Method-level Schema for complex array argument.
*/
#[Schema(
properties: [
'profiles' => [
'type' => 'array',
'description' => 'An array of user profiles to update.',
'minItems' => 1,
'items' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'data' => ['type' => 'object', 'additionalProperties' => true],
],
'required' => ['id', 'data'],
],
],
],
required: ['profiles']
)]
public function methodLevelArrayArgument(array $profiles): array
{
return ['updated_count' => \count($profiles)];
}
// ===== PARAMETER-LEVEL SCHEMA SCENARIOS =====
/**
* Parameter-level Schema with complete definition.
*/
public function parameterLevelCompleteDefinition(
#[Schema(definition: [
'type' => 'string',
'description' => 'The region to query.',
'x-mcp-header' => 'Region',
])]
string $region = 'eu',
#[Schema(definition: ['type' => 'integer', 'minimum' => 1, 'maximum' => 10])]
int $limit = 3,
): void {
}
/**
* Parameter-level definition that reshapes the parameter's type.
*/
public function definitionReshapingTheParameter(
#[Schema(definition: ['type' => 'object', 'properties' => ['id' => ['type' => 'integer']]])]
string $payload = '{}',
): void {
}
/**
* Parameter-level Schema attributes only.
*/
public function parameterLevelOnly(
#[Schema(description: 'Recipient ID', pattern: '^user_')]
string $recipientId,
#[Schema(maxLength: 1024)]
string $messageBody,
#[Schema(type: 'integer', enum: [1, 2, 5])]
int $priority = 1,
#[Schema(
type: 'object',
properties: [
'type' => ['type' => 'string', 'enum' => ['sms', 'email', 'push']],
'deviceToken' => ['type' => 'string', 'description' => 'Required if type is push'],
],
required: ['type']
)]
?array $notificationConfig = null,
): array {
return compact('recipientId', 'messageBody', 'priority', 'notificationConfig');
}
/**
* Parameter-level Schema with string constraints.
*/
public function parameterStringConstraints(
#[Schema(format: 'email')]
string $email,
#[Schema(minLength: 8, pattern: '^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$')]
string $password,
string $regularString,
): void {
}
/**
* Parameter-level Schema with numeric constraints.
*/
public function parameterNumericConstraints(
#[Schema(minimum: 18, maximum: 120)]
int $age,
#[Schema(minimum: 0, maximum: 5, exclusiveMaximum: true)]
float $rating,
#[Schema(multipleOf: 10)]
int $count,
): void {
}
/**
* Parameter-level Schema with array constraints.
*/
public function parameterArrayConstraints(
#[Schema(type: 'array', items: ['type' => 'string'], minItems: 1, uniqueItems: true)]
array $tags,
#[Schema(type: 'array', items: ['type' => 'integer', 'minimum' => 0, 'maximum' => 100], minItems: 1, maxItems: 5)]
array $scores,
): void {
}
// ===== COMBINED SCENARIOS =====
/**
* Method-level + Parameter-level Schema combination.
*
* @param string $settingKey The key of the setting
* @param mixed $newValue The new value for the setting
*/
#[Schema(
properties: [
'settingKey' => ['type' => 'string', 'description' => 'The key of the setting.'],
'newValue' => ['description' => 'The new value for the setting (any type).'],
],
required: ['settingKey', 'newValue']
)]
public function methodAndParameterLevel(
string $settingKey,
#[Schema(description: 'The specific new boolean value.', type: 'boolean')]
mixed $newValue,
): array {
return compact('settingKey', 'newValue');
}
/**
* Type hints + DocBlock + Parameter-level Schema.
*
* @param string $username The user's name
* @param int $priority Task priority level
*/
public function typeHintDocBlockAndParameterSchema(
#[Schema(minLength: 3, pattern: '^[a-zA-Z0-9_]+$')]
string $username,
#[Schema(minimum: 1, maximum: 10)]
int $priority,
): void {
}
/**
* PHPStan/Psalm integer ranges should keep their base integer type.
*
* @param int<0, max> $offset Positive offset
* @param int<min, -1> $negative Negative value
* @param int<-5, 10> $bounded Bounded value
*/
public function integerRangeTypes(
#[Schema(minimum: 0)]
int $offset,
int $negative,
int $bounded,
): void {
}
// ===== ENUM SCENARIOS =====
/**
* Various enum parameter types.
*
* @param BackedStringEnum $stringEnum Backed string enum
* @param BackedIntEnum $intEnum Backed int enum
* @param UnitEnum $unitEnum Unit enum
*/
public function enumParameters(
BackedStringEnum $stringEnum,
BackedIntEnum $intEnum,
UnitEnum $unitEnum,
?BackedStringEnum $nullableEnum = null,
BackedIntEnum $enumWithDefault = BackedIntEnum::First,
): void {
}
// ===== ARRAY TYPE SCENARIOS =====
/**
* Various array type scenarios.
*
* @param array $genericArray Generic array
* @param string[] $stringArray Array of strings
* @param int[] $intArray Array of integers
* @param array<string, mixed> $mixedMap Mixed array map
* @param array{name: string, age: int} $objectLikeArray Object-like array
* @param array{user: array{id: int, name: string}, items: int[]} $nestedObjectArray Nested object array
*/
public function arrayTypeScenarios(
array $genericArray,
array $stringArray,
array $intArray,
array $mixedMap,
array $objectLikeArray,
array $nestedObjectArray,
): void {
}
/**
* Nullable typed arrays should still recover their element type.
*
* @param string[]|null $nullableStrings Nullable list of strings
* @param array<int>|null $nullableInts Nullable list of integers
*/
public function nullableTypedArrays(
?array $nullableStrings,
?array $nullableInts = null,
): void {
}
/**
* A `null` or `|` inside an array shape belongs to one of its keys.
*
* @param array{name: string, nickname: string|null} $nullUnionMember Shape with a nullable key
* @param array{name: string, nickname: ?string} $nullableMember Shape with a nullable key
* @param array{id: int|string} $unionMember Shape with a union key
* @param array{id: int}|null $nullableShape Nullable shape
*/
public function arrayShapesWithUnionMembers(
array $nullUnionMember,
array $nullableMember,
array $unionMember,
$nullableShape,
): void {
}
/**
* PHPStan/Psalm pseudo-types and literal types map to their base type.
*
* @param non-empty-string $name Name
* @param class-string $class Class
* @param positive-int $count Count
* @param array-key $key Key
* @param 'asc'|'desc' $order Order
* @param 1|2 $level Level
* @param non-empty-list<non-empty-string> $tags Tags
* @param array{tags: list<string>, ids: array<int, int>, meta: array<string, int>, any: mixed} $settings Settings
*/
public function pseudoTypes(
string $name,
string $class,
int $count,
int|string $key,
string $order,
int $level,
array $tags,
array $settings,
): void {
}
/**
* The key type of a map inside an array shape decides between a JSON array and a JSON object.
*
* @param array{map: array<string, int>} $stringKey
* @param array{map: array<lowercase-string, int>} $pseudoStringKey
* @param array{map: array<class-string<\stdClass>, int>} $genericStringKey
* @param array{map: array<int, int>} $intKey
* @param array{map: array<int<0, max>, int>} $rangeKey
* @param array{map: array<array-key, int>} $arrayKey
* @param array{map: array<int|string, int>} $unionKey
* @param array{map: array<array-key, mixed>} $arrayKeyMixedValue
* @param array{map: array<array-key, int>|null} $nullableArrayKey
* @param array{map: array<array{a: int, b: int}>} $shapeValue
*/
public function arrayShapeMapKeys(
array $stringKey,
array $pseudoStringKey,
array $genericStringKey,
array $intKey,
array $rangeKey,
array $arrayKey,
array $unionKey,
array $arrayKeyMixedValue,
array $nullableArrayKey,
array $shapeValue,
): void {
}
/**
* @param ?non-empty-string $nullableString
* @param ?positive-int $nullableInt
* @param ?array-key $nullableArrayKey
* @param ?list<string> $nullableList
* @param non-empty-array<string, int> $stringKeyMap
* @param non-empty-array<string, int>|null $nullableStringKeyMap
* @param non-empty-array<int, int> $intKeyMap
* @param non-empty-array<array-key, int> $arrayKeyMap
* @param non-empty-array<int> $singleArgument
*/
public function nullableAndNonEmptyPseudoTypes(
?string $nullableString,
?int $nullableInt,
int|string|null $nullableArrayKey,
?array $nullableList,
array $stringKeyMap,
?array $nullableStringKeyMap,
array $intKeyMap,
array $arrayKeyMap,
array $singleArgument,
): void {
}
/**
* @param 'a|b'|'c' $pipeLiteral
* @param "a|b" $doubleQuotedPipeLiteral
* @param 'a[]' $bracketLiteral
* @param 'it\'s' $escapedLiteral
* @param array{operator: '<', limit: int} $lessThanLiteral
* @param array{operator: '>', limit: int} $greaterThanLiteral
* @param array{label: 'a,b', limit: int} $commaLiteral
* @param array{label: '{', limit: int} $braceLiteral
* @param list<'a,b'> $commaLiteralList
* @param array<array<mixed>> $nestedMixed
* @param array<array<int|mixed>> $nestedMixedUnion
* @param mixed[][] $nestedMixedBrackets
*/
public function quotedLiteralsAndNestedMixed(
string $pipeLiteral,
string $doubleQuotedPipeLiteral,
string $bracketLiteral,
string $escapedLiteral,
array $lessThanLiteral,
array $greaterThanLiteral,
array $commaLiteral,
array $braceLiteral,
array $commaLiteralList,
array $nestedMixed,
array $nestedMixedUnion,
array $nestedMixedBrackets,
): void {
}
// ===== NULLABLE AND OPTIONAL SCENARIOS =====
/**
* Nullable and optional parameter scenarios.
*
* @param string|null $nullableString Nullable string
* @param int|null $nullableInt Nullable integer
*/
public function nullableAndOptional(
?string $nullableString,
?int $nullableInt = null,
string $optionalString = 'default',
bool $optionalBool = true,
array $optionalArray = [],
): void {
}
// ===== UNION TYPE SCENARIOS =====
/**
* Union type parameters.
*
* @param string|int $stringOrInt String or integer
* @param bool|string|null $multiUnion Bool, string or null
*/
public function unionTypes(
string|int $stringOrInt,
bool|string|null $multiUnion,
): void {
}
// ===== VARIADIC SCENARIOS =====
/**
* Variadic parameter with a complete definition.
*/
public function variadicCompleteDefinition(
#[Schema(definition: ['type' => 'array', 'description' => 'Tags to apply.', 'items' => ['type' => 'string']])]
string ...$tags,
): void {
}
/**
* Variadic parameter with both a docblock description and a complete definition;
* the definition's own description must win.
*
* @param string ...$tags Docblock description that must lose to the definition's own
*/
public function variadicCompleteDefinitionWithDocblockDescription(
#[Schema(definition: ['type' => 'array', 'description' => 'Tags to apply.', 'items' => ['type' => 'string']])]
string ...$tags,
): void {
}
/**
* Variadic parameter scenarios.
*
* @param string ...$items Variadic strings
*/
public function variadicStrings(string ...$items): void
{
}
/**
* Variadic parameter without a type hint.
*
* @param mixed ...$values Variadic values
*/
public function untypedVariadic(...$values): void
{
}
/**
* Variadic with Schema constraints.
*
* @param int ...$numbers Variadic integers
*/
public function variadicWithConstraints(
#[Schema(items: ['type' => 'integer', 'minimum' => 0])]
int ...$numbers,
): void {
}
// ===== MIXED TYPE SCENARIOS =====
/**
* Mixed type scenarios.
*
* @param mixed $anyValue Any value
* @param mixed $optionalAny Optional any value
*/
public function mixedTypes(
mixed $anyValue,
mixed $optionalAny = 'default',
): void {
}
// ===== COMPLEX NESTED SCENARIOS =====
/**
* Complex nested Schema constraints.
*/
#[McpTool(
outputSchema: [
'type' => 'object',
'additionalProperties' => true,
]
)]
public function complexNestedSchema(
#[Schema(
type: 'object',
properties: [
'customer' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'string', 'pattern' => '^CUS-[0-9]{6}$'],
'name' => ['type' => 'string', 'minLength' => 2],
'email' => ['type' => 'string', 'format' => 'email'],
],
'required' => ['id', 'name'],
],
'items' => [
'type' => 'array',
'minItems' => 1,
'items' => [
'type' => 'object',
'properties' => [
'product_id' => ['type' => 'string', 'pattern' => '^PRD-[0-9]{4}$'],
'quantity' => ['type' => 'integer', 'minimum' => 1],
'price' => ['type' => 'number', 'minimum' => 0],
],
'required' => ['product_id', 'quantity', 'price'],
],
],
'metadata' => [
'type' => 'object',
'additionalProperties' => true,
],
],
required: ['customer', 'items']
)]
array $order,
): array {
return ['order_id' => uniqid()];
}
// ===== TYPE PRECEDENCE SCENARIOS =====
/**
* Testing type precedence between PHP, DocBlock, and Schema.
*
* @param int $numericString DocBlock says integer despite string type hint
* @param string $stringWithConstraints String with Schema constraints
* @param array<string> $arrayWithItems Array with Schema item overrides
*/
public function typePrecedenceTest(
string $numericString,
#[Schema(format: 'email', minLength: 5)]
string $stringWithConstraints,
#[Schema(items: ['type' => 'integer', 'minimum' => 1, 'maximum' => 100])]
array $arrayWithItems,
): void {
}
// ===== ERROR EDGE CASES =====
/**
* Method with no parameters but Schema description.
*/
#[Schema(description: 'Gets server status. Takes no arguments.', properties: [])]
#[McpTool(
outputSchema: [
'type' => 'object',
'additionalProperties' => true,
]
)]
public function noParamsWithSchema(): array
{
return ['status' => 'OK'];
}
/**
* Parameter with Schema but inferred type.
*/
public function parameterSchemaInferredType(
#[Schema(description: 'Some parameter', minLength: 3)]
$inferredParam,
): void {
}
public function withParameterNamedSession(string $_session): void
{
}
public function withParameterNamedSessionWithWeirdCase(string $_sesSion): void
{
}
public function withParameterNamedRequest(string $_request): void
{
}
/**
* @param string $query The search query
*/
public function withInjectableParameters(string $query, ClientGateway $gateway, RequestContext $context, int $limit = 10): void
{
}
// ===== OUTPUT SCHEMA FIXTURES =====
#[McpTool(
outputSchema: [
'type' => 'object',
'properties' => [
'message' => ['type' => 'string'],
],
'required' => ['message'],
'description' => 'The result of the operation',
]
)]
public function returnWithExplicitOutputSchema(): array
{
return ['message' => 'result'];
}
}