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
- Support closures, class methods, static methods, and invokable classes
- Reorder handler resolution logic to prioritize container resolution
- Add unique naming for closure handlers using spl_object_id()
- Prevent closure serialization in cache with validation warnings
- Update all registration methods to accept Closure|array|string
Copy file name to clipboardExpand all lines: README.md
+29-18Lines changed: 29 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ This SDK enables you to expose your PHP application's functionality as standardi
14
14
-**🏗️ Modern Architecture**: Built with PHP 8.1+ features, PSR standards, and modular design
15
15
-**📡 Multiple Transports**: Supports `stdio`, `http+sse`, and new **streamable HTTP** with resumability
16
16
-**🎯 Attribute-Based Definition**: Use PHP 8 Attributes (`#[McpTool]`, `#[McpResource]`, etc.) for zero-config element registration
17
+
-**🔧 Flexible Handlers**: Support for closures, class methods, static methods, and invokable classes
17
18
-**📝 Smart Schema Generation**: Automatic JSON schema generation from method signatures with optional `#[Schema]` attribute enhancements
18
19
-**⚡ Session Management**: Advanced session handling with multiple storage backends
19
20
-**🔄 Event-Driven**: ReactPHP-based for high concurrency and non-blocking operations
@@ -332,7 +333,7 @@ $server->discover(
332
333
333
334
### 2. 🔧 Manual Registration
334
335
335
-
Register elements programmatically using the `ServerBuilder` before calling `build()`. Useful for dynamic registration or when you prefer explicit control.
336
+
Register elements programmatically using the `ServerBuilder` before calling `build()`. Useful for dynamic registration, closures, or when you prefer explicit control.
336
337
337
338
```php
338
339
use App\Handlers\{EmailHandler, ConfigHandler, UserHandler, PromptHandler};
@@ -354,10 +355,21 @@ $server = Server::make()
354
355
// Register invokable class as tool
355
356
->withTool(UserHandler::class) // Handler: Invokable class
356
357
357
-
// Register a resource
358
+
// Register a closure as tool
359
+
->withTool(
360
+
function(int $a, int $b): int { // Handler: Closure
['role' => 'user', 'content' => "Write about {$topic} in a {$tone} tone"]
387
+
];
388
+
},
389
+
name: 'writing_prompt' // Prompt name (optional)
374
390
)
375
391
376
392
->build();
377
393
```
378
394
379
-
**Key Features:**
395
+
The server supports three flexible handler formats: `[ClassName::class, 'methodName']` for class method handlers, `InvokableClass::class` for invokable class handlers (classes with `__invoke` method), and any PHP callable including closures, static methods like `[SomeClass::class, 'staticMethod']`, or function names. Class-based handlers are resolved via the configured PSR-11 container for dependency injection. Manual registrations are never cached and take precedence over discovered elements with the same identifier.
380
396
381
-
-**Handler Formats**: Use `[ClassName::class, 'methodName']` or `InvokableClass::class`
382
-
-**Dependency Injection**: Handlers resolved via configured PSR-11 container
383
-
-**Immediate Registration**: Elements registered when `build()` is called
384
-
-**No Caching**: Manual elements are never cached (always fresh)
385
-
-**Precedence**: Manual registrations override discovered elements with same identifier
397
+
> [!IMPORTANT]
398
+
> When using closures as handlers, the server generates minimal JSON schemas based only on PHP type hints since there are no docblocks or class context available. For more detailed schemas with validation constraints, descriptions, and formats, you have two options:
399
+
>
400
+
> - Use the [`#[Schema]` attribute](#-schema-generation-and-validation) for enhanced schema generation
401
+
> - Provide a custom `$inputSchema` parameter when registering tools with `->withTool()`
386
402
387
403
### 🏆 Element Precedence & Discovery
388
404
@@ -1289,8 +1305,3 @@ The MIT License (MIT). See [LICENSE](LICENSE) for details.
1289
1305
- Built on the [Model Context Protocol](https://modelcontextprotocol.io/) specification
1290
1306
- Powered by [ReactPHP](https://reactphp.org/) for async operations
1291
1307
- Uses [PSR standards](https://www.php-fig.org/) for maximum interoperability
1292
-
1293
-
---
1294
-
1295
-
**Ready to build powerful MCP servers with PHP?** Start with our [Quick Start](#-quick-start-stdio-server-with-discovery) guide! 🚀
0 commit comments