UNPKG

behemoth-cli

Version:

🌍 BEHEMOTH CLIv3.760.4 - Level 50+ POST-SINGULARITY Intelligence Trading AI

230 lines 8.15 kB
/** * MCP Tool Mapper * Maps BEHEMOTH MCP tools to CLI tool system */ import { createToolResponse } from '../tools/tools.js'; export class MCPToolMapper { connectionManager; toolSchemas = new Map(); toolCategories = new Map(); riskLevels = new Map(); constructor(connectionManager) { this.connectionManager = connectionManager; this.setupEventHandlers(); } /** * Initialize tool mappings from connected MCP servers */ async initialize() { await this.refreshToolMappings(); } /** * Refresh tool mappings from all connected servers */ async refreshToolMappings() { this.toolSchemas.clear(); this.toolCategories.clear(); this.riskLevels.clear(); const availableTools = this.connectionManager.getAllAvailableTools(); for (const tool of availableTools) { const schema = this.mapBehemothToolToSchema(tool); this.toolSchemas.set(tool.name, schema); this.toolCategories.set(tool.name, tool.category); this.riskLevels.set(tool.name, tool.riskLevel); } } /** * Get all mapped tool schemas for the CLI system */ getMappedToolSchemas() { return Array.from(this.toolSchemas.values()); } /** * Get tool schemas filtered by category */ getToolSchemasByCategory(category) { const schemas = []; for (const [toolName, schema] of this.toolSchemas) { if (this.toolCategories.get(toolName) === category) { schemas.push(schema); } } return schemas; } /** * Execute a mapped MCP tool */ async executeMappedTool(toolName, toolArgs) { try { // Check if tool exists if (!this.toolSchemas.has(toolName)) { return createToolResponse(false, undefined, '', `Unknown MCP tool: ${toolName}`); } // Get tool info const category = this.toolCategories.get(toolName); const riskLevel = this.riskLevels.get(toolName); // Create BEHEMOTH tool call const toolCall = { tool: toolName, ...toolArgs }; // Execute via connection manager const result = await this.connectionManager.executeTool(toolCall); // Convert BEHEMOTH result to CLI ToolResult return this.convertBehemothResultToToolResult(result, toolName, category, riskLevel); } catch (error) { return createToolResponse(false, undefined, '', `MCP tool execution failed: ${error instanceof Error ? error.message : String(error)}`); } } /** * Check if a tool is available */ isToolAvailable(toolName) { return this.toolSchemas.has(toolName); } /** * Get tool risk level */ getToolRiskLevel(toolName) { return this.riskLevels.get(toolName); } /** * Get tool category */ getToolCategory(toolName) { return this.toolCategories.get(toolName); } /** * Get available tool categories */ getAvailableCategories() { const categories = new Set(); for (const category of this.toolCategories.values()) { categories.add(category); } return Array.from(categories); } // Private methods setupEventHandlers() { this.connectionManager.on('serverConnected', () => { this.refreshToolMappings(); }); this.connectionManager.on('serverDisconnected', () => { this.refreshToolMappings(); }); } mapBehemothToolToSchema(tool) { // Create CLI-compatible tool schema const schema = { type: 'function', function: { name: tool.name, description: this.enhanceToolDescription(tool), parameters: { type: 'object', properties: this.mapToolProperties(tool), required: tool.schema.inputSchema.required || [] } } }; return schema; } enhanceToolDescription(tool) { let description = tool.description; // Add category and risk level information description += ` [Category: ${tool.category}]`; description += ` [Risk: ${tool.riskLevel}]`; if (tool.requiresAuth) { description += ' [Requires Authentication]'; } // Add common parameters info based on category switch (tool.category) { case 'trading': description += ' Example: {"symbol": "BTCUSDT", "side": "buy", "orderType": "market", "qty": 0.01}'; break; case 'analysis': description += ' Example: {"symbol": "BTCUSDT", "period": 14}'; break; case 'market-data': description += ' Example: {"symbol": "BTCUSDT", "limit": 100}'; break; case 'cosmic': description += ' Example: {"symbol": "BTCUSDT"}'; break; case 'risk': description += ' Example: {"symbol": "BTCUSDT"}'; break; } return description; } mapToolProperties(tool) { const properties = { ...tool.schema.inputSchema.properties }; // Add common crypto trading properties if not present if (tool.category === 'trading' || tool.category === 'analysis' || tool.category === 'market-data') { if (!properties.symbol) { properties.symbol = { type: 'string', description: 'Trading pair symbol (e.g., BTCUSDT, ETHUSDT)', default: 'BTCUSDT' }; } } if (tool.category === 'analysis' && !properties.period) { properties.period = { type: 'number', description: 'Analysis period (default: 14)', default: 14, minimum: 1, maximum: 200 }; } if ((tool.category === 'market-data' || tool.category === 'analysis') && !properties.limit) { properties.limit = { type: 'number', description: 'Number of items to retrieve (default: 100)', default: 100, minimum: 1, maximum: 1000 }; } return properties; } convertBehemothResultToToolResult(behemothResult, toolName, category, riskLevel) { if (!behemothResult.success) { return createToolResponse(false, undefined, '', behemothResult.error || 'BEHEMOTH tool execution failed'); } // Format the result based on category let formattedData = behemothResult.data; let message = `${toolName} executed successfully`; if (behemothResult.executionTime) { message += ` (${behemothResult.executionTime}ms)`; } // Category-specific formatting switch (category) { case 'trading': message = `Trading operation completed: ${toolName}`; if (riskLevel === 'high') { message += ' ⚠️ HIGH RISK OPERATION'; } break; case 'analysis': message = `Technical analysis completed: ${toolName}`; break; case 'market-data': message = `Market data retrieved: ${toolName}`; break; case 'cosmic': message = `Cosmic analysis completed: ${toolName} ✨`; break; case 'risk': message = `Risk analysis completed: ${toolName}`; break; case 'monitoring': message = `System monitoring: ${toolName}`; break; } return createToolResponse(true, formattedData, message); } } //# sourceMappingURL=tool-mapper.js.map