ツールリスト
コンテンツ詳細
代替品
インストール
🚀 MCP Testbed
このプロジェクトは、Model Context Protocol (MCP) を使用した感情分析をデモンストレートしています。Hugging Face MCPコースに基づいています。HuggingFaceのコースで使用されている gradio ライブラリの代わりに、このプロジェクトでは fastmcp ライブラリを利用してMCPサーバーとクライアントを実装しています。
✨ 主な機能
アーキテクチャ図
+-------------------+ +-------------------+
| | | |
| mcp_client_stdio |<----------------->| |
| | stdio | |
+-------------------+ | |
| |
+-------------------+ | |
| |<----------------->| |
| mcp_client_sse |-----/sse--------->| MCP Server |
| | | (app_fastmcp.py) |
+-------------------+ | |
| |
+-------------------+ | |
| |<----------------->| |
| mcp_client_stream-|----/mcp---------->| |
| able.py | | |
+-------------------+ +-------------------+
各クライアントモジュールは、異なるトランスポートプロトコルを使用してMCPサーバーと通信します。
mcp_client_stdio.pyは標準入出力 (stdio) を使用します。mcp_client_sse.pyは/sseでServer-Sent Events (SSE) を使用します。mcp_client_streamable.pyは/mcpでストリーム可能なHTTPを使用します。
サーバー
- mcp-sentiment/app_fastmcp.py
FastMCPを使用してMCPサーバーを実装しています。sentiment_analysisツールを公開し、TextBlobを使用して入力テキストの感情分析を行い、極性、主観性、および全体的な評価をJSON形式で返します。
クライアント
- mcp-sentiment/mcp_client_stdio.py
標準入出力トランスポートを使用するMCPクライアントです。サブプロセスを介してサーバーに接続し、利用可能なツールをリストし、感情分析のためのテキストを送信します。argparseを使用してコマンドライン引数を処理します。 - mcp-sentiment/mcp_client_sse.py
Server-Sent Events (SSE) トランスポートを使用するMCPクライアントです。HTTP SSEエンドポイントを介してサーバーに接続し、ツールをリストし、分析のためのテキストを送信します。argparseを使用してコマンドライン引数を処理します。 - mcp-sentiment/mcp_client_streamable.py
ストリーム可能なHTTPトランスポートを使用するMCPクライアントです。双方向ストリーミングを備えたHTTPを介してサーバーに接続し、ツールをリストし、分析のためのテキストを送信します。argparseを使用してコマンドライン引数を処理します。
テスト
- tests/test_sentiment_analysis.py
感情分析ツールの単体テストで、さまざまな入力ケースとエラーハンドリングをチェックします。 - tests/test_client_stdio.py
標準入出力クライアントのテストで、リクエストハンドリングの単体テストとエンドツーエンドの感情分析の統合テストを含みます。 - tests/test_client_sse.py
SSEクライアントの統合テストで、サーバープロセス管理と感情分析のチェックを含みます。 - tests/test_client_streamable.py
ストリーム可能なHTTPクライアントの統合テストで、サーバープロセス管理と感情分析のチェックを含みます。
ディレクトリ構造
mcp_testlab/
├── mcp-sentiment/
│ ├── app_fastmcp.py # 感情分析ツールを公開するMCPサーバー
│ ├── mcp_client_stdio.py # MCPクライアント (標準入出力トランスポート)
│ ├── mcp_client_sse.py # MCPクライアント (SSEトランスポート)
│ └── mcp_client_streamable.py # MCPクライアント (ストリーム可能なHTTPトランスポート)
├── tests/
│ ├── test_sentiment_analysis.py # 感情分析ツールの単体テスト
│ ├── test_client_stdio.py # 標準入出力クライアントのテスト
│ ├── test_client_sse.py # SSEクライアントのテスト
│ └── test_client_streamable.py # ストリーム可能なHTTPクライアントのテスト
├── requirements.txt # Pythonの依存関係
├── README.md # プロジェクトのドキュメントと使用方法
├── LICENSE # ライセンスファイル
├── .gitignore # Gitの無視ルール
└── .gitattributes # Git LFSの属性
🚀 クイックスタート
このアプリケーションは、Model Context Protocol (MCP) を使用してクライアントと感情分析サーバー間の通信を容易にします。クライアントをテキスト文字列とともに実行すると、以下の手順で動作します。
- サーバースクリプト (
app_fastmcp.py) またはsse/streamable-httpエンドポイントに接続します。 - 感情分析のためのテキストを送信します。
- 感情分析の結果 (肯定的、否定的、または中立的) を返します。
サーバー (app_fastmcp.py)
app_fastmcp.py ファイルは、FastMCPを使用してMCPサーバーを実装しています。
- Model Context Protocolを介して感情分析ツールを公開します。
- TextBlobライブラリを使用して提供されたテキストの感情分析を行います。
- 以下の情報を含むJSON結果を返します。
- 極性スコア (-1から1、負から正)
- 主観性スコア (0から1、客観的から主観的)
- 全体的な評価 (肯定的、否定的、または中立的)
- クライアントとの通信に
stdioまたはsseまたはstreamable-httpトランスポートをサポートします。
クライアント (mcp_client_stdio.py)
mcp_client_stdio.py ファイルは、MCPクライアントを実装しています。
- argparseライブラリを使用してコマンドライン引数を処理します。
- 必須の位置引数としてテキスト入力を受け取ります。
- オプションの
--serverパラメータを受け取り、サーバースクリプトのパスを指定します。 - オプションの
--verboseフラグを受け取り、詳細なツール情報を表示します。 - 標準入出力トランスポートを使用してMCPサーバーに接続します。
- 接続されたサーバー上の利用可能なツールをリストします。
- 入力テキストを
sentiment_analysisツールに送信します。 - 極性、主観性、および評価を示すフォーマットされた感情分析結果を表示します。
- 非同期コンテキストマネージャと
AsyncExitStackを使用してリソースを適切に管理します。
クライアント (mcp_client_sse.py)
mcp_client_sse.py ファイルは、MCPクライアントを実装しています。
- argparseライブラリを使用してコマンドライン引数を処理します。
- 必須の位置引数としてテキスト入力を受け取ります。
- オプションの
--urlパラメータを受け取り、サーバーエンドポイントを指定します (デフォルト:http://localhost:8000/sse)。 - オプションの
--verboseフラグを受け取り、詳細なツール情報を表示します。 - SSEトランスポートを使用してMCPサーバーに接続します。
- 接続されたサーバー上の利用可能なツールをリストします。
- 入力テキストを
sentiment_analysisツールに送信します。 - 極性、主観性、および評価を示すフォーマットされた感情分析結果を表示します。
- 非同期コンテキストマネージャと
AsyncExitStackを使用してリソースを適切に管理します。
クライアント (mcp_client_streamable.py)
mcp_client_streamable.py ファイルは、MCPクライアントを実装しています。
- argparseライブラリを使用してコマンドライン引数を処理します。
- 必須の位置引数としてテキスト入力を受け取ります。
- オプションの
--urlパラメータを受け取り、サーバーエンドポイントを指定します (デフォルト:http://localhost:8000/mcp)。 - オプションの
--verboseフラグを受け取り、詳細なツール情報を表示します。 - ストリーム可能なHTTPトランスポートを使用してMCPサーバーに接続します。
- 利用可能な場合、セッションIDを表示します。
- 接続されたサーバー上の利用可能なツールをリストします。
- 入力テキストを
sentiment_analysisツールに送信します。 - 極性、主観性、および評価を示すフォーマットされた感情分析結果を表示します。
- 非同期コンテキストマネージャと
AsyncExitStackを使用してリソースを適切に管理します。
💻 使用例
標準入出力 (stdio) トランスポートの使用例
コマンドライン引数
((venv) ) Mac:jim mcp_testlab[529]$ python mcp-sentiment/mcp_client_stdio.py --help
usage: mcp_client_stdio.py [-h] [--server SERVER] [--verbose] text
MCP Client for sentiment analysis using stdio transport
positional arguments:
text Text to analyze for sentiment
options:
-h, --help show this help message and exit
--server SERVER Path to the MCP server script (default: mcp-sentiment/app_fastmcp.py)
--verbose, -v Display detailed information about available tools
クライアント
((venv) ) Mac:jim mcp_testlab[512]$ python mcp-sentiment/mcp_client_stdio.py "i love mcp"
Connected to MCP server at mcp-sentiment/app_fastmcp.py
Listing available tools...
[07/29/25 13:45:16] INFO Processing request of type ListToolsRequest server.py:619
Connected to MCP server. Listing available tools...
INFO Processing request of type ListToolsRequest server.py:619
Available tools: ['sentiment_analysis']
Analyzing sentiment for: 'i love mcp'
INFO Processing request of type CallToolRequest server.py:619
{'polarity': 0.5, 'subjectivity': 0.6, 'assessment': 'positive'}
Sentiment Analysis Result:
Polarity: 0.5 (-1=negative, 1=positive)
Subjectivity: 0.6 (0=objective, 1=subjective)
Assessment: positive
Sentiment Analysis Result: (0.5, 0.6, 'positive')
((venv) ) Mac:jim mcp_testlab[513]$ python mcp-sentiment/mcp_client_stdio.py "i hate java"
Connected to MCP server at mcp-sentiment/app_fastmcp.py
Listing available tools...
[07/29/25 13:45:38] INFO Processing request of type ListToolsRequest server.py:619
Connected to MCP server. Listing available tools...
INFO Processing request of type ListToolsRequest server.py:619
Available tools: ['sentiment_analysis']
Analyzing sentiment for: 'i hate java'
INFO Processing request of type CallToolRequest server.py:619
{'polarity': -0.8, 'subjectivity': 0.9, 'assessment': 'negative'}
Sentiment Analysis Result:
Polarity: -0.8 (-1=negative, 1=positive)
Subjectivity: 0.9 (0=objective, 1=subjective)
Assessment: negative
Sentiment Analysis Result: (-0.8, 0.9, 'negative')
((venv) ) Mac:jim mcp_testlab[514]$ python mcp-sentiment/mcp_client_stdio.py "i really like python" -v
Connected to MCP server at mcp-sentiment/app_fastmcp.py
Listing available tools...
[07/29/25 13:46:35] INFO Processing request of type ListToolsRequest server.py:619
Connected to MCP server. Listing available tools...
INFO Processing request of type ListToolsRequest server.py:619
sentiment_analysis:
Description:
Analyze the sentiment of the given text.
Args:
text (str): The text to analyze
Returns:
str: A JSON string containing polarity, subjectivity, and assessment
Annotations: None
Inputschema: {'properties': {'text': {'title': 'Text', 'type': 'string'}}, 'required': ['text'], 'title': 'sentiment_analysisArguments', 'type': 'object'}
Meta: None
/Users/jim/Desktop/modelcontextprotocol/mcp_testlab/mcp-sentiment/mcp_client_stdio.py:154: PydanticDeprecatedSince211: Accessing the 'model_computed_fields' attribute on the instance is deprecated. Instead, you should access this attribute from the model class. Deprecated in Pydantic V2.11 to be removed in V3.0.
value = getattr(tool, attr)
Model_computed_fields: {}
Model_config: {'extra': 'allow'}
Model_extra: {}
/Users/jim/Desktop/modelcontextprotocol/mcp_testlab/mcp-sentiment/mcp_client_stdio.py:154: PydanticDeprecatedSince211: Accessing the 'model_fields' attribute on the instance is deprecated. Instead, you should access this attribute from the model class. Deprecated in Pydantic V2.11 to be removed in V3.0.
value = getattr(tool, attr)
Model_fields: {'name': FieldInfo(annotation=str, required=True), 'title': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'description': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'inputSchema': FieldInfo(annotation=dict[str, Any], required=True), 'outputSchema': FieldInfo(annotation=Union[dict[str, Any], NoneType], required=False, default=None), 'annotations': FieldInfo(annotation=Union[ToolAnnotations, NoneType], required=False, default=None), 'meta': FieldInfo(annotation=Union[dict[str, Any], NoneType], required=False, default=None, alias='_meta', alias_priority=2)}
Model_fields_set: {'description', 'inputSchema', 'outputSchema', 'name'}
Outputschema: {'properties': {'result': {'title': 'Result', 'type': 'string'}}, 'required': ['result'], 'title': 'sentiment_analysisOutput', 'type': 'object'}
Title: None
Analyzing sentiment for: 'i really like python'
INFO Processing request of type CallToolRequest server.py:619
{'polarity': 0.2, 'subjectivity': 0.2, 'assessment': 'positive'}
Sentiment Analysis Result:
Polarity: 0.2 (-1=negative, 1=positive)
Subjectivity: 0.2 (0=objective, 1=subjective)
Assessment: positive
Sentiment Analysis Result: (0.2, 0.2, 'positive')
SSEトランスポートの使用例
コマンドライン引数
((venv) ) Mac:jim mcp_testlab[532]$ python mcp-sentiment/mcp_client_sse.py --help
usage: mcp_client_sse.py [-h] [--url URL] [--verbose] text_to_test
MCP SSE Client for Sentiment Analysis
positional arguments:
text_to_test Text to analyze for sentiment
options:
-h, --help show this help message and exit
--url URL URL of the SSE server endpoint (default: http://localhost:8000/sse)
--verbose, -v Display detailed information about available tools
クライアント
python mcp-sentiment/mcp_client_sse.py "i really like python"
Connecting to MCP server at http://localhost:8000/sse...
Connected to MCP server. Listing available tools...
Available tools: ['sentiment_analysis']
Analyzing sentiment for: 'i really like python'
Sentiment Analysis Result:
Polarity: 0.2 (-1=negative, 1=positive)
Subjectivity: 0.2 (0=objective, 1=subjective)
Assessment: positive
SSEサーバー
((venv) ) Mac:jim mcp_testlab[506]$ python mcp-sentiment/app_fastmcp.py --transport sse
INFO: Started server process [8908]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:49538 - "GET /sse HTTP/1.1" 200 OK
INFO: 127.0.0.1:49540 - "POST /messages/?session_id=fb1a89915f0b40e98f44385af5b6db58 HTTP/1.1" 202 Accepted
INFO: 127.0.0.1:49540 - "POST /messages/?session_id=fb1a89915f0b40e98f44385af5b6db58 HTTP/1.1" 202 Accepted
INFO: 127.0.0.1:49540 - "POST /messages/?session_id=fb1a89915f0b40e98f44385af5b6db58 HTTP/1.1" 202 Accepted
[07/29/25 06:13:08] INFO Processing request of type ListToolsRequest server.py:619
INFO: 127.0.0.1:49540 - "POST /messages/?session_id=fb1a89915f0b40e98f44385af5b6db58 HTTP/1.1" 202 Accepted
INFO Processing request of type CallToolRequest server.py:619
INFO: 127.0.0.1:49543 - "GET /sse HTTP/1.1" 200 OK
INFO: 127.0.0.1:49545 - "POST /messages/?session_id=632a380ef69344eab4bf145158e05051 HTTP/1.1" 202 Accepted
INFO: 127.0.0.1:49545 - "POST /messages/?session_id=632a380ef69344eab4bf145158e05051 HTTP/1.1" 202 Accepted
INFO: 127.0.0.1:49545 - "POST /messages/?session_id=632a380ef69344eab4bf145158e05051 HTTP/1.1" 202 Accepted
[07/29/25 06:13:46] INFO Processing request of type ListToolsRequest server.py:619
INFO: 127.0.0.1:49545 - "POST /messages/?session_id=632a380ef69344eab4bf145158e05051 HTTP/1.1" 202 Accepted
INFO Processing request of type CallToolRequest server.py:619
INFO: 127.0.0.1:49547 - "GET /sse HTTP/1.1" 200 OK
INFO: 127.0.0.1:49549 - "POST /messages/?session_id=f6a51e126c12412398bacc85753174a3 HTTP/1.1" 202 Accepted
INFO: 127.0.0.1:49549 - "POST /messages/?session_id=f6a51e126c12412398bacc85753174a3 HTTP/1.1" 202 Accepted
INFO: 127.0.0.1:49549 - "POST /messages/?session_id=f6a51e126c12412398bacc85753174a3 HTTP/1.1" 202 Accepted
[07/29/25 06:14:17] INFO Processing request of type ListToolsRequest server.py:619
INFO: 127.0.0.1:49549 - "POST /messages/?session_id=f6a51e126c12412398bacc85753174a3 HTTP/1.1" 202 Accepted
INFO Processing request of type CallToolRequest server.py:619
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [8908]
Terminated: 15 python mcp-sentiment/app_fastmcp.py --transport sse
ストリーム可能なHTTPトランスポートの使用例
コマンドライン引数
((venv) ) Mac:jim mcp_testlab[533]$ python mcp-sentiment/mcp_client_streamable.py --help
usage: mcp_client_streamable.py [-h] [--url URL] [--verbose] text_to_test
MCP Streamable Client for Sentiment Analysis
positional arguments:
text_to_test Text to analyze for sentiment
options:
-h, --help show this help message and exit
--url URL URL of the streamable-http server endpoint (default: http://localhost:8000/mcp)
--verbose, -v Display detailed information about available tools
クライアント
# Using the default URL
((venv) ) Mac:jim mcp_testlab[517]$ python mcp-sentiment/mcp_client_streamable.py "MCP is the best"
Connecting to MCP server at http://localhost:8000/mcp...
Session ID: fdc3c721f04441a1ae4c22cadebc9226
Connected to MCP server. Listing available tools...
Available tools: ['sentiment_analysis']
Analyzing sentiment for: 'MCP is the best'
Sentiment Analysis Result:
Polarity: 1.0 (-1=negative, 1=positive)
Subjectivity: 0.3 (0=objective, 1=subjective)
Assessment: positive
ストリーム可能なHTTPサーバー
((venv) ) Mac:jim mcp_testlab[507]$ python mcp-sentiment/app_fastmcp.py --transport streamable-http
INFO: Started server process [2701]
INFO: Waiting for application startup.
[07/28/25 22:48:20] INFO StreamableHTTP session manager started streamable_http_manager.py:112
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:54049 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
[07/28/25 22:48:29] INFO Created new transport with session ID: 6a17c2206e6e478b830bd0da73771b8b streamable_http_manager.py:229
INFO: 127.0.0.1:54049 - "POST /mcp/ HTTP/1.1" 200 OK
INFO: 127.0.0.1:54052 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
INFO: 127.0.0.1:54053 - "GET /mcp HTTP/1.1" 307 Temporary Redirect
INFO: 127.0.0.1:54052 - "POST /mcp/ HTTP/1.1" 202 Accepted
INFO: 127.0.0.1:54053 - "GET /mcp/ HTTP/1.1" 200 OK
INFO: 127.0.0.1:54055 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
INFO: 127.0.0.1:54055 - "POST /mcp/ HTTP/1.1" 200 OK
INFO Processing request of type ListToolsRequest server.py:619
INFO: 127.0.0.1:54057 - "POST /mcp HTTP/1.1" 307 Temporary Redirect
INFO: 127.0.0.1:54057 - "POST /mcp/ HTTP/1.1" 200 OK
INFO Processing request of type CallToolRequest server.py:619
INFO: 127.0.0.1:54059 - "DELETE /mcp HTTP/1.1" 307 Temporary Redirect
INFO Terminating session: 6a17c2206e6e478b830bd0da73771b8b streamable_http.py:633
INFO: 127.0.0.1:54059 - "DELETE /mcp/ HTTP/1.1" 200 OK
INFO: Shutting down
INFO: Waiting for application shutdown.
[07/28/25 22:48:42] INFO StreamableHTTP session manager shutting down streamable_http_manager.py:116
INFO: Application shutdown complete.
INFO: Finished server process [2701]
Terminated: 15 python mcp-sentiment/app_fastmcp.py --transport streamable-http
🔧 技術詳細
単体テストが提供されており、MCPサーバーとクライアントの機能を確保します。テストを実行するには、以下のコマンドを使用します。
$ pytest -v tests
サンプル出力:
MCPインスペクター
MCPインスペクターは、Model Context Protocol (MCP) サーバーを探索および操作するためのツールです。以下の機能を提供するユーザーフレンドリーなインターフェイスを備えています。
- 利用可能なツールとその機能を発見する
- ツールにリクエストを送信し、レスポンスを表示する
- MCPの相互作用をデバッグおよびテストする
標準入出力 (stdio) トランスポートでMCPインスペクターを実行する
標準入出力トランスポートを使用するサーバーのMCPインスペクターを実行するには、以下のコマンドを使用します。
mcp dev mcp-sentiment/app_fastmcp.py
サンプル出力には、利用可能なツールとその説明が表示され、感情分析ツールとの対話が可能になります。
MCPインスペクターのツールリスト
MCPインスペクターでの感情分析テスト
SSEトランスポートでMCPインスペクターを実行する
テスト用の sse サーバーを起動する
python mcp-sentiment/app_fastmcp.py --transport sse
sse または streamable-http トランスポートを使用するサーバーのMCPインスペクターを実行するには、以下のコマンドを使用します。
npx @modelcontextprotocol/inspector
MCPインスペクターに接続する
ブラウザを開いてMCPインスペクターのインターフェイスにアクセスし、必要に応じて http から https に変更します。MCPインスペクターが起動したら、「Transport Type」を sse または streamable-http に設定し、サーバーURLを実行中のMCPサーバーに向けて設定し (例: http://localhost:8000/sse または http://localhost:8000/mcp)、「Connect」ボタンをクリックします。
sse サーバーURL: http://localhost:8000/sse
ストリーム可能なHTTPトランスポートでMCPインスペクターを実行する
テスト用の streamable-http サーバーを起動する
python mcp-sentiment/app_fastmcp.py --transport streamable-http
streamable-http サーバーURL: http://localhost:8000/mcp
📦 インストール
必要条件
- Python 3.12以上
- 依存関係:
pip install -r requirements.txt - 感情分析に必要なNLPライブラリ
- MCPインスペクターを実行するには
nodeのバージョンがv20.xより新しい必要があります (詳細はGH Issue on unexpected tokenを参照)
代替品









