集成 OpenAI、Git、文件系统和 Prometheus 的 MCP 服务器
该存储库包含一个模型控制平面 (MCP) 服务器实现,支持 OpenAI 服务、Git 存储库分析、本地文件系统操作和 Prometheus 集成。
项目结构
MCP/
├── mcp/ # Core MCP library modules
├── scripts/ # Utility scripts and test tools
├── prometheus/ # Prometheus configuration
├── docker-compose.yml # Docker configuration
├── mcp_server.py # Main server implementation
├── mcp_run # Main runner script (shortcut)
└── README.md # This fileRelated MCP server: MCP GitHub Repository Server
要求
Python 3.8+
快速API
优维康
OpenAI SDK
GitPython
请求
Docker 和 Docker Compose(用于 Prometheus 功能)
安装
克隆此存储库
安装依赖项:
pip install -r requirements.txt环境变量
设置以下环境变量:
对于 Azure OpenAI:
export AZURE_OPENAI_ENDPOINT="your-azure-endpoint"
export AZURE_OPENAI_API_KEY="your-azure-api-key"
export AZURE_OPENAI_API_VERSION="2023-05-15"
export AZURE_DEPLOYMENT_NAME="your-deployment-name"对于标准 OpenAI:
export OPENAI_API_KEY="your-openai-api-key"
# Optional: Specify which models to use
export OPENAI_CHAT_MODEL="gpt-4o-mini" # Default if not specified
export OPENAI_COMPLETION_MODEL="gpt-3.5-turbo-instruct" # Default if not specified对于普罗米修斯:
export PROMETHEUS_URL="http://localhost:9090" # Default if not specified运行服务器
启动 MCP 服务器:
python scripts/start_mcp_server.py或了解更多选项:
python scripts/start_mcp_server.py --host 0.0.0.0 --port 8000 --debug该服务器将在http://localhost:8000上可用。
统一测试工具
我们提供统一的测试脚本,为您提供所有测试功能的用户友好界面:
./mcp_run该交互式脚本提供:
文件系统测试
Git 集成测试
内存分析工具
Prometheus 测试和内存压力
MCP 服务器管理
环境设置
个人测试
您也可以直接运行单个测试:
测试 OpenAI 集成:
python scripts/test_mcp_client.py测试 Git 集成(提供 Git 存储库 URL):
python scripts/test_git_integration.py https://github.com/username/repository测试 Git diff 功能(分析需求兼容性):
python scripts/test_git_diff.py https://github.com/username/repository [commit-sha]测试文件系统功能:
python scripts/test_filesystem.py测试 langflow 与 MCP 的集成:
python scripts/test_langflow_integration.py [OPTIONAL_REPO_URL]测试 Prometheus 集成:
python scripts/test_prometheus.py [prometheus_url]高级 Git 分析
要使用 AI 建议进行更高级的 Git 存储库分析:
python scripts/langflow_git_analyzer.py https://github.com/username/repository您还可以在存储库中搜索特定的模式:
python scripts/langflow_git_analyzer.py https://github.com/username/repository --search "def main"或者使用 AI 洞察分析最后一次提交的差异:
python scripts/langflow_git_analyzer.py https://github.com/username/repository --diff内存分析工具
MCP 包含几个用于内存监控和分析的工具:
# Basic memory diagnostics with AI analysis
python scripts/ai_memory_diagnostics.py
# Interactive memory dashboard
python scripts/mcp_memory_dashboard.py
# Memory alerting system
python scripts/mcp_memory_alerting.py您还可以模拟内存压力进行测试:
python scripts/simulate_memory_pressure.py --target 85 --duration 300Prometheus 集成
设置
使用 Docker Compose 启动 Prometheus 堆栈:
docker compose up -d这将开始:
Prometheus 服务器(可通过http://localhost:9090访问)
节点导出器(用于主机指标)
cAdvisor(用于容器指标)
对于压力测试,您可以启动内存压力容器:
docker compose up -d --build memory-stress或者使用容器测试脚本:
./scripts/container-memory-test.sh startDocker 配置和重置脚本
该项目包括多个 Docker 配置和重置脚本,以便在不同环境中可靠运行:
Docker 配置
标准配置(
docker-compose.yml):使用 Prometheus 和 Langflow 的自定义 Dockerfiles 来确保跨系统的权限一致。桥接网络配置(
docker-compose.bridge.yml):对于主机网络有问题的环境,使用桥接网络的替代配置。
自定义 Dockerfile 解决权限问题
该项目针对 Prometheus 和 Langflow 使用自定义 Dockerfile 来解决常见的权限问题:
Dockerfile.prometheus :为
nobody用户设置具有适当权限的 Prometheus 配置。Dockerfile.langflow :将组件目录复制到容器中,而不改变文件所有权,从而允许 Langflow 访问组件而不会出现权限错误。
这种方法消除了卷安装的需要,从而避免了不同机器和用户配置之间的权限冲突。
重置脚本
所有服务重置(
reset-all.sh):使用单个命令重置所有容器。# Basic reset (rebuilds containers with existing volumes) ./reset-all.sh # Full reset (removes volumes and rebuilds containers) ./reset-all.sh --clean个人服务重置:
# Reset only Prometheus ./reset-prometheus.sh # Reset only Langflow ./reset-langflow.sh
这些脚本确保容器正确配置了正确的权限和最新的代码更改。
故障排除
如果您遇到权限问题:
使用重置脚本重建容器
使用
docker compose logs <service_name>检查日志确保添加到 Langflow 的所有组件都包含在 Dockerfile.langflow 中
跨机器部署
部署到新机器时:
克隆存储库
使重置脚本可执行:
chmod +x *.sh运行重置脚本:
./reset-all.shreset-all.sh
自定义 Dockerfiles 自动处理不同系统之间可能出现的所有权限问题。
使用 Prometheus 客户端
MCPAIComponent类包含 Prometheus 功能:
from langflow import MCPAIComponent
# Initialize the client
mcp = MCPAIComponent(mcp_server_url="http://localhost:8000")
# Instant query (current metric values)
result = mcp.prometheus_query("up")
# Range query (metrics over time)
result = mcp.prometheus_query_range(
query="rate(node_cpu_seconds_total{mode='system'}[1m])",
start="2023-03-01T00:00:00Z",
end="2023-03-01T01:00:00Z",
step="15s"
)
# Get all labels
labels = mcp.prometheus_get_labels()
# Get label values
values = mcp.prometheus_get_label_values("job")
# Get targets
targets = mcp.prometheus_get_targets()
# Get alerts
alerts = mcp.prometheus_get_alerts()有用的 PromQL 查询
CPU 使用率:
rate(node_cpu_seconds_total{mode!="idle"}[1m])内存使用情况:
node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes磁盘使用情况:
node_filesystem_avail_bytes{mountpoint="/"} / node_filesystem_size_bytes{mountpoint="/"}容器 CPU 使用率:
rate(container_cpu_usage_seconds_total[1m])容器内存使用情况:
container_memory_usage_bytes
API 端点
OpenAI 端点
GET
/v1/models- 列出所有可用模型GET
/v1/models/{model_id}- 获取特定模型的信息POST
/v1/models/azure-gpt-4/completion- 使用 Azure OpenAI 生成文本补全POST
/v1/models/azure-gpt-4/chat- 使用 Azure OpenAI 生成聊天响应POST
/v1/models/openai-gpt-chat/chat- 使用 OpenAI 聊天模型生成聊天响应POST
/v1/models/openai-gpt-completion/completion- 使用 OpenAI 补全模型生成文本补全
Git 集成端点
POST
/v1/models/git-analyzer/analyze- 分析 Git 存储库POST
/v1/models/git-analyzer/search- 在 Git 存储库中搜索与模式匹配的文件POST
/v1/models/git-analyzer/diff- 获取存储库中最后一次提交的差异
文件系统端点
POST
/v1/models/filesystem/list- 列出目录的内容POST
/v1/models/filesystem/read- 读取文件内容POST
/v1/models/filesystem/read-multiple- 一次读取多个文件POST
/v1/models/filesystem/write- 将内容写入文件POST
/v1/models/filesystem/edit- 编辑具有多个替换的文件POST
/v1/models/filesystem/mkdir- 创建目录POST
/v1/models/filesystem/move- 移动文件或目录POST
/v1/models/filesystem/search- 搜索符合模式的文件POST
/v1/models/filesystem/info- 获取有关文件或目录的信息
Prometheus 端点
POST
/v1/models/prometheus/query- 执行即时查询POST
/v1/models/prometheus/query_range- 执行范围查询POST
/v1/models/prometheus/series- 获取系列数据GET
/v1/models/prometheus/labels- 获取所有可用标签POST
/v1/models/prometheus/label_values- 获取特定标签的值GET
/v1/models/prometheus/targets- 获取所有目标GET
/v1/models/prometheus/rules- 获取所有规则GET
/v1/models/prometheus/alerts- 获取所有警报
客户端使用情况
您可以通过提供 MCP 服务器 URL 在您的 LangFlow 管道中使用MCPAIComponent :
from langflow import MCPAIComponent
mcp = MCPAIComponent(mcp_server_url="http://localhost:8000")
# List available models
models = mcp.list_models()
print(models)
# Generate chat completion with OpenAI model
chat_response = mcp.chat(
model_id="openai-gpt-chat",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a joke about programming."}
],
max_tokens=100,
temperature=0.7
)
print(chat_response)
# Generate text completion with OpenAI model
completion_response = mcp.completion(
model_id="openai-gpt-completion",
prompt="Write a function in Python to calculate the factorial of a number:",
max_tokens=150,
temperature=0.7
)
print(completion_response)
# Analyze a Git repository
repo_analysis = mcp.analyze_git_repo("https://github.com/username/repository")
print(repo_analysis)
# Search a Git repository
search_results = mcp.search_git_repo("https://github.com/username/repository", "def main")
print(search_results)
# Get the diff of the last commit
diff_info = mcp.get_git_diff("https://github.com/username/repository")
print(diff_info)
# List files in the current directory
dir_contents = mcp.list_directory()
print(dir_contents)
# Read a file
file_content = mcp.read_file("path/to/file.txt")
print(file_content)
# Write to a file
write_result = mcp.write_file("path/to/new_file.txt", "Hello, world!")
print(write_result)
# Search for files
search_result = mcp.search_files("*.py")
print(search_result)使用 GitCodeAnalyzer 类
对于更结构化的 Git 分析,可以使用GitCodeAnalyzer类:
from langflow_git_analyzer import GitCodeAnalyzer
# Initialize the analyzer
analyzer = GitCodeAnalyzer(mcp_server_url="http://localhost:8000")
# Analyze a repository
analyzer.analyze_repository("https://github.com/username/repository")
# Get a summary
summary = analyzer.get_repository_summary()
print(summary)
# Get AI recommendations
recommendations = analyzer.get_repository_recommendations()
print(recommendations)
# Analyze code patterns
pattern_analysis = analyzer.analyze_code_pattern("def process")
print(pattern_analysis)
# Get the last commit diff
diff_info = analyzer.get_last_commit_diff()
print(diff_info)
# Get a formatted summary of the diff
diff_summary = analyzer.get_formatted_diff_summary()
print(diff_summary)
# Get AI analysis of the commit changes
diff_analysis = analyzer.analyze_commit_diff()
print(diff_analysis)故障排除
Prometheus 问题
验证 Prometheus 是否正在运行:
docker ps | grep prometheus检查您是否可以访问 Prometheus UI: http://localhost:9090
验证 MCP 服务器是否正在运行且可访问
检查 MCP 服务器日志是否存在错误
首先尝试简单查询来验证连接性(例如,
up查询)
OpenAI 问题
验证您的 API 密钥设置是否正确
检查速率限制或配额问题
验证您使用的 API 密钥支持的模型
Git 问题
确保 Git 存储库 URL 可访问
如果使用私有存储库,请检查身份验证问题
确保 GitPython 已正确安装