Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 31 additions & 1 deletion src/mcp.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -153,11 +155,39 @@ export function callTool(q: Quorum, name: string, args: unknown): Promise<ToolRe
}
}

export function createMcpServer(q: Quorum): Server {
const TOOL_NAMES: ReadonlySet<string> = 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<string, unknown>,
Expand Down
33 changes: 33 additions & 0 deletions test/mcp-log.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>[]; log: ReturnType<typeof createLogger> } {
const lines: Record<string, unknown>[] = [];
const log = createLogger("debug", (l) => lines.push(JSON.parse(l) as Record<string, unknown>));
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" });
});
});
Loading