一个完整的PHP实现模型上下文协议(MCP),支持服务器和客户端,支持STDIO和HTTP传输,并集成框架
概览
一个完整的PHP实现,提供服务器和客户端功能,并支持多种传输协议。 - 🚀 - 支持 MCP 2025-03-26 规范 - 🔧 - 工具、资源和提示支持 - 🔌 - STDIO ✅、HTTP ✅、可流式HTTP 🚧 - 🌐 - 支持任何兼容 PSR 的框架,内置 Hyperf 集成 - 📚 - 提供中文和英文的全面指南 如果你用的是Hyperf框架,集成非常简单: : - 🔐 - 自定义认证 - 📊 - 动态工具/资源注册 - 📝 - 自动注册工具、资源和提示 👉 查看完整的Hyperf集成指南 - - 所有指南和参考资料 - - 架构、功能和用例 - - 5分钟教程 - - 构建MCP服务器 - - 创建MCP客户端 请查看 /examples 目录:- stdio-server-test.php - 完整 STDIO 服务器 - http-server-test.php - 带有工具的 HTTP 服务器 - stdio-client-test.php - STDIO 客户端示例 - http-client-test.php - HTTP 客户端示例 - : 7.
概述(AI 翻译)
完整的PHP实现了模型上下文协议(MCP),同时为服务器和客户端提供功能,并支持多种传输协议。
语言:英语 |简体中文
✨ 主要特色
- 🚀 最新MCP协议 - 支持MCP 2025-03-26规范
- 🔧 完整实现 - 工具、资源和提示支持
- 🔌 多重传输 - STDIO ✅、HTTP ✅、可流式HTTP 🚧
- 🌐 框架兼容 - 支持任何符合PSR标准的框架,内置Hyperf集成
- 📚 文档详尽 - 英文和中文的全面指南
🚀 快速启动
安装
Hyperf Framework 快速集成
如果你用的是Hyperf框架,集成非常简单:
基于注释的注册:
高级选项:
- 🔐 AuthenticatorInterface - 自定义认证
- 📊 HttpTransportAuthenticatedEvent - 动态工具/资源注册
- 📝 注释系统 - 自动注册工具、资源和提示
👉 查看完整的Hyperf集成指南
基础服务器示例
基本客户示例
替代方法:
📖 文献资料
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
PHP MCP
A complete PHP implementation of the Model Context Protocol (MCP), providing both server and client functionality with support for multiple transport protocols.
✨ 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', [
安装
This server does not publish a one-line install command.
Open the repository installation guide 新手?先看 mcpskill.org 教程 →