Pre-flight validator for Binance Agent OS MCP server
Blocks agent-proposed trades violating user-defined risk rules before they reach Binance's own confirmation step. Two independent checks, not one replacing the other.
Binance Agent OS Mini Hackathon — Track A: Trading Workflows
TradeGuard intercepts trade proposals from AI agents using Binance's Agent OS MCP server and validates them against deterministic, config-driven risk rules:
- MaxLeverageRule: blocks futures trades exceeding 5x leverage (configurable)
- MaxOrderSizeRule: blocks orders exceeding $1,000 USDT notional (configurable)
- PriceDeviationRule: blocks trades with prices >2% off live market — catches stale/hallucinated prices
- SymbolWhitelistRule: blocks trades on symbols outside the allowed list (BTCUSDT, ETHUSDT, BNBUSDT by default)
If any rule fails, the trade is blocked before reaching Binance's confirm-before-execute prompt. If all rules pass, the trade proceeds to Binance's own human confirmation step normally.
- No external-address risk screening: Not applicable — Binance Agent OS has no withdrawal scope by design
- No strategy generation: TradeGuard validates trades, doesn't propose them
- No backtesting: Live validation only
- No multi-exchange support: Binance Agent OS only
Leverage and size caps check the agent's intent. This rule checks whether the agent's view of the market is still true.
If the agent proposes a limit order at $95,000 — from context cached twenty minutes ago, a stale web result, or a hallucination — while the live market is at $68,000, the order is denied before it reaches Binance's confirmation step. The rule fetches the current price at validation time and compares; it does not trust the price in the payload.
The 2% default sits above measured intraday true range for all three whitelisted symbols (1-minute p99 under 0.3%; worst 15-minute candle 1.78% on ETHUSDT). See ARCHITECTURE.md for the measurements and the caveat about volatile sessions.
TradeGuard works with any MCP-compatible agent — not just Claude Code. Pick the mode that fits your setup:
TradeGuard runs as a standalone MCP server that proxies Binance's MCP endpoint. Works with any agent: Claude Code, OpenClaw, custom agents, or any MCP client.
Flow:
Agent → TradeGuard MCP Server (validates) → Binance MCP (executes)
Why this mode:
- ✅ Agent-agnostic — works with Claude Code, OpenClaw, custom agents
- ✅ Deterministic enforcement — agent cannot bypass via reasoning
- ✅ Two independent checks: TradeGuard validates → Binance's confirm-before-execute prompt
- ✅ Standard MCP protocol — no custom integrations needed
TradeGuard runs as a PreToolUse hook inside Claude Code's harness.
Flow:
Claude Code → PreToolUse hook (validates) → Binance MCP (executes)
Why this mode:
- ✅ Zero-config deployment (just copy hook JSON)
- ✅ No bearer token management required
- ❌ Claude Code only — doesn't work with OpenClaw or other agents
- Node.js 22+
- Binance account with Agentic sub-account
- Mode 1 (any agent): Bearer token from Binance OAuth
- Mode 2 (hook): Claude Code CLI
git clone https://github.com/devIykee/tradeguard.git
cd tradeguard
npm install
npm run buildConnect to Binance Agent OS from any agent or browser:
https://agent.binance.com/mcp/agentic
Complete the OAuth flow. Then open browser dev tools → Network tab, find a request to agent.binance.com, and copy the Authorization: Bearer <token> header value.
BINANCE_TOKEN=your_bearer_token node bin/start-server.jsLeave this running in a terminal. TradeGuard will log "Ready to validate Binance trades".
Claude Code:
claude mcp add tradeguard stdio node /absolute/path/to/tradeguard/bin/start-server.jsSet BINANCE_TOKEN in your environment before starting Claude Code:
export BINANCE_TOKEN=your_bearer_token
claudeOpenClaw (Telegram): In OpenClaw's MCP server settings, add a custom stdio server:
{
"command": "node",
"args": ["/absolute/path/to/tradeguard/bin/start-server.js"],
"env": { "BINANCE_TOKEN": "your_bearer_token" }
}Any other MCP-compatible agent:
{
"mcpServers": {
"tradeguard": {
"command": "node",
"args": ["/absolute/path/to/tradeguard/bin/start-server.js"],
"env": { "BINANCE_TOKEN": "your_bearer_token" }
}
}
}TradeGuard exposes the same tools as the Binance MCP server — your agent doesn't change how it calls tools. TradeGuard validates, then forwards passing calls to Binance automatically.
claude mcp add binance-mcp-server --transport http https://agent.binance.com/mcp/agenticFollow the browser OAuth flow. Grant scopes: Market data + Account + Trade.
Hooks are registered in settings.json — either ~/.claude/settings.json (all projects) or .claude/settings.json (one project). Add the hooks block, keeping any existing keys in the file:
{
"hooks": {
"PreToolUse": [
{
"matcher": "mcp__binance-mcp-server__.*",
"hooks": [
{
"type": "command",
"command": "/absolute/path/to/tradeguard/bin/validate-trade.js"
}
]
}
]
}
}The nested hooks array inside the matcher is required — a flat { matcher, command } object is silently ignored.
Using multiple Claude Code profiles? Each CLAUDE_CONFIG_DIR is a separate config root, so register the hook in every profile you use (e.g. ~/.claude-b/settings.json, ~/.claude-c/settings.json).
The agent cannot move funds into its own sub-account — that transfer is always manual, via the Binance web UI:
https://www.binance.com/en/my/sub-account/asset-management/transfer?asset=BTC
Funding is only needed for orders that actually execute. Rule denials happen before the order reaches Binance, so they work on an empty sub-account.
Edit config/risk-rules.json — thresholds live here, never in code:
{
"maxLeverage": 5,
"maxOrderSizeUSDT": 1000,
"maxPriceDeviationPct": 2.0,
"allowedSymbols": ["BTCUSDT", "ETHUSDT", "BNBUSDT"]
}Rebuild after changes: npm run build
Run the test suite:
npm test61 tests passing:
- Contract test suite (Liskov Substitution Principle enforced)
- Per-rule unit tests (all rules tested against fakes, zero network calls)
- Integration tests (validation flow end-to-end)
tradeguard/
├── src/
│ ├── rules/ # RuleEngine + 4 rules (pure logic, zero I/O)
│ ├── interfaces/ # Dependency inversion (MarketDataSource, etc.)
│ ├── binance/ # BinanceMcpHttpClient (implements interfaces)
│ ├── server/ # TradeGuardServer (MCP proxy mode)
│ └── config/ # risk-rules-loader (Zod validation)
├── bin/
│ ├── validate-trade.js # Hook entry point (Mode 2)
│ └── start-server.js # MCP server entry point (Mode 1)
├── tests/unit/ # 61 tests, contract suite + per-rule tests
├── config/
│ ├── risk-rules.json # User-editable thresholds
│ └── risk-rules.schema.json
├── docs/architecture/ # ARCHITECTURE.md, API.md, DEVELOPMENT.md
├── CONTRIBUTING.md
└── README.md
The PreToolUse hook is registered in your own settings.json — see
Mode 2 Setup for the block to add.
TradeGuard follows SOLID principles strictly:
- Single Responsibility:
RuleEngineevaluates rules only.BinanceMcpHttpClienttranslates MCP calls only. - Open/Closed: Adding a new rule means adding one file, zero edits to
RuleEngine. - Liskov Substitution: Any
TradeRuleis fully swappable (contract test suite proves this). - Interface Segregation:
MarketDataSource,AccountReader,TradeExecutorare separate interfaces. - Dependency Inversion: Rules depend on interfaces, never concrete implementations.
See ARCHITECTURE.md for full breakdown.
| File | Description |
|---|---|
| README.md | Installation and setup (this file) |
| CONTRIBUTING.md | Contribution guidelines, PR process |
| docs/architecture/ARCHITECTURE.md | Full design, SOLID principles, data flow |
| docs/architecture/API.md | Complete API reference with examples |
| docs/architecture/DEVELOPMENT.md | Dev guide, troubleshooting, rule checklist |
- Spot orders only, under the scopes Binance currently grants. Futures order placement and
changeInitialLeverageare not exposed by the Binance MCP server formcp:spot:trade/mcp:account:read/mcp:margin:loan/mcp:wallet:transfer/mcp:master:read.MaxLeverageRulegates any order payload carrying a leverage field, but no such payload can be produced on an account without a futures trade scope. See ARCHITECTURE.md for the verified tool surface. - Bearer token management. MCP proxy mode takes the token via
BINANCE_TOKENand does not refresh it. Tokens expire; re-authenticate and restart the server. - No velocity or drawdown rule. Rate limiting across trades and cumulative-loss circuit breaking are not implemented.
MIT — see LICENSE file.
https://github.com/devIykee/tradeguard
Built for the Binance Agent OS Mini Hackathon (Track A — Trading Workflows).