|
| 1 | +import { appendFileSync, mkdirSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | + |
| 4 | +export interface ProtocolLogEntry { |
| 5 | + timestamp: string; |
| 6 | + sessionId: string; |
| 7 | + direction: "incoming" | "outgoing"; |
| 8 | + method?: string; |
| 9 | + id?: string | number | null; |
| 10 | + params?: unknown; |
| 11 | + result?: unknown; |
| 12 | + error?: unknown; |
| 13 | + duration?: number; |
| 14 | + clientCapabilities?: unknown; |
| 15 | + clientInfo?: unknown; |
| 16 | + protocolVersion?: string; |
| 17 | +} |
| 18 | + |
| 19 | +const LOG_DIR = join(process.cwd(), "mcp-session-logs"); |
| 20 | +let logDirCreated = false; |
| 21 | + |
| 22 | +function ensureLogDir(): void { |
| 23 | + if (!logDirCreated) { |
| 24 | + mkdirSync(LOG_DIR, { recursive: true }); |
| 25 | + logDirCreated = true; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Appends a JSON-lines log entry for the given session. |
| 31 | + * Writes to mcp-session-logs/<sessionId>.jsonl. |
| 32 | + * Uses synchronous I/O to keep the MCP stdio channel clean. |
| 33 | + */ |
| 34 | +export function logProtocolMessage(sessionId: string, entry: ProtocolLogEntry): void { |
| 35 | + try { |
| 36 | + ensureLogDir(); |
| 37 | + const line = JSON.stringify(entry) + "\n"; |
| 38 | + appendFileSync(join(LOG_DIR, `${sessionId}.jsonl`), line, "utf-8"); |
| 39 | + } catch { |
| 40 | + // Never crash the server over a logging failure |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Extracts a structured log entry from an incoming JSON-RPC request body. |
| 46 | + * Special-cases initialize requests to surface ClientCapabilities at the top level. |
| 47 | + */ |
| 48 | +export function buildIncomingLogEntry(sessionId: string, body: Record<string, unknown>): ProtocolLogEntry { |
| 49 | + const entry: ProtocolLogEntry = { |
| 50 | + timestamp: new Date().toISOString(), |
| 51 | + sessionId, |
| 52 | + direction: "incoming", |
| 53 | + method: typeof body.method === "string" ? body.method : undefined, |
| 54 | + id: (body.id as string | number | null | undefined) ?? undefined |
| 55 | + }; |
| 56 | + |
| 57 | + const method = entry.method; |
| 58 | + |
| 59 | + if (method === "initialize") { |
| 60 | + const params = body.params as Record<string, unknown> | undefined; |
| 61 | + if (params) { |
| 62 | + entry.protocolVersion = params.protocolVersion as string | undefined; |
| 63 | + entry.clientInfo = params.clientInfo; |
| 64 | + entry.clientCapabilities = params.capabilities; |
| 65 | + } |
| 66 | + } else if (method === "tools/call") { |
| 67 | + const params = body.params as Record<string, unknown> | undefined; |
| 68 | + entry.params = params ? { name: params.name, arguments: params.arguments } : undefined; |
| 69 | + } else { |
| 70 | + // For other methods log params as-is (but omit large payloads) |
| 71 | + entry.params = body.params; |
| 72 | + } |
| 73 | + |
| 74 | + return entry; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Builds a log entry for an outgoing response. |
| 79 | + */ |
| 80 | +export function buildOutgoingLogEntry( |
| 81 | + sessionId: string, |
| 82 | + method: string | undefined, |
| 83 | + id: string | number | null | undefined, |
| 84 | + result: unknown, |
| 85 | + error: unknown, |
| 86 | + duration: number |
| 87 | +): ProtocolLogEntry { |
| 88 | + return { |
| 89 | + timestamp: new Date().toISOString(), |
| 90 | + sessionId, |
| 91 | + direction: "outgoing", |
| 92 | + method, |
| 93 | + id, |
| 94 | + result: error ? undefined : result, |
| 95 | + error, |
| 96 | + duration |
| 97 | + }; |
| 98 | +} |
0 commit comments