examples/MCP-tools Public
import assert from "node:assert/strict";import { test } from "node:test";import { connect, textOf } from "./connect.ts";test("initialize reports the server and its tools capability", async () => { const client = await connect(); assert.equal(client.getServerVersion()?.name, "mcp-tools"); assert.ok(client.getServerCapabilities()?.tools); await client.close();});test("tools/list returns each tool with its schemas and read-only hint", async () => { const client = await connect(); const { tools } = await client.listTools(); assert.deepEqual(tools.map((t) => t.name), ["convert_units", "time_in_zone", "text_stats"]); const convert = tools[0]; assert.deepEqual(convert.inputSchema.required, ["value", "from", "to"]); assert.ok(convert.outputSchema?.properties?.result); assert.equal(convert.annotations?.readOnlyHint, true); await client.close();});test("tools/call returns structured content and the same JSON as text", async () => { const client = await connect(); const result = await client.callTool({ name: "convert_units", arguments: { value: 5, from: "km", to: "m" } }); assert.equal(result.isError, undefined); assert.deepEqual(result.structuredContent, { quantity: "length", value: 5, from: "km", result: 5000, to: "m" }); assert.deepEqual(JSON.parse(textOf(result)), result.structuredContent); await client.close();});test("the server passes its clock to the tools", async () => { const client = await connect({ now: () => new Date("2026-10-06T08:30:00Z") }); const result = await client.callTool({ name: "time_in_zone", arguments: { zone: "Europe/Berlin" } }); assert.deepEqual( { ...(result.structuredContent as object), formatted: undefined }, { zone: "Europe/Berlin", instant: "2026-10-06T08:30:00.000Z", date: "2026-10-06", time: "10:30:00", weekday: "Tuesday", utc_offset: "+02:00", utc_offset_minutes: 120, formatted: undefined }, ); await client.close();});test("defaults from the input schema reach the tool", async () => { const client = await connect(); const result = await client.callTool({ name: "text_stats", arguments: { text: "One two three." } }); const stats = result.structuredContent as { words: number; reading_time_seconds: number }; assert.equal(stats.words, 3); assert.equal(stats.reading_time_seconds, 1); await client.close();});test("a ToolError comes back as an isError result, not a protocol error", async () => { const client = await connect(); const result = await client.callTool({ name: "convert_units", arguments: { value: 1, from: "kg", to: "m" } }); assert.equal(result.isError, true); assert.match(textOf(result), /Cannot convert kg \(mass\) to m \(length\)/); await client.close();});test("arguments that fail the input schema come back as an isError result", async () => { const client = await connect(); const result = await client.callTool({ name: "convert_units", arguments: { value: "ten", from: "km", to: "m" } }); assert.equal(result.isError, true); assert.match(textOf(result), /Invalid arguments for tool convert_units/); await client.close();});