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): skip org fetch for project-scoped tokens (#60234)

Georgiy Tarasovca900cd9e94d
diff --git a/services/mcp/src/lib/StateManager.ts b/services/mcp/src/lib/StateManager.ts
index a1e4d62a78cd..44ef87ec1522 100644
--- a/services/mcp/src/lib/StateManager.ts
+++ b/services/mcp/src/lib/StateManager.ts
@@ -279,20 +279,20 @@ export class StateManager {
}
async getCachedOrFetchOrg(): Promise<CachedOrg | undefined> {
+ const apiKey = await this.getApiKey()
+ // `/api/organizations/{id}/` is not project-nested. Backend permission
+ // checks reject project-scoped tokens there even when they carry
+ // `organization:read` or `*`, so skip the best-effort fetch entirely.
+ if (apiKey.scoped_teams.length > 0 || !hasScope(apiKey.scopes, 'organization:read')) {
+ return undefined
+ }
+
// Use the non-throwing resolver: callers like `getEnvironmentPrompt` and
// consent checks treat "no org" as "skip", not as a hard error.
const orgId = await this._resolveOrganizationId()
if (!orgId) {
return undefined
}
- // `/api/organizations/{id}/` requires `organization:read`. Project-scoped
- // personal API keys do not carry that scope, so the fetch would 403 on
- // every session init and dogpile error tracking. Mirror the `group:read`
- // gate in `mcp.ts` and skip the fetch when the scope is absent.
- const apiKey = await this.getApiKey()
- if (!hasScope(apiKey.scopes, 'organization:read')) {
- return undefined
- }
return this.getOrFetchCached({
name: 'org',
cacheKey: `cachedOrg:${orgId}` as const,
diff --git a/services/mcp/tests/unit/StateManager.test.ts b/services/mcp/tests/unit/StateManager.test.ts
index 5ddc76d1bf0d..033cc77e71cb 100644
--- a/services/mcp/tests/unit/StateManager.test.ts
+++ b/services/mcp/tests/unit/StateManager.test.ts
@@ -325,6 +325,11 @@ describe('StateManager', () => {
it('returns undefined when no org can be resolved (does not throw)', async () => {
// Preserves the best-effort contract used by getEnvironmentPrompt and
// consent checks: if no org is in scope, the call is a no-op.
+ vi.spyOn(stateManager, 'getApiKey').mockResolvedValue({
+ scopes: ['organization:read'],
+ scoped_organizations: [],
+ scoped_teams: [],
+ })
vi.spyOn(stateManager, 'setDefaultOrganizationAndProject').mockResolvedValue({
organizationId: undefined,
projectId: undefined,
@@ -359,6 +364,48 @@ describe('StateManager', () => {
expect(orgGet).not.toHaveBeenCalled()
})
+ it.each([['organization:read'], ['*']])(
+ 'skips the org fetch for project-scoped API keys even when they carry %s',
+ async (scope) => {
+ await cache.set('orgId', 'org-1')
+ vi.spyOn(stateManager, 'getApiKey').mockResolvedValue({
+ scopes: [scope],
+ scoped_organizations: [],
+ scoped_teams: [456],
+ })
+ const orgGet = vi.fn()
+ ;(stateManager as any)._api = {
+ organizations: () => ({ get: orgGet }),
+ }
+
+ const result = await stateManager.getCachedOrFetchOrg()
+
+ expect(result).toBeUndefined()
+ expect(orgGet).not.toHaveBeenCalled()
+ }
+ )
+
+ it('skips org resolution for project-scoped API keys', async () => {
+ vi.spyOn(stateManager, 'getApiKey').mockResolvedValue({
+ scopes: ['*'],
+ scoped_organizations: [],
+ scoped_teams: [456],
+ })
+ const setDefaultSpy = vi.spyOn(stateManager, 'setDefaultOrganizationAndProject')
+ const getProjectSpy = vi.spyOn(stateManager, 'getCachedOrFetchProject')
+ const orgGet = vi.fn()
+ ;(stateManager as any)._api = {
+ organizations: () => ({ get: orgGet }),
+ }
+
+ const result = await stateManager.getCachedOrFetchOrg()
+
+ expect(result).toBeUndefined()
+ expect(setDefaultSpy).not.toHaveBeenCalled()
+ expect(getProjectSpy).not.toHaveBeenCalled()
+ expect(orgGet).not.toHaveBeenCalled()
+ })
+
it.each([['organization:read'], ['organization:write'], ['*']])(
'fetches the org when the API key carries %s',
async (scope) => {