MCP PHP入门
   
PHP中一个功能完整的模型上下文协议(MCP)服务器模板。此入门程序使用干净的生产就绪代码演示了MCP的所有主要功能。
📚 文档
✨ 特性
| 类别 | 功能 | 描述 |
|---|---|---|
| 工具 | hello | 向某人问好 |
get_weather | 获取一个城市的当前天气 | |
long_task | 使用进度更新模拟长时间运行的任务 | |
load_bonus_tool | 动态注册新的奖励工具 | |
ask_llm | 使用采样向连接的LLM提问 | |
confirm_action | 在继续之前请求用户确认 | |
get_feedback | 请求用户反馈 | |
| 资源 | about://server | 有关此MCP服务器的信息 |
doc://example | 示例文档资源 | |
| 资源模板 | greeting://{name} | 为特定的人提供个性化的问候 |
item://{id} | 按ID显示特定项目的数据 | |
| 提示 | greet | 生成问候语 |
code_review | 审查代码以寻找潜在的改进 |
🚀 快速开始
先决条件
安装
# Clone the repository
git clone https://github.com/SamMorrowDrums/mcp-php-starter.git
cd mcp-php-starter
# Install dependencies
composer install运行服务器
stdio传输 (地方发展):
composer start:stdio
# or
php bin/server-stdio.phpHTTP传输 (用于远程/web部署):
composer start:http
# Server runs on http://localhost:3000🔧 VS代码集成
该项目包括用于无缝开发的VS代码配置:
- 在VS Code中打开项目
- MCP配置位于
.vscode/mcp.json - 使用VS Code的MCP工具测试服务器
📁 项目结构
.
├── bin/
│ ├── server-stdio.php # stdio transport entrypoint
│ └── server-http.php # HTTP transport entrypoint
├── public/
│ └── index.php # HTTP web entrypoint
├── src/
│ └── McpElements.php # Tools, resources, and prompts
├── tests/
│ └── ...
├── .vscode/
│ └── mcp.json # MCP server configuration
├── composer.json
├── server.json # MCP discovery configuration
└── README.md🛠️ 发展
# Install dependencies
composer install
# Run code style check
composer cs-check
# Fix code style
composer cs-fix
# Run tests
composer test🔍 MCP 检查员
这 MCP 检查员 是测试和调试MCP服务器的重要开发工具。
运行检查器
npx @modelcontextprotocol/inspector php bin/server-stdio.php检查员提供什么
- 工具选项卡:列出并调用所有已注册的带有参数的工具
- 资源选项卡:浏览和阅读资源
- 提示选项卡:查看和测试提示模板
- 日志选项卡:请参阅客户端和服务器之间的JSON-RPC消息
📖 功能示例
具有自定义模式的工具
所有工具都是使用自定义输入模式手动注册的,这些模式包括 title 和 description 跨语言一致性字段:
// In ServerFactory.php
->addTool(
handler: [$elements, 'hello'],
name: 'hello',
description: 'Say hello to a person',
inputSchema: [
'type' => 'object',
'properties' => [
'name' => [
'type' => 'string',
'title' => 'Name',
'description' => 'Name of the person to greet',
],
],
'required' => ['name'],
]
)资源
->addResource(
handler: [$elements, 'getAbout'],
uri: 'about://server',
name: 'About',
description: 'Information about this MCP server',
mimeType: 'text/plain'
)资源模板
->addResourceTemplate(
handler: [$elements, 'getPersonalizedGreeting'],
uriTemplate: 'greeting://{name}',
name: 'Personalized Greeting',
description: 'A personalized greeting for a specific person',
mimeType: 'text/plain'
)提示
->addPrompt(
handler: [$elements, 'greetPrompt'],
name: 'greet',
description: 'Generate a greeting message'
)🔐 环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
PORT | HTTP服务器端口 | 3000 |
🤝 贡献
欢迎投稿!请确保您的更改与其他语言初学者保持功能对等。
📄 许可证
MIT许可证-请参阅 许可证 了解详情。
