Skip to main content
Glama

这是什么?

alpaka-mcp 是一个可直接投入生产的 MCP 服务器 + CLI,可将 AI 助手(Claude Desktop、Cursor、VS Code Copilot)和你的终端直接连接到 Alpaca Trading API。通过自然语言——或从命令行——交易股票、ETF 和加密货币。

基于 alpaca-pyMCP Python SDK 构建。

默认使用模拟交易 — 真实市场数据、模拟资金、无真实资金风险。


Related MCP server: FinClaw

快速开始

1. 获取 Alpaca API 密钥(免费)

alpaca.markets 注册 → 模拟交易 → API 密钥

2. 安装

git clone https://github.com/vikrambtech2025-png/alpaka-mcp.git
cd alpaka-mcp
uv sync

3. 配置

cp .env.example .env
# Add your API keys to .env

4. 运行

uv run alpaka-mcp          # MCP server (stdio for Claude/Cursor)
uv run alpaka account      # CLI — check account
uv run mcp dev src/alpaka_mcp/server.py  # MCP Inspector

连接 AI 助手

添加到 %AppData%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "alpaka": {
      "command": "uv",
      "args": ["--directory", "C:\\path\\to\\alpaka-mcp", "run", "alpaka-mcp"],
      "env": {
        "ALPACA_API_KEY": "your_key",
        "ALPACA_SECRET_KEY": "your_secret",
        "ALPACA_PAPER": "true"
      }
    }
  }
}

添加到项目根目录下的 .cursor/mcp.json

{
  "mcpServers": {
    "alpaka": {
      "command": "uv",
      "args": ["--directory", "/path/to/alpaka-mcp", "run", "alpaka-mcp"],
      "env": {
        "ALPACA_API_KEY": "your_key",
        "ALPACA_SECRET_KEY": "your_secret",
        "ALPACA_PAPER": "true"
      }
    }
  }
}

添加到 .vscode/mcp.json

{
  "servers": {
    "alpaka": {
      "command": "uv",
      "args": ["--directory", "/path/to/alpaka-mcp", "run", "alpaka-mcp"],
      "env": {
        "ALPACA_API_KEY": "your_key",
        "ALPACA_SECRET_KEY": "your_secret",
        "ALPACA_PAPER": "true"
      }
    }
  }
}

MCP 工具(16 个)

工具

描述

get_account

余额、权益、购买力、日内交易

get_portfolio

所有未平仓持仓及盈亏(P&L)

工具

描述

buy

下达买单(市价/限价/止损/止损限价)

sell

下达卖单(市价/限价)

get_orders

按状态和标的列出订单

cancel_order

取消指定订单

cancel_all_orders

取消所有未平仓订单

工具

描述

get_position

单个持仓详情

close_position

平仓(全部或部分)

工具

描述

get_stock_quote

最新买卖盘报价(买价/卖价)

get_stock_bars

历史 OHLCV K线

get_stock_snapshot

完整快照(成交、报价、日线、1分钟)

工具

描述

get_crypto_quote

最新加密货币买卖报价

get_crypto_bars

历史加密货币 OHLCV K线

工具

描述

search_assets

按名称或代码搜索股票/ETF/加密货币

get_market_clock

市场开/闭市状态


CLI 命令(12 个)

alpaka account                    # Account balance & buying power
alpaka portfolio                  # Open positions with color-coded P&L
alpaka quote AAPL                 # Bid/ask quote (stocks or crypto)
alpaka buy AAPL --qty 10          # Buy 10 shares
alpaka buy BTC/USD -n 500         # Buy $500 of Bitcoin
alpaka sell AAPL --qty 5          # Sell 5 shares
alpaka orders                     # List open orders
alpaka orders --status closed     # List filled orders
alpaka cancel <order-id>          # Cancel specific order
alpaka cancel-all                 # Cancel everything open
alpaka bars AAPL --tf 1Day        # Historical bars
alpaka clock                      # Is the market open?
alpaka search apple               # Find tradeable assets

所有命令均支持 --json 以输出机器可读结果:

$ uv run alpaka account --json
{
  "status": "ACTIVE",
  "equity": "100000.00",
  "buying_power": "200000.00",
  "cash": "100000.00",
  "portfolio_value": "100000.00",
  "daytrade_count": 0,
  "pattern_day_trader": false,
  "mode": "paper"
}

生产级特性

特性

工作方式

弹性客户端

透明代理为每个 API 调用包装速率限制 + 重试

速率限制

