fix(mcp): flush analytics within the request lifetime and bound API-key auth fetch#1270
Open
abhay-codes07 wants to merge 2 commits into
Open
Conversation
posthog-node buffers capture() calls and only sends them when flushAt (20 events) or flushInterval (10s) trips. The MCP tool handlers called capture() fire-and-forget with nothing keeping the Durable Object alive, so whenever the isolate was evicted before the periodic flush — the normal case for low-traffic sessions emitting one memory_added / memory_search / memory_forgot event per tool call — the buffered events were silently dropped. The exported shutdown() was never wired to anything either. Flush explicitly after each capture and keep the returned promise alive past the response with ctx.waitUntil(), the standard Workers pattern for post-response work.
…s OAuth validateOAuthToken aborts its upstream session call after 30 seconds, but validateApiKey had no signal at all, so a hung /v3/session response stalled every API-key-authenticated MCP request until platform limits kicked in instead of failing cleanly. Apply the same AbortSignal timeout; the existing catch already maps TimeoutError to a null auth result.
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.
What
Two request-lifecycle reliability gaps in the MCP worker, one commit each:
1. PostHog events are buffered but never flushed, so they're routinely lost.
posthog-nodebufferscapture()calls and only sends them whenflushAt(20 events) orflushInterval(10s) trips. The tool handlers call the capture helpers fire-and-forget with nothing keeping the Durable Object alive, so whenever the isolate is evicted before a periodic flush — the normal case for a session that emits onememory_added/memory_search/memory_forgotper tool call — the buffered events are silently dropped. The exportedshutdown()was never wired to anything either. Analytics undercount real usage.Fix: each helper now flushes explicitly after capturing, and the three call sites in
server.tskeep the returned promise alive past the response withthis.ctx.waitUntil(...)— the standard Workers pattern for post-response work. Error handling is unchanged (tracking failures still only log).2.
validateApiKeyhas no upstream timeout.validateOAuthTokenaborts its session call after 30s (AbortSignal.timeout(30_000)), but the API-key path had no signal at all — a hung/v3/sessionresponse stalls every API-key-authenticated MCP request until platform limits kick in. Fix: apply the same timeout; the existingcatchalready mapsTimeoutErrorto a null auth result (clean 401).Testing
vitest runinapps/mcp— format + auth suites pass; the 3 pre-existing failures ine2e/oauth.test.ts(no-secret tests against the production server) fail identically on a cleanmaincheckout, verified side by sidewrangler deploy --dry-run— worker bundles cleanlybiome checkon the three touched files — cleanposthog-node5.28.0 (the installed version) exposesflush(): Promise<void>, andDurableObjectState.waitUntilis present in the pinned@cloudflare/workers-types