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
+12-5Lines changed: 12 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,17 @@
1
1
# MCP PHP SDK
2
2
3
-
The official PHP SDK for Model Context Protocol (MCP). It provides a framework-agnostic API for implementing MCP servers and clients in PHP.
3
+
The official PHP SDK for Model Context Protocol (MCP). It provides a framework-agnostic API for implementing MCP servers
4
+
and clients in PHP.
4
5
5
6
> [!IMPORTANT]
6
-
> This SDK is currently in active development with ongoing refinement of its architecture and features. While functional, the API may experience changes as we work toward stabilization.
7
+
> This SDK is currently in active development with ongoing refinement of its architecture and features. While
8
+
> functional, the API may experience changes as we work toward stabilization.
7
9
>
8
10
> If you want to help us stabilize the SDK, please see the [issue tracker](https://github.com/modelcontextprotocol/php-sdk/issues).
9
11
10
-
This project represents a collaboration between [the PHP Foundation](https://thephp.foundation/) and the [Symfony project](https://symfony.com/). It adopts development practices and standards from the Symfony project, including [Coding Standards](https://symfony.com/doc/current/contributing/code/standards.html) and the [Backward Compatibility Promise](https://symfony.com/doc/current/contributing/code/bc.html).
12
+
This project represents a collaboration between [the PHP Foundation](https://thephp.foundation/) and the [Symfony project](https://symfony.com/). It adopts
13
+
development practices and standards from the Symfony project, including [Coding Standards](https://symfony.com/doc/current/contributing/code/standards.html) and the
Until the first major release, this SDK is considered [experimental](https://symfony.com/doc/current/contributing/code/experimental.html).
13
17
@@ -249,11 +253,14 @@ $server = Server::builder()
249
253
250
254
## Contributing
251
255
252
-
We are passionate about supporting contributors of all levels of experience and would love to see you get involved in the project. See the [contributing guide](CONTRIBUTING.md) to get started before you [report issues](https://github.com/modelcontextprotocol/php-sdk/issues) and [send pull requests](https://github.com/modelcontextprotocol/php-sdk/pulls).
256
+
We are passionate about supporting contributors of all levels of experience and would love to see you get involved in
257
+
the project. See the [contributing guide](CONTRIBUTING.md) to get started before you [report issues](https://github.com/modelcontextprotocol/php-sdk/issues) and [send pull requests](https://github.com/modelcontextprotocol/php-sdk/pulls).
253
258
254
259
## Credits
255
260
256
-
The starting point for this SDK was the [PHP-MCP](https://github.com/php-mcp/server) project, initiated by [Kyrian Obikwelu](https://github.com/CodeWithKyrian), and the [Symfony AI initiative](https://github.com/symfony/ai). We are grateful for the work done by both projects and their contributors, which created a solid foundation for this SDK.
261
+
The starting point for this SDK was the [PHP-MCP](https://github.com/php-mcp/server) project, initiated by
262
+
[Kyrian Obikwelu](https://github.com/CodeWithKyrian), and the [Symfony AI initiative](https://github.com/symfony/ai). We are grateful for the work
263
+
done by both projects and their contributors, which created a solid foundation for this SDK.
Copy file name to clipboardExpand all lines: docs/examples.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
# Examples
2
2
3
-
The MCP PHP SDK includes comprehensive examples demonstrating different patterns and use cases. Each example showcases specific features and can be run independently to understand how the SDK works.
3
+
The MCP PHP SDK includes comprehensive examples demonstrating different patterns and use cases. Each example showcases
4
+
specific features and can be run independently to understand how the SDK works.
4
5
5
6
## Table of Contents
6
7
@@ -13,7 +14,8 @@ The MCP PHP SDK includes comprehensive examples demonstrating different patterns
13
14
14
15
## Getting Started
15
16
16
-
All examples are located in the `examples/` directory and use the SDK dependencies from the root project. Most examples can be run directly without additional setup.
17
+
All examples are located in the `examples/` directory and use the SDK dependencies from the root project. Most examples
Copy file name to clipboardExpand all lines: docs/mcp-elements.md
+16-7Lines changed: 16 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# MCP Elements
2
2
3
-
MCP elements are the core capabilities of your server: Tools, Resources, Resource Templates, and Prompts. These elements define what your server can do and how clients can interact with it. The PHP MCP SDK provides both attribute-based discovery and manual registration methods.
3
+
MCP elements are the core capabilities of your server: Tools, Resources, Resource Templates, and Prompts. These elements
4
+
define what your server can do and how clients can interact with it. The PHP MCP SDK provides both attribute-based
5
+
discovery and manual registration methods.
4
6
5
7
## Table of Contents
6
8
@@ -26,7 +28,8 @@ MCP defines four types of capabilities:
26
28
27
29
Each capability can be registered using two methods:
28
30
29
-
1.**Attribute-Based Discovery**: Use PHP attributes (`#[McpTool]`, `#[McpResource]`, etc.) on methods or classes. The server automatically discovers and registers them.
31
+
1.**Attribute-Based Discovery**: Use PHP attributes (`#[McpTool]`, `#[McpResource]`, etc.) on methods or classes. The
32
+
server automatically discovers and registers them.
@@ -315,7 +318,8 @@ public function getFile(string $path): string
315
318
316
319
## Resource Templates
317
320
318
-
Resource templates are **dynamic resources** that use parameterized URIs with variables. They follow all the same rules as static resources (URI schemas, return values, MIME types, etc.) but accept variables using [RFC 6570 URI template syntax](https://datatracker.ietf.org/doc/html/rfc6570).
321
+
Resource templates are **dynamic resources** that use parameterized URIs with variables. They follow all the same rules
322
+
as static resources (URI schemas, return values, MIME types, etc.) but accept variables using [RFC 6570 URI template syntax](https://datatracker.ietf.org/doc/html/rfc6570).
319
323
320
324
```php
321
325
use Mcp\Capability\Attribute\McpResourceTemplate;
@@ -476,7 +480,9 @@ The SDK automatically validates that all messages have valid roles and converts
476
480
477
481
## Completion Providers
478
482
479
-
Completion providers help MCP clients offer auto-completion suggestions for Resource Templates and Prompts. Unlike Tools and static Resources (which can be listed via `tools/list` and `resources/list`), Resource Templates and Prompts have dynamic parameters that benefit from completion hints.
483
+
Completion providers help MCP clients offer auto-completion suggestions for Resource Templates and Prompts. Unlike Tools
484
+
and static Resources (which can be listed via `tools/list` and `resources/list`), Resource Templates and Prompts have
485
+
dynamic parameters that benefit from completion hints.
480
486
481
487
### Completion Provider Types
482
488
@@ -572,11 +578,13 @@ public function getUserProfile(
572
578
573
579
> **Important**
574
580
>
575
-
> Completion providers only offer **suggestions** to users. Users can still input any value, so **always validate parameters** in your handlers. Providers don't enforce validation - they're purely for UX improvement.
581
+
> Completion providers only offer **suggestions** to users. Users can still input any value, so **always validate
582
+
> parameters** in your handlers. Providers don't enforce validation - they're purely for UX improvement.
576
583
577
584
## Schema Generation and Validation
578
585
579
-
The SDK automatically generates JSON schemas for **tool parameters** using a sophisticated priority system. Schema generation applies to both attribute-discovered and manually registered tools.
586
+
The SDK automatically generates JSON schemas for **tool parameters** using a sophisticated priority system. Schema
587
+
generation applies to both attribute-discovered and manually registered tools.
580
588
581
589
### Schema Generation Priority
582
590
@@ -682,7 +690,8 @@ public function makeApiRequest(string $endpoint, string $method, array $headers)
682
690
}
683
691
```
684
692
685
-
**Warning:** Only use complete schema override if you're well-versed with JSON Schema specification and have complex validation requirements that cannot be achieved through the priority system.
693
+
**Warning:** Only use complete schema override if you're well-versed with JSON Schema specification and have complex
694
+
validation requirements that cannot be achieved through the priority system.
Copy file name to clipboardExpand all lines: docs/server-builder.md
+23-10Lines changed: 23 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Server Builder
2
2
3
-
The server `Builder` is a fluent builder class that simplifies the creation and configuration of an MCP server instance. It provides methods for setting server information, configuring discovery, registering capabilities, and customizing various aspects of the server behavior.
3
+
The server `Builder` is a fluent builder class that simplifies the creation and configuration of an MCP server instance.
4
+
It provides methods for setting server information, configuring discovery, registering capabilities, and customizing
5
+
various aspects of the server behavior.
4
6
5
7
## Table of Contents
6
8
@@ -40,7 +42,8 @@ $server = (new Builder())
40
42
->build();
41
43
```
42
44
43
-
Both methods return a `Builder` instance that you can configure with fluent methods. The `build()` method returns the final `Server` instance ready for use.
45
+
Both methods return a `Builder` instance that you can configure with fluent methods. The `build()` method returns the
46
+
final `Server` instance ready for use.
44
47
45
48
## Server Configuration
46
49
@@ -126,7 +129,8 @@ $server = Server::builder()
126
129
- If scanning `lib/` and there's `lib/tests/`, it will be excluded
127
130
- But if `vendor/` and `tests/` are at the same level as `src/`, they're not scanned anyway (not in `scanDirs`)
128
131
129
-
> **Performance**: Always use a cache in production. The first run scans and caches all discovered MCP elements, making subsequent server startups nearly instantaneous.
132
+
> **Performance**: Always use a cache in production. The first run scans and caches all discovered MCP elements, making
133
+
> subsequent server startups nearly instantaneous.
130
134
131
135
## Session Management
132
136
@@ -282,15 +286,17 @@ $server = Server::builder()
282
286
);
283
287
```
284
288
285
-
**Note:**`name` and `description` are optional for all manual registrations. If not provided, they will be derived from the handler's method name and docblock.
289
+
**Note:**`name` and `description` are optional for all manual registrations. If not provided, they will be derived from
290
+
the handler's method name and docblock.
286
291
287
292
For more details on MCP elements, handlers, and attribute-based discovery, see [MCP Elements](mcp-elements.md).
288
293
289
294
## Service Dependencies
290
295
291
296
### Container
292
297
293
-
The container is used to resolve handlers and their dependencies when handlers inject dependencies in their constructors. The SDK includes a basic container with simple auto-wiring capabilities.
298
+
The container is used to resolve handlers and their dependencies when handlers inject dependencies in their constructors.
299
+
The SDK includes a basic container with simple auto-wiring capabilities.
294
300
295
301
```php
296
302
use Mcp\Capability\Registry\Container;
@@ -340,7 +346,8 @@ $server = Server::builder()
340
346
341
347
## Custom Capability Handlers
342
348
343
-
**Advanced customization for specific use cases.** Override the default capability handlers when you need completely custom behavior for how tools are executed, resources are read, or prompts are generated. Most users should stick with the default implementations.
349
+
**Advanced customization for specific use cases.** Override the default capability handlers when you need completely custom
350
+
behavior for how tools are executed, resources are read, or prompts are generated. Most users should stick with the default implementations.
344
351
345
352
The default handlers work by:
346
353
1. Looking up registered tools/resources/prompts by name/URI
@@ -350,7 +357,8 @@ The default handlers work by:
350
357
351
358
### Custom Tool Caller
352
359
353
-
Replace how tool execution requests are processed. Your custom `ToolCallerInterface` receives a `CallToolRequest` (with tool name and arguments) and must return a `CallToolResult`.
360
+
Replace how tool execution requests are processed. Your custom `ToolCallerInterface` receives a `CallToolRequest` (with
361
+
tool name and arguments) and must return a `CallToolResult`.
354
362
355
363
```php
356
364
use Mcp\Capability\Tool\ToolCallerInterface;
@@ -377,7 +385,8 @@ $server = Server::builder()
377
385
378
386
### Custom Resource Reader
379
387
380
-
Replace how resource reading requests are processed. Your custom `ResourceReaderInterface` receives a `ReadResourceRequest` (with URI) and must return a `ReadResourceResult`.
388
+
Replace how resource reading requests are processed. Your custom `ResourceReaderInterface` receives a `ReadResourceRequest`
389
+
(with URI) and must return a `ReadResourceResult`.
381
390
382
391
```php
383
392
use Mcp\Capability\Resource\ResourceReaderInterface;
@@ -402,7 +411,8 @@ $server = Server::builder()
402
411
403
412
### Custom Prompt Getter
404
413
405
-
Replace how prompt generation requests are processed. Your custom `PromptGetterInterface` receives a `GetPromptRequest` (with prompt name and arguments) and must return a `GetPromptResult`.
414
+
Replace how prompt generation requests are processed. Your custom `PromptGetterInterface` receives a `GetPromptRequest`
415
+
(with prompt name and arguments) and must return a `GetPromptResult`.
406
416
407
417
```php
408
418
use Mcp\Capability\Prompt\PromptGetterInterface;
@@ -426,7 +436,10 @@ $server = Server::builder()
426
436
->setPromptGetter(new CustomPromptGetter());
427
437
```
428
438
429
-
> **Warning**: Custom capability handlers bypass the entire default registration system (discovered attributes, manual registration, container resolution, etc.). You become responsible for all aspect of execution, including error handling, logging, and result formatting. Only use this for very specific advanced use cases like custom authentication, complex routing, or integration with external systems.
439
+
> **Warning**: Custom capability handlers bypass the entire default registration system (discovered attributes, manual
440
+
> registration, container resolution, etc.). You become responsible for all aspect of execution, including error handling,
441
+
> logging, and result formatting. Only use this for very specific advanced use cases like custom authentication, complex
Copy file name to clipboardExpand all lines: docs/transports.md
+10-5Lines changed: 10 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
# Transports
2
2
3
-
Transports handle the communication layer between MCP servers and clients. The PHP MCP SDK provides two main transport implementations: STDIO for command-line integration and HTTP for web-based communication.
3
+
Transports handle the communication layer between MCP servers and clients. The PHP MCP SDK provides two main transport
4
+
implementations: STDIO for command-line integration and HTTP for web-based communication.
4
5
5
6
## Table of Contents
6
7
@@ -45,7 +46,8 @@ $transport = new StdioTransport(
45
46
-**`logger`** (optional): `LoggerInterface` - PSR-3 logger for debugging. Defaults to `NullLogger`.
46
47
47
48
> [!IMPORTANT]
48
-
> When using STDIO transport, **never** write to `STDOUT` in your handlers as it's reserved for JSON-RPC communication. Use `STDERR` for debugging instead.
49
+
> When using STDIO transport, **never** write to `STDOUT` in your handlers as it's reserved for JSON-RPC communication.
50
+
> Use `STDERR` for debugging instead.
49
51
50
52
### Example Server Script
51
53
@@ -90,7 +92,8 @@ For MCP clients like Claude Desktop:
90
92
91
93
## HTTP Transport
92
94
93
-
The HTTP transport was designed to sit between any PHP project, regardless of the HTTP implementation or how they receive and process requests and send responses. It provides a flexible architecture that can integrate with any PSR-7 compatible application.
95
+
The HTTP transport was designed to sit between any PHP project, regardless of the HTTP implementation or how they receive
96
+
and process requests and send responses. It provides a flexible architecture that can integrate with any PSR-7 compatible application.
94
97
95
98
```php
96
99
use Psr\Http\Message\ServerRequestInterface;
@@ -114,7 +117,8 @@ $transport = new StreamableHttpTransport(
114
117
115
118
### Architecture
116
119
117
-
The HTTP transport doesn't run its own web server. Instead, it processes PSR-7 requests and returns PSR-7 responses that your application can handle however it needs to:
120
+
The HTTP transport doesn't run its own web server. Instead, it processes PSR-7 requests and returns PSR-7 responses that
121
+
your application can handle however it needs to:
118
122
119
123
```
120
124
Your Web App → PSR-7 Request → StreamableHttpTransport → PSR-7 Response → Your Web App
@@ -294,7 +298,8 @@ You should route **all methods** to your MCP endpoint, not just POST.
294
298
295
299
### Session Management
296
300
297
-
HTTP transport requires persistent sessions since PHP doesn't maintain state between requests. Unlike STDIO transport where in-memory sessions work fine, HTTP transport needs a persistent session store:
301
+
HTTP transport requires persistent sessions since PHP doesn't maintain state between requests. Unlike STDIO transport
302
+
where in-memory sessions work fine, HTTP transport needs a persistent session store:
0 commit comments