🚀 MCP DevOps蓝图#从拉取请求到生产:MCP蓝图
此存储库包含以下内容的最小实现 模型上下文协议(MCP) 服务器和客户端在随附的博客文章中进行了描述。它演示了如何将AI模型连接到开发人员工具,以编排常见的工作流程,如搜索代码、运行测试、生成发布说明和协调PR。

##概述

- mcp_server.py 使用定义MCP服务器 FastMCP 它公开了三个工具:
用于AI驱动的DevOps自动化的模型上下文协议(MCP)的生产就绪实现 - search_repo:在存储库文件中搜索关键字(不区分大小写)。run_tests:通过执行单元测试pytest并总结失败。
通过将AI模型直接连接到DevOps工具来改变您的开发工作流程。此蓝图演示了如何构建健壮的MCP服务器和客户端,通过自然语言编排复杂的工作流。 - generate_release_notes:根据最近的提交构建发行说明。
- mcp_client.py 定义了一个简单的客户端,该客户端通过stdio传输生成服务器,列出可用的工具,并使用Anthropics API根据自然语言请求决定调用哪些工具。
需求
✨ 特性
- Python 3.10+
- 🔍 智能存储库搜索 -使用AI驱动的语义搜索在代码库中查找文件- FastMCP
- 🧪 自动化测试 -运行测试并智能分析故障- GitPython
- 📝 发行说明生成 -从git历史自动生成发行说明- pytest
- 🤖 AI编排 -让Claude根据自然语言请求决定使用哪些工具- 人类
- 🏗️ 生产准备就绪 -包括错误处理、日志记录、类型提示和全面测试
- 📦 易于集成 -用于添加自己的工具的可扩展架构使用pip安装依赖项:
🎯 快速启动\\bash
pip安装fastmcp gitpython pytest人机界面
先决条件\\\`
- Python 3.10或更高版本##用法
- Git仓库(完整功能)
- 无烟煤API键 (适用于AI客户端)1。 运行服务器
安装在一个终端中启动MCP服务器:
bash bash
克隆存储库python mcp_server.py
克隆https://github.com/yourusername/mcp-blueprint.git \\\`
cd-mcp蓝图
服务器默认使用stdio传输,并通过stdin/stdout监听JSON-RPC消息。
创建虚拟环境
python-m venv-venv2。 运行客户端
源venv/bin/activate#在Windows上:venv\\Scripts\\activate
在另一个终端中,运行客户端。如果服务器尚未运行,它将在内部生成服务器,然后处理自然语言查询:
安装依赖项
pip安装-r要求.txt \\bash
python mcp_client.py
或作为软件包安装\\\`
pip安装-e。
### Basic Usage## Security Considerations
#### Running the ServerThis sample implementation is for demonstration purposes. When building production servers and clients, follow the [MCP security best practices](https://modelcontextprotocol.io/specification/draft/basic/security_best_practices) including:
Using the new modular version- Verify all inbound requests and use secure session IDs.
python -m mcp_devops.server- Require explicit user consent before executing commands.
- Apply least‑privilege scopes to tokens.
Or using the standalone script
python mcp_server.py## License
本项目根据麻省理工学院许可证提供用于教育目的。
服务器通过MCP公开了三个工具:
- `search_repo` -在存储库文件中搜索关键字
- `run_tests` -执行pytest并总结结果
- `generate_release_notes` -从git提交创建发行说明
#### 使用AI客户端
Set your API key
export ANTHROPIC_API_KEY='your-api-key-here'
Run the client
python mcp_client.py
客户端使用Claude智能地编排工作流。
## 📖 文档
### 建筑
┌─────────────────┐ │ Claude API │ Natural language understanding └────────┬────────┘ │ ┌────▼─────┐ │ Client │ MCP Client (Anthropic SDK) └────┬─────┘ │ JSON-RPC over stdio ┌────▼─────┐ │ Server │ MCP Server (FastMCP) └────┬─────┘ │ ┌────▼─────────────────┐ │ DevOps Tools │ │ • Git │ │ • pytest │ │ • Custom scripts │ └──────────────────────┘
### 添加自定义工具
from mcp_devops import create_server
mcp = create_server()
@mcp.tool def deploy_to_staging(branch: str) -> dict: """Deploy a branch to staging environment.""" return {"status": "deployed", "url": "https://staging.example.com"}
mcp.run()
## 🧪 测试
Run all tests
pytest
Run with coverage
pytest --cov=src --cov-report=html
Test the server without AI
python test_server.py
## 📦 项目结构
mcp-blueprint/ ├── src/ │ └── mcp_devops/ # Modern package structure │ ├── __init__.py │ ├── server.py # MCP server implementation │ └── client.py # AI-powered client ├── tests/ ├── examples/ ├── mcp_server.py # Standalone server ├── mcp_client.py # Standalone client ├── pyproject.toml # Modern Python packaging └── README.md
## 🔒 安全
此实现如下 [MCP安全最佳实践](https://modelcontextprotocol.io/specification/draft/basic/security_best_practices):
- ✅ 无stdout日志记录(使用stdio传输时)
- ✅ 所有工具参数的输入验证
- ✅ 长时间运行操作的超时保护
- ✅ 错误处理和清理
## 🤝 贡献
欢迎投稿!拜托:
1. 分叉存储库
1. 创建要素分支
1. 进行更改
1. 运行测试和梳理
1. 提交拉取请求
## 📄 许可证
MIT许可证-请参阅 [许可证](LICENSE) 文件以获取详细信息。
## 🙏 致谢
- [模型上下文协议](https://modelcontextprotocol.io/)
- [Anthropic](https://www.anthropic.com/) -API克劳德
- [FastMCP](https://gofastmcp.com/)
## 📚 资源
- [MCP文件](https://modelcontextprotocol.io/docs)
- [MCP规范](https://spec.modelcontextprotocol.io/)
- [API人类克劳德](https://docs.anthropic.com/)
## 💡 用例
- **CI/CD自动化** -智能构建工作流
- **代码审查** -自动PR分析
- **文档** -自动生成文档
- **监控** -智能事件响应
______________________________________________________________________