使用说明
IBKR MCP服务器
基于FastMCP 2.0和MCP StreamableHTTP的盈透证券(IBKR)MCP服务器实现,提供账户管理、交易操作和市场数据查询功能。
特性
- 🔗 连接管理:与IBKR TWS/网关的稳定连接
- 📊 账户信息:查询账户摘要、头寸和余额
- 💹 交易操作:下订单、取消订单、查询订单状态
- 📈 市场数据:实时和历史市场数据检索
- 🛡️ 类型安全:使用Pydantic进行数据验证
- ⚡ 异步架构:高性能异步I/O操作
- 📝 丰富的日志记录:结构化日志记录
- 🔧 灵活的配置:支持环境变量和配置文件
安装
从源代码安装
git clone https://github.com/yourusername/ibkr-mcp-server.git
cd ibkr-mcp-server
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install the package
pip install -e .
开发设置
# Install development dependencies
pip install -e ".[dev]"
# Setup pre-commit hooks
pre-commit install
快速开始
1.配置环境
创建一个 .env 项目根目录中的文件:
# MCP Server Settings
MCP__HOST=0.0.0.0
MCP__PORT=8080
# IBKR Connection Settings
IBKR__HOST=127.0.0.1
IBKR__PORT=4002
IBKR__CLIENT_ID=1
IBKR__READONLY=false
# Logging Settings
LOGGING__LEVEL=INFO
2.测试连接
# Test IBKR connection
python -m ibkr_mcp_server.cli test --host 127.0.0.1 --port 4002
3.启动服务器
# Start server
python -m ibkr_mcp_server.cli serve
# Or with custom parameters
python -m ibkr_mcp_server.cli serve --host 0.0.0.0 --port 8080
配置
环境变量
| 变量 | 默认值 | 描述 |
|---|---|---|
MCP__HOST |
0.0.0.0 |
MCP服务器侦听地址 |
MCP__PORT |
8080 |
MCP服务器端口 |
IBKR__HOST |
127.0.0.1 |
IBKR TWS/网关地址 |
IBKR__PORT |
4002 |
IBKR TWS/网关端口 |
IBKR__CLIENT_ID |
1 |
IBKR客户端ID |
IBKR__READONLY |
false |
只读模式 |
LOGGING__LEVEL |
INFO |
日志记录级别 |
IBKR端口配置
| 平台 | 演示端口 | 实时端口 |
|---|---|---|
| 总价值 | 4002 | 7496 |
| 网关 | 4002 | 4001 |
MCP工具
服务器提供9个MCP工具:
账户管理
get_account_summary:获取帐户摘要信息get_positions:获取位置信息
交易操作
place_order:下订单cancel_order:取消订单get_open_orders:获取未结订单
市场数据
get_market_data:获取实时市场数据get_historical_data:获取历史数据
连接管理
connection_status:检查连接状态reconnect:重新连接到IBKR
使用示例
下单
{
"tool": "place_order",
"arguments": {
"contract": {
"symbol": "AAPL",
"sec_type": "STK",
"exchange": "SMART",
"currency": "USD"
},
"order": {
"action": "BUY",
"total_quantity": 100,
"order_type": "LMT",
"lmt_price": 150.0
}
}
}
获取职位
{
"tool": "get_positions",
"arguments": {}
}
获取历史数据
{
"tool": "get_historical_data",
"arguments": {
"contract": {
"symbol": "AAPL",
"sec_type": "STK",
"exchange": "SMART",
"currency": "USD"
},
"duration": "1 D",
"bar_size": "1 min"
}
}
建筑
┌─────────────────────┐
│ MCP Client │
│ (Claude Desktop, │
│ Custom Client) │
└─────────┬───────────┘
│ HTTP/WebSocket
┌─────────┴───────────┐
│ FastMCP Server │
│ (MCP Protocol Layer)│
├─────────────────────┤
│ IBKR MCP Server │
│ (Business Logic) │
├─────────────────────┤
│ IBKR Client │
│ (API Wrapper) │
└─────────┬───────────┘
│ TWS API
┌─────────┴───────────┐
│ TWS/Gateway │
│ (IBKR Platform) │
└─────────────────────┘
发展
项目结构
src/ibkr_mcp_server/
├── __init__.py # Package initialization
├── server.py # MCP server implementation
├── client.py # IBKR client wrapper
├── models.py # Data models
├── config.py # Configuration management
├── exceptions.py # Exception definitions
└── cli.py # Command line interface
代码规范
- 使用
black用于代码格式化 - 使用
isort用于进口分拣 - 使用
flake8用于代码过滤 - 使用
mypy用于类型检查
测试
# Run tests
pytest
# Generate coverage report
pytest --cov=src --cov-report=html
部署
Docker部署
# Build image
docker build -t ibkr-mcp-server .
# Run container
docker run -p 8080:8080 --env-file .env ibkr-mcp-server
Docker Compose
# Start services
docker-compose up -d
# View logs
docker-compose logs -f
与Claude Desktop集成
添加到您的Claude Desktop MCP配置中:
{
"mcpServers": {
"ibkr": {
"command": "python",
"args": ["-m", "ibkr_mcp_server.cli", "serve"],
"env": {
"IBKR__HOST": "127.0.0.1",
"IBKR__PORT": "4002",
"IBKR__CLIENT_ID": "1"
}
}
}
}
重要提示
- TWS/网关:确保IBKR TWS或网关在启用API连接的情况下运行
- 端口配置:确保TWS/Gateway API端口与您的配置匹配
- 权限:确保您的帐户具有适当的交易权限
- 风险管理:请在生产环境中实施适当的风险控制
- 市场数据:某些市场数据可能需要订阅
故障排除
常见问题
- 连接失败:检查TWS/Gateway是否正在运行并且API是否已启用
- 客户端ID冲突:为多个连接使用不同的客户端ID
- 港口问题:验证TWS/网关设置的正确端口
- 市场数据错误:确保您有适当的市场数据订阅
日志记录
启用调试日志以进行故障排除:
LOGGING__LEVEL=DEBUG python -m ibkr_mcp_server.cli serve
许可证
MIT许可证-请参阅 许可证 详细信息文件
贡献
欢迎投稿!请随时提交拉取请求。
支持
如果您遇到任何问题,请提交 问题.
中文文档: README_zh_CN.md