1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHubでAIを強化する | 第5章:最適化とコミュニティへの貢献

Posted at

はじめに

これまでの第1章から第4章では、GitHubModel Context Protocol(MCP)を活用してAIエージェントを構築しました。Issueやコミットデータの取得、タスク自動化、コード分析、リアルタイム管理を通じて、GitHubのAPIとMCPの柔軟性を最大限に引き出しました。この最終章では、GitHub用MCPサーバーの最適化セキュリティ強化、そしてコミュニティへの貢献に焦点を当てます。

この第5章では、サーバーのパフォーマンスを向上させ、安全性を確保する方法を解説します。さらに、サーバーをオープンソースとして共有し、MCPコミュニティに貢献する方法を探ります。コード例を通じて、キャッシュ、レートリミティング、セキュリティログの実装も学びます。GitHubとMCPの未来を一緒に切り開きましょう!

MCPサーバーの最適化

GitHub用MCPサーバーを本番環境で運用するには、以下の最適化が必要です:

1. キャッシュの活用

  • 目的:頻繁なAPIリクエスト(例:コミット取得、イベントデータ)を削減。
  • 方法:Redisなどのインメモリキャッシュを使用してデータを一時保存。
  • :Issueやコミットデータを5分間キャッシュし、GitHub APIへの負荷を軽減。

2. レートリミティング

  • 目的:GitHub APIの制限(例:5000リクエスト/時間)を遵守し、サーバーの安定性を確保。
  • 方法:リクエストごとに制限を設定(例:1分間に50リクエスト)。
  • ratelimitライブラリを使用して制限を管理。

3. 非同期処理

  • 目的:Webhookイベントや大量のリクエストを効率的に処理。
  • 方法asyncioやメッセージキュー(例:RabbitMQ)を活用。
  • :イベント処理をキューイングし、レスポンス時間を短縮。

セキュリティ強化

リアルタイムAIを安全に運用するには、以下のセキュリティ対策が重要です:

1. HTTPSの有効化

  • 目的:Webhook通信を暗号化し、盗聴を防止。
  • 方法:Let’s EncryptやクラウドプロバイダーのSSL証明書を使用。
  • 推奨事項:本番環境ではTLS 1.3を採用。

2. Webhook署名検証

  • 目的:GitHubからのWebhookリクエストが正規であることを確認。
  • 方法:GitHubのX-Hub-Signature-256をHMAC-SHA256で検証。
  • :シークレットキーを使用して署名をチェック。

3. セキュリティログ

  • 目的:不正アクセスやエラーを追跡。
  • 方法:リクエスト、Webhookイベント、レスポンスをログに記録。

コード例:最適化とセキュリティの実装

以下のコードは、第4章のリアルタイム管理サーバーにキャッシュ(Redis)、レートリミティング、セキュリティログ、Webhook署名検証を追加した例です:

from mcp import MCPServer
import os
from dotenv import load_dotenv
import requests
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import redis
import logging
import hmac
import hashlib
import time
import threading
from ratelimit import limits

# ログ設定
logging.basicConfig(
    filename="github_realtime_server.log",
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s"
)

# レートリミット設定(1分間に50リクエスト)
CALLS = 50
PERIOD = 60

