by alpacahq
Alpaca’s MCP server lets you trade stocks and options, analyze market data, and build strategies through Alpaca's Trading API.
Alpaca MCP Server is a tool that allows Large Language Models (LLMs) to connect with Alpaca's trading infrastructure. It enables users to perform trading activities, manage their portfolio, and access market data using natural language commands through compatible clients like Claude for Desktop, Cursor, or VS Code.
To use the server, you need Python 3.10+, an Alpaca API key (paper or live), and a compatible MCP client. First, clone the repository, install the dependencies, and set your Alpaca API keys as environment variables. Then, run the server and connect to it from your MCP client.
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 (English). This server supports stock trading, options trading, portfolio management, watchlist handling, and real-time market data access.
Clone the repository and navigate to the directory:
git clone https://github.com/alpacahq/alpaca-mcp-server.git
cd alpaca-mcp-server
Create and activate a virtual environment:
python3 -m venv venv
source venv/bin/activate
Note: The virtual environment will use the Python version that was used to create it. If you run the command with Python 3.10 or newer, your virtual environment will also use Python 3.10+. If you want to confirm the version, you can run python3 --version
after activating the virtual environment.
Install the required packages:
pip install -r requirements.txt
After cloning and activating the virtual environment, your directory structure should look like this:
alpaca-mcp-server/ ← This is the workspace folder (= project root)
├── alpaca_mcp_server.py ← Script is directly in workspace root
├── .vscode/ ← VS Code settings (for VS Code users)
│ └── mcp.json
├── venv/ ← Virtual environment folder
│ └── bin/python
├── .env.example ← Environment template (use this to create `.env` file)
├── .gitignore
├── Dockerfile ← Docker configuration (for Docker use)
├── .dockerignore ← Docker ignore (for Docker use)
├── requirements.txt
└── README.md
Copy the example environment file in the project root by running this command:
cp .env.example .env
Replace the credentials (e.g. API keys) in the .env
file:
ALPACA_API_KEY = "your_alpaca_api_key_for_paper_account"
ALPACA_SECRET_KEY = "your_alpaca_secret_key_for_paper_account"
ALPACA_PAPER_TRADE = True
TRADE_API_URL = None
TRDE_API_WSS = None
DATA_API_URL = None
STREAM_DATA_WSS = None
To use Alpaca MCP Server with Claude Desktop, please follow the steps below. The official Claude Desktop setup document is available here: https://modelcontextprotocol.io/quickstart/user
Open a terminal in the project root directory and run the following command (or use python -m alpaca_mcp_server
):
python alpaca_mcp_server.py
Settings → Developer → Edit Config
claude_desktop_config.json
:Note:
Replace <project_root> with the path to your cloned alpaca-mcp-server directory. This should point to the Python executable inside the virtual environment you created with python3 -m venv venv
in the terminal.
{
"mcpServers": {
"alpaca": {
"command": "<project_root>/venv/bin/python",
"args": [
"/path/to/alpaca-mcp-server/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"
}
}
}
}
To use Alpaca MCP Server with Claude Code, please follow the steps below.
The claude mcp add command
is part of Claude Code. If you have the Claude MCP CLI tool installed (e.g. by npm install -g @anthropic-ai/claude-code
), you can use this command to add the server to Claude Code:
claude mcp add alpaca \
/path/to/your/alpaca-mcp-server/venv/bin/python \
/path/to/your/alpaca-mcp-server/alpaca_mcp_server.py \
-e ALPACA_API_KEY=your_api_key \
-e ALPACA_SECRET_KEY=your_secret_key
Note: Replace the paths with your actual project directory paths. This command automatically adds the MCP server configuration to Claude Code without manual JSON editing.
The Claude MCP CLI tool needs to be installed separately. Check following the official pages for availability and installation instructions
To use Alpaca MCP Server with VS Code, please follow the steps below.
VS Code supports MCP servers through GitHub Copilot's agent mode. The official VS Code setup document is available here: https://code.visualstudio.com/docs/copilot/chat/mcp-servers
Prerequisites
Recommendation: Use workspace-specific configuration (.vscode/mcp.json
) instead of user-wide configuration. This allows different projects to use different API keys (multiple paper accounts or live trading) and keeps trading tools isolated from other development work.
For workspace-specific settings:
Create .vscode/mcp.json
in your project root.
Add the Alpaca MCP server configuration manually to the mcp.json file:
For Linux/macOS:
{
"mcp": {
"servers": {
"alpaca": {
"type": "stdio",
"command": "bash",
"args": ["-c", "cd ${workspaceFolder} && source ./venv/bin/activate && python alpaca_mcp_server.py"],
"env": {
"ALPACA_API_KEY": "your_alpaca_api_key",
"ALPACA_SECRET_KEY": "your_alpaca_secret_key"
}
}
}
}
}
For Windows:
{
"mcp": {
"servers": {
"alpaca": {
"type": "stdio",
"command": "cmd",
"args": ["/c", "cd /d ${workspaceFolder} && .\\venv\\Scripts\\activate && python alpaca_mcp_server.py"],
"env": {
"ALPACA_API_KEY": "your_alpaca_api_key",
"ALPACA_SECRET_KEY": "your_alpaca_secret_key"
}
}
}
}
}
Note: Replace ${workspaceFolder}
with your actual project path. For example:
/Users/username/Documents/alpaca-mcp-server
C:\\Users\\username\\Documents\\alpaca-mcp-server
For user-wide settings:
To configure an MCP server for all your workspaces, you can add the server configuration to your user settings.json file. This allows you to reuse the same server configuration across multiple projects.
Specify the server in the mcp
VS Code user settings (settings.json
) to enable the MCP server across all workspaces.
{
"mcp": {
"servers": {
"alpaca": {
"type": "stdio",
"command": "bash",
"args": ["-c", "cd ${workspaceFolder} && source ./venv/bin/activate && python alpaca_mcp_server.py"],
"env": {
"ALPACA_API_KEY": "your_alpaca_api_key",
"ALPACA_SECRET_KEY": "your_alpaca_secret_key"
}
}
}
}
}
To use Alpaca MCP Server with Docker, please follow the steps below.
Prerequisite:
You must have Docker installed on your system.
docker run -it --rm \
-e ALPACA_API_KEY=your_alpaca_api_key \
-e ALPACA_SECRET_KEY=your_alpaca_secret_key \
ghcr.io/chand1012/alpaca-mcp-server:latest
This pulls and runs the latest published version of the server. Replace your_alpaca_api_key
and your_alpaca_secret_key
with your actual keys. If the server exposes a port (e.g., 8080), add -p 8080:8080
to the command.
docker build -t alpaca-mcp-server .
docker run -it --rm \
-e ALPACA_API_KEY=your_alpaca_api_key \
-e ALPACA_SECRET_KEY=your_alpaca_secret_key \
alpaca-mcp-server
Use this if you want to run a modified or development version of the server.
{
"mcpServers": {
"alpaca": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e", "ALPACA_API_KEY",
"-e", "ALPACA_SECRET_KEY",
"ghcr.io/chand1012/alpaca-mcp-server:latest"
],
"env": {
"ALPACA_API_KEY": "your_alpaca_api_key",
"ALPACA_SECRET_KEY": "your_alpaca_secret_key"
}
}
}
}
Environment variables can be set either with -e
flags or in the "env"
object, but not both. For Claude Desktop, use the "env"
object.
Security Note:
Never share your API keys or commit them to public repositories. Be cautious when passing secrets as environment variables, especially in shared or production environments.
For more advanced Docker usage:
See the official Docker documentation.
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:
Provide your live account keys as environment variables in the .env
file:
ALPACA_API_KEY = "your_alpaca_api_key_for_live_account"
ALPACA_SECRET_KEY = "your_alpaca_secret_key_for_live_account"
ALPACA_PAPER_TRADE = False
TRADE_API_URL = None
TRADE_API_WSS = None
DATA_API_URL = None
STREAM_DATA_WSS = None
Update Configuration file
For example, when using Claude Desktop, provide your live account keys as environment variables in claude_desktop_config.json
:
{
"mcpServers": {
"alpaca": {
"command": "<project_root>/venv/bin/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"
}
}
}
}
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, days=5, timeframe="1Day", limit=None, start=None, end=None)
– OHLCV historical bars with flexible timeframes (1Min, 5Min, 1Hour, 1Day, etc.)get_stock_latest_trade(symbol)
– Latest market trade priceget_stock_latest_bar(symbol)
– Most recent OHLC barget_stock_snapshot(symbol_or_symbols, feed=None, currency=None)
– Comprehensive snapshot with latest quote, trade, minute bar, daily bar, and previous daily 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
Reviews feature coming soon
Stay tuned for community discussions and feedback