Skip to main content
Glama
by wubbyweb
client.tsโ€ข2.69 kB
#!/usr/bin/env node import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; import { spawn } from "child_process"; import { ListToolsResult, CallToolResult } from "@modelcontextprotocol/sdk/types.js"; class MCPTestClient { private client: Client; constructor() { this.client = new Client( { name: "test-client", version: "1.0.0", }, { capabilities: {}, } ); } async connect(): Promise<void> { // Spawn the server process const serverProcess = spawn("node", ["dist/server.js"], { stdio: ["pipe", "pipe", "inherit"], }); // Create transport using the server process const transport = new StdioClientTransport({ stdin: serverProcess.stdin!, stdout: serverProcess.stdout!, } as any); await this.client.connect(transport); console.log("โœ… Connected to MCP server"); } async listTools(): Promise<void> { try { const response = await this.client.request( { method: "tools/list", }, {} as any ) as ListToolsResult; console.log("๐Ÿ“š Available tools:"); if (response.tools) { response.tools.forEach((tool) => { console.log(` - ${tool.name}: ${tool.description}`); }); } } catch (error) { console.error("โŒ Error listing tools:", error); } } async testWordReversal(word: string): Promise<void> { try { console.log(`\n๐Ÿ”„ Testing word reversal with: "${word}"`); const response = await this.client.request( { method: "tools/call", }, { name: "reverse_word", arguments: { word }, } as any ) as CallToolResult; if (response.content) { response.content.forEach((content) => { if (content.type === "text") { console.log(`โœจ Result: ${content.text}`); } }); } } catch (error) { console.error("โŒ Error calling reverse_word tool:", error); } } async runTests(): Promise<void> { console.log("๐Ÿš€ Starting MCP Client Tests\n"); try { await this.connect(); await this.listTools(); // Test cases const testWords = ["hello", "world", "mcp", "server", "demo"]; for (const word of testWords) { await this.testWordReversal(word); } console.log("\nโœ… All tests completed!"); } catch (error) { console.error("โŒ Test failed:", error); } } } const client = new MCPTestClient(); client.runTests().catch(console.error);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/wubbyweb/mcp-demo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server