Ethereum Trading MCP Server
一个基于 Rust 构建的以太坊交易 MCP (Model Context Protocol) 服务器,为 AI 代理提供查询余额和执行代币交换的能力。
概述
本项目实现了一个 MCP 服务器,支持以下核心功能:
get_balance- 查询 ETH 和 ERC20 代币余额get_token_price- 获取代币当前价格(以 ETH 为单位)swap_tokens- 执行代币交换模拟(基于 Uniswap V2)
技术栈
- Rust - 使用 async/await 异步编程
- Tokio - 异步运行时
- Ethers-rs - 以太坊 RPC 客户端库
- Axum - HTTP 服务器框架
- Rust Decimal - 高精度金融计算
- Tracing - 结构化日志记录
项目结构
src/
├── bin/ # 调试和测试二进制文件
├── error.rs # 错误处理
├── lib.rs # 库入口
├── main.rs # 主程序入口
├── mcp/ # MCP 协议相关
│ ├── interface/ # MCP 接口定义
│ └── types/ # 请求/响应类型定义
├── server/ # HTTP 服务器实现
│ ├── http.rs # HTTP 处理器
│ ├── mod.rs # 模块定义
│ └── server.rs # 服务器核心逻辑
└── tools/ # 核心工具实现
├── get_balance/ # 余额查询工具
├── get_token_price/ # 价格查询工具
└── swap/ # 代币交换工具安装和运行
前置要求
- Rust 1.70+
- Cargo
安装依赖
cargo build启动服务
# 启动 MCP 服务器
cargo run --bin eth_trade_mcp服务器将在 http://localhost:3000 启动。
运行测试
# 运行所有 Rust 单元测试
cargo test
# 运行 Python 自动化测试(需要先启动服务)
python test/test_automation.py
# 运行特定模块的测试
cargo test get_balance
cargo test get_token_price
cargo test swap_tokensAPI 使用示例
1. 查询 ETH 余额
请求:
{
"id": "1",
"method": "get_balance",
"params": {
"address": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
}
}响应:
{
"id": "1",
"success": true,
"data": {
"address": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
"token_address": null,
"symbol": "ETH",
"balance": "1234.567890123456789",
"decimals": 18
},
"error": null
}2. 查询 ERC20 代币余额
请求:
{
"id": "2",
"method": "get_balance",
"params": {
"address": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
"token_address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
}
}3. 获取代币价格
请求:
{
"id": "3",
"method": "get_token_price",
"params": {
"symbol": "USDC"
}
}响应:
{
"id": "3",
"success": true,
"data": {
"symbol": "USDC",
"price": "0.000312"
},
"error": null
}4. 代币交换模拟
请求:
{
"id": "4",
"method": "swap_tokens",
"params": {
"from_token": "ETH",
"to_token": "USDC",
"amount": 0.1,
"slippage_tolerance": 0.5,
"wallet_address": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
}
}响应:
{
"id": "4",
"success": true,
"data": {
"estimated_output": "320.45",
"minimum_output": "318.84",
"estimated_gas": 150000,
"gas_price": 20000000000,
"executable": true,
"protocol": "V2",
"error": null
},
"error": null
}设计决策
- 模块化架构: 将不同功能分离到独立的模块中,便于维护和测试。每个工具(get_balance、get_token_price、swap_tokens)都有自己的目录和实现。
- 真实链上数据: 所有余额查询都直接从以太坊主网获取真实数据,使用公共 RPC 端点(eth.llamarpc.com)确保数据的准确性和实时性。
- Uniswap V2 集成: 代币交换功能基于 Uniswap V2 协议,通过构建真实的交换交易并使用
eth_call进行模拟,确保估算的准确性而不实际执行交易。
- 高精度计算: 使用
rust_decimal库处理所有金融计算,避免浮点数精度问题,确保代币金额和价格计算的准确性。
- 异步设计: 全面采用 Tokio 异步运行时,支持高并发请求处理,提高服务器性能和响应速度。
已知限制
- 网络依赖: 服务依赖于外部以太坊 RPC 节点,网络问题可能影响服务可用性。
- 代币支持: 价格查询目前主要支持主流代币(USDC、USDT、DAI、WETH 等),对于小众代币可能无法获取价格。
- 仅模拟交换: 代币交换功能仅进行模拟,不执行实际的链上交易。实际交易需要私钥管理和交易签名功能。
- Gas 估算: Gas 费用估算基于当前网络状况,实际执行时可能因网络拥堵而有所不同。
- 滑点保护: 当前滑点容忍度设置为固定值,在高波动市场中可能需要动态调整。
测试
项目包含完整的测试套件:
# 运行所有测试
cargo test
# 运行特定模块测试
cargo test get_balance
cargo test get_token_price
cargo test swap_tokens
# 运行集成测试
python test/test_automation.py调试工具
项目提供了多个调试二进制文件用于测试特定功能:
# 测试完整的代币交换流程
cargo run --bin debug_full_swap
# 测试代币地址解析
cargo run --bin debug_token_resolver
# 测试交换请求处理
cargo run --bin debug_swap_request贡献
欢迎提交 Issue 和 Pull Request。在提交代码前,请确保:
- 代码通过所有测试:
cargo test - 代码格式正确:
cargo fmt - 代码通过 lint 检查:
cargo clippy
许可证
本项目采用 MIT 许可证。详见 LICENSE 文件。
