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"]); const [convert] = tools; 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("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();});