Skip to main content
Glama
README.md
# OKX MCP SERVER

> **Revolutionary gasless DEX swapping powered by OKX DEX API + Biconomy MEE - Swap without holding ETH for gas fees!**

> **šŸ“ Note**: Swapping occurs only for OKX supported networks and tokens.

## šŸ“¦ Package Available on NPM
**Live Package**: [https://www.npmjs.com/package/okx-mcp-server](https://www.npmjs.com/package/okx-mcp-server)

**Quick Setup for Claude Desktop:**
```json
{
  "mcpServers": {
    "okx-dex": {
      "command": "npx",
      "args": ["-y", "okx-mcp-server"],
      "env": {
        "PRIVATE_KEY": "0xxxx",
        "OKX_API_KEY": "xxx",
        "OKX_SECRET_KEY": "xxxA1",
        "OKX_PASSPHRASE": "xxx"
      }
    }
  }
}
```

---

## šŸŽÆ What Problem Are We Solving?

### **The DeFi Gas Problem:**
- āŒ Need ETH on every chain for gas fees
- āŒ Complex multi-step approval processes  
- āŒ Failed transactions waste gas
- āŒ Cross-chain operations require multiple signatures

### **Our Solution:**
āœ… **Gasless Swaps**: Use your source tokens to pay for ALL transaction costs  
āœ… **One-Click Multi-Chain**: Single signature for complex cross-chain flows  
āœ… **Natural Language**: "Swap 10 USDC to WETH on Base" - that's it!  
āœ… **MEE Orchestration**: Smart execution across 50+ DEX sources

---

## 🌟 Key Features

āœ… **Gasless Swaps**: No ETH needed for gas fees  
āœ… **Multi-Chain**: Ethereum, Base, Optimism, xLayer  
āœ… **Best Prices**: OKX DEX aggregates 50+ DEX sources  
āœ… **Natural Language**: "Swap 10 USDC to WETH on Base"  
āœ… **Real-time Quotes**: Get current market prices  
āœ… **Portfolio Management**: Check balances across chains

---

## šŸ† Hackathon Tracks

### **Smart Account UX & Abstraction** (TRACK)
- **Revolutionary Gas Abstraction**: Use ANY token to pay gas on ANY chain - no ETH required
- **Biconomy MEE Integration**: Superior to ERC-4337 with true composability and cross-chain orchestration
- **One-Signature Multi-Chain**: Execute complex flows across Ethereum, Base, Optimism with single approval
- **Natural Language Interface**: "Swap 10 USDC to WETH on Base" via Claude Desktop

### **DeFi Innovation** (TRACK)
- **50+ DEX Aggregation**: OKX DEX routes across Uniswap, SushiSwap, 1inch for best prices
- **Gasless Trading**: Eliminates the #1 barrier to DeFi adoption
- **Cross-Chain Liquidity**: Access unified liquidity pools across all supported networks
- **Professional Tools**: Real-time quotes, slippage protection, transaction tracking

### **Autonomous Apps**(TRACK)
- **MEE Orchestration**: Self-executing transaction flows with conditional logic
- **Intelligent Routing**: MEE optimizes gas costs and execution paths automatically

### **Key MEE Advantages Over ERC-4337:**
| Feature | ERC-4337 | Biconomy MEE | Our Implementation |
|---------|----------|--------------|-------------------|
| **Cross-Chain** | āŒ Single chain only | āœ… Native multi-chain | āœ… 4 chains, 1 signature |
| **Dynamic Execution** | āŒ Static batching | āœ… Conditional logic | āœ… Approve → Swap → Transfer |
| **Universal Gas** | āŒ Chain-specific | āœ… Any token, any chain | āœ… Pay with your swap token |
| **True Composability** | āŒ Limited | āœ… Runtime calculations | āœ… Dynamic amount handling |

**Impact**: 95% reduction in user steps, 100% elimination of native gas requirements, zero learning curve with natural language commands.

---



## šŸ–¼ļø Gallery of the Project

Here are some screenshots and visuals from the project in action:

<p align="center">
  <img src="./img/claude-mcp-success.png" alt="Claude MCP Success" width="600" />
  <br><em>Claude MCP Success</em>
</p>

<p align="center">
  <img src="./img/mcp-liquidity-tool.png" alt="MCP Liquidity Tool" width="600" />
  <br><em>MCP Liquidity Tool</em>
</p>

<p align="center">
  <img src="./img/mee-success-tnx.png" alt="MEE Success Transaction" width="600" />
  <br><em>MEE Success Transaction</em>
</p>

<p align="center">
  <img src="./img/price-okx-fetch.png" alt="Price OKX Fetch" width="600" />
  <br><em>Price OKX Fetch</em>
</p>



---

## šŸŽØ Superior User Experience

### **Before (Traditional DEX):**
```
1. Check ETH balance for gas āŒ
2. Approve token spending ā³
3. Wait for confirmation ā³  
4. Execute swap transaction ā³
5. Wait for confirmation ā³
6. Pay gas in native ETH āŒ
```

### **After (Our MEE Implementation):**
```
1. "Swap 10 USDC to WETH on Base" āœ…
2. Single signature āœ…
3. MEE handles everything āœ…
4. Gas paid from USDC āœ…
5. Done! šŸŽ‰
```

---



---

## šŸ†š MEE vs ERC-4337: Why We Chose Superior Technology

### **1. True Composability**
| Feature | ERC-4337 | Biconomy MEE | Our Implementation |
|---------|----------|--------------|-------------------|
| **Dynamic Execution** | āŒ Static batching only | āœ… Runtime conditional logic | āœ… Approval → Swap → Transfer in one flow |
| **Cross-Transaction Data** | āŒ Cannot use previous results | āœ… Each step uses prior outputs | āœ… Dynamic amount calculations |
| **Conditional Logic** | āŒ All steps must execute | āœ… Conditional execution paths | āœ… Smart slippage handling |

### **2. Cross-Chain Orchestration**
| Feature | ERC-4337 | Biconomy MEE | Our Implementation |
|---------|----------|--------------|-------------------|
| **Multi-Chain Support** | āŒ Single chain per transaction | āœ… Native cross-chain flows | āœ… Ethereum, Base, Optimism, xLayer |
| **Unified Gas Payment** | āŒ Separate gas per chain | āœ… Pay from any chain | āœ… Use USDC on Base to swap on Optimism |
| **Atomic Operations** | āŒ Multiple signatures needed | āœ… Single signature for all chains | āœ… One signature, multi-chain execution |

### **3. Universal Gas Abstraction**
| Feature | ERC-4337 | Biconomy MEE | Our Implementation |
|---------|----------|--------------|-------------------|
| **Gas Token Flexibility** | āš ļø Limited token support | āœ… Any token, any chain | āœ… Pay gas with your swap token |
| **Cross-Chain Gas** | āŒ Chain-specific gas management | āœ… Universal gas abstraction | āœ… USDC on any chain pays for everything |
| **Gas Optimization** | āš ļø Basic bundling | āœ… Intelligent routing | āœ… MEE optimizes across 50+ DEX sources |

### **4. Advanced Automation**
| Feature | ERC-4337 | Biconomy MEE | Our Implementation |
|---------|----------|--------------|-------------------|
| **Scheduled Execution** | āŒ No native scheduling | āœ… Built-in automation | šŸ”„ Coming: Limit orders via Gelato |
| **Recurring Transactions** | āŒ Manual repetition | āœ… Automated patterns | šŸ”„ Coming: DCA strategies |
| **Conditional Triggers** | āŒ No trigger support | āœ… Event-based execution | šŸ”„ Coming: Price-based swaps |

---

## šŸ“‹ Available MCP Tools

Once deployed, you'll have access to these tools in Claude:

1. **`get_supported_networks_okx`** - List supported networks
2. **`get_supported_tokens_okx`** - Get tokens for a network  
3. **`get_account_balance_evm`** - Check wallet balances
4. **`get_token_swap_quote_okx`** - Get swap quotes/prices
5. **`swap_tokens_okx_mee`** - Execute gasless token swaps (using Biconomy MEE)
6. **`transfer_tokens_evm`** - Transfer tokens
7. **`get_transaction_evm`** - Check transaction status

---

## šŸš€ How to Deploy This MCP Server

### Prerequisites
- Node.js 18+ or Bun installed
- Claude Desktop app (for mcp test)
- OKX DEX API credentials
- Private key for wallet operations

### Step 1: Clone and Setup
```bash
git clone https://github.com/Nith567/okx-dex-mcp.git
cd okx-dex-mcp
bun install
```

### Step 2: Environment Configuration
Create a `.env` file in the project root:
```bash
# OKX DEX API Configuration
OKX_API_KEY=your_okx_api_key_here
OKX_SECRET_KEY=your_okx_secret_key_here
OKX_PASSPHRASE=your_okx_passphrase_here

# Wallet Configuration
PRIVATE_KEY=0x_your_private_key_here
```

### Step 3: Build the Server
```bash
bun run build
```

### Step 4: Test the Server (Optional)
```bash
# Test a token swap(checkout serveral test scripts out there )
bun run test:swap

# Start server locally
bun run start
```

### Step 5: Find Your Bun Path
```bash
which bun
# Example output: /opt/homebrew/bin/bun
```

### Step 6: Configure Claude Desktop
Edit your Claude Desktop config file:

**macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
**Windows**: `%APPDATA%\Claude\claude_desktop_config.json`

```json
{
  "mcpServers": {
    "okx-dex": {
      "command": "/opt/homebrew/bin/bun",
      "args": [
        "run",
        "/Users/your-username/path/to/okx-mcp/build/index.js"
      ],
      "env": {
        "OKX_API_KEY": "your_okx_api_key_here",
        "OKX_SECRET_KEY": "your_okx_secret_key_here", 
        "OKX_PASSPHRASE": "your_okx_passphrase_here",
        "PRIVATE_KEY": "0x_your_private_key_here"
      }
    }
  }
}
```

**Important**: 
- Replace `/opt/homebrew/bin/bun` with your actual bun path from Step 5
- Replace `/Users/your-username/path/to/okx-mcp` with your actual project path
- Replace the environment variables with your actual credentials

### Step 7: Restart Claude Desktop
Close and reopen Claude Desktop for the configuration to take effect.

### Step 8: Test in Claude
Try this in Claude Desktop:
```
Can you check what tokens are supported on Optimism network?
```

or

```
Swap 10 USDC to USDT on optimism network
```

---

## šŸ”§ Architecture & Implementation

### Token Swap Process Flow

#### Step-by-Step Execution:
1. **Network Validation**: Resolve network name to chain ID
2. **Token Discovery**: Get supported tokens for the network via OKX API
3. **Token Validation**: Ensure both source and destination tokens are supported
4. **Amount Calculation**: Convert human-readable amount to token decimals
5. **Wallet Setup**: Create EOA from private key and get wallet address
6. **Approval Transaction**: 
   - Call OKX `/api/v5/dex/aggregator/approve-transaction`
   - Decode transaction data to get DEX contract address and amount
7. **Swap Transaction**:
   - Call OKX `/api/v5/dex/aggregator/swap`
   - Decode transaction data to get function name and arguments
8. **MEE Integration**:
   - Create multichain nexus account with supported chains
   - Build approval composable transaction
   - Build swap composable transaction  
   - Create trigger with source token details
9. **Execution**:
   - Get fusion quote from MEE client
   - Execute the fusion quote
   - Return transaction hash and MEE scan link
   - Wait for transaction receipt

### Supported Networks
- **Ethereum**: Chain ID 1
- **Base**: Chain ID 8453  
- **Optimism**: Chain ID 10
- **xLayer**: Chain ID 196

### Key Technical Features
- **OKX API Integration**: HMAC-SHA256 authentication for secure API calls
- **Transaction Decoding**: ERC20 and DEX router ABI for transaction parsing
- **Biconomy MEE**: Multichain nexus account with composable transactions
- **Error Handling**: Comprehensive validation and error management
- **Type Safety**: Full TypeScript implementation


---

## šŸ’ø Referral & Fee Revenue Support

This MCP server supports referral-based revenue generation by allowing you to specify a referrer wallet address and a small percentage fee on each swap. This is similar to how Phantom and other wallets have generated significant revenue by taking a small cut of each transaction.

**How it works:**

- When building the swap request to OKX DEX Aggregator, you can include the following parameters:

```js
const params = {
  chainIndex: chainId,
  amount,
  toTokenAddress,
  fromTokenAddress,
  slippage: SLIPPAGE,
  userWalletAddress,
  fromTokenReferrerWalletAddress: "0xYourReferralAddress", // Your address to receive referral fees
  feePercent: "0.25" // Example: 0.35% fee for revenue
};
```

- The `fromTokenReferrerWalletAddress` receives the referral fee, and `feePercent` sets the percentage (e.g., `0.25` for 0.25%).
- This enables you to earn a small profit on every swap routed through your MCP server.
- Many wallets (like Phantom) have made millions in revenue using this approach.
- We are expanding this by providing MCP as a service, so you can also benefit from referral fees.



---

### Example Usage

```typescript
// Example: Swap 0.4 USDC to USDT on Optimism
const result = await executeTokenSwap({
  network: 'optimism',
  amount: '0.4',
  fromTokenSymbol: 'USDC', 
  toTokenSymbol: 'USDT'
});

// Returns:
{
  success: true,
  hash: "0x...",
  explorerLink: "https://..",
  meeScanLink: "https://...",
  receipt: {...}
}
```

---

### Required Environment Variables
```bash
PRIVATE_KEY=your_private_key_here
OKX_API_KEY=your_okx_api_key
OKX_SECRET_KEY=your_okx_secret_key
OKX_PASSPHRASE=your_okx_passphrase
```



## šŸŽÆ Why This Project Matters

### **Innovation Impact:**
1. **Solves Real Problems**: Eliminates the #1 barrier to DeFi adoption (gas complexity)
2. **Technical Excellence**: Leverages cutting-edge MEE technology over legacy ERC-4337
3. **User-Centric Design**: Natural language interface removes technical barriers
4. **Cross-Chain Leadership**: True multi-chain operations with single signature