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): resolve AI consent via users/@me for team-scoped tokens (#63302)

Georgiy Tarasovec392ee817c6
diff --git a/services/mcp/src/lib/StateManager.ts b/services/mcp/src/lib/StateManager.ts
index 83fcb93303d9..24c1ad656ed7 100644
--- a/services/mcp/src/lib/StateManager.ts
+++ b/services/mcp/src/lib/StateManager.ts
@@ -128,7 +128,10 @@ export class StateManager {
// with no `current_organization` / `current_team` (newly provisioned
// accounts, users who left their last org) — fall through to the
// scoped-org fallback below when either is missing.
- if (activeOrganization && (scoped_organizations.length === 0 || scoped_organizations.includes(activeOrganization.id))) {
+ if (
+ activeOrganization &&
+ (scoped_organizations.length === 0 || scoped_organizations.includes(activeOrganization.id))
+ ) {
return activeTeam
? { organizationId: activeOrganization.id, projectId: activeTeam.id }
: { organizationId: activeOrganization.id }
@@ -386,11 +389,25 @@ export class StateManager {
async getAiConsentGiven(): Promise<boolean | undefined> {
try {
const org = await this.getCachedOrFetchOrg()
- if (!org) {
- return undefined
+ if (org) {
+ const consent = (org as { is_ai_data_processing_approved?: boolean | null })
+ .is_ai_data_processing_approved
+ return !!consent
}
- const consent = (org as { is_ai_data_processing_approved?: boolean | null }).is_ai_data_processing_approved
- return !!consent
+
+ // Team-scoped tokens (e.g. sandbox OAuth tokens) can never fetch
+ // `/api/organizations/{id}/` — see the guard in getCachedOrFetchOrg.
+ // But `/api/users/@me/` is exempt from team scoping and embeds the
+ // full org serializer (including the consent flag) for the user's
+ // *current* org. That org isn't necessarily the one owning the
+ // scoped project, so only trust the flag when it matches the active
+ // project's owning org; otherwise stay undefined so callers keep
+ // failing closed.
+ const [user, project] = await Promise.all([this.getCachedOrFetchUser(), this.getCachedOrFetchProject()])
+ if (user?.organization && project?.organization === user.organization.id) {
+ return !!user.organization.is_ai_data_processing_approved
+ }
+ return undefined
} catch {
return undefined
}
diff --git a/services/mcp/src/schema/api.ts b/services/mcp/src/schema/api.ts
index ad1e5e6573be..0bd0473b9830 100644
--- a/services/mcp/src/schema/api.ts
+++ b/services/mcp/src/schema/api.ts
@@ -22,6 +22,10 @@ export interface ApiUser {
organization: {
id: string
name: string
+ // `/api/users/@me/` embeds the full OrganizationSerializer for the
+ // current org, so the consent flag is always present on the wire; it is
+ // optional here because some tests construct partial users.
+ is_ai_data_processing_approved?: boolean | null
} | null
}
diff --git a/services/mcp/tests/unit/StateManager.test.ts b/services/mcp/tests/unit/StateManager.test.ts
index 1a5e0dc76e1a..3a6459e1105c 100644
--- a/services/mcp/tests/unit/StateManager.test.ts
+++ b/services/mcp/tests/unit/StateManager.test.ts
@@ -493,6 +493,102 @@ describe('StateManager', () => {
)
})
+ describe('getAiConsentGiven', () => {
+ it.each([
+ [true, true],
+ [false, false],
+ [null, false],
+ ])('returns consent from the fetched org when the org is resolvable (flag %s)', async (flag, expected) => {
+ vi.spyOn(stateManager, 'getCachedOrFetchOrg').mockResolvedValue({
+ id: 'org-1',
+ name: 'Org 1',
+ is_ai_data_processing_approved: flag,
+ } as any)
+ const userSpy = vi.spyOn(stateManager, 'getCachedOrFetchUser')
+
+ const result = await stateManager.getAiConsentGiven()
+
+ expect(result).toBe(expected)
+ expect(userSpy).not.toHaveBeenCalled()
+ })
+
+ it.each([
+ [true, true],
+ [false, false],
+ ])(
+ 'falls back to users/@me consent when the org is unreachable and the current org owns the active project (flag %s)',
+ async (flag, expected) => {
+ // Team-scoped tokens (e.g. sandbox OAuth tokens) can never fetch
+ // `/api/organizations/{id}/`, so getCachedOrFetchOrg yields undefined.
+ vi.spyOn(stateManager, 'getCachedOrFetchOrg').mockResolvedValue(undefined)
+ vi.spyOn(stateManager, 'getCachedOrFetchUser').mockResolvedValue({
+ ...mockUser,
+ organization: { id: 'org-1', name: 'Org 1', is_ai_data_processing_approved: flag },
+ })
+ vi.spyOn(stateManager, 'getCachedOrFetchProject').mockResolvedValue({
+ id: 456,
+ organization: 'org-1',
+ } as any)
+
+ const result = await stateManager.getAiConsentGiven()
+
+ expect(result).toBe(expected)
+ }
+ )
+
+ it("returns undefined when the user's current org does not own the active project", async () => {
+ vi.spyOn(stateManager, 'getCachedOrFetchOrg').mockResolvedValue(undefined)
+ vi.spyOn(stateManager, 'getCachedOrFetchUser').mockResolvedValue({
+ ...mockUser,
+ organization: { id: 'org-other', name: 'Other Org', is_ai_data_processing_approved: true },
+ })
+ vi.spyOn(stateManager, 'getCachedOrFetchProject').mockResolvedValue({
+ id: 456,
+ organization: 'org-1',
+ } as any)
+
+ const result = await stateManager.getAiConsentGiven()
+
+ expect(result).toBeUndefined()
+ })
+
+ it('returns undefined when the user has no current org', async () => {
+ vi.spyOn(stateManager, 'getCachedOrFetchOrg').mockResolvedValue(undefined)
+ vi.spyOn(stateManager, 'getCachedOrFetchUser').mockResolvedValue({ ...mockUser, organization: null })
+ vi.spyOn(stateManager, 'getCachedOrFetchProject').mockResolvedValue({
+ id: 456,
+ organization: 'org-1',
+ } as any)
+
+ const result = await stateManager.getAiConsentGiven()
+
+ expect(result).toBeUndefined()
+ })
+
+ it('returns undefined when no project is resolvable', async () => {
+ vi.spyOn(stateManager, 'getCachedOrFetchOrg').mockResolvedValue(undefined)
+ vi.spyOn(stateManager, 'getCachedOrFetchUser').mockResolvedValue({
+ ...mockUser,
+ organization: { id: 'org-1', name: 'Org 1', is_ai_data_processing_approved: true },
+ })
+ vi.spyOn(stateManager, 'getCachedOrFetchProject').mockResolvedValue(undefined)
+
+ const result = await stateManager.getAiConsentGiven()
+
+ expect(result).toBeUndefined()
+ })
+
+ it('returns undefined (fail closed) when the fallback fetches throw', async () => {
+ vi.spyOn(stateManager, 'getCachedOrFetchOrg').mockResolvedValue(undefined)
+ vi.spyOn(stateManager, 'getCachedOrFetchUser').mockRejectedValue(new Error('boom'))
+ vi.spyOn(stateManager, 'getCachedOrFetchProject').mockResolvedValue(undefined)
+
+ const result = await stateManager.getAiConsentGiven()
+
+ expect(result).toBeUndefined()
+ })
+ })
+
describe('getProjectId', () => {
it('should return cached projectId if available', async () => {
await cache.set('projectId', 'cached-project-id')