#!/usr/bin/env node
/**
* MCP Server Test Client
*
* This script demonstrates how to connect to the MCP server and call tools.
* Run with: node test-client.js
*/
import { EventSource } from 'eventsource';
import fetch from 'node-fetch';
const SERVER_URL = 'http://localhost:3000';
let sessionId = null;
// Connect to SSE endpoint
console.log('π Connecting to MCP server...');
const eventSource = new EventSource(`${SERVER_URL}/sse`);
eventSource.onopen = () => {
console.log('β
Connected to SSE endpoint');
};
eventSource.addEventListener('endpoint', (event) => {
console.log('π¨ Received endpoint event:', event.data);
// Extract session ID from the endpoint URL
const match = event.data.match(/sessionId=(\w+)/);
if (match) {
sessionId = match[1];
console.log(`π Session ID: ${sessionId}`);
// Start testing after we have the session ID
setTimeout(() => testMCPServer(), 1000);
}
});
eventSource.addEventListener('message', (event) => {
console.log('π© Received message:', event.data);
try {
const data = JSON.parse(event.data);
console.log('π¦ Parsed message:', JSON.stringify(data, null, 2));
} catch (e) {
console.log('π¦ Raw message:', event.data);
}
});
eventSource.onerror = (error) => {
console.error('β SSE Error:', error);
console.error('Error details:', {
type: error.type,
message: error.message,
target: error.target?.url
});
};
// Test the MCP server
async function testMCPServer() {
if (!sessionId) {
console.error('β No session ID available');
return;
}
console.log('\nπ Testing MCP Tools...\n');
try {
// Test 1: List available tools
console.log('1οΈβ£ Listing available tools...');
await sendMCPRequest({
jsonrpc: '2.0',
id: 1,
method: 'tools/list',
params: {}
});
// Wait a bit before next test
await new Promise(resolve => setTimeout(resolve, 2000));
// Test 2: Call read_widget_resource
console.log('\n2οΈβ£ Calling read_widget_resource...');
await sendMCPRequest({
jsonrpc: '2.0',
id: 2,
method: 'tools/call',
params: {
name: 'read_widget_resource',
arguments: {}
}
});
// Wait a bit before next test
await new Promise(resolve => setTimeout(resolve, 2000));
// Test 3: Call read_page_resource
console.log('\n3οΈβ£ Calling read_page_resource...');
await sendMCPRequest({
jsonrpc: '2.0',
id: 3,
method: 'tools/call',
params: {
name: 'read_page_resource',
arguments: {}
}
});
// Close connection after tests
setTimeout(() => {
console.log('\nβ
Tests completed. Closing connection...');
eventSource.close();
process.exit(0);
}, 5000);
} catch (error) {
console.error('β Test error:', error);
eventSource.close();
process.exit(1);
}
}
async function sendMCPRequest(message) {
try {
const response = await fetch(`${SERVER_URL}/message?sessionId=${sessionId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(message)
});
console.log(`π€ Sent request: ${message.method}`);
console.log(`π₯ Response status: ${response.status}`);
if (!response.ok) {
const text = await response.text();
console.error('Response body:', text);
}
} catch (error) {
console.error('β Request error:', error);
throw error;
}
}
// Handle process termination
process.on('SIGINT', () => {
console.log('\nπ Closing connection...');
eventSource.close();
process.exit(0);
});