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): enable CLI mode for all Anthropic clients (#62184)

Georgiy Tarasova62f1f437501
diff --git a/services/mcp/src/hono/request-state-resolver.ts b/services/mcp/src/hono/request-state-resolver.ts
index 3b5d7e830e7c..667f0e239c2b 100644
--- a/services/mcp/src/hono/request-state-resolver.ts
+++ b/services/mcp/src/hono/request-state-resolver.ts
@@ -42,7 +42,7 @@ export function resolveMode(args: { mode: McpMode | undefined; clientProfile: MC
const useSingleExec =
mode === 'cli' ||
(mode !== 'tools' &&
- (clientProfile.isCodingAgent() ||
+ (clientProfile.isCliModeEnabled() ||
clientProfile.isPostHogCodeConsumer() ||
clientProfile.isVibeCodingClient()))
return { mode: mode ?? (useSingleExec ? 'cli' : 'tools'), useSingleExec }
diff --git a/services/mcp/src/hono/tool-executor.ts b/services/mcp/src/hono/tool-executor.ts
index a936bfd83257..f9eb0f21867f 100644
--- a/services/mcp/src/hono/tool-executor.ts
+++ b/services/mcp/src/hono/tool-executor.ts
@@ -137,7 +137,7 @@ export class ToolExecutor {
toolMeta: tool._meta,
toolName: tool.name,
params: validation.data,
- suppressStructuredContentForFormattedResults: state.clientProfile.isCodingAgent(),
+ suppressStructuredContentForFormattedResults: state.clientProfile.isCliModeEnabled(),
distinctId,
})
} catch (error: unknown) {
@@ -179,7 +179,7 @@ export class ToolExecutor {
toolMeta: resolved._meta,
toolName: 'exec',
params: validation.data,
- suppressStructuredContentForFormattedResults: state.clientProfile.isCodingAgent(),
+ suppressStructuredContentForFormattedResults: state.clientProfile.isCliModeEnabled(),
distinctId: undefined,
})
} catch (error: unknown) {
diff --git a/services/mcp/src/lib/client-detection.ts b/services/mcp/src/lib/client-detection.ts
index dc35dc564610..47f86288a1f7 100644
--- a/services/mcp/src/lib/client-detection.ts
+++ b/services/mcp/src/lib/client-detection.ts
@@ -8,9 +8,13 @@
*
* The `MCPClientProfile` class owns all per-client behavior decisions:
*
- * - `isCodingAgent()` matches coding agents that should default to single-exec
- * mode and drop `structuredContent` when a `formatted_results` override is
- * set. Cursor is deliberately excluded — it reads text for the model and
+ * - `isCliModeEnabled()` matches clients that should default to single-exec
+ * ("CLI") mode and drop `structuredContent` when a `formatted_results`
+ * override is set. Every known Anthropic client qualifies — matched against
+ * the `x-anthropic-client` (`vendorClient`) header, since Anthropic pools MCP
+ * transports across all its products and reports the live one there. Other
+ * coding agents are matched by self-reported client name. Cursor is
+ * deliberately excluded from the name match — it reads text for the model and
* renders `structuredContent` in UI, so it does not need single-exec mode or
* the formatted-results workaround.
*
@@ -71,6 +75,15 @@ export const CODING_AGENT_CLIENT_NAME_FRAGMENTS = [
'notion',
] as const
+// Known `x-anthropic-client` (`vendorClient`) header values. Anthropic pools
+// MCP transports across all its products and reports the live one in this
+// header, so it's the reliable identifier for an Anthropic client (the
+// `initialize` body's `clientName` is the pool owner, e.g. `Anthropic/ClaudeAI`).
+// Every Anthropic product runs in CLI (single-exec) mode. Matched as normalized
+// substrings, so vendor-prefixed shapes like `Anthropic/ClaudeAI` resolve to
+// `claudeai`.
+export const ANTHROPIC_CLIENT_NAME_FRAGMENTS = ['claudecode', 'claudeai', 'cowork'] as const
+
// Value sent in `x-posthog-mcp-consumer` by PostHog Code (the Tasks sandbox
// wrapper around the Claude Agent SDK) when the task was launched from the
// PostHog Code UI. Used to force coding-agent behavior and to gate UI-apps
@@ -139,8 +152,16 @@ export class MCPClientProfile {
this.vendorClient = input.vendorClient
}
- isCodingAgent(): boolean {
- return matchesAnyFragment(this.vendorClient ?? this.clientName, CODING_AGENT_CLIENT_NAME_FRAGMENTS)
+ isCliModeEnabled(): boolean {
+ // Every known Anthropic client (matched against the `x-anthropic-client`
+ // header) runs in CLI (single-exec) mode — Anthropic pools MCP transports
+ // across all its products (Claude Code, Claude.ai, Cowork, …) and reports
+ // the live product in that header.
+ if (matchesAnyFragment(this.vendorClient, ANTHROPIC_CLIENT_NAME_FRAGMENTS)) {
+ return true
+ }
+ // Otherwise fall back to the self-reported client name for coding agents.
+ return matchesAnyFragment(this.clientName, CODING_AGENT_CLIENT_NAME_FRAGMENTS)
}
isPostHogCodeConsumer(): boolean {
@@ -169,8 +190,8 @@ export class MCPClientProfile {
}
}
-export function isCodingAgentClient(clientName: string | undefined): boolean {
- return new MCPClientProfile({ clientName }).isCodingAgent()
+export function isCliModeEnabledClient(clientName: string | undefined): boolean {
+ return new MCPClientProfile({ clientName }).isCliModeEnabled()
}
export function isPostHogCodeConsumer(mcpConsumer: string | undefined): boolean {
diff --git a/services/mcp/tests/hono/request-state-resolver.test.ts b/services/mcp/tests/hono/request-state-resolver.test.ts
index 8f6818ec3ff6..b573a05014a3 100644
--- a/services/mcp/tests/hono/request-state-resolver.test.ts
+++ b/services/mcp/tests/hono/request-state-resolver.test.ts
@@ -142,7 +142,7 @@ describe('RequestStateResolver MCP client contexts', () => {
expect(result.requestContext.mcpClientName).toBe('Claude Desktop')
expect(result.sessionContext?.mcpClientName).toBe('claude-code')
expect(result.clientProfile.clientName).toBe('claude-code')
- expect(result.clientProfile.isCodingAgent()).toBe(true)
+ expect(result.clientProfile.isCliModeEnabled()).toBe(true)
})
it('uses cached session client props for instruction capabilities without overwriting request props', async () => {
diff --git a/services/mcp/tests/hono/session-mode-pool.test.ts b/services/mcp/tests/hono/session-mode-pool.test.ts
index 3966bd74beb7..80f636b7d282 100644
--- a/services/mcp/tests/hono/session-mode-pool.test.ts
+++ b/services/mcp/tests/hono/session-mode-pool.test.ts
@@ -136,12 +136,12 @@ describe('Resolved mode is preserved across pooled-transport vendor flips', () =
expect(cachedTools.tools).toHaveLength(1)
expect(cachedTools.tools[0]!.name).toBe('exec')
- // headers_2 — pool member with a non-coding-agent vendor. If the
- // resolver re-derived mode from the live request, `vendorClient='ClaudeAI'`
- // + cached `clientName='Anthropic/ClaudeAI'` both miss the coding-agent
- // fragments -> resolves to tools mode -> a `tools/list` would expose the
- // full multi-tool roster. With the initial vendor hint cached, the request
- // stays in cli mode and the roster still collapses to a single `exec`.
+ // headers_2 — pool member flipping to a different Anthropic vendor.
+ // `ClaudeAI` is itself an Anthropic client, so it resolves to cli mode on
+ // its own merits; combined with the cached session hints from init, the
+ // request stays in cli mode and the roster still collapses to a single
+ // `exec`. Every Anthropic vendor (the `x-anthropic-client` header) runs
+ // in cli mode, so no pooled vendor flip can downgrade to tools mode.
const pooledRoster = await listToolsOnSession(sessionId!, 'ClaudeAI')
expect(pooledRoster.map((t) => t.name)).toEqual(['exec'])
diff --git a/services/mcp/tests/hono/tool-executor-metrics.test.ts b/services/mcp/tests/hono/tool-executor-metrics.test.ts
index 066aebda8afa..9fdc246a44c0 100644
--- a/services/mcp/tests/hono/tool-executor-metrics.test.ts
+++ b/services/mcp/tests/hono/tool-executor-metrics.test.ts
@@ -64,7 +64,7 @@ function makeState(tools: { name: string }[], overrides: Partial<ResolvedState>
apiKeyScopes: [],
clientProfile: {
capabilities: { supportsInstructions: true },
- isCodingAgent: vi.fn(() => false),
+ isCliModeEnabled: vi.fn(() => false),
} as any,
requestContext: {
sessionId: 'sess-1',
diff --git a/services/mcp/tests/hono/tool-executor.test.ts b/services/mcp/tests/hono/tool-executor.test.ts
index f77086bdf499..de88d81c2ef6 100644
--- a/services/mcp/tests/hono/tool-executor.test.ts
+++ b/services/mcp/tests/hono/tool-executor.test.ts
@@ -38,7 +38,7 @@ function makeState(tools: { name: string }[], overrides: Partial<ResolvedState>
apiKeyScopes: [],
clientProfile: {
capabilities: { supportsInstructions: true },
- isCodingAgent: vi.fn(() => false),
+ isCliModeEnabled: vi.fn(() => false),
} as any,
requestContext: {
sessionId: 'sess-1',
diff --git a/services/mcp/tests/unit/client-detection.test.ts b/services/mcp/tests/unit/client-detection.test.ts
index 643e1d93d1f0..044920a003c4 100644
--- a/services/mcp/tests/unit/client-detection.test.ts
+++ b/services/mcp/tests/unit/client-detection.test.ts
@@ -1,17 +1,18 @@
import { describe, expect, it } from 'vitest'
import {
+ ANTHROPIC_CLIENT_NAME_FRAGMENTS,
CODING_AGENT_CLIENT_NAME_FRAGMENTS,
DEFAULT_CLIENT_CAPABILITIES,
MCPClientProfile,
POSTHOG_CODE_CONSUMER,
VIBE_CODING_OAUTH_CLIENT_NAME_FRAGMENTS,
- isCodingAgentClient,
+ isCliModeEnabledClient,
isPostHogCodeConsumer,
isVibeCodingClient,
} from '@/lib/client-detection'
-describe('isCodingAgentClient', () => {
+describe('isCliModeEnabledClient', () => {
describe('detects known coding-agent clients', () => {
it.each([
// Exact names from the fragment list.
@@ -29,7 +30,7 @@ describe('isCodingAgentClient', () => {
['librechat'],
['notion'],
])('returns true for %s', (clientName) => {
- expect(isCodingAgentClient(clientName)).toBe(true)
+ expect(isCliModeEnabledClient(clientName)).toBe(true)
})
it.each([
@@ -52,7 +53,7 @@ describe('isCodingAgentClient', () => {
['Notion'],
['Devin'],
])('returns true for variant %s (case-insensitive substring match)', (clientName) => {
- expect(isCodingAgentClient(clientName)).toBe(true)
+ expect(isCliModeEnabledClient(clientName)).toBe(true)
})
})
@@ -66,7 +67,7 @@ describe('isCodingAgentClient', () => {
['PostHog'],
[''],
])('returns false for %s', (clientName) => {
- expect(isCodingAgentClient(clientName)).toBe(false)
+ expect(isCliModeEnabledClient(clientName)).toBe(false)
})
})
@@ -74,23 +75,23 @@ describe('isCodingAgentClient', () => {
// Cursor sends content[].text to the model and displays structuredContent in UI,
// so the workaround isn't needed. Guard against someone adding it back.
it('returns false for cursor (intentionally excluded)', () => {
- expect(isCodingAgentClient('cursor')).toBe(false)
- expect(isCodingAgentClient('Cursor')).toBe(false)
- expect(isCodingAgentClient('cursor-editor')).toBe(false)
+ expect(isCliModeEnabledClient('cursor')).toBe(false)
+ expect(isCliModeEnabledClient('Cursor')).toBe(false)
+ expect(isCliModeEnabledClient('cursor-editor')).toBe(false)
})
})
describe('edge cases', () => {
it('returns false for undefined', () => {
- expect(isCodingAgentClient(undefined)).toBe(false)
+ expect(isCliModeEnabledClient(undefined)).toBe(false)
})
it('returns false for empty string', () => {
- expect(isCodingAgentClient('')).toBe(false)
+ expect(isCliModeEnabledClient('')).toBe(false)
})
it('treats whitespace-only as non-match', () => {
- expect(isCodingAgentClient(' ')).toBe(false)
+ expect(isCliModeEnabledClient(' ')).toBe(false)
})
})
@@ -101,6 +102,16 @@ describe('isCodingAgentClient', () => {
expect(fragment.length).toBeGreaterThan(0)
}
})
+
+ it('keeps the Anthropic client fragment list non-empty, lowercased, and separator-free', () => {
+ // Fragments are compared against the normalized header value (separators
+ // stripped, lowercased), so they must be pre-normalized to match.
+ expect(ANTHROPIC_CLIENT_NAME_FRAGMENTS.length).toBeGreaterThan(0)
+ for (const fragment of ANTHROPIC_CLIENT_NAME_FRAGMENTS) {
+ expect(fragment).toBe(fragment.toLowerCase().replace(/[-_\s]+/g, ''))
+ expect(fragment.length).toBeGreaterThan(0)
+ }
+ })
})
describe('isPostHogCodeConsumer', () => {
@@ -159,7 +170,7 @@ describe('isVibeCodingClient', () => {
})
describe('MCPClientProfile', () => {
- describe('isCodingAgent()', () => {
+ describe('isCliModeEnabled()', () => {
it.each([
['claude-code'],
['Claude Code'],
@@ -180,43 +191,59 @@ describe('MCPClientProfile', () => {
['libre-chat'],
['notion-mcp-client'],
])('returns true for %s', (clientName) => {
- expect(new MCPClientProfile({ clientName }).isCodingAgent()).toBe(true)
+ expect(new MCPClientProfile({ clientName }).isCliModeEnabled()).toBe(true)
})
it.each([['Claude Desktop'], ['claude-desktop'], ['cursor'], ['mcp-inspector'], [''], [' ']])(
'returns false for %s',
(clientName) => {
- expect(new MCPClientProfile({ clientName }).isCodingAgent()).toBe(false)
+ expect(new MCPClientProfile({ clientName }).isCliModeEnabled()).toBe(false)
}
)
it('returns false when clientName is undefined', () => {
- expect(new MCPClientProfile({}).isCodingAgent()).toBe(false)
+ expect(new MCPClientProfile({}).isCliModeEnabled()).toBe(false)
})
- describe('vendorClient precedence', () => {
- it('prefers vendorClient over clientName for detection', () => {
- // Claude pools MCP transports: the initialize body says
- // `Anthropic/ClaudeAI` (the pool owner) but the live inner
- // client is `ClaudeCode` (a coding agent). The header wins.
+ describe('Anthropic vendor client', () => {
+ it.each([['ClaudeCode'], ['ClaudeAI'], ['Cowork'], ['Anthropic/ClaudeAI']])(
+ 'enables CLI mode for known Anthropic client %s regardless of clientName',
+ (vendorClient) => {
+ // Anthropic pools MCP transports across all its products and
+ // reports the live one in `x-anthropic-client`. Every known
+ // Anthropic client runs in CLI mode, even when the initialize
+ // body's clientName looks non-coding (e.g. the pool owner).
+ expect(
+ new MCPClientProfile({ clientName: 'Claude Desktop', vendorClient }).isCliModeEnabled()
+ ).toBe(true)
+ }
+ )
+
+ it('does not enable CLI mode for an unknown vendorClient value', () => {
+ // Detection matches the known-header list, not mere presence.
expect(
new MCPClientProfile({
- clientName: 'Anthropic/ClaudeAI',
- vendorClient: 'ClaudeCode',
- }).isCodingAgent()
+ clientName: 'Claude Desktop',
+ vendorClient: 'SomeUnknownClient',
+ }).isCliModeEnabled()
+ ).toBe(false)
+ })
+
+ it('falls back to clientName for coding agents when vendorClient is unknown', () => {
+ expect(
+ new MCPClientProfile({
+ clientName: 'claude-code',
+ vendorClient: 'SomeUnknownClient',
+ }).isCliModeEnabled()
).toBe(true)
})
it('falls back to clientName when vendorClient is missing', () => {
- expect(new MCPClientProfile({ clientName: 'claude-code' }).isCodingAgent()).toBe(true)
+ expect(new MCPClientProfile({ clientName: 'claude-code' }).isCliModeEnabled()).toBe(true)
})
- it('treats coding-agent clientName as non-coding when vendorClient says otherwise', () => {
- // Inverse case: if a pool of coding-agent clients somehow
- // carries a non-coding request, the live header rules.
- expect(
- new MCPClientProfile({ clientName: 'claude-code', vendorClient: 'ClaudeAI' }).isCodingAgent()
- ).toBe(false)
+ it('uses clientName for non-Anthropic clients (no vendorClient)', () => {
+ expect(new MCPClientProfile({ clientName: 'Claude Desktop' }).isCliModeEnabled()).toBe(false)
})
})
})