3 个独立令牌桶(各 200 次请求/分钟)— 交易、股票数据、加密货币

重试 + 退避

对 429、500 和连接错误进行 3 次尝试,采用 1s/2s/4s 指数退避

非阻塞异步

MCP 工具使用 asyncio.to_thread() — 事件循环永不阻塞

结构化日志

JSON 审计跟踪:logs/trades.jsonl + logs/errors.jsonl

输入验证

在 API 调用前验证代码、数量、价格和订单类型

实盘交易保护

实盘交易要求 ALPACA_LIVE_CONFIRM=true + 5 秒倒计时

配置验证

拒绝占位 API 密钥,并提供清晰的设置说明

线程安全

lru_cache 客户端初始化,无可变全局状态

CLI 错误处理

每个命令均被包装 — 错误清晰,无回溯堆栈


架构

src/alpaka_mcp/
├── server.py          # MCP server entry point (stdio)
├── cli.py             # 12 Typer commands + JSON output
├── config.py          # pydantic-settings + credential validation
├── clients.py         # ResilientClient proxy (rate limit + retry + async)
├── utils.py           # Response formatting
├── logging.py         # Structured JSON logging
├── rate_limiter.py    # Token bucket rate limiters (sync + async)
├── retry.py           # Exponential backoff (sync + async)
├── validation.py      # Input validation
└── tools/             # 16 MCP tools
    ├── account.py     # get_account, get_portfolio
    ├── trading.py     # buy, sell, get_orders, cancel_order, cancel_all
    ├── positions.py   # get_position, close_position
    ├── stock_data.py  # get_stock_quote, get_stock_bars, get_stock_snapshot
    ├── crypto_data.py # get_crypto_quote, get_crypto_bars
    └── discovery.py   # search_assets, get_market_clock

Docker

# MCP server
docker compose up alpaka-mcp

# CLI via docker
docker compose run --rm alpaka-cli account
docker compose run --rm alpaka-cli portfolio

日志持久化保存在 Docker 卷 /app/logs 中。


环境变量

变量

默认值

描述

ALPACA_API_KEY

(必填)

你的 Alpaca API 密钥

ALPACA_SECRET_KEY

(必填)

你的 Alpaca 密钥

ALPACA_PAPER

true

模拟交易模式

ALPACA_LIVE_CONFIRM

false

实盘交易时必须为 true

ALPACA_LOG_DIR

logs

日志文件目录

ALPACA_LOG_LEVEL

INFO

日志详细程度


测试

# Unit tests (no API keys needed)
uv run pytest tests/ --ignore=tests/test_integration.py -v

# Integration tests (needs real paper API keys)
ALPACA_API_KEY=pk_xxx ALPACA_SECRET_KEY=sk_xxx uv run pytest tests/test_integration.py -v

安全

  • 默认模拟交易 — 使用真实资金需要明确选择加入

  • 实盘交易要求同时满足 ALPACA_PAPER=falseALPACA_LIVE_CONFIRM=true

  • 实盘交易启动时有 5 秒倒计时

  • 所有错误格式清晰 — 不会向 LLM 发送回溯堆栈

  • 速率限制可防止无意的 API 滥用

  • 每笔交易都有结构化审计跟踪


许可证

MIT


F
license - not found
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    D
    quality
    D
    maintenance
    MCP server that exposes Alpaca Market Data & Broker API as tools, enabling access to financial data like stock bars, assets, market days, and news through the Message Control Protocol.
    4
    54
    2
    ISC
  • A
    license
    Not graded
    quality
    D
    maintenance
    MCP server that provides AI agents with financial tools including real-time quotes, backtesting, technical analysis, and multi-exchange data via a simple CLI interface.
    1
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    An MCP server implementation for Alpaca's Trading API that enables LLMs to interact with Alpaca's trading infrastructure using natural language, supporting stock, options, and crypto trading, portfolio management, watchlists, and market data.
    4
    MIT
  • A
    license
    A
    quality
    A
    maintenance
    This MCP server connects AI assistants to a Public.com brokerage account, enabling natural language trading of stocks, options, and crypto, along with portfolio management, quotes, and orders.
    37
    65
    Apache 2.0

View all related MCP servers

Related MCP Connectors

  • MCP server for Gainium — manage trading bots, deals, and balances via AI assistants

  • MCP server exposing the Backtest360 engine API as tools for AI agents.

  • Alpaca MCP — real-time US stock market data via the Alpaca Market Data API

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vikrambtech2025-png/alpaka-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server