: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mcp): read ai consent from the project for scoped tokens (#129516)
Adam Bowker44829da3e8ab
diff --git a/frontend/src/generated/core/api.schemas.ts b/frontend/src/generated/core/api.schemas.tsindex eedf13c1c01a..6de4ac373b98 100644--- a/frontend/src/generated/core/api.schemas.ts+++ b/frontend/src/generated/core/api.schemas.ts@@ -2948,6 +2948,11 @@ export const FlagEvaluationsModeEnumApi = {export interface ProjectBackwardCompatApi {readonly id: numberreadonly organization: string+ /**+ * Whether the project's organization approved AI data processing. AI features need this approval.+ * @nullable+ */+ readonly is_ai_data_processing_approved: boolean | null/*** Project name. Must be unique within the organization (case-insensitive). If omitted on creation, a unique default name is generated.* @minLength 1@@ -3839,6 +3844,11 @@ export type PatchedProjectBackwardCompatApiManagedViewsets = { [key: string]: boexport interface PatchedProjectBackwardCompatApi {readonly id?: numberreadonly organization?: string+ /**+ * Whether the project's organization approved AI data processing. AI features need this approval.+ * @nullable+ */+ readonly is_ai_data_processing_approved?: boolean | null/*** Project name. Must be unique within the organization (case-insensitive). If omitted on creation, a unique default name is generated.* @minLength 1diff --git a/posthog/api/project.py b/posthog/api/project.pyindex e504e785d753..1ca419f82dd1 100644--- a/posthog/api/project.py+++ b/posthog/api/project.py@@ -657,6 +657,12 @@ class ProjectBackwardCompatSerializer("everyone else and when none has been generated."),) # Compat with TeamSerializer+ is_ai_data_processing_approved = serializers.BooleanField(+ source="organization.is_ai_data_processing_approved",+ read_only=True,+ allow_null=True,+ help_text="Whether the project's organization approved AI data processing. AI features need this approval.",+ )# project_id mirrors TeamSerializer.project_id; for a Project it equals its own id (Project ↔ Team is 1:1)project_id = serializers.IntegerField(source="id", read_only=True, help_text="ID of the project this environment belongs to."@@ -736,6 +742,7 @@ class ProjectBackwardCompatSerializer(fields: tuple[str, ...] = ("id","organization",+ "is_ai_data_processing_approved","name","logo_media_id","product_description",diff --git a/posthog/api/test/test_project.py b/posthog/api/test/test_project.pyindex 24b732bbab96..78ebf2765a6e 100644--- a/posthog/api/test/test_project.py+++ b/posthog/api/test/test_project.py@@ -84,6 +84,26 @@ class TestProjectAPI(team_api_test_factory()): # type: ignore"Only the project belonging to the scoped organization should be listed, the other one should be excluded",)+ @parameterized.expand([(True,), (False,)])+ def test_project_scoped_key_reads_organization_ai_consent(self, approved: bool) -> None:+ self.organization.is_ai_data_processing_approved = approved+ self.organization.save()+ personal_api_key = generate_random_token_personal()+ PersonalAPIKey.objects.create(+ label="X",+ user=self.user,+ secure_value=hash_key_value(personal_api_key),+ scoped_teams=[self.team.id],+ scopes=["project:read"],+ )++ response = self.client.get(+ f"/api/projects/{self.project.id}/", headers={"authorization": f"Bearer {personal_api_key}"}+ )++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertIs(response.json()["is_ai_data_processing_approved"], approved)+@parameterized.expand([("exact_match", "Hedgebox"),diff --git a/posthog/api/test/test_team_project_parity.py b/posthog/api/test/test_team_project_parity.pyindex 69621d768dda..3bc07b0d600f 100644--- a/posthog/api/test/test_team_project_parity.py+++ b/posthog/api/test/test_team_project_parity.py@@ -28,6 +28,7 @@ PROJECT_ONLY_SERIALIZER_FIELDS = {"deletion_scheduled_at","tags","logo_media_id",+ "is_ai_data_processing_approved",}# Actions that legitimately exist only on the project surface (operate on the Project, not the Team).diff --git a/products/desktop/packages/api-client/src/generated.ts b/products/desktop/packages/api-client/src/generated.tsindex 69abf148d872..2b0e06f463ae 100644--- a/products/desktop/packages/api-client/src/generated.ts+++ b/products/desktop/packages/api-client/src/generated.ts@@ -16155,6 +16155,10 @@ export namespace Schemas {export type ProjectBackwardCompat = {id: number;organization: string;+ /**+ * Whether the project's organization approved AI data processing. AI features need this approval.+ */+ is_ai_data_processing_approved: boolean | null;/*** A project and its settings, including the settings that live on its passthrough Team.*diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 128d54c3b96a..1586a1f165b6 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -84545,6 +84545,11 @@ export namespace Schemas {export interface PatchedProjectBackwardCompat {readonly id?: number;readonly organization?: string;+ /**+ * Whether the project's organization approved AI data processing. AI features need this approval.+ * @nullable+ */+ readonly is_ai_data_processing_approved?: boolean | null;/*** Project name. Must be unique within the organization (case-insensitive). If omitted on creation, a unique default name is generated.* @minLength 1@@ -90614,6 +90619,11 @@ export namespace Schemas {export interface ProjectBackwardCompat {readonly id: number;readonly organization: string;+ /**+ * Whether the project's organization approved AI data processing. AI features need this approval.+ * @nullable+ */+ readonly is_ai_data_processing_approved: boolean | null;/*** Project name. Must be unique within the organization (case-insensitive). If omitted on creation, a unique default name is generated.* @minLength 1diff --git a/services/mcp/src/lib/StateManager.ts b/services/mcp/src/lib/StateManager.tsindex dd2dd8bda9fe..2482f1ef6bbd 100644--- a/services/mcp/src/lib/StateManager.ts+++ b/services/mcp/src/lib/StateManager.ts@@ -739,6 +739,10 @@ export class StateManager {}async getAiConsentGiven(): Promise<boolean | undefined> {+ const project = await this.getCachedOrFetchProject().catch(() => undefined)+ if (project?.is_ai_data_processing_approved !== undefined) {+ return !!project.is_ai_data_processing_approved+ }return this.getOrgField((org) => !!org.is_ai_data_processing_approved)}diff --git a/services/mcp/tests/unit/StateManager.test.ts b/services/mcp/tests/unit/StateManager.test.tsindex 00339db7e095..99e96c0063d9 100644--- a/services/mcp/tests/unit/StateManager.test.ts+++ b/services/mcp/tests/unit/StateManager.test.ts@@ -994,6 +994,27 @@ describe('StateManager', () => {})describe('getAiConsentGiven', () => {+ it.each([+ [true, true],+ [false, false],+ [null, false],+ ])('reads consent from the project when the org is unreachable (flag %s)', async (flag, expected) => {+ vi.spyOn(stateManager, 'getCachedOrFetchOrg').mockResolvedValue(undefined)+ vi.spyOn(stateManager, 'getCachedOrFetchUser').mockResolvedValue({+ ...mockUser,+ organization: { id: 'org-other', name: 'Other Org', is_ai_data_processing_approved: !expected },+ })+ vi.spyOn(stateManager, 'getCachedOrFetchProject').mockResolvedValue({+ id: 456,+ organization: 'org-1',+ is_ai_data_processing_approved: flag,+ } as any)++ const result = await stateManager.getAiConsentGiven()++ expect(result).toBe(expected)+ })+it.each([[true, true],[false, false],