""" Example Node.js client for testing the Hologres MCP Server in SSE mode. This script demonstrates how to connect to the server and use its features. """ const fetch = require('node-fetch'); const EventSource = require('eventsource'); // Configuration const SERVER_URL = 'http://localhost:8001'; // Change to your server's URL const SSE_ENDPOINT = `${SERVER_URL}/sse`; let MESSAGE_ENDPOINT = null; // Will be set after connecting to SSE endpoint // Helper function to generate a unique ID for each request function generateId() { return Date.now(); } // Connect to the SSE endpoint function connectToSSE() { console.log(`Connecting to SSE endpoint: ${SSE_ENDPOINT}`); return new Promise((resolve, reject) => { const eventSource = new EventSource(SSE_ENDPOINT); eventSource.onopen = () => { console.log('SSE connection established'); }; eventSource.onerror = (error) => { console.error('SSE connection error:', error); eventSource.close(); reject(error); }; // Listen for the endpoint event eventSource.addEventListener('endpoint', (event) => { MESSAGE_ENDPOINT = `${SERVER_URL}${event.data}`; console.log(`Received message endpoint: ${MESSAGE_ENDPOINT}`); resolve(eventSource); }); // Listen for ping events eventSource.addEventListener('ping', (event) => { console.log(`Received ping: ${event.data}`); }); // Set a timeout in case we don't receive the endpoint event setTimeout(() => { if (!MESSAGE_ENDPOINT) { eventSource.close(); reject(new Error('Timeout waiting for endpoint event')); } }, 5000); }); } // Send a JSON-RPC message to the server async function sendMessage(method, params = null) { if (!MESSAGE_ENDPOINT) { throw new Error('Not connected to server. Call connectToSSE() first.'); } const message = { jsonrpc: '2.0', id: generateId(), method: method }; if (params) { message.params = params; } console.log('Sending message:', JSON.stringify(message, null, 2)); const response = await fetch(MESSAGE_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(message) }); if (!response.ok) { throw new Error(`Failed to send message: ${response.status}`); } const result = await response.json(); console.log('Received response:', JSON.stringify(result, null, 2)); return result; } // Initialize the connection to the server async function initialize() { return sendMessage('initialize', { clientInfo: { name: 'nodejs-test-client', version: '1.0.0' } }); } // List available tools async function listTools() { return sendMessage('listTools'); } // Call a tool with arguments async function callTool(name, arguments) { return sendMessage('callTool', { name: name, arguments: arguments }); } // Read a resource by URI async function readResource(uri) { return sendMessage('readResource', { uri: uri }); } // Main function to run the examples async function main() { let eventSource = null; try { // Connect to SSE endpoint eventSource = await connectToSSE(); // Initialize the connection await initialize(); // List available tools const toolsResponse = await listTools(); // Example 1: Execute a SQL query console.log('\n=== Example 1: Execute SQL Query ==='); const sqlResult = await callTool('execute_sql', { query: "SELECT 'Hello, Hologres!' AS greeting" }); // Example 2: List all schemas console.log('\n=== Example 2: List All Schemas ==='); const schemasResult = await readResource('hologres:///schemas'); // Example 3: Get query plan console.log('\n=== Example 3: Get Query Plan ==='); const planResult = await callTool('get_query_plan', { query: 'SELECT * FROM information_schema.tables LIMIT 10' }); console.log('\nAll examples completed successfully!'); } catch (error) { console.error('Error:', error.message); } finally { // In a real application, you might want to keep the connection open // For this example, we'll close it after a short delay setTimeout(() => { if (eventSource) { console.log('Closing SSE connection...'); eventSource.close(); } console.log('Exiting...'); }, 1000); } } // Run the examples main();