|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | +/** |
| 3 | + * Test script to verify the stdio server works correctly. |
| 4 | + * Connects to the server, lists resources, and reads the MCP App resource. |
| 5 | + */ |
| 6 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 7 | +import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; |
| 8 | + |
| 9 | +async function main() { |
| 10 | + console.log("Starting stdio client test...\n"); |
| 11 | + |
| 12 | + const transport = new StdioClientTransport({ |
| 13 | + command: "npx", |
| 14 | + args: ["tsx", "src/stdio.ts"], |
| 15 | + }); |
| 16 | + |
| 17 | + const client = new Client({ |
| 18 | + name: "test-client", |
| 19 | + version: "1.0.0", |
| 20 | + }); |
| 21 | + |
| 22 | + await client.connect(transport); |
| 23 | + console.log("Connected to server\n"); |
| 24 | + |
| 25 | + // Initialize |
| 26 | + const serverInfo = client.getServerVersion(); |
| 27 | + console.log("Server info:", serverInfo); |
| 28 | + console.log(); |
| 29 | + |
| 30 | + // List resources |
| 31 | + console.log("Listing resources..."); |
| 32 | + const resources = await client.listResources(); |
| 33 | + console.log(`Found ${resources.resources.length} resources:`); |
| 34 | + for (const resource of resources.resources) { |
| 35 | + console.log(` - ${resource.name} (${resource.uri})`); |
| 36 | + } |
| 37 | + console.log(); |
| 38 | + |
| 39 | + // Find and read the MCP App resource |
| 40 | + const mcpAppResource = resources.resources.find((r) => |
| 41 | + r.uri.startsWith("ui://") |
| 42 | + ); |
| 43 | + if (mcpAppResource) { |
| 44 | + console.log(`Reading MCP App resource: ${mcpAppResource.uri}`); |
| 45 | + const content = await client.readResource({ uri: mcpAppResource.uri }); |
| 46 | + const textContent = content.contents[0]; |
| 47 | + if ("text" in textContent) { |
| 48 | + console.log(` MIME type: ${textContent.mimeType}`); |
| 49 | + console.log(` Content length: ${textContent.text.length} bytes`); |
| 50 | + console.log(` First 200 chars: ${textContent.text.slice(0, 200)}...`); |
| 51 | + } |
| 52 | + console.log(); |
| 53 | + } |
| 54 | + |
| 55 | + // List tools |
| 56 | + console.log("Listing tools..."); |
| 57 | + const tools = await client.listTools(); |
| 58 | + console.log(`Found ${tools.tools.length} tools:`); |
| 59 | + for (const tool of tools.tools) { |
| 60 | + const meta = tool._meta as Record<string, unknown> | undefined; |
| 61 | + const uiUri = meta?.["ui/resourceUri"]; |
| 62 | + console.log( |
| 63 | + ` - ${tool.name}${uiUri ? ` (UI: ${uiUri})` : ""}` |
| 64 | + ); |
| 65 | + } |
| 66 | + console.log(); |
| 67 | + |
| 68 | + console.log("All tests passed!"); |
| 69 | + await client.close(); |
| 70 | + // Give the server a moment to clean up before we exit |
| 71 | + setTimeout(() => process.exit(0), 100); |
| 72 | +} |
| 73 | + |
| 74 | +main().catch((error) => { |
| 75 | + console.error("Test failed:", error); |
| 76 | + process.exit(1); |
| 77 | +}); |
0 commit comments