Skip to content

Instrument application with PostHog - #38

Merged
nighthawk merged 3 commits into
mainfrom
posthog/wizard-01a1c8a8e671
Nov 11, 2026
Merged

nighthawk merged 3 commits into
mainfrom
posthog/wizard-01a1c8a8e671

Conversation

@posthog-eu

@posthog-eu posthog-eu Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

PostHog setup report

⚠️ Needs your attention

  • Configure POSTHOG_API_KEY and POSTHOG_HOST as production Cloudflare Worker bindings before deploying; local .env configuration does not deploy with the Worker.
  • Runtime event delivery has not been observed. Deploy, invoke a safe MCP tool, and verify the resulting telemetry in PostHog.
  • The scoped format check still reports pre-existing whole-file spacing drift in package.json, src/index.ts, and src/tools/tripgo.ts.

PostHog is set up for this Cloudflare Worker MCP server with guarded server-side capture, MCP tool analytics, exception autocapture, and a starter dashboard. Compilation and scoped lint checks passed; no event delivery was exercised or observed during this run.

Installed and initialized

  • Installed posthog-node at ^5.68.2 and @posthog/mcp at exact version 1.1.0; both are recorded in package.json and package-lock.json.
  • Added src/posthog.ts, which exposes the guarded getPostHog(env) client accessor. It reads POSTHOG_API_KEY and POSTHOG_HOST, uses exception autocapture, and is configured for short-lived Worker delivery (flushAt: 1, flushInterval: 0, 2-second timeout, and no retries).
  • src/index.ts initializes the shared client and performs a caught per-request flush after /mcp handling, so analytics failures do not affect MCP responses.
  • .env.example documents POSTHOG_API_KEY and POSTHOG_HOST; both are configured locally in the ignored .env. Production deployment still requires both as Cloudflare Worker environment bindings.

Events instrumented

These custom events are captured only after their associated tool handler completes successfully. They are personless and use aggregate, non-identifying properties; runtime capture has not been confirmed.

Event What it measures File
trip_routing_completed A routing request completing, including trip-option totals and non-identifying preferences. src/tools/tripgo.ts
location_search_completed A location search completing, including result totals and non-identifying search configuration. src/tools/tripgo.ts
departures_retrieved A departures lookup completing, including departure totals and non-identifying request configuration. src/tools/tripgo.ts

Identification

User identification was deliberately skipped. This publicly reachable MCP Worker has no authentication boundary, user model, session-to-user mapping, or verified stable caller ID. MCP session IDs, shared API keys, request headers, IP addresses, and derived values were not used as substitute distinct IDs.

Follow-up issue — analytics cannot be attributed to users: Until the application provides an authoritative authenticated user primary key, events and errors remain un-identified and cannot support person-level analysis. If authentication is added, wire the verified primary key at the request boundary using posthog-node request-scoped context; do not use email as the distinct ID.

Error tracking

Exception autocapture is enabled centrally through enableExceptionAutocapture: true in src/posthog.ts, and the client is initialized by src/index.ts. No duplicate global handlers or manual $exception events were added. Compatibility with the configured Cloudflare Worker nodejs_compat runtime was type-checked, but an exception was not intentionally triggered or observed arriving in PostHog.

MCP analytics

The single Cloudflare Streamable HTTP MCP server at /mcp in src/index.ts is instrumented with @posthog/mcp immediately after McpServer construction and before tool registration. It emits $mcp_tool_call and the package’s default MCP analytics properties for registered tool calls. The wrapper is guarded when PostHog configuration is absent, and request completion flushes are caught.

No distinct ID is supplied because the server has no trustworthy authenticated caller identity. No other MCP servers were identified. Event delivery is unconfirmed because the Worker was not run during this integration.

AI Observability

Not configured: no production LLM provider, model gateway, agent framework, or Cloudflare AI runtime call path exists in this Worker. The observed outbound calls are TripGo REST requests only. No AI Observability package or wrapper was added, and privacy mode is not applicable.

Logs

