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.

fix(mcp): stamp canonical error type on CLI tool-call events (#78967)

Lucas Ricoy81159141fcae
diff --git a/services/mcp/src/cli/tool-call-properties.ts b/services/mcp/src/cli/tool-call-properties.ts
index d9392a67fb9b..344209bff0b0 100644
--- a/services/mcp/src/cli/tool-call-properties.ts
+++ b/services/mcp/src/cli/tool-call-properties.ts
@@ -24,8 +24,10 @@ export function buildToolCallProperties(
output_format: properties.output_format,
...(category ? { $mcp_tool_category: category } : {}),
...(description ? { $mcp_tool_description: description } : {}),
- ...(properties.success ? {} : { error_class: errorClass(properties) }),
- ...(properties.error_status !== undefined ? { error_status: properties.error_status } : {}),
+ ...(properties.success ? {} : { error_class: errorClass(properties), $mcp_error_type: errorType(properties) }),
+ ...(properties.error_status !== undefined
+ ? { error_status: properties.error_status, $mcp_error_status: properties.error_status }
+ : {}),
}
}
@@ -38,3 +40,27 @@ function errorClass(properties: ExecInnerCallProperties): 'validation_error' | '
}
return 'error'
}
+
+/**
+ * The hosted server's `$mcp_error_type` vocabulary, derived from the little the
+ * CLI records (a validation flag and an HTTP status) — without it, CLI failures
+ * land in the analytics tools' untyped bucket and can't be broken down by reason.
+ */
+function errorType(
+ properties: ExecInnerCallProperties
+): 'validation' | 'permission' | 'rate_limited' | 'api_4xx' | 'api_5xx' | 'internal' {
+ if (properties.validation_error) {
+ return 'validation'
+ }
+ const status = properties.error_status
+ if (status === undefined) {
+ return 'internal'
+ }
+ if (status === 401 || status === 403) {
+ return 'permission'
+ }
+ if (status === 429) {
+ return 'rate_limited'
+ }
+ return status >= 500 ? 'api_5xx' : 'api_4xx'
+}
diff --git a/services/mcp/tests/cli/tool-call-properties.test.ts b/services/mcp/tests/cli/tool-call-properties.test.ts
index 5769deed9549..7efd73780301 100644
--- a/services/mcp/tests/cli/tool-call-properties.test.ts
+++ b/services/mcp/tests/cli/tool-call-properties.test.ts
@@ -34,13 +34,40 @@ describe('buildToolCallProperties', () => {
$mcp_is_error: true,
output_format: 'text',
error_class: 'error',
+ $mcp_error_type: 'internal',
...catalogMetadata('notebook-edit'),
})
})
+ // `$mcp_error_type`/`$mcp_error_status` mirror the hosted server's vocabulary so
+ // the MCP analytics failure tools can bucket CLI errors instead of showing them
+ // as typeless.
it.each([
- ['schema rejection', { validation_error: true }, { error_class: 'validation_error' }],
- ['typed API failure', { error_status: 429 }, { error_class: 'api_error', error_status: 429 }],
+ [
+ 'schema rejection',
+ { validation_error: true },
+ { error_class: 'validation_error', $mcp_error_type: 'validation' },
+ ],
+ [
+ 'rate-limited API failure',
+ { error_status: 429 },
+ { error_class: 'api_error', error_status: 429, $mcp_error_type: 'rate_limited', $mcp_error_status: 429 },
+ ],
+ [
+ 'permission API failure',
+ { error_status: 403 },
+ { error_class: 'api_error', error_status: 403, $mcp_error_type: 'permission', $mcp_error_status: 403 },
+ ],
+ [
+ 'client API failure',
+ { error_status: 404 },
+ { error_class: 'api_error', error_status: 404, $mcp_error_type: 'api_4xx', $mcp_error_status: 404 },
+ ],
+ [
+ 'server API failure',
+ { error_status: 502 },
+ { error_class: 'api_error', error_status: 502, $mcp_error_type: 'api_5xx', $mcp_error_status: 502 },
+ ],
])('classifies a %s without carrying the message', (_name, extra, expected) => {
expect(buildToolCallProperties('feature-flag-get-all', { ...base, ...extra })).toEqual({
tool_name: 'feature-flag-get-all',