$git clone https://github.com/php-mcp/serverA comprehensive PHP SDK for building [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) servers. Create production-ready MCP servers in PHP with modern architecture, extensive testing, and flexible transport options.
| 1 | # PHP MCP Server SDK |
| 2 | |
| 3 | [](https://packagist.org/packages/php-mcp/server) |
| 4 | [](https://packagist.org/packages/php-mcp/server) |
| 5 | [](https://github.com/php-mcp/server/actions/workflows/tests.yml) |
| 6 | [](LICENSE) |
| 7 | |
| 8 | **A comprehensive PHP SDK for building [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) servers. Create production-ready MCP servers in PHP with modern architecture, extensive testing, and flexible transport options.** |
| 9 | |
| 10 | This SDK enables you to expose your PHP application's functionality as standardized MCP **Tools**, **Resources**, and **Prompts**, allowing AI assistants (like Anthropic's Claude, Cursor IDE, OpenAI's ChatGPT, etc.) to interact with your backend using the MCP standard. |
| 11 | |
| 12 | ## 🚀 Key Features |
| 13 | |
| 14 | - **🏗️ Modern Architecture**: Built with PHP 8.1+ features, PSR standards, and modular design |
| 15 | - **📡 Multiple Transports**: Supports `stdio`, `http+sse`, and new **streamable HTTP** with resumability |
| 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 |
| 18 | - **📝 Smart Schema Generation**: Automatic JSON schema generation from method signatures with optional `#[Schema]` attribute enhancements |
| 19 | - **⚡ Session Management**: Advanced session handling with multiple storage backends |
| 20 | - **🔄 Event-Driven**: ReactPHP-based for high concurrency and non-blocking operations |
| 21 | - **📊 Batch Processing**: Full support for JSON-RPC batch requests |
| 22 | - **💾 Smart Caching**: Intelligent caching of discovered elements with manual override precedence |
| 23 | - **🧪 Completion Providers**: Built-in support for argument completion in tools and prompts |
| 24 | - **🔌 Dependency Injection**: Full PSR-11 container support with auto-wiring |
| 25 | - **📋 Comprehensive Testing**: Extensive test suite with integration tests for all transports |
| 26 | |
| 27 | This package supports the **2025-03-26** version of the Model Context Protocol with backward compatibility. |
| 28 | |
| 29 | ## 📋 Requirements |
| 30 | |
| 31 | - **PHP** >= 8.1 |
| 32 | - **Composer** |
| 33 | - **For HTTP Transport**: An event-driven PHP environment (CLI recommended) |
| 34 | - **Extensions**: `json`, `mbstring`, `pcre` (typically enabled by default) |
| 35 | |
| 36 | ## 📦 Installation |
| 37 | |
| 38 | ```bash |
| 39 | composer require php-mcp/server |
| 40 | ``` |
| 41 | |
| 42 | > **💡 Laravel Users**: Consider using [`php-mcp/laravel`](https://github.com/php-mcp/laravel) for enhanced framework integration, configuration management, and Artisan commands. |
| 43 | |
| 44 | ## ⚡ Quick Start: Stdio Server with Discovery |
| 45 | |
| 46 | This example demonstrates the most common usage pattern - a `stdio` server using attribute discovery. |
| 47 | |
| 48 | **1. Define Your MCP Elements** |
| 49 | |
| 50 | Create `src/CalculatorElements.php`: |
| 51 | |
| 52 | ```php |
| 53 | <?php |
| 54 | |
| 55 | namespace App; |
| 56 | |
| 57 | use PhpMcp\Server\Attributes\McpTool; |
| 58 | use PhpMcp\Server\Attributes\Schema; |
| 59 | |
| 60 | class CalculatorElements |
| 61 | { |
| 62 | /** |
| 63 | * Adds two numbers together. |
| 64 | * |
| 65 | * @param int $a The first number |
| 66 | * @param int $b The second number |
| 67 | * @return int The sum of the two numbers |
| 68 | */ |
| 69 | #[McpTool(name: 'add_numbers')] |
| 70 | public function add(int $a, int $b): int |
| 71 | { |
| 72 | return $a + $b; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Calculates power with validation. |
| 77 | */ |
| 78 | #[McpTool(name: 'calculate_power')] |
| 79 | public function power( |
| 80 | #[Schema(type: 'number', minimum: 0, maximum: 1000)] |
| 81 | float $base, |
| 82 | |
| 83 | #[Schema(type: 'integer', minimum: 0, maximum: 10)] |
| 84 | int $exponent |
| 85 | ): float { |
| 86 | return pow($base, $exponent); |
| 87 | } |
| 88 | } |
| 89 | ``` |
| 90 | |
| 91 | **2. Create the Server Script** |
| 92 | |
| 93 | Create `mcp-server.php`: |
| 94 | |
| 95 | ```php |
| 96 | #!/usr/bin/env php |
| 97 | <?php |
| 98 | |
| 99 | declare(st |