模型控制平面服务器

一个集成了OpenAI服务、Git仓库分析、本地文件系统操作和Prometheus监控的模型控制平面服务器,适用于AI开发、代码分析和系统监控场景。

  • by dvladimirov
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 dvladimirov/mcp

使用说明

MCP服务器与OpenAI、Git、文件系统和Prometheus集成

此存储库包含一个模型控制平面(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 file

需求

  • Python 3.8+
  • 快速 API
  • 乌维科恩
  • OpenAI SDK
  • GitPython
  • 请求:
  • Docker和Docker Compose(用于普罗米修斯功能)

安装

  1. 克隆此存储库
  2. 安装依赖项:
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集成测试
  • 内存分析工具
  • 普罗米修斯测试和记忆压力
  • MCP服务器管理
  • 环境设置

个人测试

您还可以直接运行单个测试:

测试OpenAI集成:

python scripts/test_mcp_client.py

测试Git集成(提供Git存储库URL):

python scripts/test_git_integration.py https://github.com/username/repository

测试Git差异功能(分析需求兼容性):

python scripts/test_git_diff.py https://github.com/username/repository [commit-sha]

测试文件系统功能:

python scripts/test_filesystem.py

使用MCP测试langflow集成:

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 300

普罗米修斯集成

设置

  1. 使用Docker Compose启动Prometheus堆栈:
docker compose up -d

这将开始:

  • Prometheus服务器(可在以下网址访问http://localhost:9090)
  • 节点导出器(用于主机指标)
  • cAdvisor(用于容器度量)
  1. 对于压力测试,您可以启动内存压力容器:
docker compose up -d --build memory-stress

或者使用容器测试脚本:

./scripts/container-memory-test.sh start

Docker配置和重置脚本

该项目包括多种Docker配置和重置脚本,可在不同环境中可靠运行:

Docker配置

  • 标准配置 (docker-compose.yml):为Prometheus和Langflow使用自定义的Dockerfiles,以确保跨系统的权限一致。
  • 网桥网络配置 (docker-compose.bridge.yml):在主机网络有问题的环境中使用网桥网络的替代配置。

用于解决权限问题的自定义Dockerfiles

该项目使用Prometheus和Langflow的自定义Dockerfiles来解决常见的权限问题:

  • Dockerfile.promotheus:使用适当的权限设置Prometheus配置 nobody 用户。
  • Docker文件.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
    

这些脚本确保容器配置正确,具有正确的权限和最新的代码更改。

故障排除

如果您遇到权限问题:

  1. 使用重置脚本重建容器
  2. 检查日志 docker compose logs <service_name>
  3. 确保添加到Langflow的任何组件都包含在Dockerfile.Langflow中

跨机器部署

部署到新机器时:

  1. 克隆存储库
  2. 使重置脚本可执行: chmod +x *.sh
  3. 运行重置脚本: ./reset-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端点

  • 获取 /v1/models -列出所有可用型号
  • 获取 /v1/models/{model_id} -获取特定型号的信息
  • 发布 /v1/models/azure-gpt-4/completion -使用Azure OpenAI生成文本补全
  • 发布 /v1/models/azure-gpt-4/chat -使用Azure OpenAI生成聊天响应
  • 发布 /v1/models/openai-gpt-chat/chat -使用OpenAI聊天模型生成聊天响应
  • 发布 /v1/models/openai-gpt-completion/completion -使用OpenAI完成模型生成文本完成

Git集成端点

  • 发布 /v1/models/git-analyzer/analyze -分析Git存储库
  • 发布 /v1/models/git-analyzer/search -在Git存储库中搜索与模式匹配的文件
  • 发布 /v1/models/git-analyzer/diff -获取存储库中上次提交的差异

文件系统端点

  • 发布 /v1/models/filesystem/list -列出目录的内容
  • 发布 /v1/models/filesystem/read -读取文件内容
  • 发布 /v1/models/filesystem/read-multiple -一次读取多个文件
  • 发布 /v1/models/filesystem/write -将内容写入文件
  • 发布 /v1/models/filesystem/edit -编辑具有多个替换项的文件
  • 发布 /v1/models/filesystem/mkdir -创建目录
  • 发布 /v1/models/filesystem/move -移动文件或目录
  • 发布 /v1/models/filesystem/search -搜索与模式匹配的文件
  • 发布 /v1/models/filesystem/info -获取有关文件或目录的信息

普罗米修斯端点

  • 发布 /v1/models/prometheus/query -执行即时查询
  • 发布 /v1/models/prometheus/query_range -执行范围查询
  • 发布 /v1/models/prometheus/series -获取系列数据
  • 获取 /v1/models/prometheus/labels -获取所有可用标签
  • 发布 /v1/models/prometheus/label_values -获取特定标签的值
  • 获取 /v1/models/prometheus/targets -获取所有目标
  • 获取 /v1/models/prometheus/rules -获取所有规则
  • 获取 /v1/models/prometheus/alerts -获取所有警报

客户端使用情况

您可以使用 MCPAIComponent 在LangFlow管道中,通过提供MCP服务器URL:

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)

故障排除

普罗米修斯问题

  1. 验证Prometheus是否正在运行: docker ps | grep prometheus
  2. 检查您是否可以访问Prometheus UI:http://localhost:9090
  3. 验证MCP服务器是否正在运行且可访问
  4. 检查MCP服务器日志是否有错误
  5. 首先尝试简单的查询来验证连接性(例如。, up 查询)

OpenAI问题

  1. 验证API密钥设置是否正确
  2. 检查是否存在利率限制或配额问题
  3. 验证您正在为API密钥使用受支持的模型

Git问题

  1. 确保Git存储库URL可访问
  2. 如果使用私有存储库,请检查身份验证问题
  3. 确保GitPython安装正确