rust-ethereum-mcp-server

altbee/rust-ethereum-mcp-server

3.2

If you are the rightful owner of rust-ethereum-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.

This repository provides a minimal Model Context Protocol (MCP)-style server written in Rust for Ethereum trading operations.

Tools
3
Resources
0
Prompts
0

Ethereum Trading MCP Server (Rust) - Solution

Summary

This repository provides a minimal Model Context Protocol (MCP)-style server written in Rust. It exposes three tools via JSON-RPC POST:

  • get_balance: Query ETH or ERC20 balances.
  • get_token_price: Query a token price (via Uniswap V2 simulation; see notes).
  • swap_tokens: Construct and simulate a Uniswap V2 swap using getAmountsOut + estimateGas (does not send transaction).

The server uses:

  • ethers-rs for Ethereum RPC interaction.
  • warp to accept JSON-RPC HTTP POSTs.
  • tokio runtime for async operations.
  • tracing for structured logging.

Setup Instructions

  1. Clone the repository and cd into it.

  2. Copy .env.example to .env and set values:

cp .env.example .env
# Edit .env:
# - ETH_RPC_URL (Infura/Alchemy or other Ethereum RPC endpoint)
# - Optionally set WALLET_PRIVATE_KEY for swap simulations
  1. Build and run tests:
cargo build
cargo test
# To see debug logs during tests:
cargo test -- --nocapture
  1. Run the MCP server:
cargo run

The server will start on 127.0.0.1:3030 by default.


Example MCP Tool Calls

Get Balance

Request:

{
  "jsonrpc": "2.0",
  "method": "get_balance",
  "params": {
    "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e", 
    "token": "0x618e75ac90b12c6049ba3b27f5d5f8651b0037f6"
  },
  "id": 1
}

Response:

{
  "jsonrpc": "2.0",
  "result": {
    "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
    "balance": "32258568.67038",
    "decimals": 6,
    "raw": "32258568670380",
    "token": "0x618e75ac90b12c6049ba3b27f5d5f8651b0037f6"
  },
  "id": 1
}

Simulate Swap

Request:

{
  "jsonrpc": "2.0",
  "method": "swap_tokens",
  "params": {
    "from_token": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
    "to_token": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
    "amount": "1.0",
    "slippage": 0.01
  },
  "id": 1
}

Response:

{
  "jsonrpc": "2.0",
  "result": {
    "from_token": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
    "to_token": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
    "amount_in": "1.0",
    "estimated_amount_out": "1875.123456",
    "estimated_gas": 120000,
    "gas_price_gwei": "33.000000",
    "note": "This is a simulation using getAmountsOut + estimate_gas; transaction not sent on-chain"
  },
  "id": 1
}

Get Token Price

Request:

{
  "jsonrpc": "2.0",
  "method": "get_token_price",
  "params": {
    "token": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",   // WETH
    // or "symbol": "WETH"
    "in": "USD" // or "ETH"
  },
  "id": 2
}

Response:

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": "3056.51248"
}

Design Decisions

  • Wallet Integration: Uses a LocalWallet from the environment to simulate swaps with real from addresses.
  • Decimal Handling: Uses rust_decimal to avoid floating point rounding errors converting between wei and token units.
  • Flexible Denominators: get_token_price supports USD-stablecoins (USDT/USDC) or ETH (WETH) as denominators.
  • Simulation-First Approach: All swaps are simulated first using getAmountsOut + estimateGas to reduce risk before sending on-chain.
  • Composable Structure: UniswapService can be reused for price queries, swap simulations, or real on-chain swaps.

Known Limitations / Assumptions

  • Only supports Uniswap V2; V3 or other DEXes are not supported.
  • When using a symbol instead of a token address, the service only checks a local mapping (symbol → token address). It does not query on-chain token lists.
  • Assumes token contracts return correct decimals.
  • Gas estimation may be inaccurate under low liquidity or congested RPC nodes.
  • Private key is required for sending real transactions; wallet management is external.
  • Slippage is applied as a simple multiplier; multi-hop swaps are not yet supported.
  • Currently supports price denominators: "USD" (via USDT/USDC) and "ETH" (via WETH) only.