Enables AI to perform stock/options trading, portfolio management, and real-time market data access through Alpaca Trading API
This is a Model Context Protocol (MCP) server implementation for Alpaca's Trading API. It enables large language models (LLMs) on Claude Desktop, Cursor, or VScode to interact with Alpaca's trading infrastructure using natural language. This server supports stock trading, options trading, portfolio management, watchlist handling, and real-time market data access.
Clone the repository and move to the repository:
git clone https://github.com/YOUR_USERNAME/alpaca-mcp-server.git
cd alpaca-mcp-server
Install the required packages:
pip install mcp alpaca-py python-dotenv
.env
file for your credentials in the project directoryALPACA_API_KEY = "your_alpaca_api_key_for_paper_account"
ALPACA_SECRET_KEY = "your_alpaca_secret_key_for_paper_account"
Open a terminal and run the command below from the project directory:
python alpaca_mcp_server.py
Or use a module invocation:
python -m alpaca_mcp_server
Settings → Developer → Edit Config
claude_desktop_config.json
:{
"mcpServers": {
"alpaca": {
"command": "python",
"args": [
"/path/to/alpaca_mcp_server.py"
],
"env": {
"ALPACA_API_KEY": "your_alpaca_api_key_for_paper_account",
"ALPACA_SECRET_KEY": "your_alpaca_secret_key_for_paper_account"
}
}
}
}
This MCP server connects to Alpaca's paper trading API by default for safe testing. To enable live trading with real funds, update the following configuration files:
Claude for Desktop Configuration
In claude_desktop_config.json
, provide your keys for your live account as environment variables:
{
"mcpServers": {
"alpaca": {
"command": "python",
"args": [
"/path/to/alpaca_mcp_server.py"
],
"env": {
"ALPACA_API_KEY": "your_alpaca_api_key_for_live_account",
"ALPACA_SECRET_KEY": "your_alpaca_secret_key_for_live_account"
}
}
}
}
.env
in the project directory
ALPACA_API_KEY = "your_alpaca_api_key_for_live_account"
ALPACA_SECRET_KEY = "your_alpaca_secret_key_for_live_account"
PAPER = False
get_account_info()
– View balance, margin, and account statusget_positions()
– List all held assetsget_open_position(symbol)
– Detailed info on a specific positionclose_position(symbol, qty|percentage)
– Close part or all of a positionclose_all_positions(cancel_orders)
– Liquidate entire portfolioget_stock_quote(symbol)
– Real-time bid/ask quoteget_stock_bars(symbol, start_date, end_date)
– OHLCV historical barsget_stock_latest_trade(symbol)
– Latest market trade priceget_stock_latest_bar(symbol)
– Most recent OHLC barget_stock_trades(symbol, start_time, end_time)
– Trade-level historyget_orders(status, limit)
– Retrieve all or filtered ordersplace_stock_order(symbol, side, quantity, order_type="market", limit_price=None, stop_price=None, trail_price=None, trail_percent=None, time_in_force="day", extended_hours=False, client_order_id=None)
– Place a stock order of any type (market, limit, stop, stop_limit, trailing_stop)cancel_order_by_id(order_id)
– Cancel a specific ordercancel_all_orders()
– Cancel all open ordersget_option_contracts(underlying_symbol, expiration_date)
– Fetch contractsget_option_latest_quote(option_symbol)
– Latest bid/ask on contractget_option_snapshot(symbol_or_symbols)
– Get Greeks and underlyingplace_option_market_order(legs, order_class, quantity)
– Execute option strategyget_market_clock()
– Market open/close scheduleget_market_calendar(start, end)
– Holidays and trading daysget_corporate_announcements(...)
– Earnings, dividends, splitscreate_watchlist(name, symbols)
– Create a new listupdate_watchlist(id, name, symbols)
– Modify an existing listget_watchlists()
– Retrieve all saved watchlistsget_asset_info(symbol)
– Search asset metadataget_all_assets(status)
– List all tradable instrumentsSee the "Example Queries" section below for 50 real examples covering everything from trading to corporate data to option strategies.
The MCP server provides detailed, well-formatted responses for various trading queries. Here are some examples:
Query: "What are the option Greeks for TSLA250620P00500000?"
Response: Option Details:
Greeks:
Key Insights:
Query: "Place a bull call spread using AAPL June 6th options: one with a 190.00 strike and the other with a 200.00 strike."
Response: Order Details:
Spread Legs:
Long Leg (BUY):
Short Leg (SELL):
Strategy Summary:
These examples demonstrate the server's ability to provide:
The server maintains this level of detail and formatting across all supported queries, making it easy to understand and act on the information provided.
This server can place real trades and access your portfolio. Treat your API keys as sensitive credentials. Review all actions proposed by the LLM carefully, especially for complex options strategies or multi-leg trades.
MIT