class OptimizedGitHubRealtimeServer(MCPServer):
    def __init__(self, host, port, token, repo, webhook_secret, redis_host, redis_port):
        super().__init__(host, port)
        self.token = token
        self.repo = repo
        self.webhook_secret = webhook_secret
        self.base_url = "https://api.github.com"
        self.headers = {
            "Authorization": f"Bearer {token}",
            "Accept": "application/vnd.github.v3+json"
        }
        self.redis = redis.Redis(host=redis_host, port=redis_port, decode_responses=True)
        self.logger = logging.getLogger(__name__)
        self.latest_event = None
        self.register_resource("get_latest_event", self.get_latest_event)
        self.register_tool("add_issue_comment", self.add_issue_comment)
        self.start_webhook_server()

    def verify_webhook_signature(self, body, signature):
        try:
            computed_sig = "sha256=" + hmac.new(
                self.webhook_secret.encode("utf-8"),
                body.encode("utf-8"),
                hashlib.sha256
            ).hexdigest()
            return hmac.compare_digest(computed_sig, signature)
        except Exception as e:
            self.logger.error(f"署名検証失敗: エラー={str(e)}")
            return False

    @limits(calls=CALLS, period=PERIOD)
    def add_issue_comment(self, params):
        request_id = str(time.time())
        self.logger.info(f"リクエスト受信 [ID: {request_id}]: パラメータ={params}")
        try:
            issue_number = params.get("issue_number", 0)
            comment = params.get("comment", "")
            if not issue_number or not comment:
                self.logger.warning(f"リクエスト失敗 [ID: {request_id}]: Issue番号とコメントが必要です")
                return {"status": "error", "message": "Issue番号とコメントが必要です"}
            
            url = f"{self.base_url}/repos/{self.repo}/issues/{issue_number}/comments"
            payload = {"body": comment}
            response = requests.post(url, headers=self.headers, json=payload)
            response.raise_for_status()
            self.logger.info(f"リクエスト成功 [ID: {request_id}]: コメントID={response.json()['id']}")
            return {"status": "success", "comment_id": response.json()["id"]}
        except Exception as e:
            self.logger.error(f"リクエスト失敗 [ID: {request_id}]: エラー={str(e)}")
            return {"status": "error", "message": str(e)}

    def get_latest_event(self, params):
        request_id = str(time.time())
        self.logger.info(f"リクエスト受信 [ID: {request_id}]: パラメータ={params}")
        try:
            cache_key = f"github_event:{self.repo}"
            cached_event = self.redis.get(cache_key)
            if cached_event:
                self.logger.info(f"キャッシュヒット: キー={cache_key}")
                return json.loads(cached_event)

            if self.latest_event:
                event_type = self.latest_event.get("event", "")
                event = self.latest_event.get("payload", {})
                event_info = {
                    "event_type": event_type,
                    "action": event.get("action", ""),
                    "issue_number": event.get("issue", {}).get("number", 0),
                    "pr_number": event.get("pull_request", {}).get("number", 0),
                    "user": event.get("sender", {}).get("login", "unknown"),
                    "created_at": event.get("issue", {}).get("created_at", "") or event.get("pull_request", {}).get("created_at", "")
                }
                self.redis.setex(cache_key, 300, json.dumps({"status": "success", "event_info": event_info}))
                self.logger.info(f"リクエスト成功 [ID: {request_id}]: イベント={event_type}")
                return {"status": "success", "event_info": event_info}
            self.logger.info(f"リクエスト成功 [ID: {request_id}]: イベントなし")
            return {"status": "success", "event_info": None, "message": "イベントなし"}
        except Exception as e:
            self.logger.error(f"リクエスト失敗 [ID: {request_id}]: エラー={str(e)}")
            return {"status": "error", "message": str(e)}

    def start_webhook_server(self):
        class WebhookHandler(BaseHTTPRequestHandler):
            def do_POST(self):
                content_length = int(self.headers["Content-Length"])
                post_data = self.rfile.read(content_length)
                signature = self.headers.get("X-Hub-Signature-256", "")

                # Webhook署名検証
                if not self.server.parent.verify_webhook_signature(post_data.decode("utf-8"), signature):
                    self.send_response(401)
                    self.end_headers()
                    self.wfile.write(b"Invalid signature")
                    self.server.parent.logger.error("Webhook署名検証失敗")
                    return

                data = json.loads(post_data.decode("utf-8"))
                event_type = self.headers.get("X-GitHub-Event", "")

                # イベント処理
                if event_type in ["issues", "pull_request"]:
                    self.server.parent.latest_event = {"event": event_type, "payload": data}
                    self.server.parent.logger.info(f"Webhook受信: イベント={event_type}")

                self.send_response(200)
                self.end_headers()
                self.wfile.write(b"Webhook received")

        server = HTTPServer(("localhost", 8131), WebhookHandler)
        server.parent = self
        threading.Thread(target=server.serve_forever, daemon=True).start()
        print("Webhookサーバーを起動中: http://localhost:8131")

if __name__ == "__main__":
    load_dotenv()
    server = OptimizedGitHubRealtimeServer(
        host="localhost",
        port=8131,
        token=os.getenv("GITHUB_TOKEN"),
        repo=os.getenv("GITHUB_REPO"),
        webhook_secret=os.getenv("GITHUB_WEBHOOK_SECRET"),
        redis_host=os.getenv("REDIS_HOST", "localhost"),
        redis_port=int(os.getenv("REDIS_PORT", 6379))
    )
    print("最適化GitHubリアルタイムMCPサーバーを起動中: http://localhost:8131")
    server.start()

コードの説明

  • Redisキャッシュ:イベントデータをキャッシュ(5分間有効)。setexで有効期限を設定。
  • レートリミティングratelimitライブラリで1分間に50リクエストを制限。
  • セキュリティログ:リクエスト、Webhook受信、署名検証をgithub_realtime_server.logに記録。
  • Webhook署名検証:GitHubのX-Hub-Signature-256をHMAC-SHA256で検証。
  • add_issue_comment:Issueコメント追加をレートリミット付きで実行。
  • get_latest_event:キャッシュまたは最新イベントを取得。

前提条件

  • Redisサーバーが稼働(例:docker run -p 6379:6379 redis)。
  • GitHubリポジトリにWebhookが設定済み(issuespull_requestイベント)。
  • .envファイルにGITHUB_TOKENGITHUB_REPOGITHUB_WEBHOOK_SECRETREDIS_HOSTREDIS_PORTが設定済み。
  • ngrokでWebhook URLが公開され、GitHubに登録済み。
  • トークンにrepoadmin:repo_hookスコープが付与されている。

サーバーのテスト

