: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): advertise only scopes the enabled tools require (#62486)
Matt Brookerd9102d4c270f
diff --git a/services/mcp/src/hono/public-routes.ts b/services/mcp/src/hono/public-routes.tsindex e9fa71861d11..638d962f7782 100644--- a/services/mcp/src/hono/public-routes.ts+++ b/services/mcp/src/hono/public-routes.ts@@ -2,9 +2,10 @@ import { serveStatic } from '@hono/node-server/serve-static'import type { Hono } from 'hono'import { buildRedirectUrl, getPublicUrl, matchAuthServerRedirect } from '@/lib/routing'+import { getAdvertisedOAuthScopes } from '@/tools/toolDefinitions'import type { Lifecycle } from './app'-import { AUTH_REDIRECT_PATHS, getAuthorizationServerUrl, MCP_DOCS_URL, OAUTH_SCOPES_SUPPORTED } from './constants'+import { AUTH_REDIRECT_PATHS, getAuthorizationServerUrl, MCP_DOCS_URL } from './constants'import { register } from './metrics'import type { HonoCtx, RedisWithPing } from './types'@@ -56,7 +57,7 @@ const wellKnownHandler = (c: HonoCtx): Response => {{resource: resourceUrl.toString().replace(/\/$/, ''),authorization_servers: [getAuthorizationServerUrl()],- scopes_supported: OAUTH_SCOPES_SUPPORTED,+ scopes_supported: getAdvertisedOAuthScopes(),bearer_methods_supported: ['header'],},200,diff --git a/services/mcp/src/index.ts b/services/mcp/src/index.tsindex ecc0fd5ce973..fe92211c2536 100644--- a/services/mcp/src/index.ts+++ b/services/mcp/src/index.ts@@ -1,10 +1,11 @@-import { MCP_DOCS_URL, OAUTH_SCOPES_SUPPORTED, getAuthorizationServerUrl } from '@/lib/constants'+import { MCP_DOCS_URL, getAuthorizationServerUrl } from '@/lib/constants'import { isIdJagAccessToken } from '@/lib/id-jag'import { RequestLogger, withLogging } from '@/lib/logging'import { extractClientInfoFromBody } from '@/lib/mcp-client-info'import { RequestProperties } from '@/lib/request-properties'import { buildRedirectUrl, matchAuthServerRedirect } from '@/lib/routing'import { extractBearerToken, hash, parseMcpMode, sanitizeHeaderValue } from '@/lib/utils'+import { getAdvertisedOAuthScopes } from '@/tools/toolDefinitions'import type { CloudRegion } from '@/tools/types'import { proxyToHono, resolveProxyRegion } from './proxy'@@ -167,7 +168,7 @@ const handleRequest = async (JSON.stringify({resource: resourceUrl.toString().replace(/\/$/, ''),authorization_servers: [authorizationServer],- scopes_supported: OAUTH_SCOPES_SUPPORTED,+ scopes_supported: getAdvertisedOAuthScopes(),bearer_methods_supported: ['header'],}),{diff --git a/services/mcp/src/tools/toolDefinitions.ts b/services/mcp/src/tools/toolDefinitions.tsindex 7b829166e7e1..cd4935553dbf 100644--- a/services/mcp/src/tools/toolDefinitions.ts+++ b/services/mcp/src/tools/toolDefinitions.ts@@ -1,6 +1,7 @@import z from 'zod'import { hasScope, hasScopes } from '@/lib/api'+import { OAUTH_SCOPES_SUPPORTED } from '@/lib/oauth-scopes.generated'import type { EvaluatedFlags } from '@/lib/posthog/flags'import generatedToolDefinitionsJson from '../../schema/generated-tool-definitions.json'@@ -67,6 +68,30 @@ export function getToolDefinitions(): ToolDefinitions {return { ..._toolDefinitions, ...generated }}+let _advertisedOAuthScopes: readonly string[] | undefined = undefined++/**+ * Scopes published as `scopes_supported` in the MCP protected-resource+ * metadata: every grantable scope the tool catalog requires, plus identity+ * scopes (no `:`) that ride every authorize. Narrower than+ * `OAUTH_SCOPES_SUPPORTED` (the authorization server's full grantable set) so+ * clients are not asked to consent to write access no tool exercises. Filtering+ * `OAUTH_SCOPES_SUPPORTED` keeps the result a subset of the AS, so no advertised+ * scope is rejected at `/authorize`.+ */+export function getAdvertisedOAuthScopes(): readonly string[] {+ if (!_advertisedOAuthScopes) {+ const required = new Set<string>()+ for (const definition of Object.values(getToolDefinitions())) {+ for (const scope of definition.required_scopes ?? []) {+ required.add(scope)+ }+ }+ _advertisedOAuthScopes = OAUTH_SCOPES_SUPPORTED.filter((scope) => !scope.includes(':') || required.has(scope))+ }+ return _advertisedOAuthScopes+}+export function getToolDefinition(toolName: string): ToolDefinition {const toolDefinitions = getToolDefinitions()diff --git a/services/mcp/tests/unit/tool-filtering.test.ts b/services/mcp/tests/unit/tool-filtering.test.tsindex d6ce9e38ccea..8ee9b061ba25 100644--- a/services/mcp/tests/unit/tool-filtering.test.ts+++ b/services/mcp/tests/unit/tool-filtering.test.ts@@ -5,6 +5,7 @@ import type { EvaluatedFlags } from '@/lib/posthog/flags'import { SessionManager } from '@/lib/SessionManager'import { getToolsFromContext } from '@/tools'import {+ getAdvertisedOAuthScopes,getToolDefinitions,getRequiredFeatureFlags,getToolsForFeatures,@@ -393,6 +394,38 @@ describe('OAUTH_SCOPES_SUPPORTED completeness', () => {})})+describe('getAdvertisedOAuthScopes', () => {+ const supported = new Set<string>(OAUTH_SCOPES_SUPPORTED)+ const advertised = getAdvertisedOAuthScopes()+ const advertisedSet = new Set(advertised)++ it('stays a subset of OAUTH_SCOPES_SUPPORTED so nothing is rejected at /authorize', () => {+ const outside = advertised.filter((s) => !supported.has(s))+ expect(outside, `advertised scopes not grantable by the AS: ${outside.join(', ')}`).toEqual([])+ })++ it('keeps the identity scopes that ride every authorize', () => {+ for (const scope of OAUTH_SCOPES_SUPPORTED.filter((s) => !s.includes(':'))) {+ expect(advertisedSet.has(scope), `missing identity scope: ${scope}`).toBe(true)+ }+ })++ it('covers every grantable scope the tool catalog requires', () => {+ const required = new Set<string>()+ for (const def of Object.values(getToolDefinitions())) {+ for (const scope of def.required_scopes) {+ required.add(scope)+ }+ }+ const missing = [...required].filter((s) => supported.has(s) && !advertisedSet.has(s)).sort()+ expect(missing, `tool-required scopes dropped from the advertised list: ${missing.join(', ')}`).toEqual([])+ })++ it('narrows the full grantable set rather than mirroring it', () => {+ expect(advertised.length).toBeLessThan(OAUTH_SCOPES_SUPPORTED.length)+ })+})+describe('Tool Filtering - excludeTools', () => {const excludeTests = [{