Backpack Exchange MCP Server
A Model Context Protocol (MCP) server that provides AI assistants with tools to interact with the Backpack Exchange API, enabling order management, position tracking, and account balance retrieval.
README
🎒 Backpack Exchange MCP Server
A Model Context Protocol (MCP) server that provides AI assistants with tools to interact with the Backpack Exchange API. Manage your orders directly from Cursor or other MCP-compatible AI assistants.
Features
- Order Management:
- List open spot orders (optionally filtered by trading pair)
- Create limit or market orders (buy/sell) for both SPOT and PERP markets
- Cancel specific orders by ID
- Position Management:
- List all open perpetual positions with PnL, entry price, liquidation price, etc.
- Account Management:
- Get account balances (available, locked, staked, and lent funds)
- Secure Authentication: ED25519 signature-based authentication
- Local-Only: Uses stdio transport for secure local communication
Project Structure
backpack-mcp/
├── auth.py # ED25519 authentication module
├── backpack_client.py # Backpack API client wrapper
├── mcp_server.py # MCP server with tools
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── .env # Your API keys (not in git)
├── examples/ # Example code
│ └── example_auth.py # Direct API usage examples
└── test_integration.py # Integration tests
Prerequisites
Before installing, ensure you have:
- Python 3.8 or higher (Python 3.12+ recommended)
- make (usually pre-installed on macOS/Linux)
Python Installation
If you don't have Python 3.8+ installed, we recommend using pyenv to manage Python versions:
Install pyenv:
# macOS (using Homebrew)
brew install pyenv
# Linux (using pyenv-installer)
curl https://pyenv.run | bash
Install Python 3.12:
pyenv install 3.12.12
pyenv local 3.12.12
Verify Python version:
python3 --version # Should show Python 3.8 or higher
The Makefile will automatically check if Python 3 is available and show an error if it's missing.
Installation
1. Clone or Navigate to Project
cd backpack-mcp
2. Install Dependencies
Option A: Using Makefile (Recommended)
The easiest way to set up the project:
make setup
This will:
- Create a virtual environment
- Install all dependencies
- Copy
.env.exampleto.env(if it doesn't exist)
Then edit .env and add your API keys (see step 3 below).
Other useful Makefile commands:
make help # Show all available commands
make test # Run integration tests
make clean # Remove virtual environment
Option B: Manual Installation
If you prefer to install manually:
# Using pip
pip3 install -r requirements.txt
# Or using virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
3. Configure API Keys
Copy the example environment file and add your keys:
cp .env.example .env
Edit .env and add your Backpack Exchange API keys:
BACKPACK_PRIVATE_KEY=your_base64_encoded_private_key
BACKPACK_PUBLIC_KEY=your_base64_encoded_public_key
To generate key pair:
python3 -c "from cryptography.hazmat.primitives.asymmetric import ed25519; import base64; key = ed25519.Ed25519PrivateKey.generate(); seed = key.private_bytes_raw(); pub = key.public_key().public_bytes_raw(); print(f'Seed: {base64.b64encode(seed).decode()}\nPublic Key: {base64.b64encode(pub).decode()}')"
To get your API key:
- Log in to Backpack Exchange
- Go to Settings > API Keys
- Click New API key
- Add the public key
- Add the generated key pair to your
.envfile
Usage
MCP Server (Recommended)
The MCP server allows AI assistants like Cursor to interact with your Backpack account.
Setup in Cursor
- Create MCP configuration at
~/.cursor/mcp.json:
{
"mcpServers": {
"backpack": {
"command": "/path/to/backpack-mcp/venv/bin/python",
"args": [
"/path/to/backpack-mcp/mcp_server.py"
]
}
}
}
Important: Replace /path/to/backpack-mcp with the actual path to your project directory. For example:
- On macOS/Linux:
/Users/yourusername/Code/backpack-mcpor~/Code/backpack-mcp - On Windows:
C:\Users\yourusername\Code\backpack-mcp
You can find your project path by running pwd (macOS/Linux) or cd (Windows) in your project directory.
Why API keys aren't in the MCP configuration:
The MCP configuration (~/.cursor/mcp.json) only tells Cursor where to find the Python script and interpreter. It does not contain your API keys. This is a security best practice:
- Separation of concerns: Configuration (where to run code) is separate from credentials (API keys)
- Security: The
.envfile with your keys is gitignored and never committed - Runtime loading: The MCP server loads keys from
.envwhen it starts, not from the MCP config - Flexibility: You can change keys without modifying the MCP configuration
How ED25519 key pairs connect to subaccounts:
According to the official Backpack Exchange API documentation:
-
One key pair per main account: The ED25519 key pair (public/private) authenticates your main Backpack account, not individual subaccounts.
-
Subaccounts are identified by parameter: When you want to use a specific subaccount, you include
subaccountIdas a parameter in API requests. The same key pair authenticates all subaccounts under your main account. -
No separate keys needed: You do not need different key pairs for different subaccounts. One key pair gives you access to all subaccounts, and you specify which one to use via the
subaccountIdparameter.
Example flow:
- Generate one ED25519 key pair in Backpack Exchange settings
- Store it in
.env(BACKPACK_PRIVATE_KEY and BACKPACK_PUBLIC_KEY) - The MCP server uses these keys to sign all requests
- To access a specific subaccount, include
subaccountIdin the request parameters (if the endpoint supports it)
-
Restart Cursor completely (quit and reopen)
-
Use the tools by asking Cursor:
- "List my open orders"
- "Create a limit buy order for 0.001 BTC at $80,000"
- "Cancel order 12345 for BTC_USDC"
- "Show my positions"
- "Get my balances"
- "Open a long position in SOL_USDC_PERP for $10"
Available MCP Tools
list_orders
List all open spot orders.
Parameters:
symbol(optional): Trading pair filter (e.g., "BTC_USDC")
Returns:
orders: List of order objectscount: Number of orderssymbol: Filter used
Example:
List my open orders
List my BTC_USDC orders
create_order
Create a new order (limit or market). Works for both SPOT and PERP markets.
Parameters:
symbol(required): Trading pair (e.g., "BTC_USDC" for spot, "BTC_USDC_PERP" for perpetual)side(required): "Bid" (buy) or "Ask" (sell)orderType(required): "Limit" or "Market"quantity(optional): Order quantity (required for limit orders, optional for market if quoteQuantity provided)price(optional): Limit price (required for Limit orders)timeInForce(optional): "GTC" (default), "IOC", or "FOK"quoteQuantity(optional): Quote quantity for market orders (e.g., "10" for $10 worth)
Returns:
success: Booleanorder: Order object with ID and detailserror: Error message (if failed)
Example:
Create a limit buy order for 0.001 BTC at $80,000
Open a long position in SOL_USDC_PERP for $10 (market order)
cancel_order
Cancel a specific order by ID.
Parameters:
orderId(required): Order ID to cancelsymbol(required): Trading pair (e.g., "BTC_USDC")
Returns:
success: Booleanorder: Cancelled order objecterror: Error message (if failed)
Example:
Cancel order 12345 for BTC_USDC
list_positions
List all open perpetual positions.
Parameters:
- None
Returns:
positions: List of position objects with:symbol: Trading pairnetQuantity: Net quantity (positive = long, negative = short)entryPrice: Entry pricemarkPrice: Current mark pricepnlUnrealized: Unrealized profit/losspnlRealized: Realized profit/lossestLiquidationPrice: Estimated liquidation price- And more...
count: Number of positions
Example:
Show my positions
List my perpetual positions
get_balances
Get all account balances including lent funds.
Parameters:
showZeroBalances(optional): If False (default), only show assets with non-zero balances. If True, show all assets.
Returns:
balances: Dictionary with asset symbols as keys, each containing:available: Available balance (can be used for trading)locked: Locked balance (committed to open orders)staked: Staked balance (staked for rewards)lent: Lent balance (funds currently lent out, earning interest)
count: Number of assets with non-zero balancestotalAssets: Total number of assets
Example:
Get my balances
Show my account balances
Testing
Run the integration tests:
# Using virtual environment
venv/bin/python test_integration.py
# Or system Python
python3 test_integration.py
The tests verify:
- Scenario 1: Full workflow (create → list → cancel orders)
- Scenario 2: Error handling for all tools
- Scenario 3: Response structure validation
- Scenario 4: Positions functionality
- Scenario 5: Balances functionality (including lent funds)
All tests are integrated into test_integration.py and cover:
- Order management (list, create, cancel)
- Position management (list positions)
- Account management (get balances with lent funds)
- Error handling and edge cases
Security
- Local-Only: MCP server uses stdio transport (no network exposure)
- Environment Variables: API keys stored in
.env(gitignored) - ED25519 Signing: All requests are cryptographically signed
- No Key Logging: Logging redacts sensitive information
Requirements
- Python 3.8+ (see Prerequisites for installation instructions)
- Backpack Exchange API keys (ED25519) - see Configure API Keys
- Dependencies (automatically installed via
make setuporpip install -r requirements.txt):mcp[cli]- MCP Python SDKrequests- HTTP clientcryptography- ED25519 signingpython-dotenv- Environment variables
Troubleshooting
MCP Server Not Connecting
- Check Python path in
~/.cursor/mcp.jsonmatches your system - Restart Cursor completely after configuration changes
- Verify dependencies:
pip3 install -r requirements.txt - Check API keys: Ensure
.envfile exists with valid keys
Import Errors
# Make sure you're using the virtual environment
venv/bin/python -c "from mcp_server import list_orders; print('OK')"
API Errors
- Verify API keys are correct and base64-encoded
- Check you have sufficient funds for orders
- Ensure network connectivity to
api.backpack.exchange
License
This project is for personal use. Use at your own risk when trading.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。