以太坊交易MCP服务器

一个基于Rust构建的模型上下文协议(MCP)服务器,使AI代理能够查询区块链数据并模拟以太坊上的代币交换。

  • by zhenlohuang
  • Rust
  • 本地部署
  • 更新于
在 GitHub 查看 zhenlohuang/ethereum-trading-mcp

使用说明

以太坊交易MCP服务器

Rust内置的模型上下文协议(MCP)服务器,使AI代理能够查询区块链数据并模拟以太坊上的代币交换。

特性

  • get_balance -查询任何钱包地址的ETH和ERC20代币余额
  • get_token_price -从链上来源(Chainlink、Uniswap)获取美元或以太坊的当前代币价格
  • swap_tokens -使用代币符号(WETH、ETH、USDC、USDT、DAI、WBTC、LINK、UNI)模拟Uniswap V2/V3掉期

先决条件

  • Rust 1.90+(通过安装 生锈)
  • 以太坊RPC端点(例如。, 炼金术, 英富拉,或公共端点)
  • 用于交换模拟的钱包私钥

设置

1.克隆存储库

git clone https://github.com/yourusername/ethereum-trading-mcp.git
cd ethereum-trading-mcp

2.设置环境变量

创建一个 .env 项目根目录中的文件:

# Required
export ETHEREUM_RPC_URL="https://mainnet.infura.io/v3/YOUR_API_KEY"
export ETHEREUM_PRIVATE_KEY="0x..."

# Optional
LOG_LEVEL=info

⚠️ 安全说明:永远不要提交你的私钥。密钥仅在本地用于事务签名和模拟。

3.建设项目

cargo build --release

4.运行服务器

./target/release/ethereum-trading-mcp

服务器通过stdio进行通信,并遵循MCP协议。

测试

1.运行单元测试

# Run unit tests
cargo test --lib

# Or using make
make test

2.运行集成测试

集成测试需要访问以太坊主网的网络。确保你有一个 .env 配置了有效凭据的文件:

# .env file
ETHEREUM_RPC_URL="https://mainnet.infura.io/v3/YOUR_API_KEY"
ETHEREUM_PRIVATE_KEY="0x..."

然后运行集成测试:

# Run integration tests
cargo test --test integration_tests -- --ignored

# Or using make
make test-integration

# Run all tests (unit + integration)
make test-all

集成测试涵盖了所有三个MCP工具:

  • get_balance:ETH和ERC20余额查询,错误处理
  • get_token_price:来自Chainlink和Uniswap的价格查询,各种代币
  • swap_tomates:交换模拟、滑动处理、错误案例

3.测试覆盖率

使用生成测试覆盖率报告 cargo-llvm-cov:

# Install cargo-llvm-cov (one-time setup)
cargo install cargo-llvm-cov

# Generate text coverage report
make coverage

# Generate HTML coverage report
make coverage-html

# Generate LCOV report (for CI/Codecov)
make coverage-lcov

HTML报告将在 target/llvm-cov/html/index.html.

4.通过MCP检查员进行检查

npx @modelcontextprotocol/inspector --config mcp.dev.json \
  -e ETHEREUM_RPC_URL="https://mainnet.infura.io/v3/YOUR_API_KEY" \
  -e ETHEREUM_PRIVATE_KEY="0x..."

项目结构

src/
├── main.rs                 # Application entry point
├── lib.rs                  # Library root, re-exports
├── config/
│   └── mod.rs              # Configuration management
├── error/
│   └── mod.rs              # Error types and handling
├── mcp/
│   ├── mod.rs              # MCP module root
│   └── server.rs           # MCP server implementation
├── ethereum/
│   ├── mod.rs              # Ethereum module root
│   ├── client.rs           # Ethereum RPC client
│   ├── constants.rs        # Ethereum constants (addresses, chain IDs)
│   ├── wallet.rs           # Wallet management
│   └── contracts/
│       ├── mod.rs          # Contract module root
│       ├── erc20.rs        # ERC20 ABI
│       ├── uniswap_v2.rs   # Uniswap V2 contracts
│       ├── uniswap_v3.rs   # Uniswap V3 contracts
│       └── chainlink.rs    # Chainlink price feeds
├── services/
│   ├── mod.rs              # Services module root
│   ├── balance.rs          # Balance query logic
│   ├── price.rs            # Price fetching logic
│   ├── swap.rs             # Swap simulation logic
│   └── token_registry.rs   # Token registry and metadata
└── types/
    ├── mod.rs              # Types module root
    ├── token.rs            # Token-related types
    └── swap.rs             # Swap-related types

tests/
├── common/
│   └── mod.rs              # Shared test utilities
├── test_get_balance.rs     # Balance query integration tests
├── test_get_token_price.rs # Price query integration tests
├── test_server.rs          # MCP server integration tests
└── test_swap_tokens.rs     # Swap simulation integration tests

api参考

看 api参考 有关可用工具的详细文档。

截图

get_balance

get_token_price

swap_tomates

集成

已知限制

  • 仅限主网:目前仅支持以太坊主网(链ID 1)
  • 直接互换:多跳路由仅限于WETH作为中间令牌
  • 价格影响:简化了价格影响计算;真正的DEX聚合器使用更复杂的方法
  • 无缓存:每次都会获取新的令牌元数据和价格
  • 单DEX:仅支持Uniswap V2/V3;无聚合器集成

许可证

麻省理工学院