dalehurley / php-mcp-sdk
Model Context Protocol (MCP) implementation for PHP
Requires
- php: ^8.1
- ext-json: *
- ext-mbstring: *
- amphp/amp: ^3.0
- amphp/byte-stream: ^2.0
- amphp/http-client: ^5.0
- amphp/http-server: ^3.0
- amphp/process: ^2.0
- amphp/socket: ^2.0
- amphp/websocket-server: ^4.0
- evenement/evenement: ^3.0
- guzzlehttp/guzzle: ^7.8
- guzzlehttp/psr7: ^2.6
- monolog/monolog: ^3.5
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- ramsey/uuid: ^4.7
- respect/validation: ^2.2
- symfony/console: ^6.4|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.48
- mockery/mockery: ^1.6
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.7
- symfony/var-dumper: ^6.4|^7.0
- vimeo/psalm: ^5.20 || ^6.0@dev
Suggests
- amphp/http-server-sse: For Server-Sent Events support
- amphp/websocket-client: For WebSocket client support
- firebase/php-jwt: For JWT token parsing and validation
- league/oauth2-client: For OAuth 2.0 client support
- ratchet/pawl: Alternative WebSocket client
README
PHP implementation of the Model Context Protocol (MCP), enabling seamless integration between LLM applications and external data sources and tools.
โจ Features
- ๐ Complete MCP Protocol Support - Full implementation of the MCP specification
- ๐ง Type-Safe - Leverages PHP 8.1+ type system with enums, union types, and strict typing
- โก Async First - Built on Amphp for non-blocking I/O operations
- ๐ Multiple Transports - STDIO, HTTP Streaming, and WebSocket
- ๐ OAuth 2.0 Ready - Built-in authentication with PKCE support
- ๐๏ธ Framework Integration - Laravel, Symfony, and PSR-compatible design
- ๐ฆ PSR Compliant - Follows PSR-4, PSR-7, PSR-12, and PSR-15 standards
- ๐ก๏ธ Production Ready - Comprehensive error handling, logging, and monitoring
- ๐ค Agentic AI Support - Build intelligent AI agents with MCP tool orchestration
- ๐ญ Real-World Examples - Complete applications (Blog CMS, Task Manager, API Gateway)
- ๐ Comprehensive Documentation - Best-in-class documentation with tested examples
- ๐งช Automated Testing - All documentation examples are automatically tested
๐ Requirements
- PHP 8.1+ - Leverages modern PHP features
- Composer - For dependency management
- ext-json - JSON processing
- ext-mbstring - String handling
๐ Installation
Via Composer
composer require dalehurley/php-mcp-sdk
Development Version
composer require dalehurley/php-mcp-sdk:dev-main
โก Quick Start
Creating an MCP Server
#!/usr/bin/env php <?php require_once __DIR__ . '/vendor/autoload.php'; use MCP\Server\McpServer; use MCP\Server\Transport\StdioServerTransport; use MCP\Types\Implementation; use function Amp\async; // Create the simplest possible MCP server $server = new McpServer( new Implementation( 'hello-world-server', '1.0.0' ) ); // Add a simple "say_hello" tool $server->tool( 'say_hello', 'Says hello to someone', [ 'type' => 'object', 'properties' => [ 'name' => [ 'type' => 'string', 'description' => 'Name of the person to greet' ] ], 'required' => ['name'] ], function (array $args): array { $name = $args['name'] ?? 'World'; return [ 'content' => [ [ 'type' => 'text', 'text' => "Hello, {$name}! ๐ Welcome to MCP!" ] ] ]; } ); // Start the server async(function () use ($server) { echo "๐ Hello World MCP Server starting...\n"; $transport = new StdioServerTransport(); $server->connect($transport)->await(); })->await();
Creating an MCP Client
#!/usr/bin/env php <?php require_once __DIR__ . '/vendor/autoload.php'; use MCP\Client\Client; use MCP\Client\Transport\StdioClientTransport; use MCP\Types\Implementation; use Amp\Loop; // Create client $client = new Client( new Implementation('weather-client', '1.0.0') ); // Connect to weather server $transport = new StdioClientTransport([ 'command' => 'php', 'args' => [__DIR__ . '/weather-server.php'] ]); Amp\async(function() use ($client, $transport) { try { // Connect to server yield $client->connect($transport); echo "โ Connected to weather server\n"; // List available tools $result = yield $client->listTools(); echo "๐ Available tools:\n"; foreach ($result['tools'] as $tool) { echo " - {$tool['name']}: {$tool['description']}\n"; } // Call the weather tool $result = yield $client->callTool('get-weather', [ 'location' => 'London, UK' ]); echo "\n๐ค๏ธ Weather result:\n"; echo $result['content'][0]['text'] . "\n"; yield $client->close(); } catch (\Exception $error) { echo "โ Error: " . $error->getMessage() . "\n"; } finally { Loop::stop(); } }); Loop::run();
Test Your Implementation
# Run the hello-world server php examples/getting-started/hello-world-server.php # Test with Claude Desktop by adding to your configuration: { "mcpServers": { "hello-world": { "command": "php", "args": ["/path/to/examples/getting-started/hello-world-server.php"] } } } # Or test with the MCP Inspector (Node.js required) npx @modelcontextprotocol/inspector examples/getting-started/hello-world-server.php
๐ฏ Examples Overview
The PHP MCP SDK includes 20+ comprehensive examples across all skill levels:
๐ Getting Started (4 examples)
- Hello World - Simplest possible server and client
- Calculator - Multi-tool server with math operations
- File Reader - Secure file system integration
- Weather Client - External API integration patterns
๐๏ธ Framework Integration (2 examples)
- Laravel Integration - Complete Laravel patterns with service container
- Symfony Integration - Full Symfony integration with DI container
๐ค Agentic AI (4 examples)
- Working Agentic Demo - Rule-based agent reasoning
- Personal Assistant - Multi-MCP server coordination
- Multi-Agent Orchestrator - Specialized agent coordination
- OpenAI Integration - LLM-powered intelligent agents
๐ญ Real-World Applications (5 examples)
- Blog CMS - Complete content management system
- Task Manager - Project management with analytics
- API Gateway - Enterprise API orchestration
- Code Analyzer - Development quality tools
- Data Pipeline - ETL and data processing
๐ข Enterprise & Production (3 examples)
- Docker Deployment - Production containerization
- Microservices Architecture - Distributed systems patterns
- Monitoring & Observability - Production monitoring
All examples are tested and working! ๐
Framework Integration
The PHP MCP SDK is designed to work with any PHP framework through its PSR-compliant architecture.
Laravel Integration
You can use the core PHP MCP SDK directly in Laravel applications:
composer require dalehurley/php-mcp-sdk
// In a Laravel controller or service use MCP\Server\McpServer; use MCP\Types\Implementation; class McpController extends Controller { public function createServer() { $server = new McpServer( new Implementation('my-laravel-app', '1.0.0') ); // Register your tools, resources, and prompts $server->tool('search-users', 'Search for users', [ 'type' => 'object', 'properties' => [ 'query' => ['type' => 'string', 'description' => 'Search query'] ] ], function($params) { return [ 'content' => [ [ 'type' => 'text', 'text' => json_encode(User::where('name', 'like', "%{$params['query']}%")->get()) ] ] ]; }); return $server; } }
For a complete Laravel package with service providers, Artisan commands, and Laravel-specific features, see the separate laravel-mcp-sdk
package.
๐ Documentation
The most comprehensive MCP SDK documentation in the ecosystem is available in the docs/ directory:
๐ Getting Started (Beginner-Friendly)
- ๐ Complete Documentation - Start here for full overview
- โก Quick Start Guide - Get up and running in 5 minutes
- ๐๏ธ First Server - Build your first server in 10 minutes
- ๐ฑ First Client - Build your first client in 10 minutes
- ๐ง Understanding MCP - Deep dive into MCP protocol
- ๐ก Core Concepts - Understand MCP fundamentals
- ๐ง Troubleshooting - Common issues and solutions
๐๏ธ Implementation Guides
- ๐ฑ Creating Clients - Build MCP clients
- ๐ Security Best Practices - OAuth 2.0 and security
- ๐ ๏ธ Tools Guide - Server development with tools
- ๐ Resources Guide - Server development with resources
- ๐ค OpenAI Integration - AI tool calling
- ๐ FullCX Integration - Product management
๐ค Agentic AI Development
- ๐ง Build Agentic AI Agents - Complete agentic AI tutorial
- ๐ฏ Agent Examples - Working agent implementations
- ๐ Multi-Agent Systems - Agent coordination
๐ญ Real-World Applications
- ๐ Blog CMS - Complete content management system
- ๐ Task Manager - Project management system
- ๐ API Gateway - Enterprise API management
- ๐ Code Analyzer - Development quality tools
- ๐ Data Pipeline - ETL and data processing
๐ข Enterprise & Production
- ๐ณ Docker Deployment - Containerization
- ๐๏ธ Microservices - Distributed systems
- ๐ Monitoring - Observability
๐ API Reference
- ๐ง Server API - Complete server API
- ๐ก Client API - Complete client API
- ๐ Types & Schemas - Type system reference
๐ Migration & Examples
- ๐ป Working Examples - 20+ tested examples
- ๐ TypeScript Migration - Migration guide
Testing
# Run tests composer test # Run tests with coverage composer test-coverage # Run static analysis composer phpstan composer psalm # Fix code style composer cs-fix # Run all checks composer check
Contributing
Contributions are welcome! Please read our Contributing Guide for details.
Changelog
All notable changes to this project are documented in the CHANGELOG.md. Please update it when making changes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Based on the MCP TypeScript SDK
- Built with Amphp for async operations
- Uses Respect/Validation for schema validation