fix: drop connection/render-triggered events, gate MCP tool calls#509
Merged
Conversation
…gate MCP tool calls - Remove mcp:server_view (fired on every GET to the MCP endpoint; idle connections in configs inflated it). - Gate mcp:tool_call to actual JSON-RPC tools/call requests, ignoring protocol chatter (initialize, tools/list, notifications/*). Peek at a cloned request body so the handler still reads an intact stream; handle batched arrays and attach the tool name. - Remove agent:session_create (fired on every render for agent-enabled repos); keep session minting and cookie logic intact. Add is_first_message and session_id to agent:message_send instead. - Remove docs:bundle_view; docs:page_view remains the canonical view metric. docs:bundle_fail is untouched.
|
|
|
🚅 Deployed to the docs.page-pr-509 environment in docs.page
|
Move the mcp:tool_call analytics capture out of the POST route's JSON-RPC body peek and into the per-request MCP tool handlers. This removes the double body read (req.clone().json()) and ensures the event only fires when a tool actually executes.
13 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requested by Elliot Hesp · Slack thread
Summary
Several PostHog events were being emitted by connections and page renders rather than by real usage, which distorted the analytics (idle MCP connections and every doc render were inflating the top-line numbers). This trims those events so each metric counts genuine activity.
Before / After
mcp:server_view— Before: fired on everyGETto the MCP endpoint, so a connection just sitting in someone's MCP config counted as a "view" and inflated it to the top event. After: removed entirely; real MCP usage is measured bymcp:tool_call.mcp:tool_call— Before: fired on everyPOSTto the MCP endpoint, including JSON-RPC protocol chatter (initialize,tools/list,notifications/*). After: captured from inside the individual tool handlers, so it only fires when a tool actually executes (read_doc_page/list_doc_files) and carries the tool name directly. Protocol chatter no longer counts.agent:session_create— Before: fired on every server-side render of an agent-enabled repo page, whether or not anyone used the agent. After: removed; the session is still minted and the cookie is still set, we just stop emitting the event. First-use is now signalled on the message event instead.agent:message_send— Before: capturedmessage_countonly. After: also capturesis_first_message(messages.length === 1) andsession_id, so the first message of a session is identifiable without a separate render-triggered event.docs:bundle_view— Before: fired on every successful bundle fetch, double-counting views alongsidedocs:page_view. After: removed.docs:page_viewremains the canonical view metric anddocs:bundle_failis untouched.Scope
app/(hosted site, MCP, Ask AI)packages/cli/packages/mdx-bundler/docs/(product documentation)Type of change
How
app/src/app/api/[owner]/[repo]/mcp/route.ts— dropped themcp:server_viewcapture fromGET. ThePOSThandler no longer peeks at the request body; it just resolves context and delegates tohandleMcpPost, so there is noreq.clone().json()double read of the request stream.app/src/server/mcp/server.ts— moved themcp:tool_callcapture into the per-request tool handlers.createMcpServernow builds a smallcaptureToolCall(tool)helper (over the request'sroutecontext and the shared PostHog client) and each tool handler calls it at the top —read_doc_pageandlist_doc_files. This fires the event only when a tool actually executes, attaches the tool name directly, and avoids parsing JSON-RPC or cloning the request; protocol chatter (initialize,tools/list,notifications/*) is naturally excluded because those never reach a tool handler.app/src/pages/[[...path]].tsx— removed theagent:session_createcapture;createAgentSession(...)and theSet-Cookielogic are unchanged.app/src/app/api/agent/route.ts— addedis_first_messageandsession_id: session.sidto the existingagent:message_sendcapture;distinctIdis unchanged.app/src/app/api/bundle/route.ts— removed thedocs:bundle_viewcapture;docs:bundle_failand the rest of the handler are unchanged.Test plan
next build(fromapp/) passes — compiles, TypeScript check clean, static generation succeeds.Notes for reviewers
No behavioural change beyond analytics: sessions are still minted, cookies still set, and MCP requests are still handled identically. The MCP
POSThandler no longer clones or peeks at the request body —mcp:tool_callis now captured inside the tool handlers themselves, so the event fires on real tool execution rather than on protocol traffic.