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): accept id alias for switch-organization orgId (#71324)

posthog[bot]8651fdf27398
diff --git a/services/mcp/schema/tool-inputs.json b/services/mcp/schema/tool-inputs.json
index 3b1c0dc15d91..6853937f299b 100644
--- a/services/mcp/schema/tool-inputs.json
+++ b/services/mcp/schema/tool-inputs.json
@@ -510,13 +510,28 @@
"required": ["insightId"]
},
"OrganizationSetActiveSchema": {
- "type": "object",
- "properties": {
- "orgId": {
- "type": "string"
+ "anyOf": [
+ {
+ "type": "object",
+ "properties": {
+ "orgId": {
+ "type": "string",
+ "description": "The organization to switch to: the `id` returned by `organizations-list` (a UUID-like string, not the organization name). Use `organizations-list` to resolve a name to its id."
+ }
+ },
+ "required": ["orgId"]
+ },
+ {
+ "type": "object",
+ "properties": {
+ "id": {
+ "type": "string",
+ "description": "Alias for `orgId`. Accepts the `id` returned by `organizations-list` / `organization-get`."
+ }
+ },
+ "required": ["id"]
}
- },
- "required": ["orgId"]
+ ]
},
"PathCleaningRulesUpdateSchema": {
"type": "object",
diff --git a/services/mcp/src/schema/tool-inputs.ts b/services/mcp/src/schema/tool-inputs.ts
index 554b28bdd655..7f76dcde34ae 100644
--- a/services/mcp/src/schema/tool-inputs.ts
+++ b/services/mcp/src/schema/tool-inputs.ts
@@ -312,9 +312,34 @@ export const AIObservabilityGetCostsSchema = z.object({
days: z.number().optional(),
})
-export const OrganizationSetActiveSchema = z.object({
- orgId: z.string(),
-})
+// Accept both `orgId` and its `id` alias. `organizations-list` and
+// `organization-get` return/accept the organization under an `id` key, so agents
+// naturally reach for `id` here too. Requiring `orgId` alone made this the odd
+// tool out and drove a stream of validation failures in exec mode. Modeled as a
+// union so the advertised JSON schema expresses "exactly one identifier is
+// required" (anyOf) rather than silently allowing an empty object; normalized to
+// `orgId` so the handler stays simple.
+export const OrganizationSetActiveSchema = z
+ .union(
+ [
+ z.object({
+ orgId: z
+ .string()
+ .describe(
+ 'The organization to switch to: the `id` returned by `organizations-list` (a UUID-like string, not the organization name). Use `organizations-list` to resolve a name to its id.'
+ ),
+ }),
+ z.object({
+ id: z
+ .string()
+ .describe(
+ 'Alias for `orgId`. Accepts the `id` returned by `organizations-list` / `organization-get`.'
+ ),
+ }),
+ ],
+ { error: () => 'provide the organization id via "orgId" (get it from organizations-list)' }
+ )
+ .transform((data) => ({ orgId: 'orgId' in data ? data.orgId : data.id }))
export const ProjectGetAllSchema = z.object({})
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/switch-organization.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/switch-organization.json
index 4e7f865c54e2..2c5447f80ba0 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/switch-organization.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/switch-organization.json
@@ -1,10 +1,25 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
- "properties": {
- "orgId": {
- "type": "string"
+ "anyOf": [
+ {
+ "properties": {
+ "orgId": {
+ "description": "The organization to switch to: the `id` returned by `organizations-list` (a UUID-like string, not the organization name). Use `organizations-list` to resolve a name to its id.",
+ "type": "string"
+ }
+ },
+ "required": ["orgId"],
+ "type": "object"
+ },
+ {
+ "properties": {
+ "id": {
+ "description": "Alias for `orgId`. Accepts the `id` returned by `organizations-list` / `organization-get`.",
+ "type": "string"
+ }
+ },
+ "required": ["id"],
+ "type": "object"
}
- },
- "required": ["orgId"],
- "type": "object"
+ ]
}
diff --git a/services/mcp/tests/unit/organization-set-active-schema.test.ts b/services/mcp/tests/unit/organization-set-active-schema.test.ts
new file mode 100644
index 000000000000..fd0d89c2fd0f
--- /dev/null
+++ b/services/mcp/tests/unit/organization-set-active-schema.test.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from 'vitest'
+
+import { OrganizationSetActiveSchema } from '@/schema/tool-inputs'
+
+describe('OrganizationSetActiveSchema', () => {
+ it.each([
+ ['orgId', { orgId: 'org-123' }, 'org-123'],
+ // `organizations-list` / `organization-get` return the org under `id`; agents
+ // reach for it here too, so it must normalize to `orgId`.
+ ['id alias', { id: 'org-456' }, 'org-456'],
+ ['orgId takes precedence over id', { orgId: 'org-123', id: 'org-456' }, 'org-123'],
+ ])('accepts %s and normalizes to orgId', (_label, input, expected) => {
+ const result = OrganizationSetActiveSchema.safeParse(input)
+ expect(result.success).toBe(true)
+ expect(result.data).toEqual({ orgId: expected })
+ })
+
+ it('rejects input with neither orgId nor id', () => {
+ const result = OrganizationSetActiveSchema.safeParse({})
+ expect(result.success).toBe(false)
+ expect(result.error?.issues[0]?.message).toContain('orgId')
+ })
+})