Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(mcp): record requested tool filters on initialize events (#130346)

Jake Ruth4cd58c83938d
diff --git a/services/mcp/README.md b/services/mcp/README.md
index 10ffa05f1aed..6802312ab5e8 100644
--- a/services/mcp/README.md
+++ b/services/mcp/README.md
@@ -244,7 +244,7 @@ Available features:
| `health_issues` | Health |
| `hog_function_templates` | CDP function template browsing |
| `hog_functions` | [Functions](https://posthog.com/docs/cdp) |
-| `insights` | [Insights & analytics](https://posthog.com/docs/product-analytics/insights) |
+| `insights` | [Run insight queries](https://posthog.com/docs/product-analytics/insights) |
| `integrations` | Integrations |
| `links` | PostHog app URL generation |
| `llm_analytics` | [AI observability](https://posthog.com/docs/ai-observability) |
@@ -258,7 +258,7 @@ Available features:
| `notebooks` | [Notebooks](https://posthog.com/docs/notebooks) |
| `persons` | [Persons](https://posthog.com/docs/data/persons) |
| `platform_features` | Platform Features |
-| `product_analytics` | Product analytics |
+| `product_analytics` | Saved insights and other product analytics tools |
| `reminders` | Reminders |
| `replay` | [Session replays](https://posthog.com/docs/session-replay) |
| `replay_vision` | Replay vision |
diff --git a/services/mcp/src/hono/analytics.ts b/services/mcp/src/hono/analytics.ts
index a2fa08b87adc..817fdd05b4c5 100644
--- a/services/mcp/src/hono/analytics.ts
+++ b/services/mcp/src/hono/analytics.ts
@@ -14,7 +14,7 @@ import {
MCP_ANALYTICS_VERSION,
type MCPAnalyticsContext,
} from '@/lib/posthog/analytics'
-import type { RequestProperties } from '@/lib/request-properties'
+import { type RequestProperties, sanitizeToolFilterNames } from '@/lib/request-properties'
import { resolveScopePreset } from '@/lib/scope-preset'
import { isPrivateScoutTrialTool } from '@/lib/tool-privacy'
import type { SkillInvocation } from '@/tools/exec-learn'
@@ -106,6 +106,8 @@ export async function trackInitEvent(state: ResolvedState): Promise<void> {
: undefined
const { properties, groups } = buildBaseProperties(state, analyticsContext)
+ const requestedFeatures = sanitizeToolFilterNames(requestContext.features)
+ const requestedTools = sanitizeToolFilterNames(requestContext.tools)
// Emits `$mcp_initialize`. The SDK maps `durationMs` → `$mcp_duration_ms`
// and `sessionId` → `$session_id`; everything else rides on `properties`.
@@ -125,6 +127,9 @@ export async function trackInitEvent(state: ResolvedState): Promise<void> {
has_project_id: !!requestContext.projectId,
read_only: !!requestContext.readOnly,
via_sse_redirect: !!requestContext.viaSseRedirect,
+ ...(requestedFeatures ? { requested_features: requestedFeatures } : {}),
+ ...(requestedTools ? { requested_tools: requestedTools } : {}),
+ ...(requestContext.excludeTools ? { requested_exclude_tools: requestContext.excludeTools } : {}),
},
})
} catch {
diff --git a/services/mcp/src/hono/mcp-context.ts b/services/mcp/src/hono/mcp-context.ts
index f56a3fe87629..cec03b4ee0b2 100644
--- a/services/mcp/src/hono/mcp-context.ts
+++ b/services/mcp/src/hono/mcp-context.ts
@@ -29,6 +29,9 @@ export interface MCPRequestContext
| 'mcpSessionId'
| 'mcpConversationId'
| 'region'
+ | 'features'
+ | 'tools'
+ | 'excludeTools'
> {
mode?: McpMode | undefined
authMethod: McpAuthMethod
@@ -58,6 +61,9 @@ export function buildMCPRequestContext(props: RequestProperties): MCPRequestCont
mcpConsumer: props.mcpConsumer,
mode: props.mode,
region: props.region,
+ features: props.features,
+ tools: props.tools,
+ excludeTools: props.excludeTools,
mcpVendorClient: props.mcpVendorClient,
authMethod: classifyAuthMethod(props.apiToken),
}
diff --git a/services/mcp/src/lib/request-properties.ts b/services/mcp/src/lib/request-properties.ts
index 95ed36efed65..2ffdc18013d0 100644
--- a/services/mcp/src/lib/request-properties.ts
+++ b/services/mcp/src/lib/request-properties.ts
@@ -70,15 +70,15 @@ function splitCsv(value: string | null): string[] | undefined {
}
const MCP_TOOL_NAME = /^[a-z0-9][a-z0-9_-]{0,63}$/i
-const MAX_EXCLUDE_TOOLS = 32
+const MAX_TOOL_FILTER_NAMES = 32
-function parseExcludeTools(value: string | undefined): string[] | undefined {
- if (!value) {
+export function sanitizeToolFilterNames(names: readonly string[] | undefined): string[] | undefined {
+ if (!names) {
return undefined
}
const unique: string[] = []
const seen = new Set<string>()
- for (const part of value.split(',')) {
+ for (const part of names) {
const name = part.trim()
const gateway = isGatewayToolName(name)
const token = gateway ? name : name.toLowerCase()
@@ -88,13 +88,17 @@ function parseExcludeTools(value: string | undefined): string[] | undefined {
}
seen.add(token)
unique.push(token)
- if (unique.length >= MAX_EXCLUDE_TOOLS) {
+ if (unique.length >= MAX_TOOL_FILTER_NAMES) {
break
}
}
return unique.length > 0 ? unique : undefined
}
+function parseExcludeTools(value: string | undefined): string[] | undefined {
+ return value ? sanitizeToolFilterNames(value.split(',')) : undefined
+}
+
export function parseRequestProperties(
request: Request,
clientInfo: ClientInfo,
diff --git a/services/mcp/tests/hono/analytics.test.ts b/services/mcp/tests/hono/analytics.test.ts
index 76c6f5e9a15e..18ef254fe8b1 100644
--- a/services/mcp/tests/hono/analytics.test.ts
+++ b/services/mcp/tests/hono/analytics.test.ts
@@ -67,6 +67,9 @@ function makeState(overrides: Partial<ResolvedState> = {}): ResolvedState {
mcpConsumer: 'request-consumer',
mode: 'cli',
region: 'us',
+ features: ['insights', 'product_analytics', 'insights', 'bad\nvalue'],
+ tools: ['dashboard-get', 'x'.repeat(200)],
+ excludeTools: ['insight-delete'],
mcpVendorClient: 'ClaudeAI',
},
sessionContext: {
@@ -244,6 +247,9 @@ describe('Hono MCP analytics contexts', () => {
mcp_session_protocol_version: '2025-03-26',
mcp_session_consumer: 'session-consumer',
mcp_session_vendor_client: 'ClaudeCode',
+ requested_features: ['insights', 'product_analytics'],
+ requested_tools: ['dashboard-get'],
+ requested_exclude_tools: ['insight-delete'],
})
})