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): keep switch-organization available when only a project is pinned (#73023)

posthog[bot]6b6756071a19
diff --git a/services/mcp/src/hono/request-state-resolver.ts b/services/mcp/src/hono/request-state-resolver.ts
index 2dd0901e78e5..ac53b607a58b 100644
--- a/services/mcp/src/hono/request-state-resolver.ts
+++ b/services/mcp/src/hono/request-state-resolver.ts
@@ -62,6 +62,29 @@ export function resolveMode(args: { mode: McpMode | undefined; clientProfile: MC
return { mode: resolved, useSingleExec: resolved === 'cli' }
}
+/**
+ * Which navigation switch tools to hide given the context the client explicitly
+ * pinned via request params.
+ *
+ * Pinning fixes the *default* active context — it must not disable navigation.
+ * Only an explicitly pinned organization is a hard lock: the client asked to
+ * operate inside one org, so `switch-organization` is dropped while project
+ * switching stays available. A pinned *project* excludes nothing, because the
+ * documented cross-org flow depends on it: from an active project an agent
+ * resolves an org via `organizations-get`, calls `switch-organization`, then
+ * `switch-project` to reach a project in another organization (see the
+ * `switch-project` tool description). Excluding the switch tools on a project
+ * pin — which nearly every connection sends — made that flow impossible.
+ *
+ * Note this only affects keys that can act across orgs. A project-scoped key
+ * (`scoped_teams`) never sees `switch-organization` regardless, because
+ * `getToolsForFeatures` independently strips every `organization:*` tool the
+ * backend would 403 for such a token.
+ */
+export function switchToolsToExclude(pinned: { organizationId?: string | undefined }): string[] {
+ return pinned.organizationId ? ['switch-organization'] : []
+}
+
// ─── Resolver ───
const SESSION_CONTEXT_KEYS = [
@@ -159,12 +182,7 @@ export class RequestStateResolver {
const availableFeatures = await context.stateManager.getAvailableFeatures()
const isCloud = isCloudApi()
- const excludeTools: string[] = []
- if (projectId) {
- excludeTools.push('switch-organization', 'switch-project')
- } else if (organizationId) {
- excludeTools.push('switch-organization')
- }
+ const excludeTools = switchToolsToExclude({ organizationId })
const filterOptions = {
features,
diff --git a/services/mcp/tests/unit/switch-tools-to-exclude.test.ts b/services/mcp/tests/unit/switch-tools-to-exclude.test.ts
new file mode 100644
index 000000000000..cea3f5fe2e8b
--- /dev/null
+++ b/services/mcp/tests/unit/switch-tools-to-exclude.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from 'vitest'
+
+import { switchToolsToExclude } from '@/hono/request-state-resolver'
+
+describe('switchToolsToExclude', () => {
+ const cases = [
+ {
+ description: 'excludes nothing when nothing is pinned',
+ pinned: {},
+ expected: [],
+ },
+ {
+ description: 'excludes switch-organization when an organization is pinned',
+ pinned: { organizationId: 'org-1' },
+ expected: ['switch-organization'],
+ },
+ {
+ // The regression guard: a pinned project must NOT drop the switch tools,
+ // or the documented cross-org flow (organizations-get → switch-organization
+ // → switch-project) becomes impossible from an active project.
+ description: 'excludes nothing when only a project is pinned',
+ pinned: { projectId: '2' } as { organizationId?: string; projectId?: string },
+ expected: [],
+ },
+ {
+ description: 'keeps project switching available when both are pinned',
+ pinned: { organizationId: 'org-1', projectId: '2' },
+ expected: ['switch-organization'],
+ },
+ ]
+
+ it.each(cases)('$description', ({ pinned, expected }) => {
+ expect(switchToolsToExclude(pinned)).toEqual(expected)
+ })
+})