You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+52-1Lines changed: 52 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ This package simplifies building MCP servers through:
14
14
***Attribute-Based Definition:** Define MCP elements using PHP 8 Attributes (`#[McpTool]`, `#[McpResource]`, `#[McpPrompt]`, `#[McpResourceTemplate]`) on your methods or invokable classes.
15
15
***Manual Registration:** Programmatically register elements using a fluent builder API.
16
16
***Explicit Discovery:** Trigger attribute scanning on demand via the `$server->discover()` method.
17
-
***Metadata Inference:** Intelligently generate MCP schemas and descriptions from type hints and DocBlocks.
17
+
***Metadata Inference:** Intelligently generate MCP schemas and descriptions from PHP type hints and DocBlocks.
18
18
***Selective Caching:** Optionally cache *discovered* element definitions to speed up startup, while always preserving manually registered elements.
19
19
***Flexible Transports:** Supports `stdio` and `http+sse`, separating core logic from network communication.
20
20
***PSR Compliance:** Integrates with PSR-3 (Logging), PSR-11 (Container), and PSR-16 (SimpleCache).
@@ -138,6 +138,8 @@ The server uses a decoupled architecture:
138
138
***`Server`:** The central object holding the configured state and core logic components (`Registry`, `Processor`, `ClientStateManager`, `Configuration`). It's transport-agnostic. Provides methods to `discover()` elements and `listen()` via a specific transport.
139
139
***`Registry`:** Stores MCP element definitions. **Distinguishes between manually registered and discovered elements.** Handles optional caching of *discovered* elements only. Loads cached discovered elements upon instantiation if available.
***`Protocol`:** Internal bridge listening to transport events, interacting with `Processor` and `ClientStateManager`.
@@ -324,6 +326,16 @@ The value returned by your method determines the content sent back to the client
324
326
325
327
The method's return type hint (`@return` tag in DocBlock) is used to generate the tool's output schema, but the actual formatting depends on the *value* returned at runtime.
326
328
329
+
**Schema Generation**
330
+
331
+
The server automatically generates JSON Schema for tool parameters based on:
332
+
333
+
1. PHP type hints
334
+
2. DocBlock annotations
335
+
3. Schema attributes (for enhanced validation)
336
+
337
+
**Examples:**
338
+
327
339
```php
328
340
/**
329
341
* Fetches user details by ID.
@@ -338,6 +350,18 @@ public function getUserById(int $userId, bool $includeEmail = false): array
* @param string[] $tags Tags associated with the user
358
+
* @return array{success: bool, message: string}
359
+
*/
360
+
#[McpTool]
361
+
public function processUserData(array $userData, array $tags): array {
362
+
// Implementation
363
+
}
364
+
341
365
/**
342
366
* Returns PHP code as formatted text.
343
367
*
@@ -366,6 +390,33 @@ class AdderTool {
366
390
}
367
391
```
368
392
393
+
**Additional Validation with `#[Schema]`**
394
+
395
+
For enhanced schema generation and parameter validation, you can use the `Schema` attribute:
396
+
397
+
```php
398
+
use PhpMcp\Server\Attributes\Schema;
399
+
use PhpMcp\Server\Attributes\Schema\Format;
400
+
use PhpMcp\Server\Attributes\Schema\ArrayItems;
401
+
use PhpMcp\Server\Attributes\Schema\Property;
402
+
403
+
/**
404
+
* Validates user information.
405
+
*/
406
+
#[McpTool]
407
+
public function validateUser(
408
+
#[Schema(format: 'email')]
409
+
string $email,
410
+
411
+
#[Schema(minItems: 2, uniqueItems: true)]
412
+
array $tags
413
+
): bool {
414
+
// Implementation
415
+
}
416
+
```
417
+
418
+
The Schema attribute adds JSON Schema constraints like string formats, numeric ranges, array constraints, and object property validations.
419
+
369
420
#### `#[McpResource]`
370
421
371
422
Marks a method **or an invokable class** as representing a specific, static MCP Resource instance. Resources represent pieces of content or data identified by a URI. The target method (or `__invoke`) will typically be called when a client performs a `resources/read` for the specified URI.
0 commit comments