index.ts•4.76 kB
import 'dotenv/config';
import { makeSpiderfootClientFromEnv } from './spiderfootClient.js';
import { z } from 'zod';
// MCP SDK (McpServer + stdio transport)
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
const server = new McpServer({ name: 'spiderfoot-mcp-server', version: '0.1.0' });
// Create client
const sf = makeSpiderfootClientFromEnv();
// Schemas
const StartScanSchema = z.object({
scanname: z.string().min(1),
scantarget: z.string().min(1),
modulelist: z.string().optional(),
typelist: z.string().optional(),
usecase: z.enum(['all', 'investigate', 'passive', 'footprint']).optional(),
});
const ScanDataSchema = z.object({
id: z.string().min(1),
eventType: z.string().optional(),
});
const ScanLogsSchema = z.object({
id: z.string().min(1),
limit: z.number().int().positive().optional(),
reverse: z.enum(['0', '1']).optional(),
rowId: z.number().int().nonnegative().optional(),
});
const ScanInfoSchema = z.object({ id: z.string().min(1) });
const ExportJsonSchema = z.object({ ids: z.string().min(1) });
// Register tools using McpServer API
server.registerTool(
'spiderfoot_ping',
{ title: 'Ping', description: 'Ping SpiderFoot server to verify it is responding.', inputSchema: {} },
async () => ({ content: [{ type: 'text', text: JSON.stringify(await sf.ping()) }] })
);
server.registerTool(
'spiderfoot_modules',
{ title: 'Modules', description: 'List available SpiderFoot modules.', inputSchema: {} },
async () => ({ content: [{ type: 'text', text: JSON.stringify(await sf.modules()) }] })
);
server.registerTool(
'spiderfoot_event_types',
{ title: 'Event Types', description: 'List available SpiderFoot event types.', inputSchema: {} },
async () => ({ content: [{ type: 'text', text: JSON.stringify(await sf.eventTypes()) }] })
);
server.registerTool(
'spiderfoot_scans',
{ title: 'Scans', description: 'List all scans (past and present).', inputSchema: {} },
async () => ({ content: [{ type: 'text', text: JSON.stringify(await sf.scans()) }] })
);
server.registerTool(
'spiderfoot_scan_info',
{ title: 'Scan Info', description: 'Retrieve scan metadata/config for a scan ID.', inputSchema: { id: z.string() } },
async ({ id }) => ({ content: [{ type: 'text', text: JSON.stringify(await sf.scanInfo(id)) }] })
);
server.registerTool(
'spiderfoot_start_scan',
{ title: 'Start Scan', description: 'Start a new scan against a target.', inputSchema: StartScanSchema.shape },
async (input) => {
const allow = (process.env.ALLOW_START_SCAN ?? 'true').toLowerCase();
if (!(allow === 'true' || allow === '1' || allow === 'yes')) {
return { content: [{ type: 'text', text: 'Starting scans is disabled by configuration (ALLOW_START_SCAN=false).' }], isError: true };
}
const res = await sf.startScan(input as any);
return { content: [{ type: 'text', text: JSON.stringify(res) }] };
}
);
server.registerTool(
'spiderfoot_scan_data',
{ title: 'Scan Data', description: 'Fetch scan event results for a scan ID.', inputSchema: { id: z.string(), eventType: z.string().optional() } },
async ({ id, eventType }) => ({ content: [{ type: 'text', text: JSON.stringify(await sf.scanEventResults({ id, eventType })) }] })
);
server.registerTool(
'spiderfoot_scan_data_unique',
{ title: 'Scan Data Unique', description: 'Fetch unique scan event results.', inputSchema: { id: z.string(), eventType: z.string().optional() } },
async ({ id, eventType }) => ({ content: [{ type: 'text', text: JSON.stringify(await sf.scanEventResultsUnique({ id, eventType })) }] })
);
server.registerTool(
'spiderfoot_scan_logs',
{ title: 'Scan Logs', description: 'Fetch/poll scan logs for a given scan ID.', inputSchema: { id: z.string(), limit: z.number().optional(), reverse: z.enum(['0','1']).optional(), rowId: z.number().optional() } },
async ({ id, limit, reverse, rowId }) => ({ content: [{ type: 'text', text: JSON.stringify(await sf.scanLogs({ id, limit, reverse, rowId })) }] })
);
server.registerTool(
'spiderfoot_export_json',
{ title: 'Export JSON', description: 'Export scan results in JSON for CSV of IDs.', inputSchema: { ids: z.string() } },
async ({ ids }) => ({ content: [{ type: 'text', text: JSON.stringify(await sf.exportJson(ids)) }] })
);
// Start the server over stdio
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
// eslint-disable-next-line no-console
console.error('[spiderfoot-mcp-server] listening on stdio');
}
main().catch((err) => {
// eslint-disable-next-line no-console
console.error('Failed to start MCP server:', err);
process.exit(1);
});