fix
This commit is contained in:
141
http-client-example.js
Normal file
141
http-client-example.js
Normal file
@@ -0,0 +1,141 @@
|
||||
#!/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<Object>} 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 };
|
Reference in New Issue
Block a user