/** * MCP Server Integration with Neural Trader * * Demonstrates using @neural-trader/mcp for: * - Model Context Protocol server setup * - 87+ trading tools exposed via JSON-RPC 2.0 * - Claude Code integration * - Real-time trading operations * * This enables AI assistants to interact with the trading system */ // MCP Protocol configuration const mcpConfig = { server: { name: 'neural-trader-mcp', version: '2.1.0', description: 'Neural Trader MCP Server - AI-powered trading tools' }, // Transport settings transport: { type: 'stdio', // stdio, http, or websocket port: 3000 // For HTTP/WebSocket }, // Security settings security: { requireAuth: true, allowedOrigins: ['claude-code', 'claude-desktop'], rateLimits: { requestsPerMinute: 100, requestsPerHour: 1000 } } }; // Available MCP tools (87+) const mcpTools = { // Market Data Tools marketData: [ 'getQuote', 'getHistoricalData', 'getOrderBook', 'streamPrices', 'getMarketStatus', 'getExchangeInfo', 'getCryptoPrice', 'getForexRate' ], // Trading Tools trading: [ 'placeOrder', 'cancelOrder', 'modifyOrder', 'getPositions', 'getOrders', 'getAccountBalance', 'closePosition', 'closeAllPositions' ], // Analysis Tools analysis: [ 'calculateIndicator', 'runBacktest', 'analyzeStrategy', 'detectPatterns', 'getCorrelation', 'calculateVolatility', 'getSeasonality', 'performRegression' ], // Risk Management Tools risk: [ 'calculateVaR', 'getMaxDrawdown', 'calculateSharpe', 'getPositionRisk', 'checkRiskLimits', 'runStressTest', 'getGreeks', 'calculateBeta' ], // Portfolio Tools portfolio: [ 'getPortfolioSummary', 'optimizePortfolio', 'rebalancePortfolio', 'getPerformance', 'getAllocation', 'analyzeRiskContribution', 'calculateCorrelationMatrix', 'runMonteCarloSim' ], // Neural Network Tools neural: [ 'trainModel', 'predict', 'loadModel', 'saveModel', 'evaluateModel', 'getModelInfo', 'optimizeHyperparams', 'runEnsemble' ], // Accounting Tools accounting: [ 'calculateCostBasis', 'generateTaxReport', 'trackGainsLosses', 'exportTransactions', 'reconcileAccounts', 'calculateROI' ], // Utility Tools utilities: [ 'convertCurrency', 'formatNumber', 'parseTimeframe', 'validateSymbol', 'getTimezone', 'scheduleTask' ] }; async function main() { console.log('='.repeat(70)); console.log('MCP Server Integration - Neural Trader'); console.log('='.repeat(70)); console.log(); // 1. Server information console.log('1. MCP Server Configuration:'); console.log('-'.repeat(70)); console.log(` Name: ${mcpConfig.server.name}`); console.log(` Version: ${mcpConfig.server.version}`); console.log(` Transport: ${mcpConfig.transport.type}`); console.log(` Description: ${mcpConfig.server.description}`); console.log(); // 2. Available tools summary console.log('2. Available Tools Summary:'); console.log('-'.repeat(70)); const totalTools = Object.values(mcpTools).reduce((sum, arr) => sum + arr.length, 0); console.log(` Total tools: ${totalTools}`); console.log(); for (const [category, tools] of Object.entries(mcpTools)) { console.log(` ${category.charAt(0).toUpperCase() + category.slice(1)}: ${tools.length} tools`); console.log(` ${tools.slice(0, 4).join(', ')}${tools.length > 4 ? '...' : ''}`); } console.log(); // 3. Tool schema examples console.log('3. Tool Schema Examples:'); console.log('-'.repeat(70)); displayToolSchema('getQuote', { description: 'Get current quote for a symbol', inputSchema: { type: 'object', properties: { symbol: { type: 'string', description: 'Stock/crypto symbol' }, extended: { type: 'boolean', default: false, description: 'Include extended data' } }, required: ['symbol'] } }); displayToolSchema('placeOrder', { description: 'Place a trading order', inputSchema: { type: 'object', properties: { symbol: { type: 'string', description: 'Trading symbol' }, side: { type: 'string', enum: ['buy', 'sell'], description: 'Order side' }, quantity: { type: 'number', description: 'Order quantity' }, orderType: { type: 'string', enum: ['market', 'limit', 'stop'], default: 'market' }, limitPrice: { type: 'number', description: 'Limit price (if limit order)' }, timeInForce: { type: 'string', enum: ['day', 'gtc', 'ioc'], default: 'day' } }, required: ['symbol', 'side', 'quantity'] } }); displayToolSchema('runBacktest', { description: 'Run strategy backtest', inputSchema: { type: 'object', properties: { strategy: { type: 'string', description: 'Strategy name or code' }, symbols: { type: 'array', items: { type: 'string' }, description: 'Symbols to test' }, startDate: { type: 'string', format: 'date', description: 'Start date' }, endDate: { type: 'string', format: 'date', description: 'End date' }, initialCapital: { type: 'number', default: 100000, description: 'Starting capital' } }, required: ['strategy', 'symbols', 'startDate', 'endDate'] } }); console.log(); // 4. Example MCP requests console.log('4. Example MCP Requests:'); console.log('-'.repeat(70)); // Get quote example const quoteRequest = { jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'getQuote', arguments: { symbol: 'AAPL', extended: true } } }; console.log(' Get Quote Request:'); console.log(` ${JSON.stringify(quoteRequest, null, 2).split('\n').join('\n ')}`); console.log(); const quoteResponse = await simulateToolCall('getQuote', { symbol: 'AAPL', extended: true }); console.log(' Response:'); console.log(` ${JSON.stringify(quoteResponse, null, 2).split('\n').join('\n ')}`); console.log(); // Place order example const orderRequest = { jsonrpc: '2.0', id: 2, method: 'tools/call', params: { name: 'placeOrder', arguments: { symbol: 'AAPL', side: 'buy', quantity: 100, orderType: 'limit', limitPrice: 180.00 } } }; console.log(' Place Order Request:'); console.log(` ${JSON.stringify(orderRequest, null, 2).split('\n').join('\n ')}`); console.log(); // 5. RuVector integration console.log('5. RuVector Integration Features:'); console.log('-'.repeat(70)); const integrationFeatures = [ 'Pattern storage for strategy matching', 'Embedding vectors for news sentiment', 'Historical signal caching', 'Neural network weight storage', 'Trade decision logging with vector search', 'Real-time pattern similarity detection' ]; integrationFeatures.forEach((feature, i) => { console.log(` ${i + 1}. ${feature}`); }); console.log(); // 6. Security features console.log('6. Security Features:'); console.log('-'.repeat(70)); console.log(` Authentication: ${mcpConfig.security.requireAuth ? 'Required' : 'Optional'}`); console.log(` Allowed Origins: ${mcpConfig.security.allowedOrigins.join(', ')}`); console.log(` Rate Limit: ${mcpConfig.security.rateLimits.requestsPerMinute}/min`); console.log(` Daily Limit: ${mcpConfig.security.rateLimits.requestsPerHour}/hour`); console.log(); // 7. Claude Code configuration console.log('7. Claude Code Configuration:'); console.log('-'.repeat(70)); console.log(' Add to your claude_desktop_config.json:'); console.log(); console.log(` { "mcpServers": { "neural-trader": { "command": "npx", "args": ["@neural-trader/mcp", "start"], "env": { "ALPACA_API_KEY": "your-api-key", "ALPACA_SECRET_KEY": "your-secret-key" } } } }`); console.log(); // 8. Simulate tool calls console.log('8. Tool Call Simulation:'); console.log('-'.repeat(70)); // Simulate various tool calls const simulations = [ { tool: 'getPortfolioSummary', args: {} }, { tool: 'calculateIndicator', args: { symbol: 'AAPL', indicator: 'RSI', period: 14 } }, { tool: 'calculateVaR', args: { confidenceLevel: 0.99, horizon: 1 } }, { tool: 'predict', args: { symbol: 'AAPL', horizon: 5 } } ]; for (const sim of simulations) { console.log(`\n Tool: ${sim.tool}`); console.log(` Args: ${JSON.stringify(sim.args)}`); const result = await simulateToolCall(sim.tool, sim.args); console.log(` Result: ${JSON.stringify(result).substring(0, 80)}...`); } console.log(); // 9. Performance metrics console.log('9. MCP Server Performance:'); console.log('-'.repeat(70)); console.log(' Average latency: < 10ms (local)'); console.log(' Throughput: 1000+ requests/sec'); console.log(' Memory usage: ~50MB base'); console.log(' Concurrent: 100+ connections'); console.log(); console.log('='.repeat(70)); console.log('MCP Server integration demo completed!'); console.log('='.repeat(70)); } // Display tool schema function displayToolSchema(name, schema) { console.log(`\n Tool: ${name}`); console.log(` Description: ${schema.description}`); console.log(' Parameters:'); for (const [param, def] of Object.entries(schema.inputSchema.properties)) { const required = schema.inputSchema.required?.includes(param) ? '*' : ''; console.log(` - ${param}${required}: ${def.type}${def.enum ? ` (${def.enum.join('|')})` : ''}`); } } // Simulate tool call async function simulateToolCall(tool, args) { // Simulate network latency await new Promise(resolve => setTimeout(resolve, 10)); // Return simulated results based on tool switch (tool) { case 'getQuote': return { success: true, data: { symbol: args.symbol, price: 182.52, change: 2.35, changePercent: 1.30, volume: 45234567, bid: 182.50, ask: 182.54, high: 183.21, low: 180.15, open: 180.45, previousClose: 180.17 } }; case 'getPortfolioSummary': return { success: true, data: { totalValue: 985234.56, dayChange: 12345.67, dayChangePercent: 1.27, positions: 15, cash: 45678.90, marginUsed: 0, buyingPower: 145678.90 } }; case 'calculateIndicator': return { success: true, data: { symbol: args.symbol, indicator: args.indicator, period: args.period, values: [ { date: '2024-12-30', value: 65.4 }, { date: '2024-12-31', value: 67.2 } ], signal: 'neutral' } }; case 'calculateVaR': return { success: true, data: { confidenceLevel: args.confidenceLevel, horizon: args.horizon, var: 15234.56, varPercent: 1.55, cvar: 18765.43, method: 'historical' } }; case 'predict': return { success: true, data: { symbol: args.symbol, currentPrice: 182.52, predictions: [ { day: 1, price: 183.15, confidence: 0.72 }, { day: 2, price: 184.20, confidence: 0.68 }, { day: 3, price: 183.80, confidence: 0.65 }, { day: 4, price: 185.10, confidence: 0.61 }, { day: 5, price: 186.50, confidence: 0.58 } ], trend: 'bullish', modelVersion: '2.1.0' } }; default: return { success: true, data: { message: `Tool ${tool} executed successfully` } }; } } // Run the example main().catch(console.error);