Skip to main content
Glama

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統合

可視性

応答で表示される

隠れている可能性があります

プラットフォームサポート

テキストベースのLLM

APIサポートが必要

会話例

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

法学修士

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
-
license - not tested
-
quality - not tested

Related MCP Servers

  • A
    security
    A
    license
    A
    quality
    A Model Context Protocol server that provides basic mathematical and statistical functions to LLMs, enabling them to perform accurate numerical calculations through a simple API.
    Last updated -
    13
    5
    52
    MIT License
  • -
    security
    A
    license
    -
    quality
    A comprehensive toolkit that enhances LLM capabilities through the Model Context Protocol, allowing LLMs to interact with external services including command-line operations, file management, Figma integration, and audio processing.
    Last updated -
    25
    Apache 2.0
    • Linux
    • Apple
  • -
    security
    -
    license
    -
    quality
    A Model Context Protocol server that enables LLMs to interact with databases (currently MongoDB) through natural language, supporting operations like querying, inserting, deleting documents, and running aggregation pipelines.
    Last updated -
    MIT License
    • Apple
  • A
    security
    -
    license
    A
    quality
    A Model Context Protocol server that allows LLMs to interact with Python environments, enabling code execution, file operations, package management, and development workflows.
    Last updated -
    9

View all related MCP servers

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yisu201506/mcp_server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server