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): default project-get to the active project (#67475)

Lucas Faria7f0c3da29661
diff --git a/services/mcp/definitions/core.yaml b/services/mcp/definitions/core.yaml
index 48447f390819..2ea93206d140 100644
--- a/services/mcp/definitions/core.yaml
+++ b/services/mcp/definitions/core.yaml
@@ -456,10 +456,12 @@ tools:
idempotent: true
title: Get project
description: |
- Retrieve a project and its settings. Pass `@current` as the ID to fetch the caller's active project.
+ Retrieve a project and its settings. Omit the ID to get the caller's active project.
param_overrides:
id:
- description: Project ID, or `@current` to fetch the caller's active project.
+ description: Project ID. If omitted, returns the caller's active project.
+ optional: true
+ fallback: projectId
cast: string-int
response:
exclude:
diff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.json
index 1c34df2f25e1..105562a0b1a4 100644
--- a/services/mcp/schema/generated-tool-definitions.json
+++ b/services/mcp/schema/generated-tool-definitions.json
@@ -5869,7 +5869,7 @@
}
},
"project-get": {
- "description": "Retrieve a project and its settings. Pass `@current` as the ID to fetch the caller's active project.",
+ "description": "Retrieve a project and its settings. Omit the ID to get the caller's active project.",
"category": "Core",
"feature": "core",
"summary": "Get project",
diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.json
index 9f3e4b065006..d26020d1fcb0 100644
--- a/services/mcp/schema/tool-definitions-all.json
+++ b/services/mcp/schema/tool-definitions-all.json
@@ -6320,7 +6320,7 @@
}
},
"project-get": {
- "description": "Retrieve a project and its settings. Pass `@current` as the ID to fetch the caller's active project.",
+ "description": "Retrieve a project and its settings. Omit the ID to get the caller's active project.",
"category": "Core",
"feature": "core",
"summary": "Get project",
diff --git a/services/mcp/scripts/generate-tools.ts b/services/mcp/scripts/generate-tools.ts
index 15ed54394448..e6c5e11bd033 100644
--- a/services/mcp/scripts/generate-tools.ts
+++ b/services/mcp/scripts/generate-tools.ts
@@ -673,6 +673,14 @@ function composeToolSchema(
paramFallbacks[paramName] = override.fallback
}
+ // An `optional` override must also surface as optional in the agent-facing
+ // JSON Schema. When combined with `cast` the field is wrapped in
+ // `z.preprocess(...)`, which doesn't propagate inner `.optional()` (zod 4
+ // marks it required), so register it here for `wrapWithCast` to re-apply.
+ if (override.optional) {
+ optionalParamNames.add(paramName)
+ }
+
const castHelper = override.cast === 'string-int' ? 'castStringToInt' : null
if (castHelper) {
castHelperImports.add(castHelper)
diff --git a/services/mcp/src/tools/generated/core.ts b/services/mcp/src/tools/generated/core.ts
index 91a77f46d338..3db2c1ae1fb0 100644
--- a/services/mcp/src/tools/generated/core.ts
+++ b/services/mcp/src/tools/generated/core.ts
@@ -200,12 +200,14 @@ const desktopFileSystemRetrieve = (): ToolBase<typeof DesktopFileSystemRetrieveS
})
const ProjectGetSchema = OrganizationsProjectsRetrieveParams.omit({ organization_id: true }).extend({
- id: z.preprocess(
- castStringToInt,
- OrganizationsProjectsRetrieveParams.shape['id'].describe(
- "Project ID, or `@current` to fetch the caller's active project."
+ id: z
+ .preprocess(
+ castStringToInt,
+ OrganizationsProjectsRetrieveParams.shape['id']
+ .describe("Project ID. If omitted, returns the caller's active project.")
+ .optional()
)
- ),
+ .optional(),
})
const projectGet = (): ToolBase<typeof ProjectGetSchema, Schemas.ProjectBackwardCompat> => ({
@@ -213,9 +215,13 @@ const projectGet = (): ToolBase<typeof ProjectGetSchema, Schemas.ProjectBackward
schema: ProjectGetSchema,
handler: async (context: Context, params: z.infer<typeof ProjectGetSchema>) => {
const orgId = await context.stateManager.getOrgID()
+ const id = params.id ?? (await context.stateManager.getProjectId())
+ if (!id) {
+ throw new Error('id is required. Provide it explicitly or set an active project first.')
+ }
const result = await context.api.request<Schemas.ProjectBackwardCompat>({
method: 'GET',
- path: `/api/organizations/${encodeURIComponent(String(orgId))}/projects/${encodeURIComponent(String(params.id))}/`,
+ path: `/api/organizations/${encodeURIComponent(String(orgId))}/projects/${encodeURIComponent(String(id))}/`,
})
const filtered = omitResponseFields(result, [
'secret_api_token',
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/project-get.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/project-get.json
index e77f03804407..5ffa1350b14e 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/project-get.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/project-get.json
@@ -2,12 +2,11 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"properties": {
"id": {
- "description": "Project ID, or `@current` to fetch the caller's active project.",
+ "description": "Project ID. If omitted, returns the caller's active project.",
"maximum": 2147483647,
"minimum": -2147483648,
"type": "number"
}
},
- "required": ["id"],
"type": "object"
}
diff --git a/services/mcp/tests/unit/generate-tools.test.ts b/services/mcp/tests/unit/generate-tools.test.ts
index e337664f802b..a8b0ae2550dc 100644
--- a/services/mcp/tests/unit/generate-tools.test.ts
+++ b/services/mcp/tests/unit/generate-tools.test.ts
@@ -1806,3 +1806,61 @@ describe('generateToolCode with confirmed_action', () => {
expect(result.code).toContain('actionLabel: "Enforce 2FA"')
})
})
+
+describe('optional param with state fallback', () => {
+ const resolved = (): ResolvedOperation =>
+ makeResolved({
+ path: '/api/organizations/{organization_id}/things/{id}/',
+ operation: {
+ operationId: 'things_retrieve',
+ parameters: [
+ { in: 'path', name: 'organization_id', required: true, schema: { type: 'string' } },
+ { in: 'path', name: 'id', required: true, schema: { type: 'integer' } },
+ ],
+ },
+ })
+
+ const config = (): ToolConfig => ({
+ operation: 'things_retrieve',
+ enabled: true,
+ param_overrides: {
+ id: {
+ description: 'Thing ID. If omitted, uses the active project.',
+ optional: true,
+ fallback: 'projectId',
+ cast: 'string-int',
+ },
+ },
+ })
+
+ it('resolves the omitted param from state in the handler', () => {
+ const result = generateToolCode(
+ 'things-retrieve',
+ config(),
+ resolved(),
+ defaultCategory,
+ makeSpec(),
+ new Set<string>(),
+ stubGetQuerySchema
+ )
+ const collapsed = result.code.replace(/\s+/g, ' ')
+ expect(collapsed).toContain('const id = params.id ?? await context.stateManager.getProjectId()')
+ })
+
+ it('surfaces the field as optional despite the cast wrapper (z.preprocess strips inner optionality)', () => {
+ const result = generateToolCode(
+ 'things-retrieve',
+ config(),
+ resolved(),
+ defaultCategory,
+ makeSpec(),
+ new Set<string>(),
+ stubGetQuerySchema
+ )
+ // The outer `.optional()` after the preprocess is what makes the agent-facing
+ // JSON Schema treat the param as optional. Without it, the tool would still
+ // demand the id the fallback exists to supply.
+ const collapsed = result.code.replace(/\s+/g, ' ')
+ expect(collapsed).toContain('.optional()).optional()')
+ })
+})