-
Notifications
You must be signed in to change notification settings - Fork 8
Capture MCP tool calls in PostHog #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5fb3271
Capture MCP tool calls in PostHog
masnwilliams 981d564
Flush analytics after the response and drop the injected context argu…
masnwilliams b1a9ccf
Drop response payloads and credential arguments before send
masnwilliams cf6d634
Drop auth connection submit parameters before send
masnwilliams 47a73cb
Mint a session id so stateless requests share one MCP session
masnwilliams 01a6476
Stop capturing tool arguments alongside responses
masnwilliams d9b21ea
Expose the session header on the response that sets it
masnwilliams 1de2e9a
Restore generated next-env types path
masnwilliams 96adad9
Send MCP call metadata only, on an allow-list
masnwilliams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { | ||
| encodeSessionId, | ||
| instrument, | ||
| MCP_SESSION_HEADER, | ||
| newSessionId, | ||
| PostHogMCPAnalyticsEvent, | ||
| PostHogMCPAnalyticsProperty, | ||
| } from "@posthog/mcp"; | ||
| import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { PostHog } from "posthog-node"; | ||
|
|
||
| const projectToken = process.env.POSTHOG_PROJECT_TOKEN; | ||
|
|
||
| if (!projectToken && process.env.NODE_ENV !== "production") { | ||
| console.error( | ||
| "POSTHOG_PROJECT_TOKEN variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once POSTHOG_PROJECT_TOKEN is configured", | ||
| ); | ||
| } | ||
|
|
||
| // Created once per lambda instance, never per request. | ||
| const posthog = projectToken | ||
| ? new PostHog(projectToken, { | ||
| host: process.env.POSTHOG_HOST ?? "https://us.i.posthog.com", | ||
| flushAt: 1, | ||
| flushInterval: 0, | ||
| }) | ||
| : null; | ||
|
|
||
| // Every property this integration sends. An allow-list rather than a deny-list so a | ||
| // property the pinned SDK doesn't emit today — a renamed payload field, a new one — | ||
| // can't start flowing on an upgrade. Deliberately absent: $mcp_parameters and | ||
| // $mcp_response (call payloads), $mcp_error_message (the text a failed tool returned), | ||
| // and $mcp_intent / $mcp_intent_source (agent-written, and intent capture is off). | ||
| const SENT_PROPERTIES = new Set<string>([ | ||
| "$groups", | ||
| "$process_person_profile", | ||
| PostHogMCPAnalyticsProperty.ClientName, | ||
| PostHogMCPAnalyticsProperty.ClientVersion, | ||
| PostHogMCPAnalyticsProperty.DurationMs, | ||
| PostHogMCPAnalyticsProperty.ErrorType, | ||
| PostHogMCPAnalyticsProperty.IsError, | ||
| PostHogMCPAnalyticsProperty.ListedToolNames, | ||
| PostHogMCPAnalyticsProperty.ProtocolVersion, | ||
| PostHogMCPAnalyticsProperty.ResourceName, | ||
| PostHogMCPAnalyticsProperty.ServerName, | ||
| PostHogMCPAnalyticsProperty.ServerVersion, | ||
| PostHogMCPAnalyticsProperty.SessionId, | ||
| PostHogMCPAnalyticsProperty.Source, | ||
| PostHogMCPAnalyticsProperty.ToolCategory, | ||
| PostHogMCPAnalyticsProperty.ToolDescription, | ||
| PostHogMCPAnalyticsProperty.ToolName, | ||
| ]); | ||
|
|
||
| /** | ||
| * Captures every tool call, tools/list, and initialize handled by the server as a | ||
| * `$mcp_*` PostHog event. No-op when POSTHOG_PROJECT_TOKEN is unset. | ||
| */ | ||
| export function instrumentMcpAnalytics(server: McpServer) { | ||
| if (!posthog) return; | ||
|
|
||
| instrument(server, posthog, { | ||
| // Intent capture injects a required `context` argument into every tool schema. | ||
| // Left off so the public tool surface is unchanged. | ||
| context: false, | ||
| // A failed tool call otherwise fans out into a second `$exception` event whose | ||
| // `$exception_list` is built from the text the tool returned. | ||
| enableExceptionAutocapture: false, | ||
| // Events are attributed to the analytics session, with no person created. The only | ||
| // id this server holds is the Clerk subject, while every other producer in these | ||
| // projects identifies people by their Kernel user id — identifying on the subject | ||
| // would mint a second profile per person. Resolving the Kernel user id needs an | ||
| // API that doesn't exist yet, so user-level reporting is out of scope until then. | ||
| identify: null, | ||
| // No part of a call is safe to capture: arguments carry free-form input (credential | ||
| // field maps, curl headers and bodies, typed text, shell commands, Playwright | ||
| // source), results are serialized to a JSON string (see jsonResponse) so the SDK's | ||
| // key-name redaction can't see inside them, and a tool's error text is whatever | ||
| // upstream returned. Send call metadata only. | ||
| beforeSend: (event) => { | ||
| if (event.event === PostHogMCPAnalyticsEvent.Exception) return null; | ||
|
|
||
| const properties = event.properties; | ||
| if (!properties) return event; | ||
|
|
||
| for (const key of Object.keys(properties)) { | ||
| if (!SENT_PROPERTIES.has(key)) delete properties[key]; | ||
| } | ||
|
|
||
| return event; | ||
| }, | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| type InitializeRequestBody = { | ||
| method?: string; | ||
| params?: { | ||
| clientInfo?: { name?: string; version?: string }; | ||
| protocolVersion?: string; | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * mcp-handler answers over SSE with a stateless transport, so it never issues an | ||
| * `Mcp-Session-Id`. Left alone every request becomes its own PostHog session and the | ||
| * client name is lost after the handshake. Mint the SDK's session token on the | ||
| * initialize request instead: it goes back to the client on the response, the client | ||
| * replays it, and any instance decodes the same session id and client info out of it. | ||
| * | ||
| * Returns the token, or null when there's nothing to mint. Safe on the stateless | ||
| * transport, which ignores an incoming session id. | ||
| */ | ||
| export async function mintMcpSessionId(req: Request): Promise<string | null> { | ||
| if (!posthog || req.headers.get(MCP_SESSION_HEADER)) return null; | ||
|
|
||
| // Streamable HTTP only. The legacy SSE transport issues its own session id and its | ||
| // clients don't replay ours, which would put the handshake in one session and the | ||
| // calls that follow in another. | ||
| if (!new URL(req.url).pathname.endsWith("/mcp")) return null; | ||
|
|
||
| const body = (await req | ||
| .clone() | ||
| .json() | ||
| .catch(() => null)) as InitializeRequestBody | null; | ||
| if (body?.method !== "initialize") return null; | ||
|
|
||
| return encodeSessionId({ | ||
| sessionId: newSessionId(), | ||
| clientName: body.params?.clientInfo?.name, | ||
| clientVersion: body.params?.clientInfo?.version, | ||
| // Negotiated once, at the handshake. Carried in the token so the events after it | ||
| // report it too. | ||
| protocolVersion: body.params?.protocolVersion, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Drains queued events after the response has been sent, so capture never adds | ||
| * latency to a tool call. | ||
| */ | ||
| export async function flushMcpAnalytics() { | ||
| if (!posthog) return; | ||
| try { | ||
| await posthog.flush(); | ||
| } catch (error) { | ||
| console.error("Failed to flush PostHog MCP analytics events", error); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.