A Model Context Protocol (MCP) server built in Rust that enables AI agents to query balances and execute token swaps on Ethereum. This server provides three core tools for interacting with Ethereum and Uniswap V2.
- get_balance - Query ETH and ERC20 token balances for any address
- get_token_price - Get real-time token prices in ETH and USD from Uniswap V2 pools
- swap_tokens - Simulate token swaps on Uniswap V2 (does not execute on-chain)
- Rust 1.70 or later
- Ethereum RPC endpoint (Infura, Alchemy, or local node)
- Private key for transaction signing (required for gas estimation)
git clone https://github.com/yourusername/dex-mcp-rs.git
cd dex-mcp-rsCreate a .env file in the project root:
cp .env.example .envEdit .env and add your credentials:
RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY
PRIVATE_KEY=your_private_key_here.env file or share your private key!
cargo build --releasecargo run --releaseThe server will start and listen for MCP requests on stdin/stdout.
Why Alloy? Alloy is a modern, high-performance Ethereum library for Rust with excellent type safety and modular design. It provides better ergonomics and maintenance than the older ethers-rs library.
Why Uniswap V2? V2 has a simpler constant product formula (x * y = k) compared to V3's concentrated liquidity model, making it easier to implement and understand while still providing accurate pricing for most token pairs.
Why on-chain pricing? Deriving prices directly from Uniswap pool reserves eliminates external API dependencies and ensures data is always current and trustless, though it requires more RPC calls.
Why eth_call for simulation? Using eth_call allows us to simulate transactions without executing them on-chain, providing accurate gas estimates and catching potential failures without spending gas or requiring token approvals.
Run the test suite:
cargo testNote: Integration tests are marked with #[ignore] by default as they require a valid RPC connection. To run them:
cargo test -- --ignoredMake sure your .env file is configured before running integration tests.
- Mainnet only: Currently configured for Ethereum mainnet. To use testnets, update the RPC URL and contract addresses.
- Uniswap V2 only: Does not support Uniswap V3 or other DEXes.
- Requires funded wallet: Gas estimation requires a wallet with some ETH balance.
- Price accuracy: Assumes sufficient liquidity in WETH and USDC pairs. Low liquidity pairs may have inaccurate pricing.
- No transaction execution: The swap tool only simulates; it does not execute real swaps.
src/
├── main.rs              # MCP server setup and request routing
├── config.rs            # Configuration management
├── types.rs             # Shared types and request/response structures
├── ethereum/
│   ├── mod.rs           # Ethereum client wrapper
│   └── contracts.rs     # Smart contract ABIs (ERC20, Uniswap V2)
└── tools/
    ├── mod.rs           # Tool module exports
    ├── balance.rs       # get_balance implementation
    ├── price.rs         # get_token_price implementation
    └── swap.rs          # swap_tokens implementation
- Uniswap V2 Router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
- Uniswap V2 Factory: 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
- WETH: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
- USDC: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
The project follows a modular architecture:
- config: Environment and configuration management
- ethereum: Blockchain interaction layer with alloy
- tools: MCP tool implementations (balance, price, swap)
- types: Shared data structures
- Define input/output types in src/types.rs
- Implement the tool logic in src/tools/
- Register the tool in src/main.rsin thecreate_routerfunction
- Add tests in tests/integration_tests.rs
Contributions are welcome! Please ensure:
- Code compiles without warnings: cargo build
- All tests pass: cargo test
- Code is formatted: cargo fmt
- Code passes clippy: cargo clippy
MIT
This is demonstration code for educational purposes. For production use:
- Use a secure key management system (HSM, KMS)
- Implement rate limiting
- Add comprehensive error handling
- Audit all code thoroughly
- Never expose private keys in logs or errors
Built with:
- Alloy - Ethereum library
- MCP-RS - Model Context Protocol SDK
- Tokio - Async runtime
- Uniswap V2 - Decentralized exchange protocol