r/mcp • u/PassionDue4817 • 29m ago
Built TradeGate: open-source MCP server + deterministic pre-trade execution firewall for AI trading agents (paper/sandbox only)
With major brokerages rolling out MCP servers so AI agents can trade, there is a clear safety gap: probabilistic prompts cannot enforce deterministic financial risk controls. LLMs hallucinate position sizes, retry timed-out HTTP calls into duplicate fills, trade on stale balances or market holidays, and get hijacked by indirect prompt injection in external market news.
I built TradeGate — an open-source (MIT), strictly paper/sandbox-only MCP server (tradegate mcp / tradegate-mcp) and deterministic pre-trade execution firewall that sits outside the LLM between Claude Desktop / Cursor / Copilot and a paper broker adapter:
- 17 fail-closed pre-trade gates: paper-only mode lock, symbol allow/block lists, NYSE calendar & holiday checks, 300s snapshot staleness bounds, durable duplicate
client_order_id+ fingerprint window, ±5% limit price collar (catches prompt-injected off-market limit buys), daily order caps, and unpriced holdings valuation locks. - 8 monotone sizing rules + binary-search auto-trim: when an agent tries an oversized buy (e.g. $80K = 80% of equity against a 20% cap), exact binary search trims it to the largest legal lot/share count (49 shares) instead of failing the loop.
- OS single-writer lock + disk-latched kill switch: non-blocking file lock prevents split-brain writers; an unapproved/overfilled broker fill latches
kill_switch.jsonon disk until an operator explicitly releases it with a reasoned audit entry. - Tamper-evident SHA-256
audit.jsonl: every check, trim, block, fill, policy change, and kill-switch event is hash-chained and fsync'd to disk (tradegate verify-auditcatches any 1-byte edit).
Try 8 live attack scenarios in your browser (zero login, isolated SQLite paper sandbox):
https://hkedia08-tradegate.hf.space
GitHub (MIT, 37/37 tests, green CI, architecture doc + copy-paste MCP config):
https://github.com/hkedia08/tradegate
(Both included adapters — LocalPaperBroker on SQLite and AlpacaPaperBroker strictly allowlisted to paper-api.alpaca.markets — refuse live mode fail-closed.)
Curious what risk gates or paper broker adapters MCP builders here would want next — IBKR paper TWS, Coinbase sandbox, or per-agent budget quotas?