MCP服务器-命令行程序执行器
MCP(模型上下文协议)服务器,基于README文件分析执行命令行程序,与OpenAI集成,用于智能配置建议和日志分析。
🚀 快速开始
先决条件
- Python 3.8+
- OpenAI API密钥(可选,用于AI功能)
- Linux或macOS(Windows支持即将推出)
安装
- 克隆或提取项目:
cd mcp_server_project- 安装依赖项:
pip install -r requirements.txt- 在开发模式下安装软件包:
pip install -e .配置
- 复制示例配置:
cp config.yaml.example config.yaml- 编辑config.yaml并添加您的OpenAI API密钥:
openai:
api_key: "your-openai-api-key-here"
model: "gpt-4"🔥 如何跑步
方法1:命令行界面
# Run the MCP server
python -m mcp_server.main
# Or use the CLI directly
mcp-server --help方法2:Python模块
# Run from the project directory
python -m mcp_server.cli方法3:直接执行脚本
# Run the main server
python mcp_server/main.py📋 使用示例
基本用法
from mcp_server import MCPServer
# Initialize the server
server = MCPServer()
# Execute a command with README analysis
result = server.execute_command(
prompt="Run git status and explain the output",
readme_path="./README.md"
)
print(result)高级功能
# Get AI-powered flag recommendations
flags = server.recommend_flags(
command="docker run",
context="development environment"
)
# Analyze logs with AI
analysis = server.analyze_logs(
log_content="Error: permission denied",
command="docker build"
)🧪 测试
运行测试套件:
python test_mcp_server.py或运行特定测试:
python -m pytest test_mcp_server.py -v🛠️ 发展
项目结构
mcp_server/
├── __init__.py # Package initialization
├── main.py # Main server entry point
├── cli.py # Command line interface
├── config.py # Configuration management
├── command_executor.py # Command execution logic
├── readme_parser.py # README file analysis
├── openai_module.py # OpenAI API integration
├── log_analyzer.py # Log analysis with AI
├── flag_recommender.py # AI-powered flag suggestions
├── context_manager.py # Execution context management
├── security.py # Security and validation
├── models.py # Data models
└── api_gateway.py # API gateway (future)主要特点
- 🤖 人工智能集成:基于OpenAI的命令建议和日志分析
- 📖 README解析:自动从README文件中了解程序使用情况
- 🔍 智能日志分析:AI检测命令输出中的成功/失败模式
- 🚨 安全:内置命令验证和沙盒
- ⚡ 快速执行:通过上下文管理高效执行命令
🔧 配置选项
config.yaml结构
# OpenAI Configuration
openai:
api_key: "your-api-key"
model: "gpt-4"
max_tokens: 1000
temperature: 0.7
# Security Settings
security:
allowed_commands: ["git", "docker", "npm", "python"]
blocked_commands: ["rm", "sudo", "chmod"]
sandbox_mode: true
# Logging
logging:
level: "INFO"
file: "mcp_server.log"
# Database
database:
url: "sqlite:///mcp_server.db"🤝 api参考
核心方法
execute_command(prompt, readme_path)-使用README上下文执行命令recommend_flags(command, context)-获取AI标志推荐analyze_logs(log_content, command)-分析命令输出parse_readme(file_path)-从README中提取命令信息
🐛 故障排除
常见问题
- 导入错误:确保已安装
pip install -e . - OpenAI API错误:检查config.yaml中的API密钥
- 权限不足:确保执行的文件权限正确
- 数据库错误:检查mcp_server.db是否存在并且可写
调试模式
# Run with debug logging
DEBUG=1 python -m mcp_server.main📈 性能提示
- 使用SQLite数据库缓存README分析结果
- 为重复查询启用OpenAI响应缓存
- 对多个命令使用异步执行
🔐 安全说明
- 命令在执行前经过验证
- 默认情况下,敏感命令被阻止
- 所有执行都会被记录下来以供审计
- README文件无需执行代码即可安全解析
📚 文档
- 珠江三角洲:
mcp_server_prd.md-产品要求文件 - 建筑:
mcp_server_system_design.md-系统设计 - 图表:
*.mermaid文件-UML图
🤝 贡献
- 分叉存储库
- 创建要素分支
- 进行更改
- 添加测试
- 提交拉取请求
📄 许可证
MIT许可证-有关详细信息,请参阅许可证文件
______________________________________________________________________
编码愉快! 🎉
有关更多示例和高级用法,请查看 example_usage.py 文件。
