Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 5 min read

I gave Claude a trading desk: building an MCP server that backtests and runs crypto bots

Most "AI trading" demos stop at a chatbot that tells you what it thinks Bitcoin will do. I wanted something more useful: an assistant that can take an idea, test it against real market history, and, only when I say so, r

Most "AI trading" demos stop at a chatbot that tells you what it thinks Bitcoin will do. I wanted something more useful: an assistant that can take an idea, test it against real market history, and, only when I say so, run it as a bot on my own exchange account.

So we built an MCP server for TradeStaq, our algo trading platform. It's open source (MIT), on npm as @the-staq/tradestaq-mcp, and it gives any MCP client 46 tools for strategies, backtests, bots, positions and copy trading.

This post covers what it does, how to set it up in two minutes, and the design decisions that mattered most when an AI agent can touch real money.

What it looks like

You talk to your assistant normally. The server maps the request to a tool:

"Show me my portfolio"                          β†’ get_portfolio
"Generate a momentum strategy for ETH"          β†’ generate_strategy
"Backtest it on BTC/USDT for the last 3 months" β†’ what_if_backtest
"Explain the risk in plain English"             β†’ explain_strategy
"Deploy it on paper first"                      β†’ deploy_bot
"Why didn't my bot trade last night?"           β†’ get_bot_logs

That last one turned out to be one of the most used. get_bot_logs returns a run-by-run history of why a bot did or did not trade: the signal it saw, the filter that blocked it, or the error it hit. Asking "why" in plain English beats scrolling through logs.

Setup

Remote (no install), for clients that support HTTP transport:

{
  "mcpServers": {
    "tradestaq": { "url": "https://mcp.tradestaq.com/mcp" }
  }
}

Local with npx (Claude Desktop, Cursor and others):

{
  "mcpServers": {
    "tradestaq": {
      "command": "npx",
      "args": ["-y", "@the-staq/tradestaq-mcp"]
    }
  }
}

Claude Code:

claude mcp add tradestaq -- npx -y @the-staq/tradestaq-mcp

Then ask the assistant to "Authenticate with TradeStaq". It opens a browser window for an OAuth (PKCE) login, so your password never goes through the chat. The token is stored locally with 0600 permissions.

The tools, grouped

  • Market data: get_price, get_candles, search_markets, list_exchanges
  • Strategies: list_strategies, get_strategy, explain_strategy, compare_strategies, create_strategy, generate_strategy
  • Backtesting: what_if_backtest, get_backtest_results
  • Bots: deploy_bot, update_bot, start_bot, stop_bot, delete_bot, get_bot_status, get_bot_logs, close_position
  • Results: get_trade_history, get_performance_metrics, plus public chart links for any bot (create_bot_share_link)
  • Copy trading and advice: list_top_traders, follow_trader, suggest_strategies, get_market_context

It also ships three prompt templates (Trading Assistant, Strategy Builder, Portfolio Reviewer) and three browsable resources (tradestaq://portfolio, tradestaq://bots, tradestaq://strategies).

Exchanges include Binance, Bybit, OKX, Bitget, Hyperliquid and dYdX. Your exchange keys stay connected to your own account; TradeStaq never holds your funds.

Design decisions that mattered

1. Paper by default, live on purpose

deploy_bot creates a paper bot unless you pass live: true. An assistant that misreads "deploy it" should cost you nothing. Going live is an explicit choice, and changing a live bot needs a separate permission scope (mcp:live) from changing a paper one (mcp:paper). A token that can only manage paper bots can't touch real money, whatever the model decides to do.

2. Settings go in the same call that creates the bot

Early on, an agent would deploy a bot and then call update_bot to set its preset or parameters. The problem: a bot starts trading as soon as it exists, so for a few seconds it ran with default settings. Now deploy_bot takes presetId, parameterOverrides and executionMode in the create call itself, and the server applies them before the first trade. If you want to set things up in steps, start: false creates the bot paused.

3. Partial updates never wipe settings

update_bot merges what you pass onto the bot's current config. Position size and leverage change only when you send them. Models are bad at round-tripping a full config object, so the API shouldn't punish them for sending just the field they mean to change.

4. Some changes need the bot to be flat

Switching a bot's strategy or timeframe while it holds a position is a good way to orphan that position. The server refuses the switch while the bot is in a trade. The safe sequence is: pause with stopAfterClose: true, wait until get_bot_status shows no open position, switch, then start_bot. Spelling that out in the tool description means the model follows it without being told.

5. Long jobs are async

A backtest over months of candles takes 30 to 120 seconds. what_if_backtest returns a job, and get_backtest_results polls it. That keeps every tool call short, which matters for clients with tight timeouts.

6. Errors the model can act on

Every error comes back in the same shape:

{
  "error": {
    "code": "RATE_LIMITED",
    "message": "Human-readable description",
    "retryable": true,
    "retryAfterMs": 5000
  }
}

retryable and retryAfterMs let the assistant decide whether to wait and retry or tell you something is wrong, instead of guessing from an error string.

7. One sign-in, many processes

We run the server interactively and from headless claude -p jobs at the same time, all sharing one config file. Each copy used to cache the token and refresh it on its own, so one copy rotating the token would sign the others out. Now a refresh runs under a lockfile next to the config and re-reads the file inside it: if another copy already rotated the pair, this one just uses the new tokens.

Architecture

Claude Desktop / Cursor / CLI  ──stdio──►  tradestaq-mcp  ──HTTPS + JWT──►  TradeStaq API
Any web MCP client ──HTTP+SSE──►  (46 tools, 3 prompts, 3 resources)

Two transports: stdio for local use (the default) and HTTP+SSE for remote clients, hosted at mcp.tradestaq.com. It's TypeScript, tested with Vitest, and published to npm through trusted publishing with provenance, so there's no npm token in CI. Each release also updates the listing on the official MCP Registry.

Try it

There's a free plan, and every strategy can be backtested and paper traded before any real money is involved. If you build something on top of it, or find a tool description that confuses your model, open an issue. I'd like to hear about it.

Trading involves risk. Backtests show how a strategy behaved on past data; they don't promise future results.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.