diff --git a/src/http.ts b/src/http.ts index c4b8109..77c709c 100644 --- a/src/http.ts +++ b/src/http.ts @@ -120,7 +120,7 @@ export function createHttpServer(opts: HttpOptions): Server { }); return; } - const server = createMcpServer(quorum); + const server = createMcpServer(quorum, log); const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined, enableJsonResponse: true, diff --git a/src/mcp.ts b/src/mcp.ts index 0b62ae5..400edd4 100644 --- a/src/mcp.ts +++ b/src/mcp.ts @@ -1,6 +1,8 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js"; import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js"; import { failure, type ToolResult } from "./errors.js"; +import type { Logger } from "./log.js"; +import { silentLogger } from "./log.js"; import type { Quorum } from "./service.js"; import { VERSION } from "./version.js"; @@ -153,11 +155,39 @@ export function callTool(q: Quorum, name: string, args: unknown): Promise = new Set(TOOLS.map((t) => t.name)); + +/** + * One log line per tool call: tool, outcome, error code and duration. The tool name comes from + * the client, so anything outside the contract list is logged as "unknown" to keep the value + * set bounded. Arguments and results are never logged. + */ +export function logToolCall( + log: Logger, + name: string, + result: ToolResult, + durationMs: number, +): void { + const fields = { + tool: TOOL_NAMES.has(name) ? name : "unknown", + ok: result.ok, + ...(result.ok ? {} : { error_code: result.error.code }), + duration_ms: Math.round(durationMs), + }; + if (result.ok) { + log.info("tool call", fields); + } else { + log.warn("tool call failed", fields); + } +} + +export function createMcpServer(q: Quorum, log: Logger = silentLogger): Server { const server = new Server({ name: "quorum", version: VERSION }, { capabilities: { tools: {} } }); server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: TOOLS.map((t) => ({ ...t })) })); server.setRequestHandler(CallToolRequestSchema, async (req) => { + const started = performance.now(); const result = await callTool(q, req.params.name, req.params.arguments); + logToolCall(log, req.params.name, result, performance.now() - started); return { content: [{ type: "text", text: JSON.stringify(result) }], structuredContent: result as unknown as Record, diff --git a/test/mcp-log.test.ts b/test/mcp-log.test.ts new file mode 100644 index 0000000..d59bdbe --- /dev/null +++ b/test/mcp-log.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from "vitest"; +import { failure } from "../src/errors.js"; +import { createLogger } from "../src/log.js"; +import { logToolCall } from "../src/mcp.js"; + +function capture(): { lines: Record[]; log: ReturnType } { + const lines: Record[] = []; + const log = createLogger("debug", (l) => lines.push(JSON.parse(l) as Record)); + return { lines, log }; +} + +describe("logToolCall", () => { + it("logs tool, outcome and duration at info on success", () => { + const { lines, log } = capture(); + logToolCall(log, "say", { ok: true } as never, 12.4); + expect(lines).toHaveLength(1); + expect(lines[0]).toMatchObject({ level: "info", tool: "say", ok: true, duration_ms: 12 }); + expect(lines[0]).not.toHaveProperty("error_code"); + }); + + it("logs the error code at warn on failure, never the message", () => { + const { lines, log } = capture(); + logToolCall(log, "say", failure("upstream_error", "secret detail"), 3); + expect(lines[0]).toMatchObject({ level: "warn", ok: false, error_code: "upstream_error" }); + expect(JSON.stringify(lines[0])).not.toContain("secret detail"); + }); + + it("collapses client-supplied unknown tool names to keep values bounded", () => { + const { lines, log } = capture(); + logToolCall(log, "x".repeat(500), failure("invalid_argument", "unknown tool"), 1); + expect(lines[0]).toMatchObject({ tool: "unknown" }); + }); +});