Skip to main content
Glama
Trading-MCP-Server

Trading-MCP-Server

⚑ Trading-MCP-Server: Institutional Model Context Protocol Server

CI & Code Quality License: MIT Python 3.10+ FastMCP Docker Ready

Institutional Model Context Protocol (FastMCP 2.x) trading server connecting Claude Desktop, Cursor, and LLM coding assistants to multi-broker execution backends (IBKR, Binance, Alpaca). Exposes portfolio analytics, order validation, quantitative Kelly sizing, and streaming market telemetry.


πŸ›οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     LLM Clients (Claude Desktop, Cursor, Agent Swarm)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚ Standard I/O (JSON-RPC)
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               Trading-MCP-Server Core                  β”‚
β”‚                                                        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   MCP Tools      β”‚            β”‚   MCP Resources  β”‚  β”‚
β”‚  β”‚  β€’ get_quote     β”‚            β”‚  β€’ portfolio://  β”‚  β”‚
β”‚  β”‚  β€’ get_portfolio β”‚            β”‚  β€’ risk-limits   β”‚  β”‚
β”‚  β”‚  β€’ validate_risk β”‚            β”‚  β€’ health        β”‚  β”‚
β”‚  β”‚  β€’ compute_size  β”‚            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                  β”‚
β”‚           β”‚                                            β”‚
β”‚           β–Ό                                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚     Pre-Trade Risk Engine & Quantitative Math    β”‚  β”‚
β”‚  β”‚  β€’ Fat-Finger Guard ($50k)   β€’ Kelly Criterion   β”‚  β”‚
β”‚  β”‚  β€’ Max Leverage Cap (3x)     β€’ 30D HV & ATR(14)  β”‚  β”‚
β”‚  β”‚  β€’ Single-Asset Cap (25%)    β€’ 1D Parametric VaR β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β–Ό                  β–Ό                  β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Interactive     β”‚β”‚ Binance         β”‚β”‚ Alpaca Markets  β”‚
β”‚ Brokers (TWS)   β”‚β”‚ (Spot/Futures)  β”‚β”‚ (Paper/Live)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Related MCP server: ai-trader

πŸš€ Key Features

  • Universal Model Context Protocol Support: Implements native MCP tools and resources compliant with Anthropic Claude Desktop, Cursor AI, Cline, and custom agent swarms.

  • Pre-Trade Risk Guardian: Automatically intercepts and inspects every order before transmission:

    • Fat-finger limits (default: $50,000 max single order).

    • Buying power verification and margin utilization guards.

    • Single-asset portfolio concentration cap (25% default).

    • Circuit breaker lockdown upon daily drawdown threshold breach.

  • Quantitative Sizing Engine: Volatility-adjusted position sizing using the Kelly Criterion, Historical Annualized Volatility (HV30), and Average True Range (ATR14).

  • Multi-Broker Adapters:

    • Interactive Brokers: TWS / Gateway socket and Client Portal API.

    • Binance: Real-time crypto quotes and Futures margin tracking.

    • Alpaca Markets: US Equities and commission-free paper/live execution.


πŸ“‹ Available MCP Tools & Resources

MCP Tools (Callable by Claude & Cursor)

Tool Name

Parameters

Description

get_quote

symbol, broker

Returns real-time bid, ask, last price, spread, and 24h change.

get_portfolio

broker

Returns Net Liquidation Value, cash, margin, and open positions.

validate_risk

symbol, action, quantity, price, broker

Validates proposed trade against hard pre-trade risk gates.

compute_position_size

symbol, equity, risk_pct, entry, stop_loss

Computes optimal share count and capital allocation.

compute_volatility

symbol

Computes 30-day volatility, ATR14, and daily expected move.

MCP Resources

  • trading://health: Health status and latency telemetry across all broker connections.

  • trading://risk-limits: Current institutional parameters and circuit breaker status.


πŸ’» Claude Desktop & Cursor Integration

1. Claude Desktop Setup

Add the server configuration to your claude_desktop_config.json:

{
  "mcpServers": {
    "Trading-MCP-Server": {
      "command": "python",
      "args": [
        "-m",
        "src.server"
      ],
      "env": {
        "MCP_TRANSPORT": "stdio",
        "IBKR_HOST": "127.0.0.1",
        "IBKR_PORT": "7497",
        "MAX_ORDER_NOTIONAL_USD": "50000.0"
      }
    }
  }
}

2. Cursor AI Setup

Add to your project's .cursor/mcp.json:

{
  "mcp": {
    "servers": {
      "Trading-MCP-Server": {
        "command": "python",
        "args": ["main.py"],
        "transport": "stdio"
      }
    }
  }
}

⚑ Quickstart & Interactive Demonstration

1. Installation

git clone https://github.com/jahanzaib-codes/Trading-MCP-Server.git
cd Trading-MCP-Server
pip install -r requirements.txt
cp .env.example .env

2. Interactive Terminal Demo

Run the automated end-to-end institutional test suite:

python cli.py demo
================================================================================
   ⚑ TRADING-MCP-SERVER: INSTITUTIONAL DEMONSTRATION SUITE
================================================================================

[1/5] Broker Connector Telemetry:
| Broker               | Status    | Endpoint                 |
|----------------------|-----------|--------------------------|
| Interactive Brokers  | CONNECTED | 127.0.0.1:7497           |
| Binance Spot/Futures | CONNECTED | api.binance.com          |
| Alpaca Markets       | CONNECTED | paper-api.alpaca.markets |

[2/5] Real-Time Multi-Asset Market Data Quotes:
| Symbol   | Asset Class   | Bid        | Ask        | Last       | Spread          | 24h Chg   |
|----------|---------------|------------|------------|------------|-----------------|-----------|
| NVDA     | EQUITY        | $142.58    | $142.62    | $142.60    | $0.04 (0.028%)  | +1.24%    |
| SPY      | EQUITY        | $585.31    | $585.49    | $585.40    | $0.18 (0.031%)  | +1.24%    |
| BTCUSDT  | CRYPTO        | $88,445.57 | $88,454.43 | $88,450.00 | $8.85 (0.01%)   | +3.85%    |
| SOLUSDT  | CRYPTO        | $214.19    | $214.21    | $214.20    | $0.02 (0.0093%) | +3.85%    |

3. Individual CLI Commands

# Fetch live quote for NVDA
python cli.py quote NVDA --broker ibkr

# Inspect Binance portfolio
python cli.py portfolio --broker binance

# Calculate volatility-adjusted position size
python cli.py size --symbol NVDA --equity 100000 --risk 1.5 --entry 142.60 --stop 135.00

🐳 Docker Deployment

Run as an isolated container:

docker compose up -d --build

πŸ‘¨β€πŸ’» Author & Institutional Consulting

Developed by Jahanzaib β€” Quantitative Finance, Algorithmic Trading Systems, and AI Agent Architect.

For private institutional deployments, custom broker integrations, or proprietary risk modeling, reach out via Fiverr or Telegram.


πŸ“„ License

This project is licensed under the MIT License β€” see the LICENSE file for details.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      Connects your TastyTrade trading account to AI assistants like Claude Desktop and ChatGPT for conversational trading and portfolio management.
      5
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Enables AI assistants like Claude to run backtests, fetch market data, list strategies, and analyze trading algorithms via natural language.
      1,141
      GPL 3.0
    • A
      license
      Not graded
      quality
      D
      maintenance
      Connects a brokerage account to AI assistants, enabling portfolio queries, order management, and market data access through natural language.
      MIT