SPB Git

Repository Public

client.ts 2.1 KB 45 lines typescript

1// Test helper: an MCP client (official SDK) that spawns the BUILT server over stdio. Lives here so that the MCP SDK resolves2// from this package's node_modules; used by tests/integration/mcp-*.test.ts of the repository.3import path from "node:path"4import { fileURLToPath } from "node:url"5import { Client } from "@modelcontextprotocol/sdk/client/index.js"6import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"78export const SERVER_ENTRY = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../dist/index.js")910export interface ToolCall {11  isError: boolean12  /** Concatenated text content. */13  text: string14  /** structuredContent (also what the text encodes for successful calls). */15  data: any16}17export interface Spawned {18  client: Client19  /** Everything the server wrote on stderr. */20  stderr: () => string21  pid: number | null22  tools: () => Promise<{ name: string; description?: string; inputSchema: any; annotations?: any }[]>23  call: (name: string, args?: Record<string, unknown>) => Promise<ToolCall>24  close: () => Promise<void>25}2627/** Starts `node dist/index.js` with ONLY the given environment (plus PATH), and connects an MCP client to it. */28export async function spawnServer(env: Record<string, string>): Promise<Spawned> {29  const transport = new StdioClientTransport({ command: process.execPath, args: [SERVER_ENTRY], env: { PATH: process.env.PATH ?? "", ...env }, stderr: "pipe" })30  let err = ""31  transport.stderr?.on("data", (d) => (err += String(d)))32  const client = new Client({ name: "spmail-mcp-tests", version: "1.0.0" })33  await client.connect(transport)34  return {35    client, stderr: () => err, pid: transport.pid,36    tools: async () => (await client.listTools()).tools as any,37    call: async (name, args = {}) => {38      const r = (await client.callTool({ name, arguments: args })) as any39      const text = (r.content ?? []).filter((c: any) => c.type === "text").map((c: any) => c.text).join("\n")40      return { isError: !!r.isError, text, data: r.structuredContent ?? null }41    },42    close: async () => { await client.close() },43  }44}45
Files main
Menu

Theme