Skip to main content
Glama

SpiderFoot MCP Server

fixed-mcp-client.js5.04 kB
// Fixed MCP client for SpiderFoot MCP server import axios from 'axios'; const MCP_SERVER_URL = 'http://localhost:5002/mcp'; class FixedMCPClient { constructor() { this.sessionId = null; this.requestId = 1; this.axios = axios.create({ baseURL: 'http://localhost:5002', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json, text/event-stream' }, // Important: Disable automatic redirects maxRedirects: 0, validateStatus: status => status >= 200 && status < 400 }); // Add request interceptor to include session ID in all requests this.axios.interceptors.request.use(config => { if (this.sessionId) { config.headers['mcp-session-id'] = this.sessionId; } return config; }); } async initialize() { try { console.log('Initializing MCP client...'); const response = await this.axios.post('/mcp', { jsonrpc: '2.0', method: 'initialize', params: { protocolVersion: '1.0', clientInfo: { name: 'fixed-mcp-client', version: '1.0.0' }, capabilities: {} }, id: this.requestId++ }); // Extract session ID from response headers if (response.headers && response.headers['mcp-session-id']) { this.sessionId = response.headers['mcp-session-id']; console.log('Session ID:', this.sessionId); } // Parse SSE response const result = this.parseSSEResponse(response.data); console.log('Initialization successful'); return result; } catch (error) { console.error('Initialization failed:', error.message); if (error.response) { console.error('Response status:', error.response.status); console.error('Response headers:', error.response.headers); console.error('Response data:', error.response.data); } throw error; } } async listTools() { return this.callMethod('mcp.list_tools', {}); } async callMethod(method, params = {}) { if (!this.sessionId) { throw new Error('Not initialized. Call initialize() first.'); } console.log(`\nCalling method: ${method}`); console.log('Params:', JSON.stringify(params, null, 2)); try { const response = await this.axios.post('/mcp', { jsonrpc: '2.0', method: method, params: params, id: this.requestId++ }); // Parse SSE response const result = this.parseSSEResponse(response.data); console.log('Method call successful'); return result; } catch (error) { console.error(`Method ${method} call failed:`, error.message); if (error.response) { console.error('Response status:', error.response.status); console.error('Response headers:', error.response.headers); console.error('Response data:', error.response.data); } throw error; } } parseSSEResponse(data) { if (!data) return { event: null, data: null }; // If it's already a parsed object, return as is if (typeof data === 'object') { return data; } // Otherwise, try to parse as SSE const lines = data.split('\n').filter(line => line.trim() !== ''); const result = { event: null, data: null }; for (const line of lines) { const colonIndex = line.indexOf(':'); if (colonIndex === -1) continue; const field = line.slice(0, colonIndex).trim(); let value = line.slice(colonIndex + 1).trim(); if (field === 'data') { try { result.data = JSON.parse(value); } catch (e) { result.data = value; } } else if (field === 'event') { result.event = value; } } return result; } } // Test the fixed MCP client async function testFixedMCPClient() { const client = new FixedMCPClient(); try { // 1. Initialize the client console.log('=== Starting Fixed MCP Client Test ==='); await client.initialize(); if (!client.sessionId) { throw new Error('Failed to initialize MCP session'); } // 2. List available tools console.log('\n=== Listing Tools ==='); const tools = await client.listTools(); console.log('Available tools:', JSON.stringify(tools, null, 2)); // 3. If tools are available, try to call one if (tools && tools.data && tools.data.result) { const toolName = Object.keys(tools.data.result)[0]; if (toolName) { console.log(`\n=== Testing Tool: ${toolName} ===`); const toolResult = await client.callMethod(toolName, {}); console.log('Tool result:', JSON.stringify(toolResult, null, 2)); } } console.log('\n✅ Fixed MCP client test completed successfully!'); } catch (error) { console.error('\n❌ Fixed MCP client test failed:', error.message); process.exit(1); } } // Run the test testFixedMCPClient();

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/CorbettCajun/Spiderfoot-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server