: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, kea, path } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { MCP_MESSAGE_FIELD_LIMITS, MCPMessageField } from 'scenes/hog-functions/sub-templates/sub-templates'
import { HogQLQueryResponse, NodeKind } from '~/queries/schema/schema-general'
import { asString } from '../queryResultParsers'
import { MCPNotificationUseCase } from './mcpAnalyticsNotificationsLogic'
export type MCPNotificationExample = Record<MCPMessageField, string>
export type MCPNotificationExamples = Partial<Record<MCPNotificationUseCase, MCPNotificationExample>>
// The most recent real event per use case, so a preview can show the project's own traffic instead
// of invented values. One scan serves every card: the three failure use cases all live in
// $mcp_tool_call and are separated by error type, so the use case is derived first and grouped on,
// letting argMax pick each one's latest. Time-bounded to keep it cheap; the preview falls back to
// sample copy when a project has nothing (yet), so an empty result is normal, not an error.
//
// The effective-tool expression mirrors EFFECTIVE_TOOL_SQL in
// products/mcp_analytics/backend/hogql_queries/base.py — the inner tool when the call came through
// the single-exec wrapper, including rejected calls, else the directly-registered name.
const EXAMPLES_QUERY = `
SELECT
'tool-error' AS use_case,
argMax(toString(properties.$mcp_client_name), timestamp) AS client_name,
argMax(toString(properties.$mcp_server_name), timestamp) AS server_name,
argMax(toString(properties.$mcp_intent), timestamp) AS intent,
argMax(
coalesce(
nullIf(toString(properties.$mcp_exec_tool_call_name), ''),
if(properties.$mcp_tool_name = 'exec' AND properties.$mcp_exec_verb = 'call',
nullIf(nullIf(toString(properties.$mcp_exec_target_tool), ''), 'unrecognized'), NULL),
toString(properties.$mcp_tool_name)
),
timestamp
) AS tool_name
FROM events
WHERE timestamp >= now() - INTERVAL 90 DAY
AND event = '$mcp_tool_call'
AND toBool(properties.$mcp_is_error)
`
const KNOWN_USE_CASES: MCPNotificationUseCase[] = ['tool-error']
// Every field the message renders must be present, otherwise the "real" example would be a mix of
// the project's data and our invented copy — more misleading than an honest sample.
const REQUIRED_FIELDS: Record<MCPNotificationUseCase, MCPMessageField[]> = {
'tool-error': ['clientName', 'serverName', 'intent', 'toolName'],
}
// Array.from so a cut can't land inside a surrogate pair and render as a replacement character.
function truncateField(field: MCPMessageField, value: string): string {
const characters = Array.from(value)
return characters.length > MCP_MESSAGE_FIELD_LIMITS[field]
? characters.slice(0, MCP_MESSAGE_FIELD_LIMITS[field]).join('')
: value
}
export function parseExampleRows(rows: unknown[]): MCPNotificationExamples {
const examples: MCPNotificationExamples = {}
for (const row of rows) {
const [rawUseCase, clientName, serverName, intent, toolName] = row as unknown[]
const useCase = KNOWN_USE_CASES.find((candidate) => candidate === asString(rawUseCase))
if (!useCase) {
continue
}
const candidate: MCPNotificationExample = {
clientName: asString(clientName),
serverName: asString(serverName),
intent: asString(intent),
toolName: asString(toolName),
}
if (!REQUIRED_FIELDS[useCase].every((field) => candidate[field])) {
continue
}
examples[useCase] = {
clientName: truncateField('clientName', candidate.clientName),
serverName: truncateField('serverName', candidate.serverName),
intent: truncateField('intent', candidate.intent),
toolName: truncateField('toolName', candidate.toolName),
}
}
return examples
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpNotificationExamplesLogicValues {
examples: MCPNotificationExamples
examplesLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface mcpNotificationExamplesLogicActions {
loadExamples: () => any
loadExamplesFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadExamplesSuccess: (
examples: Partial<Record<'tool-error', MCPNotificationExample>>,
payload?: any
) => {
examples: Partial<Record<'tool-error', MCPNotificationExample>>
payload?: any
}
}
export type mcpNotificationExamplesLogicType = MakeLogicType<
mcpNotificationExamplesLogicValues,
mcpNotificationExamplesLogicActions
>
export const mcpNotificationExamplesLogic = kea<mcpNotificationExamplesLogicType>([
path(['products', 'mcp_analytics', 'frontend', 'notifications', 'mcpNotificationExamplesLogic']),
loaders({
examples: [
{} as MCPNotificationExamples,
{
loadExamples: async (): Promise<MCPNotificationExamples> => {
const response = (await api.query({
kind: NodeKind.HogQLQuery,
query: EXAMPLES_QUERY,
})) as HogQLQueryResponse
return parseExampleRows(response?.results ?? [])
},
},
],
}),
])