
模型上下文协议(MCP)服务器的核心PHP实现
概览
该SDK允许您将PHP应用的功能暴露为标准化的MCP、、和,允许AI助手(如Anthropic的Claude、Cursor IDE、OpenAI的ChatGPT等)通过MCP标准与您的后端交互。 - : 构建基于PHP 8.1+特性、PSR标准和模块化设计 - 支持stdio、http+sse及新建,具备可重复性 - : 使用PHP 8属性(#[McpTool]、#[McpResource]等)进行零配置元素注册 - 支持闭包、类方法、静态方法和可调用类 - : 从方法签名自动生成JSON模式,并可选增强#[Schema]属性 - : 多存储后端的高级会话处理 - : 基于ReactPHP支持高并发性和非阻塞操作 - : 完全支持 JSON-RPC 批处理请求 - : 智能缓存发现元素并手动覆盖优先级 - : 工具和提示符中内置参数补全支持 - : 完整支持 PSR-11 容器
概述(AI 翻译)
** 一个用于构建模型上下文协议(MCP)服务器的全面 PHP SDK。用 PHP 创建具现代架构、广泛测试和灵活传输选项的生产准备 MCP 服务器。**
该SDK允许你将PHP应用的功能暴露为标准化的MCP工具、资源和提示词,允许AI助手(如Anthropic的Claude、Cursor IDE、OpenAI的ChatGPT等)使用MCP标准与你的后端交互。
🚀 主要特色
- ** 🏗️ 现代架构**:采用PHP 8.1+特性、PSR标准和模块化设计构建
- ** 📡 多重传输**:支持
stdio、http+sse,以及带可续功能的新型可流式 HTTP - ** 🎯 基于属性的定义**:使用PHP 8属性(
#[McpTool]、#[McpResource]等)进行零配置元素注册 - ** 🔧 灵活处理程序**:支持闭包、类方法、静态方法和可调用类
- ** 📝 智能模式生成**:从方法签名自动生成JSON模式,并可选
#[Schema]属性增强 - ** ⚡ 会话管理**:带有多存储后端的高级会话处理
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
PHP MCP Server SDK
A comprehensive PHP SDK for building Model Context Protocol (MCP) servers. Create production-ready MCP servers in PHP with modern architecture, extensive testing, and flexible transport options.
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.
🚀 Key Features
- 🏗️ Modern Architecture: Built with PHP 8.1+ features, PSR standards, and modular design
- 📡 Multiple Transports: Supports
stdio,http+sse, and new streamable HTTP with resumability - 🎯 Attribute-Based Definition: Use PHP 8 Attributes (
#[McpTool],#[McpResource], etc.) for zero-config element registration - 🔧 Flexible Handlers: Support for closures, class methods, static methods, and invokable classes
- 📝 Smart Schema Generation: Automatic JSON schema generation from method signatures with optional
#[Schema]attribute enhancements - ⚡ Session Management: Advanced session handling with multiple storage backends
- 🔄 Event-Driven: ReactPHP-based for high concurrency and non-blocking operations
- 📊 Batch Processing: Full support for JSON-RPC batch requests
- 💾 Smart Caching: Intelligent caching of discovered elements with manual override precedence
- 🧪 Completion Providers: Built-in support for argument completion in tools and prompts
- 🔌 Dependency Injection: Full PSR-11 container support with auto-wiring
- 📋 Comprehensive Testing: Extensive test suite with integration tests for all transports
This package supports the 2025-03-26 version of the Model Context Protocol with backward compatibility.
📋 Requirements
- PHP >= 8.1
- Composer
- For HTTP Transport: An event-driven PHP environment (CLI recommended)
- Extensions:
json,mbstring,pcre(typically enabled by default)
📦 Installation
composer require php-mcp/server
💡 Laravel Users: Consider using
php-mcp/laravelfor enhanced framework integration, configuration management, and Artisan commands.
⚡ Quick Start: Stdio Server with Discovery
安装
This server does not publish a one-line install command.
Open the repository installation guide 新手?先看 mcpskill.org 教程 →配置
{
"mcpServers": {
"php-calculator": {
"command": "php",
"args": ["/absolute/path/to/your/mcp-server.php"]
}
}
}