REST与MCP:协议基准套件
一个全面的基准测试工具包,旨在比较以下各项的性能和架构差异 REST API(无状态) 和那个 模型上下文协议(MCP,有状态)该项目模拟了真实世界的AI代理工作流程,以演示协议选择如何影响延迟、带宽和可扩展性。
______________________________________________________________________
🚀 概述
随着人工智能代理变得越来越复杂,将它们连接到数据和工具的方法变得至关重要。本项目对两种方法进行了直接比较:
- REST API:传统的无状态方法,其中每个请求都必须包含完整的上下文历史。
- MCP(模型上下文协议):一种有状态的、基于连接的标准(使用JSON-RPC/SSE),专为LLM设计,用于高效维护上下文。
关键差异测试
- 上下文窗口缩放:随着聊天历史记录从1条增加到100多条消息,性能如何变化。
- 网络条件:模拟4G/LTE网络(高延迟、低带宽)以暴露协议开销。
- 工具链:多步骤代理工作流的效率。
- 实时更新:轮询(REST)与服务器发送事件(MCP)。
______________________________________________________________________
📊 视觉比较
架构概述
REST (Stateless) MCP (Stateful)
请求1:
POST /chat
{
"history": ["Hello"]
}✅ 响应
请求2:
POST /chat
{
"history": ["Hello", "How are you?"]
}✅ 响应 *(重新发送冗余数据)*
请求3:
POST /chat
{
"history": ["Hello", "How are you?", "Tell me more"]
}✅ 响应 *(有效载荷不断增长)*
初始连接:
CONNECT /mcp
SSE Stream Established✅ 会话ID: abc123
请求1:
call_tool("chat", "Hello")✅ 响应
请求2:
call_tool("chat", "How are you?")✅ 响应 *(服务器记住上下文)*
请求3:
call_tool("chat", "Tell me more")✅ 响应 *(仅发送新数据)*
主要区别:带宽随时间变化
%%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#ff6b6b','primaryTextColor':'#fff','primaryBorderColor':'#c92a2a','lineColor':'#fab005','secondaryColor':'#51cf66','tertiaryColor':'#fff'}}}%%
graph TB
subgraph REST["🔴 REST API - Linear Growth Problem"]
direction LR
R1["Turn 1
📦 100 bytes"]:::small
R2["Turn 2
📦📦 200 bytes"]:::medium
R3["Turn 3
📦📦📦 300 bytes"]:::large
R4["Turn 5
📦📦📦📦📦 500 bytes"]:::xlarge
R5["Turn 10
📦📦📦📦📦
📦📦📦📦📦 1000 bytes"]:::huge
R1 ==> R2 ==> R3 ==> R4 ==> R5
end
subgraph MCP["🟢 MCP - Constant Efficiency"]
direction LR
M1["Turn 1
📦 100 bytes"]:::constant
M2["Turn 2
📦 100 bytes"]:::constant
M3["Turn 3
📦 100 bytes"]:::constant
M4["Turn 5
📦 100 bytes"]:::constant
M5["Turn 10
📦 100 bytes"]:::constant
M1 ==> M2 ==> M3 ==> M4 ==> M5
end
classDef small fill:#ffdecc,stroke:#ff6b6b,stroke-width:2px,color:#000
classDef medium fill:#ffb3b3,stroke:#ff6b6b,stroke-width:2px,color:#000
classDef large fill:#ff9999,stroke:#c92a2a,stroke-width:3px,color:#000
classDef xlarge fill:#ff6b6b,stroke:#c92a2a,stroke-width:4px,color:#fff
classDef huge fill:#c92a2a,stroke:#a61e1e,stroke-width:5px,color:#fff,font-weight:bold
classDef constant fill:#51cf66,stroke:#2f9e44,stroke-width:2px,color:#000📈 视觉效果:
- 表征状态转移:每条消息都包含 *整个* 对话历史→ 指数型间接费用
- 主控程序:每条消息都包含 *仅* 新数据→ 服务器高效维护状态
现实世界的影响:在100个聊天回合时,REST传输 ~5MB vs MCP ~50KB (相差100倍!)
______________________________________________________________________
🛠️ 安装
- 克隆存储库
git clone https://github.com/yourusername/rest-vs-mcp-2.0.git
cd rest-vs-mcp-2.0- 创建虚拟环境(推荐)
python -m venv venv
# Windows
.\venv\Scripts\activate
# Mac/Linux
source venv/bin/activate- 再进行
pip install -r requirements.txt______________________________________________________________________
💻 用法
该项目包括一个用于自动基准测试的命令行界面和一个用于可视化的交互式仪表板。
1.交互式仪表板(GUI)
探索数据的最佳方式。启动Streamlit应用程序,实时可视化延迟、带宽和吞吐量。
python main.py --gui*或者直接:*
streamlit run dashboard/app.py2.CLI基准(无头)
运行全套高级基准测试并生成CSV/Markdown报告。
python main.py --cli --new_report--new_report:创建带时间戳的报告文件,而不是覆盖默认值。
______________________________________________________________________
📂 项目结构
REST vs MCP 2.0/
├── benchmarks/ # Core benchmarking logic and scenarios
├── clients/ # Client implementations (REST & MCP)
├── dashboard/ # Streamlit visualization app
├── reporting/ # Report generation utilities
├── reports/ # Output directory for CSV/MD results
├── servers/ # Server implementations
│ ├── rest_server.py # FastAPI based REST server
│ └── mcp_server.py # Custom JSON-RPC/SSE MCP server
├── main.py # Entry point
└── requirements.txt # Project dependencies______________________________________________________________________
🧪 基准情景
该套件测试以下具体场景:
- 有状态与无状态语境:模拟不断增长的对话。衡量重新发送历史记录(REST)与维护状态(MCP)的影响。
- 长时间运行的任务:比较“轮询”(REST)和“推送通知”(MCP)在需要时间完成的任务上的差异。
- 实时股票行情:分析高频轮询与基于订阅的更新的开销。
- 工具链:测试多步骤工作流,其中一个工具的输出是下一个的输入。
- 网络仿真:应用人工延迟(例如50ms)和带宽限制(例如5Mbps)来模拟现实世界的移动条件。
______________________________________________________________________
📄 许可证
根据MIT许可证分发。看 LICENSE 了解更多信息。
