: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): hide org-only tools for project-scoped tokens (#59957)
Alex Lider0a3009354466
diff --git a/services/mcp/src/hono/request-state-resolver.ts b/services/mcp/src/hono/request-state-resolver.tsindex 23b7b9550018..2be14c131571 100644--- a/services/mcp/src/hono/request-state-resolver.ts+++ b/services/mcp/src/hono/request-state-resolver.ts@@ -137,6 +137,7 @@ export class RequestStateResolver {}const apiKeyScopes = _apiKey?.scopes ?? []+ const apiKeyScopedTeams = _apiKey?.scoped_teams ?? []const aiConsentGiven = await context.stateManager.getAiConsentGiven()const excludeTools: string[] = []@@ -154,6 +155,7 @@ export class RequestStateResolver {readOnly,featureFlags: toolFeatureFlags,scopes: apiKeyScopes,+ scopedTeams: apiKeyScopedTeams,aiConsentGiven: aiConsentGiven ?? undefined,})diff --git a/services/mcp/src/tools/toolDefinitions.ts b/services/mcp/src/tools/toolDefinitions.tsindex 4d7c1046646d..543d2c3075ad 100644--- a/services/mcp/src/tools/toolDefinitions.ts+++ b/services/mcp/src/tools/toolDefinitions.ts@@ -94,6 +94,12 @@ export interface ToolFilterOptions {* Used to gate tools that declare `feature_flag` in their YAML config.*/featureFlags?: Record<string, boolean> | undefined+ /**+ * Project IDs the token is restricted to (`scoped_teams` on the API key).+ * When set, the backend 403s any org-level endpoint, so we drop tools that+ * need `organization:*` scopes — they'd fail anyway.+ */+ scopedTeams?: number[] | undefined}/**@@ -116,7 +122,7 @@ function normalizeFeatureName(name: string): string {}export function getToolsForFeatures(options?: ToolFilterOptions): string[] {- const { features, tools, version, readOnly, aiConsentGiven, featureFlags } = options || {}+ const { features, tools, version, readOnly, aiConsentGiven, featureFlags, scopedTeams } = options || {}const toolDefinitions = getToolDefinitions(version)let entries = Object.entries(toolDefinitions)@@ -186,5 +192,13 @@ export function getToolsForFeatures(options?: ToolFilterOptions): string[] {})}+ // Hide tools that need org-level access when the session's token is+ // project-scoped - the backend would 403 them+ if (scopedTeams && scopedTeams.length > 0) {+ entries = entries.filter(+ ([_, definition]) => !(definition.required_scopes ?? []).some((scope) => scope.startsWith('organization'))+ )+ }+return entries.map(([toolName, _]) => toolName)}diff --git a/services/mcp/tests/unit/tool-filtering.test.ts b/services/mcp/tests/unit/tool-filtering.test.tsindex f885e3a8fea3..148d3df10a52 100644--- a/services/mcp/tests/unit/tool-filtering.test.ts+++ b/services/mcp/tests/unit/tool-filtering.test.ts@@ -521,6 +521,45 @@ describe('Tool Filtering - AI Consent', () => {})})+describe('Tool Filtering - Scoped Teams', () => {+ // Tools whose `required_scopes` include an `organization:*` (or+ // `organization_member:*`, etc.) scope can't be exercised with a+ // project-scoped key — the backend 403s them. Hide from `tools/list` for+ // sessions whose token carries `scoped_teams`, surface them otherwise.+ it('hides tools requiring an org-scoped scope when scopedTeams is non-empty', () => {+ const tools = getToolsForFeatures({ scopedTeams: [42] })+ expect(tools).not.toContain('roles-list') // organization:read+ expect(tools).not.toContain('org-members-list') // organization_member:read+ expect(tools).not.toContain('organizations-list') // organization:read+ expect(tools).not.toContain('organization-get') // organization:read+ expect(tools).not.toContain('projects-get') // organization:read+ })++ it('keeps project-scoped tools visible when scopedTeams is non-empty', () => {+ const tools = getToolsForFeatures({ scopedTeams: [42] })+ expect(tools).toContain('dashboard-get')+ expect(tools).toContain('feature-flag-get-all')+ })++ it('keeps org-scope tools visible when scopedTeams is empty (unscoped token)', () => {+ const tools = getToolsForFeatures({ scopedTeams: [] })+ expect(tools).toContain('roles-list')+ expect(tools).toContain('organization-get')+ })++ it('keeps org-scope tools visible when scopedTeams is undefined', () => {+ const tools = getToolsForFeatures({ scopedTeams: undefined })+ expect(tools).toContain('roles-list')+ expect(tools).toContain('organization-get')+ })++ it('combines scopedTeams with feature filtering', () => {+ const tools = getToolsForFeatures({ features: ['workspace'], scopedTeams: [42] })+ expect(tools).not.toContain('organizations-list')+ expect(tools).not.toContain('organization-get')+ })+})+describe('Tool Filtering - Read-Only Mode', () => {it('should only return read-only tools when readOnly is true', () => {const tools = getToolsForFeatures({ readOnly: true })