Model Control Plane (MCP) Server

local-only server

The server can only run on the client’s local machine because it depends on local resources.

Integrations

  • Integrates with Docker for running Prometheus, Node Exporter, cAdvisor, and memory stress testing

  • Uses FastAPI as the web framework for implementing the MCP server API endpoints

  • Provides Git repository analysis, searching, and diff functionality through dedicated endpoints

OpenAI、Git、ファイルシステム、Prometheus 統合を備えた MCP サーバー

このリポジトリには、OpenAI サービス、Git リポジトリ分析、ローカル ファイル システム操作、および Prometheus 統合をサポートするモデル コントロール プレーン (MCP) サーバー実装が含まれています。

プロジェクト構造

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 (Prometheus 機能用)

インストール

  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 の diff 機能をテストします (要件の互換性を分析します)。

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 のカスタム Dockerfile を使用して、システム間で一貫した権限を確保します。
  • ブリッジ ネットワーク構成( docker-compose.bridge.yml ): ホスト ネットワークに問題のある環境でブリッジ ネットワークを使用する代替構成。

権限問題を解決するためのカスタム Dockerfiles

このプロジェクトでは、Prometheus と Langflow の両方にカスタム Dockerfile を使用して、一般的な権限の問題を解決します。

  • Dockerfile.prometheus : nobodyユーザーに適切な権限を与えて Prometheus 構成を設定します。
  • Dockerfile.langflow : ファイルの所有権を変更せずにコンポーネント ディレクトリをコンテナーにコピーし、Langflow が権限エラーなしでコンポーネントにアクセスできるようにします。

このアプローチにより、異なるマシンやユーザー構成間で権限の競合を引き起こす可能性のあるボリュームマウントが不要になります。

スクリプトをリセットする

  • すべてのサービスのリセット( reset-all.sh ): 1 つのコマンドですべてのコンテナをリセットします。
    # 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エンドポイント

  • 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)

トラブルシューティング

プロメテウスの問題

  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が正しくインストールされていることを確認する
ID: omow6idbzl