UNPKG

stellar-cyber-mcp-agents

Version:

Model Context Protocol (MCP) server for Stellar Cyber security operations with specialized multi-agent analysis capabilities

226 lines 7.94 kB
#!/usr/bin/env node import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from '@modelcontextprotocol/sdk/types.js'; import { MultiAgentOrchestrator, createDefaultConfig } from './orchestrator.js'; import * as dotenv from 'dotenv'; // Load environment variables dotenv.config(); // Create the MCP server const server = new Server({ name: 'stellar-cyber-agents', version: '1.0.0', }, { capabilities: { tools: {}, }, }); // Initialize the orchestrator let orchestrator = null; // Define available tools server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: 'investigate_case', description: 'Investigate a Stellar Cyber case by case ID', inputSchema: { type: 'object', properties: { caseId: { type: 'string', description: 'The case ID to investigate', }, }, required: ['caseId'], }, }, { name: 'correlate_case', description: 'Find cases related to a given case ID', inputSchema: { type: 'object', properties: { caseId: { type: 'string', description: 'The case ID to correlate', }, }, required: ['caseId'], }, }, { name: 'analyze_network', description: 'Analyze network activity for a case', inputSchema: { type: 'object', properties: { caseId: { type: 'string', description: 'The case ID to analyze', }, timeRange: { type: 'object', properties: { start: { type: 'string', description: 'Start time (ISO format)', }, end: { type: 'string', description: 'End time (ISO format)', }, }, required: ['start', 'end'], }, }, required: ['caseId'], }, }, { name: 'get_system_status', description: 'Get the status of the multi-agent system', inputSchema: { type: 'object', properties: {}, }, }, ], }; }); // Handle tool calls server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { if (!orchestrator) { // Return helpful error message when system is not initialized return { content: [ { type: 'text', text: JSON.stringify({ error: 'Multi-agent system not initialized', details: 'Please check your STELLAR_API_URL and STELLAR_API_TOKEN environment variables', tool: name, status: 'error' }, null, 2), }, ], }; } switch (name) { case 'investigate_case': { if (!args || !args.caseId) { throw new Error('caseId argument is required'); } const result = await orchestrator.investigateCase(args.caseId); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } case 'correlate_case': { if (!args || !args.caseId) { throw new Error('caseId argument is required'); } const result = await orchestrator.correlateCase(args.caseId); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } case 'analyze_network': { if (!args || !args.caseId) { throw new Error('caseId argument is required'); } const result = await orchestrator.analyzeNetwork(args.caseId, args.timeRange); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } case 'get_system_status': { const status = await orchestrator.getStatus(); return { content: [ { type: 'text', text: JSON.stringify(status, null, 2), }, ], }; } default: throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`); } } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ error: error instanceof Error ? error.message : 'Unknown error', tool: name, status: 'error' }, null, 2), }, ], }; } }); // Initialize and start the system async function initialize() { try { // Create configuration const config = createDefaultConfig(); // Override with environment variables if available if (process.env.STELLAR_API_URL) { config.stellar.apiUrl = process.env.STELLAR_API_URL; } if (process.env.STELLAR_API_TOKEN) { config.stellar.apiToken = process.env.STELLAR_API_TOKEN; } // Create and start orchestrator orchestrator = new MultiAgentOrchestrator(config); // Disable console output by redirecting events orchestrator.removeAllListeners(); await orchestrator.start(); } catch (error) { // Silently handle initialization errors to avoid breaking MCP protocol throw error; } } // Start the server async function main() { // Try to initialize the orchestrator, but don't fail if it doesn't work try { await initialize(); } catch (error) { // Continue without orchestrator - tools will return helpful error messages } // Create transport const transport = new StdioServerTransport(); // Start the server await server.connect(transport); } // Handle errors main().catch((error) => { // Silently handle server errors to avoid breaking MCP protocol process.exit(1); }); //# sourceMappingURL=mcp-server.js.map