Php MCP
A complete PHP implementation of the Model Context Protocol (MCP) with server and client support, STDIO and HTTP transports, and framework integration
Ask AI about Php MCP
Powered by Claude ยท Grounded in docs
I know everything about Php MCP. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
PHP MCP
A complete PHP implementation of the Model Context Protocol (MCP), providing both server and client functionality with support for multiple transport protocols.
Language: English | ็ฎไฝไธญๆ
โจ Key Features
- ๐ Latest MCP Protocol - Supports MCP 2025-03-26 specification
- ๐ง Complete Implementation - Tools, resources, and prompts support
- ๐ Multiple Transports - STDIO โ , HTTP โ , Streamable HTTP ๐ง
- ๐ Framework Compatible - Works with any PSR-compliant framework, built-in Hyperf integration
- ๐ Well Documented - Comprehensive guides in English and Chinese
๐ Quick Start
Installation
composer require dtyq/php-mcp
Hyperf Framework Quick Integration
If you're using Hyperf framework, integration is extremely simple:
// Just one line of code!
Router::addRoute(['POST', 'GET', 'DELETE'], '/mcp', function () {
return \Hyperf\Context\ApplicationContext::getContainer()->get(HyperfMcpServer::class)->handle('default');
});
Annotation-Based Registration:
class CalculatorService
{
#[McpTool(description: 'Mathematical calculations')]
public function calculate(string $operation, int $a, int $b): array
{
return ['result' => match($operation) {
'add' => $a + $b,
'multiply' => $a * $b,
default => 0
}];
}
#[McpResource(description: 'System information')]
public function systemInfo(): TextResourceContents
{
return new TextResourceContents('mcp://system/info',
json_encode(['php' => PHP_VERSION]), 'application/json');
}
}
Advanced Options:
- ๐ AuthenticatorInterface - Custom authentication
- ๐ HttpTransportAuthenticatedEvent - Dynamic tool/resource registration
- ๐ Annotation System - Auto-register tools, resources and prompts
๐ View Complete Hyperf Integration Guide
Basic Server Example
<?php
require_once 'vendor/autoload.php';
use Dtyq\PhpMcp\Server\McpServer;
use Dtyq\PhpMcp\Shared\Kernel\Application;
// Create server with simple container
$container = /* your PSR-11 container */;
$app = new Application($container, ['sdk_name' => 'my-server']);
$server = new McpServer('my-server', '1.0.0', $app);
// Add a tool
$server->registerTool(
new \Dtyq\PhpMcp\Types\Tools\Tool('echo', [
'type' => 'object',
'properties' => ['message' => ['type' => 'string']],
'required' => ['message']
], 'Echo a message'),
function(array $args): array {
return ['response' => $args['message']];
}
);
// Start server
$server->stdio(); // or $server->http($request)
Basic Client Example
<?php
use Dtyq\PhpMcp\Client\McpClient;
use Dtyq\PhpMcp\Client\Configuration\StdioConfig;
$client = new McpClient('my-client', '1.0.0', $app);
// โ
Recommended: Use shortcut methods with typed configuration
$config = new StdioConfig('php server.php');
$session = $client->stdio($config);
$session->initialize();
// Call a tool
$result = $session->callTool('echo', ['message' => 'Hello, MCP!']);
echo $result->getContent()[0]->getText();
Alternative Methods:
// HTTP shortcut method
use Dtyq\PhpMcp\Client\Configuration\HttpConfig;
$httpConfig = new HttpConfig('http://localhost:8080/mcp');
$session = $client->http($httpConfig);
// โ ๏ธ Legacy method (deprecated)
$session = $client->connect('stdio', ['command' => 'php server.php']);
๐ Documentation
- ๐ Complete Documentation - All guides and references
- ๐ Project Overview - Architecture, features, and use cases
- ๐ Quick Start Guide - 5-minute tutorial
- ๐ง Server Guides - Build MCP servers
- ๐ก Client Guides - Create MCP clients
Working Examples
Check the /examples directory:
stdio-server-test.php- Complete STDIO serverhttp-server-test.php- HTTP server with toolsstdio-client-test.php- STDIO client examplehttp-client-test.php- HTTP client example
๐ Transport Protocols
| Protocol | Status | Description |
|---|---|---|
| STDIO | โ | Process communication |
| HTTP | โ | JSON-RPC over HTTP |
| Streamable HTTP | ๐ง | HTTP + Server-Sent Events |
๐ ๏ธ Requirements
- PHP: 7.4+ (8.0+ recommended)
- Extensions: json, mbstring, openssl, pcntl, curl
- Composer: For dependency management
๐ค Contributing
We welcome contributions! Please see our issues page for areas where you can help.
git clone https://github.com/dtyq/php-mcp.git
cd php-mcp
composer install
composer test
๐ License
MIT License - see LICENSE file for details.
๐ Acknowledgments
- Model Context Protocol for the specification
- Anthropic for creating MCP
- The PHP community for excellent tooling and support
โญ Star this repository if you find it useful!
