Binance Agent MCP Server
by eikarna
README.md
# Binance Agent OS โ Institutional MCP Server & Trading Engine
[](https://bun.sh)
[](https://modelcontextprotocol.io/)
[](https://www.typescriptlang.org/)
[](https://github.com/eikarna/binance-agent-mcp)
[](https://opensource.org/licenses/MIT)
> **Official Submission for the Binance Agent OS Mini Hackathon ($60K USDC Prize Pool)**
> Production-grade Model Context Protocol (MCP) server and autonomous trading framework engineered for zero-float precision, cryptographic intent verification (EIP-712), deterministic idempotency, and institutional pre-trade risk policy.
---
## ๐๏ธ The 4 Institutional Safeguard Pillars
Most AI trading agents fail in production due to floating point inaccuracies, duplicate order firing on network disconnects, timestamp desynchronization, and lack of pre-trade risk guardrails. This toolkit solves them natively:
```
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Autonomous AI Agent โ
โ (Claude/Cursor/Hermes) โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ Trade Intent Request
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ HARDENED MCP INTERCEPTOR & AGENT OS โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ 1. Idempotency Engine โโโบ SHA-256 Client ID โ
โ 2. Precision Normalizer โโโบ LOT_SIZE / TICK_SIZEโ
โ 3. Policy Risk Engine โโโบ Cap & Slippage Gate โ
โ 4. Resilience Manager โโโบ Clock Sync & Backoffโ
โโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Cryptographic EIP-712 Proof
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Binance Spot/Futures โ
โ REST & WebSockets โ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
```
1. **Pre-Trade Policy Engine & Circuit Breaker (`src/policy.ts`)**
- Hard Notional USD caps per transaction and aggregate 24-hour drawdown limits.
- Dynamic price slippage collar filters (auto-rejects orders exceeding configurable basis points vs reference price).
- Strict symbol allowlisting to prevent hallucinated asset routing.
2. **Deterministic Idempotency Shield (`src/idempotency.ts`)**
- Deterministic `clientOrderId` generation via SHA-256 hashing of trade intents.
- Replay protection: Network retries and duplicate events return cached state instead of double-filling on the exchange.
3. **Exchange Resilience & Clock Drift Sentinel (`src/resilience.ts`)**
- Proactive calibration with Binance Server Time (`/api/v3/time`) eliminating error `-1021 (Timestamp for this request was 1000ms ahead/behind)`.
- Real-time `X-MBX-USED-WEIGHT-1M` header tracking with adaptive backoff to prevent IP rate-limit bans (HTTP 418 / 429).
4. **Zero-Drift Micro-Precision Normalizer (`src/precision.ts`)**
- String-slicing and BigInt decimal math bypassing JavaScript IEEE-754 floating-point drift (`0.1 + 0.2 = 0.30000000000000004`).
- Exact adherence to Binance exchange filters (`LOT_SIZE`, `PRICE_FILTER`, `MIN_NOTIONAL`).
5. **Cryptographic EIP-712 Intent Signing (`src/eip712.ts`)**
- Typed data domain separation and ECDSA secp256k1 recovery ensuring only authorized agents or human multisig keyholders can broadcast orders.
---
## โก Interactive Live Demo Runner
Run the interactive TUI demo runner to see the 4 pillars execute in real time:
```bash
bun run demo
```
### Live Terminal Snapshot
```
================================================================================
BINANCE AGENT OS โ INSTITUTIONAL RESILIENCE & MCP DEMO RUNNER
Target: Binance Agent OS Mini Hackathon ($60K USDC)
================================================================================
[INIT] Initializing Institutional Agent Engine & Calibrating Clock Offset...
โ Binance Server Time Synced: Offset = 0 ms (Protected against error -1021)
โโโ [SCENARIO 1: Micro-Precision & LOT_SIZE Normalization] โโโโโโโโโโโโโโโโโโโ
โ Raw JavaScript Floating Arithmetic: 0.30000000000000004 (Would trigger LOT_SIZE rejection)
โ Applying PrecisionEngine Normalizer: 0.3 (stepSize: 0.001)
โ Status: PASSED โ Zero Float Drift
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโ [SCENARIO 2: Pre-Trade Policy Engine & Hard Risk Guardrails] โโโโโโโโโโโโโ
โ Testing Trade: BUY 0.05 BTC @ $65,000 (Notional: $3,250.00 | Cap: $100.00)
โ Policy Intercept: BLOCKED -> NOTIONAL_CAP_EXCEEDED (Max $100.00 cap)
โ Testing Trade: BUY 0.001 BTC @ $66,000 (Ref: $65,000 | Diff: 153.8 bps > 50 bps max)
โ Policy Intercept: BLOCKED -> PRICE_COLLAR_BREACH (Exceeds 0.50% max slippage)
โ Testing Trade: BUY 0.001 BTC @ $65,010 (Notional: $65.01 | Safe Bounds)
โ Policy Intercept: APPROVED -> Notional: $65.01
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโ [SCENARIO 3: Deterministic Idempotency & Duplicate Shield] โโโโโโโโโโโโโโโโ
โ Generated Deterministic Order ID: mcp_5da191fa5e5fd27470b6e9c25b10
โ Order 1 Dispatched -> Status: EXECUTED (FILLED)
โ Simulating Network Retry / Duplicate Event...
โ Duplicate Intercept: BLOCKED DUPLICATE ORDER (Safe Replay)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโ [SCENARIO 4: Rate-Limit Sentinel & Backoff Throttling] โโโโโโโโโโโโโโโโโโโโ
โ Simulated Inbound Used Weight: 1050 / 1200 (87.5% - High Load Threshold)
โ Throttling Sentinel: Backing off for 5100ms to prevent IP ban (HTTP 418/429)
โ Status: PASSED โ Proactive Exchange Safety Protected
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
---
## ๐ ๏ธ MCP Tools & Capabilities
| Tool Name | Auth | Description |
|---|---|---|
| `binance_get_ticker` | Public | Real-time 24hr rolling window price, volume, and spread stats. |
| `binance_get_orderbook` | Public | Depth analysis with live bid/ask spread and imbalance ratio. |
| `binance_get_klines` | Public | Candlestick series for technical indicator processing (EMA/RSI/MACD). |
| `binance_detect_arbitrage` | Public | Autonomous multi-pair triangular arbitrage opportunity scanner. |
| `binance_create_order` | Signed | Safe SPOT order execution with policy validation & dry-run toggle. |
| `binance_cancel_order` | Signed | Order cancellation with deterministic ID matching. |
| `binance_get_account` | Signed | Account balances, asset allocation, and free margin inspection. |
---
## ๐ Quickstart
### 1. Prerequisites
- [Bun runtime](https://bun.sh) (`bun >= 1.1.0`)
### 2. Installation
```bash
git clone https://github.com/eikarna/binance-agent-mcp.git
cd binance-agent-mcp
bun install
```
### 3. Environment Setup (.env)
```env
BINANCE_API_KEY="your_api_key_here"
BINANCE_API_SECRET="your_api_secret_here"
BINANCE_USE_TESTNET="true" # Set to false for live production API
BINANCE_DRY_RUN="true" # Simulation mode for testing
```
### 4. Connect to Cursor / Claude Desktop / Hermes Agent
Add the server to your `claude_desktop_config.json` or Cursor MCP settings:
```json
{
"mcpServers": {
"binance-agent-os": {
"command": "bun",
"args": ["run", "C:/path/to/binance-agent-mcp/src/index.ts"],
"env": {
"BINANCE_API_KEY": "your_api_key",
"BINANCE_API_SECRET": "your_api_secret",
"BINANCE_DRY_RUN": "true"
}
}
}
}
```
---
## ๐งช Testing & Verification
```bash
# Run unit & security test suite
bun test
# Strict TypeScript type checking
bun run typecheck
# Run interactive TUI demo
bun run demo
```
---
## ๐๏ธ Full MCP Specification Compliance
Unlike tool-only wrappers, this server implements all 3 core primitives of the Model Context Protocol:
1. **Tools**: Hardened execution functions guarded by pre-trade risk policies and float-drift protection.
2. **Resources (`binance://market/{symbol}`, `binance://risk/parameters`)**: Dynamic contextual feeds exposing real-time order books, spreads, and pre-trade risk thresholds directly into the LLM context.
3. **Prompts (`analyze_orderbook_imbalance`, `execute_guarded_trade`)**: Standardized system workflows guiding autonomous agents to inspect order book depth and evaluate risk boundaries before submitting orders.
---
## ๐ License
MIT ยฉ Nix Seymour
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo issues