From 9a13ce9cd5e402416097fcb8beea054e50ca9e4c Mon Sep 17 00:00:00 2001 From: DeryFerd Date: Thu, 4 Jun 2026 21:03:56 +0700 Subject: [PATCH 1/2] feat(mcp): add structured audit logging for tool invocations --- backend/mcp/servers/helper.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/mcp/servers/helper.ts b/backend/mcp/servers/helper.ts index 4f015aec..6185a967 100644 --- a/backend/mcp/servers/helper.ts +++ b/backend/mcp/servers/helper.ts @@ -14,6 +14,7 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; import type { z } from "zod"; import { projectContextService } from '../project-context'; import { validateMcpOutput } from '../output-validator'; +import { debug } from '$shared/utils/logger'; /** * Infer argument types from Zod schema @@ -158,6 +159,22 @@ export function buildServerRegistries< }; } +const SENSITIVE_ARG_KEYS = new Set(['apiKey', 'api_key', 'token', 'secret', 'password', 'credential', 'key']); + +function sanitizeArgs(args: Record): Record { + const result: Record = {}; + for (const [key, value] of Object.entries(args)) { + if (SENSITIVE_ARG_KEYS.has(key)) { + result[key] = '[REDACTED]'; + } else if (typeof value === 'string' && value.length > 200) { + result[key] = value.slice(0, 200) + '...'; + } else { + result[key] = value; + } + } + return result; +} + // ============================================================================ // Remote MCP Server for Open Code (HTTP transport, in-process execution) // ============================================================================ @@ -209,7 +226,12 @@ export function createRemoteMcpServer( isError: true, } as any; } + const start = performance.now(); + debug.log('mcp-tool', `Invoke ${String(srv.meta.name)}/${String(toolName)}`, sanitizeArgs(args)); const result = await def.handler(args) as any; + const elapsed = (performance.now() - start).toFixed(1); + const isError = result?.isError === true; + debug.log('mcp-tool', `Result ${String(srv.meta.name)}/${String(toolName)} — ${isError ? 'error' : 'ok'} (${elapsed}ms)`); if (result?.content) { result.content = validateMcpOutput(result.content, toolName as string); } From 281847f92b69572760a7fb24f37b6226c0c2291e Mon Sep 17 00:00:00 2001 From: DeryFerd Date: Thu, 4 Jun 2026 23:07:12 +0700 Subject: [PATCH 2/2] fix(mcp): add 'mcp-tool' to LogLabel type for audit logging --- shared/utils/logger.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/utils/logger.ts b/shared/utils/logger.ts index fb19cd16..cef426a4 100644 --- a/shared/utils/logger.ts +++ b/shared/utils/logger.ts @@ -27,6 +27,7 @@ export type LogLabel = // Communication | 'websocket' | 'mcp' + | 'mcp-tool' | 'notification' | 'rate-limit'