MCP Server

Integrations

  • Manages environment variables for API keys and configuration settings through .env files

  • Provides a web API framework for hosting the MCP server, with endpoints for chat interactions and conversation management

  • Provides repository hosting for the MCP server code with instructions for cloning and contributing

MCPサーバーの実装

外部ツールを使用して大規模言語モデル機能を強化するための Model Context Protocol (MCP) の完全な Flask ベースの実装。

概要

このリポジトリは、モデルのテキスト出力内で直接ツールを呼び出すことでLLM機能を拡張する手法であるモデルコンテキストプロトコル(MCP)を処理するサーバーの構築方法を示します。関数呼び出しとは異なり、MCPはツール定義をコンテキストウィンドウに直接配置し、モデルの自然言語応答を解析してツールの使用状況を識別します。

特徴

  • 🔧完全なMCP実装:完全な解析、実行、および応答処理
  • 🌤️サンプルツール: パラメータ検証機能を備えた天気予報および計算ツール
  • 🔄会話フロー:複数のやり取りにわたってコンテキストを維持する
  • 🧩正規表現ベースの解析:ツール呼び出しのための柔軟なテキスト解析
  • 🚀 Flask API : チャット統合のための REST API エンドポイント

プロジェクト構造

mcp_server/ ├── app.py # Main Flask application ├── mcp_handler.py # MCP parsing and execution ├── mcp_example.py # Standalone MCP example ├── requirements.txt # Dependencies ├── tools/ # Tool implementations │ ├── __init__.py │ ├── weather.py # Weather API tool │ └── calculator.py # Calculator tool └── README.md # This file

インストール

  1. リポジトリをクローンします。
    git clone https://github.com/yourusername/mcp-server.git cd mcp-server
  2. 仮想環境を作成します。
    python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
  3. 依存関係をインストールします:
    pip install -r requirements.txt
  4. 環境変数を設定します。
    # Create a .env file with: LLM_API_KEY=your_llm_api_key_here WEATHER_API_KEY=your_weather_api_key_here FLASK_APP=app.py FLASK_ENV=development

使用法

サーバーの実行

Flask 開発サーバーを起動します。

flask run

生産の場合:

gunicorn app:app

APIエンドポイント

  • POST /chat : MCP でチャットメッセージを処理する
    curl -X POST http://localhost:5000/chat \ -H "Content-Type: application/json" \ -d '{ "messages": [ { "role": "user", "content": "What's the weather like in Boston?" } ] }'

スタンドアロンの例

サンプル スクリプトを実行して、MCP の動作を確認します。

python mcp_example.py

仕組み

  1. ツール登録: ツールはパラメータと実行ロジックとともに登録されます
  2. ツール定義の挿入: XML形式のツールの説明がプロンプトに追加されます
  3. LLM 応答処理: 正規表現パターンは LLM のテキスト出力内のツール呼び出しを識別します。
  4. ツール実行: パラメータが解析され、適切なツールハンドラに渡されます。
  5. 結果の挿入: ツールの実行結果がレスポンスに挿入されます

MCPと関数呼び出し

特徴MCP関数呼び出し
定義場所プロンプトテキストAPIパラメータ内
呼び出し形式自然言語構造化JSON
実装テキスト解析API統合
可視性応答で表示される隠れている可能性があります
プラットフォームサポートテキストベースのLLMAPIサポートが必要

会話例

ユーザー: ボストンの天気はどうですか?

法学修士

I'll check the weather for you. get_weather(location="Boston, MA", unit="fahrenheit")

処理後:

I'll check the weather for you. get_weather(location="Boston, MA", unit="fahrenheit") Result from get_weather: { "location": "Boston, MA", "temperature": 72, "unit": "fahrenheit", "conditions": "Partly Cloudy", "humidity": 68, "wind_speed": 5.8 }

独自のツールを追加する

  1. Toolから継承した新しいクラスを作成する
  2. パラメータと実行ロジックを定義する
  3. MCPハンドラーに登録する

例:

class MyTool(Tool): def __init__(self): parameters = [ { "name": "param1", "type": "string", "description": "Description of param1", "required": True } ] super().__init__( name="my_tool", description="Description of my tool", parameters=parameters ) def execute(self, param1): # Tool logic here return {"result": "Processed " + param1}

MCP の構成と呼び出しフロー

  1. ツール登録:
    • MCPツールはハンドラーに登録されます
    • 各ツールは、名前、説明、パラメータ定義を提供します。
  2. ツール定義の注入:
    • ツール定義がシステムメッセージに追加されます
    • MCPのフォーマットはXML構造に従います
  3. LLM応答処理:
    • LLMはツールの呼び出しを含む可能性のある応答を生成します
    • パターンマッチングはテキスト内のツール呼び出しを識別します
    • ツールパラメータは解析され、ツール実行メソッドに渡されます
  4. ツールの実行:
    • ツールは指定されたパラメータで実行されます
    • 結果が会話に反映される
  5. 会話管理:
    • ツールの結果を含む処理済みの応答は会話履歴に追加されます
    • 今後のLLMリクエストには、コンテキストのためにこの履歴が含まれます。

会話例

会話の例は次のようになります。

ユーザー: ボストンの天気はどうですか?

システム: MCPツール定義を含むプロンプトをLLMに送信します

LLMの回答:

I'll check the weather for you. get_weather(location="Boston, MA", unit="fahrenheit")

MCPハンドラ:応答を解析し、ツール呼び出しを見つけて、天気ツールを実行します。

ツール実行結果:

Result from get_weather: { "location": "Boston, MA", "temperature": 72, "unit": "fahrenheit", "conditions": "Partly Cloudy", "humidity": 68, "wind_speed": 5.8 }

処理された応答(ユーザーに返送されます):

I'll check the weather for you. get_weather(location="Boston, MA", unit="fahrenheit") Result from get_weather: { "location": "Boston, MA", "temperature": 72, "unit": "fahrenheit", "conditions": "Partly Cloudy", "humidity": 68, "wind_speed": 5.8 }

ユーザー: 144 の平方根を計算できますか?

LLMの回答:

I can calculate that for you. calculator(expression="sqrt(144)")

MCP ハンドラ:応答を解析し、計算ツールを実行します

ツール実行結果:

Result from calculator: { "expression": "sqrt(144)", "result": 12.0 }

処理された応答(ユーザーに返送されます):

I can calculate that for you. calculator(expression="sqrt(144)") Result from calculator: { "expression": "sqrt(144)", "result": 12.0 } The square root of 144 is 12.

これは、LLM のテキストベースの呼び出しから実行および応答処理までの、MCP ツールの使用の完全なフローを示しています。

ライセンス

マサチューセッツ工科大学

貢献

貢献を歓迎します!お気軽にプルリクエストを送信してください。

-
security - not tested
F
license - not found
-
quality - not tested

モデル コンテキスト プロトコルを実装し、自然言語を介して外部ツール機能で LLM を強化する Flask ベースのサーバー。これにより、天気検索や計算などのツールをモデルのテキスト出力で直接呼び出すことができます。

  1. Overview
    1. Features
      1. Project Structure
        1. Installation
          1. Usage
            1. Running the Server
            2. API Endpoints
            3. Standalone Example
          2. How It Works
            1. MCP vs. Function Calling
              1. Example Conversation
                1. Adding Your Own Tools
                  1. MCP Configuration and Invocation Flow
                    1. Example Conversation
                      1. License
                        1. Contributing
                          ID: np0gzeg9av