MCP服务器-交易指标
🔧 交易指标服务器:一个专门的MCP服务器,为交易应用程序提供全面的技术分析指标。
通过服务器发送事件(SSE)传输公开交易指标的MCP(模型上下文协议)服务器。该服务器使用功能强大的Tulind库提供对流行技术分析指标的访问。
特性
- 🌐 基于Web的MCP服务器 使用SSE传输
- 📊 7个热门交易指标:RSI、EMA、SMA、MACD、布林带、随机指标、ATR
- �� 可扩展工具系统 用于添加自定义指标
- 🔐 基于环境的配置 管理
- 📝 综合录井 用于调试
- 🔄 基于会话的传输 具有UUID跟踪功能
- 🛠️ 模块化架构 便于定制
- ⚡ 高性能 使用Tulind(基于C)库
- 🔍 自动端口检测 -自动查找可用端口
可用交易指标
动量指标
- 反复应力性损伤 (相对强弱指数)-识别超买/超卖情况
- 随机振荡器 -将收盘价与价格区间进行比较
- MACD (移动平均线收敛-发散)-趋势和动量
趋势指标
- 欧洲药品管理局 (指数移动平均线)-以近期价格为重点的趋势跟踪
- 脊髓性肌萎缩症 (简单移动平均线)-经典的趋势跟踪指标
波动性指标
- 自动目标识别 (平均真实范围)-衡量市场波动性
- 布林线 -移动平均线附近的波动带
安装
npm install配置
创建一个 .env 根目录中的文件:
# Server Configuration
HOST=0.0.0.0
PORT=3000
LOG_LEVEL=info
# CORS Configuration (optional)
# CORS_ORIGINS=http://localhost:3000,http://localhost:8080运行服务器
npm start如果3000被占用,服务器将自动找到可用端口。
可用工具
基本工具
- get_server_info -服务器信息,包括可用指示器
交易指标
RSI(相对强弱指数)
{
"name": "calculate_rsi",
"arguments": {
"prices": [100, 102, 101, 103, 105, 104, 106],
"period": 14
}
}2.EMA(指数移动平均线)
{
"name": "calculate_ema",
"arguments": {
"prices": [100, 102, 101, 103, 105, 104, 106],
"period": 20
}
}3.SMA(简单移动平均线)
{
"name": "calculate_sma",
"arguments": {
"prices": [100, 102, 101, 103, 105, 104, 106],
"period": 20
}
}4.MACD
{
"name": "calculate_macd",
"arguments": {
"prices": [100, 102, 101, 103, 105, 104, 106],
"fastPeriod": 12,
"slowPeriod": 26,
"signalPeriod": 9
}
}5.布林带
{
"name": "calculate_bollinger_bands",
"arguments": {
"prices": [100, 102, 101, 103, 105, 104, 106],
"period": 20,
"stdDev": 2.0
}
}6.随机振荡器
{
"name": "calculate_stochastic",
"arguments": {
"high": [105, 107, 106, 108, 110, 109, 111],
"low": [95, 97, 96, 98, 100, 99, 101],
"close": [100, 102, 101, 103, 105, 104, 106],
"kPeriod": 14,
"kSmoothPeriod": 3,
"dPeriod": 3
}
}7.ATR(平均真实范围)
{
"name": "calculate_atr",
"arguments": {
"high": [105, 107, 106, 108, 110, 109, 111],
"low": [95, 97, 96, 98, 100, 99, 101],
"close": [100, 102, 101, 103, 105, 104, 106],
"period": 14
}
}与CCXT集成
这些指示器旨在与CCXT OHLCV数据无缝协作:
// Example: Get BTC/USDT data from exchange and calculate RSI
const ohlcv = await exchange.fetchOHLCV('BTC/USDT', '1h', undefined, 100);
const closes = ohlcv.map(candle => candle[4]); // Extract close prices
// Use with MCP client
const rsiResult = await client.callTool({
name: "calculate_rsi",
arguments: {
prices: closes,
period: 14
}
});数据格式
输入数据
- 价格:收盘价数组(数字)
- 高/低/关闭:OHLC指示器阵列(所有长度相同)
- 时期:计算期的整数
输出格式
所有指标都返回结构化JSON,其中包含:
- 指标:指标名称
- 价值观:计算指标值
- 当前的:当前/最新值及其解释
- 分析:附加分析和信号
- 参数:使用的参数
测试
node test-mcp-client.js建筑
src/
├── tools/
│ ├── tradingIndicators.js # Index of all indicators
│ ├── rsi.js # RSI indicator
│ ├── ema.js # EMA indicator
│ ├── sma.js # SMA indicator
│ ├── macd.js # MACD indicator
│ ├── bollingerBands.js # Bollinger Bands
│ ├── stochastic.js # Stochastic Oscillator
│ ├── atr.js # ATR indicator
│ └── publicTools.js # Public tools including indicators
├── config/
│ └── config.js # Server configuration
└── mcpServer.js # MCP server logic添加自定义指标
- 在中创建新文件
src/tools/(例如。,myIndicator.js) - 出口
myIndicatorDefinition和myIndicatorHandler - 添加到
src/tools/tradingIndicators.js - 重新启动服务器
演出
- 图林德图书馆:基于C的最大性能计算
- 高效的内存使用:尽可能进行流计算
- 错误处理:全面的验证和错误消息
- 并行执行:同时计算多个指标(典型1-5ms)
- 无事件循环阻止:用于tulind操作的process.nextTick保护
MCP标准合规性
此服务器遵循 MCP规范修订版2025-06-18:
会话管理
- ✅ HTTP 404(-32004)用于未知会话ID
- ✅ HTTP 400(-32602)缺少会话ID
- ✅ 5分钟会话超时(可通过配置
MCP_SESSION_TIMEOUT_MS) - ✅ 30秒清理间隔(可通过配置
MCP_CLEANUP_INTERVAL_MS) - ✅ 超时时进行优雅的会话清理
错误处理
- ✅ 结构化错误响应(无抛出异常)
- ✅ 防止运输堵塞
- ✅ 单个操作超时(每个指示器1秒)
- ✅ 全局操作超时(批量计算为5秒)
- ✅ 超时部分结果(交易系统保护)
错误代码
-32602:参数无效/需要会话ID-32004:未找到会话-32000:错误请求-32603:内部服务器错误-32601:未找到方法
测试
运行一致性测试以验证MCP合规性:
# Basic conformance test
npm run test
# Extended conformance test (session lifecycle, errors, parallel execution)
node test-extended-conformance.js
# Stress test (concurrent execution, timeout scenarios)
node test-stress-cascade.js配置
MCP行为的环境变量:
# Session timeout (default: 5 minutes)
MCP_SESSION_TIMEOUT_MS=300000
# Cleanup interval (default: 30 seconds)
MCP_CLEANUP_INTERVAL_MS=30000
# Tool execution timeout (default: 20 seconds)
TOOL_EXECUTION_TIMEOUT_MS=20000
# Allow auto-recreate sessions (default: false, strict compliance)
ALLOW_AUTO_SESSION_RECREATE=false
# Enable session debug endpoint (default: auto-detect)
ENABLE_SESSION_DEBUG=true用例
- 算法交易:实时指标计算
- 投资组合分析:持有量的技术分析
- 市场调研:回溯测试和战略制定
- 风险管理:波动性和趋势分析
- AI/ML培训:模型的特征生成
架构说明
交通堵塞预防
此实现包括防止传输阻塞级联故障:
- 严重超时(每个指示器1秒,总共5秒)
- 会话清理超时
- 结构化错误响应(从不抛出异常)
- 并行指标执行
- 通过process.nextTick进行事件循环保护
遗留文件
这 legacy/ 目录包含已弃用的实现,仅供参考。不要在生产中使用这些文件。
贡献
- 分叉存储库
- 创建功能分支(
git checkout -b feature/new-indicator) - 按照现有模式添加指标
- 测试您的更改
- 提交拉取请求
许可证
该项目根据ISC许可证获得许可。