Not configured: this is a Cloudflare Worker isolate, and the available Node OTLP/NodeSDK log-export approach is not documented as compatible with that runtime. Existing console output remains unexported. Adding the Node-only exporter would risk Worker runtime incompatibility. If a Worker-compatible PostHog logging path becomes available, use a dedicated exporter-only logger with purpose-written, non-sensitive log lines rather than exporting existing root/console output.

Starter analytics dashboard

Created Analytics basics (wizard) with four 30-day daily-trend views: MCP tool calls, trip routing completions, location-search completions, and departures retrieved. The dashboard is ready to populate once the deployed Worker emits events; no governed metric definitions currently exist for these telemetry measures.

Verification and build conflict

Verified:

  • npm ci completed successfully with 205 packages and zero vulnerabilities.
  • npm run type-check passed before and after final formatting.
  • Scoped Biome lint passed for package.json, src/index.ts, src/posthog.ts, src/tools/tripgo.ts, and .posthog-wizard-cache/.posthog-events.json.

Unconfirmed:

  • No deployed Worker was run.
  • No custom event, $mcp_tool_call, or exception was observed arriving in PostHog.
  • The dashboard was verified to contain its four tiles, but it has no confirmed runtime data yet.

Build conflict in full: scoped format verification remains incomplete because Biome reports pre-existing whole-file spacing drift in package.json, src/index.ts, and src/tools/tripgo.ts. Broad reformatting was intentionally avoided; the integration-created src/posthog.ts and event-contract JSON pass formatting.

Next steps

  1. Supply POSTHOG_API_KEY and POSTHOG_HOST as production Cloudflare Worker bindings, then deploy the Worker.
  2. Connect an MCP client and invoke the safe, non-mutating tripgo-locations tool.
  3. Confirm $mcp_tool_call and the applicable custom completion event appear in the starter dashboard.
  4. If the Worker later gains authentication, add verified request-scoped identification before relying on person-level analytics.

Before you merge

  • Run the full production build and fix any lint or type errors introduced by generated code. The available compile gate passed, but no separate build script exists. Review package.json at the scripts declaration.
  • Run the test suite; instrumented tool call sites may need mocks or fixtures updated. Review the capture call sites in src/tools/tripgo.ts.
  • Set POSTHOG_API_KEY and POSTHOG_HOST in every Cloudflare Worker deployment environment, not only local .env. Review .env.example at the two PostHog variable declarations and the getPostHog(env) configuration reads in src/posthog.ts.
  • After deployment, connect an agent and invoke tripgo-locations; confirm a $mcp_tool_call event appears in PostHog. Review the instrument(server, posthog) call in src/index.ts and the /mcp request handler flush.
  • Trigger each successful tool outcome and confirm the corresponding custom event arrives: review the trip_routing_completed, location_search_completed, and departures_retrieved capture calls in src/tools/tripgo.ts.
  • Trigger a controlled unhandled exception in a safe non-production environment and confirm it is captured. Review enableExceptionAutocapture: true in src/posthog.ts.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T03:42:29.666273Z fe576a9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fe576a9eb1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/index.ts Resolved
nighthawk
nighthawk previously approved these changes Nov 11, 2026
SSE responses return before tools run, so awaiting flush in finally both delayed the response and ran too early to send tool events. Flush once the stream ends, under waitUntil.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nighthawk

nighthawk commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Written by Claude on behalf of Adrian.

Suggested squash-merge message:

feat: instrument MCP server with PostHog

- Add a guarded PostHog client configured for short-lived Workers
- Capture MCP tool analytics and exceptions
- Add completion events for routing, location search and departures
- Flush after the response stream ends, under waitUntil
- Add a Vitest suite and run it in CI

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

The repo had no tests, so CI could not catch regressions in the PostHog capture sites or the flush timing on /mcp.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@nighthawk
nighthawk enabled auto-merge (squash) November 11, 2026 07:30
@nighthawk nighthawk self-assigned this Nov 11, 2026
@nighthawk
nighthawk requested a review from MauroGomez November 11, 2026 07:31

@MauroGomez MauroGomez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good.

@nighthawk
nighthawk merged commit 33d6579 into main Nov 11, 2026
1 check passed
@nighthawk
nighthawk deleted the posthog/wizard-01a1c8a8e671 branch November 11, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants