eth-trading-mcp-server

VictorGithub200/eth-trading-mcp-server

3.2

If you are the rightful owner of eth-trading-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

Ethereum Trading MCP Server is a Rust-based server designed to facilitate Ethereum trading operations using the Model Context Protocol (MCP).

Tools
1
Resources
0
Prompts
0

Ethereum Trading MCP Server

环境准备

  • 安装 Rust stable(推荐使用 rustup default stable)。
  • MacOS 需具备 pkg-config 与 OpenSSL(可通过 brew install pkg-config openssl 安装)。
  • 设置必要的环境变量:
    • ETH_RPC_URL:指向主网或测试网的以太坊 RPC Endpoint。
    • PRIVATE_KEY:用于签名模拟交易的钱包私钥(十六进制,不含 0x)。
    • CHAIN_ID:链 ID(默认 1)。
    • MCP_SOCKET(可选):stdio(默认)或 TCP 地址如 127.0.0.1:4000

快速启动

cargo build
cargo clippy -- -D warnings
cargo test            # 集成测试默认忽略,需要外部 RPC
cargo run             # 以 STDIO transport 启动 MCP 服务

若需监听 TCP,可设置 MCP_SOCKET=127.0.0.1:4000 后再运行 cargo run

工具示例

模拟请求 get_balance

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "get_balance",
    "arguments": {
      "wallet_address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
    }
  }
}

可能的响应:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "content": [
      {
        "type": "application/json",
        "data": {
          "wallet_address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
          "token": "ETH",
          "balance": "1234.567890123456789"
        }
      }
    ],
    "is_error": false
  }
}

设计说明

  1. 使用 rmcp 宏构建 MCP 服务,所有工具返回 JSON 内容,便于客户端直接解析。
  2. 通过 ethers-rs Provider 访问链上数据,价格与换币均依赖 Uniswap V3 Quoter 与 SwapRouter 的真实 ABI。
  3. TokenResolver 维护常用代币元数据,缺失时会回落到链上查询并在运行时补充精度信息。
  4. UniswapFacade 仅使用单跳 500/3000/10000 bps 池子,配合滑点约束模拟 exactInputSingle 交易。

已知限制

  • 当前地址、代币配置仅覆盖以太坊主网;若切换其他链需在 .env 中显式覆盖 Uniswap 路由与 Quoter。
  • 定价逻辑未实现多跳路径搜索,若直连池不存在将返回错误(USD 情况下尝试经 WETH 两跳)。
  • 集成测试依赖真实 RPC 与私钥,默认使用 #[ignore] 需在本地设定环境后使用 cargo test -- --ignored 运行。
  • 服务仅模拟交易(eth_call),不会广播;私钥主要用于构造合法的发送者地址。

测试结果

  1. get_balance
  2. get_token_price
  3. swap_tokens