#!/usr/bin/env node /** * @fileoverview Example HTTP MCP Client for Laravel Healthcare MCP Server * Demonstrates how to connect to MCP server via HTTP transport */ import axios from 'axios'; class HttpMcpClient { constructor(serverUrl = 'http://localhost:3000/mcp') { this.serverUrl = serverUrl; this.requestId = 1; } /** * Send MCP request via HTTP * @param {string} method - MCP method name * @param {Object} params - Method parameters * @returns {Promise} Response */ async request(method, params = {}) { const request = { jsonrpc: "2.0", method: method, params: params, id: this.requestId++ }; try { console.log(`šŸ“¤ Sending MCP request: ${method}`); console.log(`šŸ“ Request:`, JSON.stringify(request, null, 2)); const response = await axios.post(this.serverUrl, request, { headers: { 'Content-Type': 'application/json' }, timeout: 30000 }); console.log(`šŸ“„ Response:`, JSON.stringify(response.data, null, 2)); return response.data; } catch (error) { console.error(`āŒ Request failed:`, error.message); if (error.response) { console.error(`šŸ“‹ Response data:`, error.response.data); } throw error; } } /** * Initialize MCP connection */ async initialize() { return await this.request('initialize', { protocolVersion: "2024-11-05", clientInfo: { name: "http-mcp-client", version: "1.0.0" } }); } /** * List available tools */ async listTools() { return await this.request('tools/list'); } /** * Execute a tool * @param {string} toolName - Tool name * @param {Object} args - Tool arguments */ async callTool(toolName, args = {}) { return await this.request('tools/call', { name: toolName, arguments: args }); } /** * Ping server */ async ping() { return await this.request('ping'); } } // Example usage async function main() { const client = new HttpMcpClient(); try { console.log("šŸš€ Starting HTTP MCP Client..."); console.log("=".repeat(50)); // Test connection console.log("1ļøāƒ£ Testing connection..."); await client.initialize(); // List tools console.log("\n2ļøāƒ£ Listing available tools..."); const toolsResponse = await client.listTools(); const tools = toolsResponse.result?.tools || []; console.log(`šŸ“Š Found ${tools.length} tools`); // Show first 5 tools console.log("\nšŸ“‹ First 5 tools:"); tools.slice(0, 5).forEach((tool, index) => { console.log(` ${index + 1}. ${tool.name}: ${tool.description}`); }); // Test tool execution (login) console.log("\n3ļøāƒ£ Testing tool execution (login)..."); await client.callTool('public_manage_login', { email: 'test@example.com', password: 'password' }); // Test ping console.log("\n4ļøāƒ£ Testing ping..."); await client.ping(); console.log("\nāœ… All tests completed successfully!"); } catch (error) { console.error("\nāŒ Client test failed:", error.message); process.exit(1); } } // Run if called directly if (import.meta.url === `file://${process.argv[1]}`) { main(); } export { HttpMcpClient };