增强型PHP MCP服务器,配备Web控制面板
高级PHP MCP服务器,配备全面的工具集和网页界面。该服务器通过基于JSON的协议使用标准输入/输出进行通信,并提供了一套丰富的可远程调用工具,以及用于管理和测试的现代网页仪表板。
🚀 功能特点
- 10个内置工具你好,时间、计算器、文件操作、系统信息、JSON解析、天气、HTTP请求
- 网络仪表盘用于工具管理、测试和监控的现代网页界面
- 安全路径限制、文件大小限制、输入验证
- 配置支持环境的集中式配置系统
- 记录日志可配置级别的全面日志记录
- CLI(命令行界面)和HTTP(超文本传输协议)支持双模式操作
- 现代PHP需要PHP 8.1+版本,并且需要正确的自动加载配置
- 全面测试单元测试、集成测试、性能测试
📋 要求
- PHP 版本 >= 8.1
- Composer(在软件开发领域,通常指用于管理PHP依赖关系的工具)
- 所需的扩展:
json,curl
🛠️ 安装
- 克隆或下载该仓库
- 安装依赖项:
composer install- 使启动脚本可执行:
chmod +x start.sh🎯 快速入门
🚀 快速入门(推荐)
# 1. Install dependencies
./start.sh 6
# 2. Run web server (browser interface)
./start.sh 2在浏览器中打开http://localhost:8888以访问API。
🎮 交互式菜单
./start.sh这将显示一个包含可选项目的多彩菜单。
🌐 Web API(用于浏览器)
./start.sh 2 # Run web server on http://localhost:8888📡 CLI 模式(适用于 MCP 客户端)
./start.sh 1 # Run MCP server via stdin/stdout🔍 服务器管理
./start.sh 4 # Check status
./start.sh 5 # View logs🖥️ CLI 模式(交互式菜单)
./start.sh如果终端不是交互式的,它将显示带有选项的菜单:
./start.sh # Show menu with options
./start.sh 1 # CLI Mode - MCP server via stdin/stdout
./start.sh 2 # Web Mode - HTTP server with Slim Framework
./start.sh 3 # All Modes - CLI + Web simultaneously
./start.sh 4 # Status - check server status
./start.sh 5 # Logs - show recent logs
./start.sh 6 # Install - install dependenciesCLI 使用示例
# Run server in interactive mode (shows menu)
./start.sh
# Run directly in CLI mode (for MCP clients)
./start.sh 1
# Run web server (for browser)
./start.sh 2
# Check status of running servers
./start.sh 4
# View recent logs
./start.sh 5手动启动
php index.phpComposer 脚本
composer start📚 可用工具
该服务器提供了10款强大的工具:
1. 你好
以名字打招呼。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"hello","arguments":{"name":"John"}}}' | php index.php2. 获取时间
返回当前的日期和时间信息。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get_time"}}' | php index.php3. 计算
执行数学运算(加、减、乘、除)。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"calculate","arguments":{"operation":"add","a":10,"b":5}}}' | php index.php4. 读取文件
安全地读取项目目录中的文件内容。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"read_file","arguments":{"path":"README.md"}}}' | php index.php5. 写文件
安全地将内容写入项目目录内的文件。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"write_file","arguments":{"path":"test.txt","content":"Hello World"}}}' | php index.php6. 列出文件
列出指定路径中的文件和目录。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"list_files","arguments":{"path":"."}}}' | php index.php7. 系统信息
返回全面的系统和PHP信息。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"system_info"}}' | php index.php8. HTTP请求
向外部API发出HTTP请求。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"http_request","arguments":{"url":"https://api.github.com","method":"GET"}}}' | php index.php9. JSON解析
解析并格式化JSON字符串。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"json_parse","arguments":{"json":"{\"test\": true}"}}}' | php index.php10. 获取天气
获取指定城市的天气信息。
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get_weather","arguments":{"city":"London"}}}' | php index.php🌐 Web API(HTTP模式)
当服务器以Web模式运行时(./start.sh 2), 以下端点可用:
基本端点
GET /- 服务器信息和可用的终端节点GET /api/tools- 可用工具列表GET /api/status- 服务器状态和指标GET /api/logs- 最近的日志GET /api/metrics- 系统指标
工具执行
POST /api/tools/call- 执行一个工具
工具调用示例:
# Greeting
curl -X POST http://localhost:8888/api/tools/call \
-H "Content-Type: application/json" \
-d '{"tool": "hello", "arguments": {"name": "John"}}'
# Calculation
curl -X POST http://localhost:8888/api/tools/call \
-H "Content-Type: application/json" \
-d '{"tool": "calculate", "arguments": {"operation": "add", "a": 10, "b": 5}}'
# File listing
curl -X POST http://localhost:8888/api/tools/call \
-H "Content-Type: application/json" \
-d '{"tool": "list_files", "arguments": {"path": "src"}}'🔧 配置
服务器配置通过(某种方式)进行管理 config/server.php:
return [
'server' => [
'name' => 'enhanced-php-mcp-server',
'version' => '2.1.0',
],
'logging' => [
'enabled' => true,
'file' => __DIR__ . '/../logs/server.log',
'level' => 'info',
],
'security' => [
'allowed_paths' => [__DIR__ . '/..'],
'max_file_size' => 10 * 1024 * 1024, // 10MB
],
// ... more configuration
];📁 项目结构
enhanced-php-mcp-server/
├── config/ # Configuration files
├── src/ # Source code
├── tests/ # Test files (Unit, Integration, Performance)
├── tools/ # Tool implementations (future)
├── logs/ # Log files
├── storage/ # Temporary storage
├── vendor/ # Composer dependencies
├── index.php # Main entry point
├── start.sh # Start script
├── composer.json # Dependencies and autoloading
└── README.md # This file🔒 安全功能
- 路径限制文件操作仅限于允许的目录
- 文件大小限制可配置的操作最大文件大小
- 输入验证所有输入都经过验证和清理
- 错误处理全面的错误处理,同时不泄露敏感信息
- 路径遍历防护防止访问项目目录以外的文件
🧪 测试
该项目包含全面的测试套件:
快速测试
# Simple bash tests
./tests/simple_test.shPHPUnit 测试
# Run all unit tests
composer test
# Run specific test
./vendor/bin/phpunit --filter testHelloTool
# Generate coverage report
./vendor/bin/phpunit --coverage-html coverage-html集成测试
# Full HTTP API tests
./tests/run_comprehensive_tests.sh性能测试
# Performance testing plan
./tests/PERFORMANCE_TESTS_PLAN.md📝 开发
代码风格
composer cs-check # Check code style
composer cs-fix # Fix code style issues测试
composer test # Run all tests依赖项
composer install # Install dependencies
composer update # Update dependencies📝 记录日志
日志会自动创建在 logs/server.log 带有时间戳和严重程度级别:
[2024-01-01 12:00:00] INFO Server started
[2024-01-01 12:00:01] INFO Tool 'hello' executed🌟 示例
初始化服务器
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | php index.php列出所有工具
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}' | php index.php连锁运营
# Write a file, then read it back
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"write_file","arguments":{"path":"demo.txt","content":"Hello from MCP!"}}}' | php index.php
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_file","arguments":{"path":"demo.txt"}}}' | php index.php🚨 故障排除
常见问题
- 权限被拒绝确保
start.sh脚本可执行(chmod +x start.sh) - 缺少依赖项跑
composer install - 缺少扩展程序安装所需的PHP扩展(
php-json,php-curl) - 文件访问检查项目目录中的文件权限
- 端口冲突服务器自动检测到端口8888、8889、8890
调试模式
通过将日志级别设置为启用详细日志记录 debug 在 config/server.php。
📊 性能指标
服务器内置监控功能:
- 响应时间跟踪
- 内存使用监控
- 工具执行统计
- 系统指标收集
通过……访问 /api/status 或者 /api/metrics 终端节点。
📄 许可证
MIT 许可证 - 详见 LICENSE 文件。
🤝 贡献(或“参与贡献”)
- 为仓库创建分支(或:克隆仓库)
- 创建一个特性分支
- 进行你的更改
- 如适用,请添加测试
- 提交拉取请求
📞 支持
对于问题和疑问:
- 检查日志中的内容
logs/server.log - 检查配置中的内容
config/server.php - 确保满足所有要求
- 运行测试套件以诊断问题