サーバーが正しく動作するか確認します:

  1. Redis起動

    docker run -p 6379:6379 redis
    
  2. ngrok起動

    ngrok http 8131
    

    ngrok URL(例:https://abc123.ngrok.io)を記録し、GitHubリポジトリのWebhook設定に設定(例:https://abc123.ngrok.io/webhook)。

  3. サーバー起動

    python optimized_github_realtime_server.py
    

    コンソールに「最適化GitHubリアルタイムMCPサーバーを起動中: http://localhost:8131」と「Webhookサーバーを起動中: http://localhost:8131」が表示。

  4. 最新イベント取得のテスト

    • GitHubのUIでリポジトリにIssueを作成(例:「バグ報告」)。
    • Pythonでリクエストを送信:
      import requests
      import json
      
      url = "http://localhost:8131"
      payload = {
          "jsonrpc": "2.0",
          "method": "get_latest_event",
          "params": {},
          "id": 1
      }
      response = requests.post(url, json=payload)
      print(json.dumps(response.json(), indent=2, ensure_ascii=False))
      
      期待されるレスポンス:
      {
        "jsonrpc": "2.0",
        "result": {
          "status": "success",
          "event_info": {
            "event_type": "issues",
            "action": "opened",
            "issue_number": 5,
            "pr_number": 0,
            "user": "yourusername",
            "created_at": "2025-04-22T13:00:00Z"
          }
        },
        "id": 1
      }
      
  5. ログ確認
    github_realtime_server.logに以下のような記録が残る:

    2025-04-22 13:00:00,123 - INFO - Webhook受信: イベント=issues
    2025-04-22 13:00:00,125 - INFO - リクエスト受信 [ID: 1617187200.125]: パラメータ={}
    2025-04-22 13:00:00,126 - INFO - キャッシュヒット: キー=github_event:yourusername/mcp-test-repo
    

コミュニティへの貢献

GitHub用MCPサーバーをオープンソースとして共有することで、コミュニティに貢献できます。以下のステップで進めます:

1. GitHubでの公開

  • リポジトリ作成:GitHubに新しいリポジトリを作成(例:github-mcp-server)。

  • コード整理:モジュール化し、再利用可能な構造にする。

  • README:インストール手順、使い方、例を記載。

    # GitHub MCP Server
    GitHubとMCPを統合し、AIエージェントを構築するサーバーです。
    
    ## インストール
    ```bash
    pip install mcp requests redis ratelimit
    

    使い方

    1. .envGITHUB_TOKENGITHUB_REPOGITHUB_WEBHOOK_SECRETを設定。
    2. python server.pyで起動。
  • ライセンス:MITライセンスを選択。

2. ドキュメントの提供

  • Qiita記事:このシリーズのようなチュートリアルを共有。
  • GitHubコミュニティ:GitHub DiscussionsやRedditでプロジェクトを紹介。
  • MCPコミュニティ:MCPのGitHub IssuesやDiscordでサーバーを提案。

3. フィードバックの収集

  • Issueトラッキング:バグ報告や機能リクエストを受け付ける。
  • プルリクエスト:他の開発者からの貢献を歓迎。
  • 改善の継続:コミュニティのフィードバックを基にサーバーを更新。

GitHubとMCPの未来

GitHubとMCPの組み合わせは、AIを開発プロセスの強力なツールに変える可能性を秘めています。以下は、長期的なビジョンです:

1. ネイティブ統合

  • ビジョン:GitHubがMCPをネイティブサポートし、ActionsやAppsからMCPサーバーを直接接続。
  • :GitHub MarketplaceにMCPエージェントを追加。

2. エンタープライズ採用

  • ビジョン:企業がGitHubとMCPを使って、開発プロセスの自動化や分析をスケール。
  • :AIが全リポジトリのデータを統合し、組織全体のコード品質を分析。

3. パーソナライズドAI

  • ビジョン:個人がプライベートリポジトリでMCPを利用し、カスタムAIで開発を支援。
  • :AIが個人のコーディングパターンを学習し、リファクタリングを提案。

シリーズのまとめ

このシリーズを通じて、GitHubとMCPを活用したAIエージェントの構築を以下のように学びました:

  • 第1章:GitHubとMCPの基本、Issueとコミットデータ取得。
  • 第2章:タスク自動化でIssue作成やPRコメントを効率化。
  • 第3章:コード分析エージェントでコミット頻度や貢献者を評価。
  • 第4章:リアルタイム管理AIでIssueやPRイベントを動的監視。
  • 第5章:サーバーの最適化、セキュリティ、コミュニティ貢献。

GitHubの強力なAPIとMCPの接続性は、AIを開発プロセスの強力なアシスタントに変えます。あなたもこのサーバーを試し、コミュニティで共有して、AIエージェントの未来を共創しませんか?


役に立ったと思ったら、「いいね」や「ストック」をしていただけると嬉しいです!次の挑戦でまたお会いしましょう!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?