stellar-cyber-mcp-agents
Version:
Model Context Protocol (MCP) server for Stellar Cyber security operations with specialized multi-agent analysis capabilities
226 lines • 8.22 kB
JavaScript
#!/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 * 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: {},
},
});
// Store orchestrator instance (lazy-loaded)
let orchestratorPromise = 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: {},
},
},
],
};
});
// Lazy load orchestrator only when needed
async function getOrchestrator() {
if (!orchestratorPromise) {
orchestratorPromise = (async () => {
try {
// Redirect console to prevent output during import
const originalConsole = global.console;
global.console = {
...originalConsole,
log: () => { },
info: () => { },
warn: () => { },
error: () => { },
debug: () => { },
};
const { MultiAgentOrchestrator, createDefaultConfig } = await import('./orchestrator.js');
// 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
const orchestrator = new MultiAgentOrchestrator(config);
// Remove all event listeners to prevent console output
orchestrator.removeAllListeners();
await orchestrator.start();
// Restore console
global.console = originalConsole;
return orchestrator;
}
catch (error) {
// Restore console in case of error
const originalConsole = global.console;
if (global.console !== originalConsole) {
global.console = originalConsole;
}
throw error;
}
})();
}
return orchestratorPromise;
}
// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
const orchestrator = await getOrchestrator();
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',
details: 'Please check your STELLAR_API_URL and STELLAR_API_TOKEN environment variables',
tool: name,
status: 'error'
}, null, 2),
},
],
};
}
});
// Start the server
async function main() {
// Create transport
const transport = new StdioServerTransport();
// Start the server
await server.connect(transport);
}
// Handle errors silently
main().catch(() => {
process.exit(1);
});
//# sourceMappingURL=mcp-server-simple.js.map