Skip to content

feat(mcp): Track report_user_action from MCP separately if it's from the wizard#49196

Merged
robbie-c merged 2 commits intomasterfrom
claude/detect-wizard-user-agent-booYw
Feb 26, 2026
Merged

feat(mcp): Track report_user_action from MCP separately if it's from the wizard#49196
robbie-c merged 2 commits intomasterfrom
claude/detect-wizard-user-agent-booYw

Conversation

@robbie-c
Copy link
Member

@robbie-c robbie-c commented Feb 26, 2026

See PostHog/wizard#291

Problem

We need to track which client is making requests to the MCP server (e.g., PostHog Wizard) for better source attribution and analytics. Currently, we extract the client identifier from the User-Agent header but don't forward it in outgoing API requests.

Changes

  • Extract posthog/foo identifier from incoming User-Agent headers in the MCP request handler
  • Store the client identifier in request context properties
  • Pass client identifier through to the ApiClient configuration
  • Update User-Agent headers in outgoing API requests to include the client identifier (e.g., posthog/mcp-server; for posthog/wizard)
  • Add WIZARD event source to the Python event tracking system to properly categorize requests from PostHog Wizard
  • Update get_event_source() to detect and classify wizard requests based on User-Agent

How did you test this code?

The changes are straightforward data flow modifications:

  • Client identifier extraction uses standard regex matching on User-Agent headers
  • User-Agent header construction is a simple string concatenation
  • Event source detection follows the existing pattern used for terraform-provider detection
  • No new external dependencies or complex logic introduced

Existing tests should continue to pass as this is additive functionality that doesn't break existing behavior.

Publish to changelog?

No

https://claude.ai/code/session_01AJEWnwWhZPuugRrFz9MgQ1

…ution

When an MCP client identifies itself with a user agent containing
"posthog/foo" (e.g. "posthog/wizard"), extract this identifier and
append it to the MCP server's outgoing user agent string, producing
e.g. "posthog/mcp-server; version: 1.0.0; for posthog/wizard".

Also adds WIZARD to the EventSource enum and detects "posthog/wizard"
in get_event_source, checked before the MCP check since the forwarded
UA contains both strings.

https://claude.ai/code/session_01AJEWnwWhZPuugRrFz9MgQ1
Copilot AI review requested due to automatic review settings February 26, 2026 10:05
@robbie-c robbie-c changed the title Add client identifier tracking for source attribution feat(mcp): Add client identifier tracking for source attribution Feb 26, 2026
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

6 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements client identifier tracking to enable source attribution for requests from different MCP clients (e.g., PostHog Wizard). The system extracts client identifiers from incoming User-Agent headers and forwards them in outgoing API requests.

Changes:

  • Added client identifier extraction from User-Agent headers and propagation through request context
  • Updated User-Agent headers in outgoing API requests to include the originating client identifier
  • Added WIZARD event source classification in the Python event tracking system

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
services/mcp/src/mcp.ts Added clientIdentifier to RequestProperties type and ApiClient configuration
services/mcp/src/lib/constants.ts Created getUserAgent() function to format User-Agent with optional client identifier
services/mcp/src/index.ts Implemented regex extraction of client identifier from incoming User-Agent header
services/mcp/src/api/fetcher.ts Updated to use getUserAgent() function with client identifier in API requests
services/mcp/src/api/client.ts Updated ApiConfig type and User-Agent header construction to support client identifier
posthog/event_usage.py Added WIZARD event source enum value and detection logic in get_event_source()
Comments suppressed due to low confidence (2)

services/mcp/src/index.ts:208

  • The regex pattern /posthog\/(\S+)/ will match and capture everything after 'posthog/' until the first whitespace, which could include unwanted characters like semicolons, commas, or version numbers. For example, 'posthog/wizard;version:1.0' would capture 'wizard;version:1.0'. Consider using /posthog\/([a-z0-9-]+)/i to capture only alphanumeric characters and hyphens, which is more appropriate for identifier matching.
    const clientIdentifierMatch = clientUserAgent.match(/posthog\/(\S+)/)

services/mcp/src/index.ts:209

  • Using clientIdentifierMatch[0] returns the entire matched string (e.g., 'posthog/wizard'), but you're capturing a group with parentheses. If you want just the identifier part without the 'posthog/' prefix, use clientIdentifierMatch[1] to access the first capture group. If you want to keep 'posthog/' in the identifier, the capturing group is unnecessary.
    const clientIdentifierMatch = clientUserAgent.match(/posthog\/(\S+)/)
    const clientIdentifier = clientIdentifierMatch ? clientIdentifierMatch[0] : undefined

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 26, 2026

🎭 Playwright report · View test results →

⚠️ 1 flaky test:

  • Can submit the signup form multiple times if there is a generic email set (chromium)

These issues are not necessarily caused by your changes.
Annoyed by this comment? Help fix flakies and failures and it'll disappear!

…es error

- Regex now uses [\w-]+ (alphanumeric, underscore, hyphen) instead of
  \S+ to avoid capturing stray punctuation from user agent strings.
- Add explicit `| undefined` to ApiConfig.clientIdentifier to satisfy
  exactOptionalPropertyTypes.

https://claude.ai/code/session_01AJEWnwWhZPuugRrFz9MgQ1
@robbie-c robbie-c changed the title feat(mcp): Add client identifier tracking for source attribution feat(mcp): Track report_user_action from MCP separately if it's from the wizard Feb 26, 2026
@robbie-c robbie-c requested review from a team February 26, 2026 11:32
@robbie-c robbie-c merged commit 683587c into master Feb 26, 2026
182 checks passed
@robbie-c robbie-c deleted the claude/detect-wizard-user-agent-booYw branch February 26, 2026 12:19
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.

4 participants