sdk-client.js•2.68 kB
// MCP Client using official SDK
import { McpClient } from '@modelcontextprotocol/sdk/client/mcp.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import { EventEmitter } from 'node:events';
class MCPClient {
constructor() {
this.client = null;
this.connected = false;
}
async connect() {
if (this.connected) return;
// Create a transport for the MCP client
const transport = new StdioClientTransport({
// We'll use a custom transport that connects to our HTTP server
// This is a workaround since the SDK doesn't have an HTTP transport
async send(message) {
console.log('Sending message:', JSON.stringify(message, null, 2));
// We'll implement the actual HTTP request in the method calls
return { result: {} };
},
onMessage: new EventEmitter()
});
// Create the MCP client
this.client = new McpClient({
transport,
clientInfo: {
name: 'spiderfoot-mcp-client',
version: '1.0.0'
}
});
// Connect to the server
await this.client.connect();
this.connected = true;
console.log('MCP client connected');
}
async listTools() {
if (!this.connected) await this.connect();
return this.client.listTools();
}
async callTool(toolName, params = {}) {
if (!this.connected) await this.connect();
return this.client.callTool(toolName, params);
}
}
// Test the MCP client
async function testMCPClient() {
const client = new MCPClient();
try {
console.log('=== Testing MCP Client ===');
// 1. List available tools
console.log('\n1. Listing tools...');
const tools = await client.listTools();
console.log('Available tools:', JSON.stringify(tools, null, 2));
// 2. Test a specific tool (e.g., spiderfoot_ping)
console.log('\n2. Testing spiderfoot_ping...');
const pingResult = await client.callTool('spiderfoot_ping', {});
console.log('Ping result:', JSON.stringify(pingResult, null, 2));
// 3. List scans if available
console.log('\n3. Listing scans...');
try {
const scans = await client.callTool('spiderfoot_scans', {});
console.log('Scans:', JSON.stringify(scans, null, 2));
} catch (error) {
console.warn('Failed to list scans:', error.message);
}
console.log('\n✅ MCP client test completed successfully!');
} catch (error) {
console.error('\n❌ MCP client test failed:', error.message);
if (error.details) {
console.error('Error details:', JSON.stringify(error.details, null, 2));
}
process.exit(1);
}
}
// Run the test
testMCPClient();