工具列表
內容詳情
替代品
安裝
🚀 MCP測試平臺
本項目展示瞭如何使用模型上下文協議(MCP)進行情感分析,它基於Hugging Face MCP課程開發。與Hugging Face課程中使用的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),路徑為/ssemcp_client_streamable.py使用可流式HTTP,路徑為/mcp
服務器
- 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
使用服務器發送事件(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屬性
🚀 快速開始
本應用程序使用模型上下文協議(MCP)來促進客戶端和情感分析服務器之間的通信。當你使用文本字符串運行客戶端時,它會執行以下操作:
- 連接到服務器腳本 (
app_fastmcp.py) 或sse/streamable-http端點。 - 發送你的文本進行情感分析。
- 返回情感分析結果(積極、消極或中性)。
服務器 (app_fastmcp.py)
app_fastmcp.py 文件使用FastMCP實現了一個MCP服務器,該服務器:
- 通過模型上下文協議暴露一個情感分析工具。
- 使用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正確管理資源。
💻 使用示例
標準輸入輸出傳輸的使用示例
命令行參數
((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
客戶端
# 使用默認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檢查器是一個用於探索和與模型上下文協議(MCP)服務器進行交互的工具。它提供了一個用戶友好的界面,用於:
- 發現可用的工具及其功能。
- 向工具發送請求並查看響應。
- 調試和測試MCP交互。
使用標準輸入輸出傳輸運行MCP檢查器
要使用標準輸入輸出傳輸為服務器運行MCP檢查器,請使用以下命令:
mcp dev mcp-sentiment/app_fastmcp.py
示例輸出將顯示可用的工具及其描述,允許你與情感分析工具進行交互。
MCP檢查器列出工具
MCP檢查器測試情感分析
使用SSE傳輸運行MCP檢查器
啟動用於測試的SSE服務器
python mcp-sentiment/app_fastmcp.py --transport sse
要使用SSE或可流式HTTP傳輸為服務器運行MCP檢查器,請使用以下命令:
npx @modelcontextprotocol/inspector
連接到MCP檢查器
打開瀏覽器訪問MCP檢查器界面,必要時將 http 更改為 https。MCP檢查器運行後,為 sse 或 streamable-http 配置“傳輸類型”,並將服務器URL設置為指向正在運行的MCP服務器(例如,http://localhost:8000/sse 或 http://localhost:8000/mcp),然後點擊“連接”按鈕。
SSE服務器URL:http://localhost:8000/sse
📦 安裝指南
- Python 3.12+
- 依賴項:
pip install -r requirements.txt - 情感分析所需的NLP庫
- 運行MCP檢查器需要
node版本大於 v20.x(請參閱 GH Issue on unexpected token)
替代品









