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): return invalid api path rejections to the agent (#119202)

posthog[bot]718b3102c663
diff --git a/services/mcp/src/api/client.ts b/services/mcp/src/api/client.ts
index 7623bc0c5859..8d81a87ed464 100644
--- a/services/mcp/src/api/client.ts
+++ b/services/mcp/src/api/client.ts
@@ -13,6 +13,7 @@ import {
PostHogRateLimitError,
PostHogTransportError,
PostHogValidationError,
+ ToolInputValidationError,
} from '@/lib/errors'
import { getSearchParamsFromRecord, sanitizeHeaders, sanitizeHeaderValue } from '@/lib/utils.js'
import type {
@@ -365,7 +366,9 @@ export class ApiClient {
pathname.includes('//') ||
new URL(pathname, this.baseUrl).pathname !== pathname
) {
- throw new Error('Invalid API path')
+ throw new ToolInputValidationError(
+ 'Invalid API path: a path parameter is empty, ".", or "..". Send a valid resource id and try again.'
+ )
}
const searchParams = new URLSearchParams()
if (opts.query) {
diff --git a/services/mcp/tests/unit/api-client.test.ts b/services/mcp/tests/unit/api-client.test.ts
index 45df49b6684c..9dd83fd7e3b8 100644
--- a/services/mcp/tests/unit/api-client.test.ts
+++ b/services/mcp/tests/unit/api-client.test.ts
@@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ApiClient } from '@/api/client'
import { MCP_CLIENT_IP_HEADERS, verifySignedClientIp } from '@/lib/client-ip-signature'
import { USER_AGENT, getUserAgent } from '@/lib/constants'
-import { PostHogApiError, PostHogTransportError } from '@/lib/errors'
+import { PostHogApiError, PostHogTransportError, ToolInputValidationError } from '@/lib/errors'
import { getToolByName } from '@/shared/test-utils'
import { GENERATED_TOOLS as PROXY_TOOLS } from '@/tools/generated/proxy-records'
import { GENERATED_TOOLS } from '@/tools/generated/skills'
@@ -17,7 +17,9 @@ describe('ApiClient', () => {
const context = { api: client, stateManager: { getOrgID: async () => 'example' } } as unknown as Context
const tool = getToolByName(PROXY_TOOLS, 'proxy-delete')
try {
- await expect(tool.handler(context, tool.schema.parse({ id: '..' }))).rejects.toThrow('Invalid API path')
+ await expect(tool.handler(context, tool.schema.parse({ id: '..' }))).rejects.toThrow(
+ ToolInputValidationError
+ )
expect(send).not.toHaveBeenCalled()
} finally {
vi.unstubAllGlobals()
@@ -33,7 +35,7 @@ describe('ApiClient', () => {
try {
await expect(
client.request({ method: 'DELETE', path: `/api/organizations/example/proxy_records/${id}/` })
- ).rejects.toThrow('Invalid API path')
+ ).rejects.toThrow(ToolInputValidationError)
expect(send).not.toHaveBeenCalled()
} finally {
vi.unstubAllGlobals()