Custom events and metadata

Add metadata to captured events with an event properties callback, or send your own events with analytics.capture().

Add metadata to every event

Pass a callback to attach extra properties to automatically captured MCP events. The callback receives request context, such as headers, transport, and the request ID.

import { instrument, getRequestHeaders } from "@posthog/mcp"
const analytics = instrument(server, posthog, {
eventProperties: async (request, extra) => ({
$app_version: process.env.GIT_SHA ?? "unknown",
request_id: getRequestHeaders(extra)?.["x-request-id"],
}),
})

The returned object is spread flat onto the event's properties alongside the built-in $mcp_* keys:

JSON
{
"event": "$mcp_tool_call",
"properties": {
"$mcp_tool_name": "search_events",
"$app_version": "a1b2c3d",
"region": "iad",
"request_id": "req_…"
}
}

Return constants for values that never change, or read the request context for values that vary.

For group analytics, return groups from identify. The SDK adds $groups to events for the session.

Don't return $mcp_*, $groups, $set, or $session_id keys. Go drops them. Set person processing with the process_person_profile event option, not a property.

Returned values must be JSON-serializable. The SDK catches callback errors and sends them to your logger. These errors do not interrupt tool execution.

Send a custom event

Use analytics.capture() for events that aren't MCP requests, such as UI feedback or workflow milestones. It uses the SDK's sanitization, current server session and identity, and beforeSend hook. In TypeScript it returns a promise, and in Python it returns a coroutine, so await it. In Ruby it runs synchronously and returns nil.

Custom capture has no request context and doesn't run the event properties callback. Pass custom metadata in properties.

You name the event. It's sent verbatim – it's your event, so it is not $-prefixed.

const analytics = instrument(server, posthog)
await analytics.capture({
event: "feedback_submitted",
properties: { rating: 5 },
})

The Go SDK has no analytics handle. Call client.Enqueue(posthog.Capture{...}) on your posthog-go client instead. That path has no SDK redaction.

Set event options

Event options, such as cookieless_mode or process_person_profile, control how PostHog processes an event. MCP SDKs take them under a name that keeps them apart from the SDK's own MCP options. TypeScript needs @posthog/mcp 1.3.0, Python needs posthog-python 8.0, Go needs posthog-go v2, and Rust needs the posthog crate 1.0.

SDKEvery auto-captured eventOne event
TypeScripteventOptions callback in instrument()eventOptions on analytics.capture() and the PostHogMCP capture methods
PythonMCPAnalyticsOptions(event_options=...)event_options on analytics.capture() and the PostHogMCP capture_* methods
Goposthogmcpsdk.WithEventOptions, for tool calls and their $exceptionToolCall.EventOptions and EventContext.EventOptions
RustMcpOptionsBuilder::event_optionsToolCallRequest::event_option

Your SDK's installation page has an example of each. To set options on a custom event:

await analytics.capture({
event: "feedback_submitted",
properties: { rating: 5 },
eventOptions: { cookieless_mode: true },
})

An MCP event without an identity always gets process_person_profile: false. In TypeScript, an explicit process_person_profile in eventOptions wins over that default.

Still have questions?

Was this page useful?