Skip to content

Commit b1a9ccf

Browse files
committed
Drop response payloads and credential arguments before send
1 parent 981d564 commit b1a9ccf

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/lib/mcp/analytics.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { instrument } from "@posthog/mcp";
1+
import { instrument, PostHogMCPAnalyticsProperty } from "@posthog/mcp";
22
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
33
import { PostHog } from "posthog-node";
44

@@ -20,6 +20,14 @@ const posthog = projectToken
2020
})
2121
: null;
2222

23+
// Tools that take or return credentials. Their arguments carry arbitrary field/value
24+
// maps (e.g. manage_credentials `values`) that key-name redaction can't cover.
25+
const CREDENTIAL_TOOLS = new Set([
26+
"manage_api_keys",
27+
"manage_credentials",
28+
"manage_proxies",
29+
]);
30+
2331
function clerkUserId(extra?: Record<string, unknown>): string | null {
2432
const authInfo = extra?.authInfo as
2533
| { extra?: { userId?: string | null } }
@@ -45,6 +53,22 @@ export function instrumentMcpAnalytics(server: McpServer) {
4553
const userId = clerkUserId(extra);
4654
return userId ? { distinctId: userId } : null;
4755
},
56+
beforeSend: (event) => {
57+
const properties = event.properties;
58+
if (!properties) return event;
59+
60+
// Every tool serializes its result to a JSON string (see jsonResponse), so the
61+
// SDK's key-name redaction can't see inside it — a created API key or a TOTP
62+
// code would go out verbatim. Keep the call metadata, drop the payload.
63+
delete properties[PostHogMCPAnalyticsProperty.Response];
64+
65+
const toolName = properties[PostHogMCPAnalyticsProperty.ToolName];
66+
if (typeof toolName === "string" && CREDENTIAL_TOOLS.has(toolName)) {
67+
delete properties[PostHogMCPAnalyticsProperty.Parameters];
68+
}
69+
70+
return event;
71+
},
4872
});
4973
}
5074

0 commit comments

Comments
 (0)