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(oauth): derive MCP consent scopes from both tool catalogs (#71355)

Matt Brookerdf336612c80c
diff --git a/posthog/api/oauth/mcp_resource_scopes.py b/posthog/api/oauth/mcp_resource_scopes.py
index 8fd6bbf3661c..487c8b65def4 100644
--- a/posthog/api/oauth/mcp_resource_scopes.py
+++ b/posthog/api/oauth/mcp_resource_scopes.py
@@ -14,11 +14,17 @@ from posthog.settings.base_variables import BASE_DIR
logger = structlog.get_logger(__name__)
-# The MCP server builds its RFC 9728 `scopes_supported` from this same committed
-# artifact (services/mcp/src/tools/toolDefinitions.ts reads it too). Deriving the
-# consent list from it here keeps the authorization server's promise sourced from
-# its own state instead of a runtime fetch to the resource server.
-_TOOL_DEFINITIONS_PATH = os.path.join(BASE_DIR, "services", "mcp", "schema", "generated-tool-definitions.json")
+# The MCP server builds its RFC 9728 `scopes_supported` from these same committed
+# artifacts (services/mcp/src/tools/toolDefinitions.ts merges both). Deriving the
+# consent list from them here keeps the authorization server's promise sourced from
+# its own state instead of a runtime fetch to the resource server. Both files are
+# needed: hand-written tools (e.g. read-data-schema) live in tool-definitions.json
+# and are the only users of some scopes, so reading the generated file alone drops
+# their scopes from the consent default.
+_TOOL_DEFINITIONS_PATHS = (
+ os.path.join(BASE_DIR, "services", "mcp", "schema", "tool-definitions.json"),
+ os.path.join(BASE_DIR, "services", "mcp", "schema", "generated-tool-definitions.json"),
+)
# Keep in sync with services/mcp/src/lib/routing.ts regional MCP hostnames.
PRODUCTION_MCP_HOSTS = frozenset(
@@ -58,12 +64,15 @@ def is_trusted_posthog_mcp_resource(resource_url: str) -> bool:
@lru_cache(maxsize=1)
def _tool_required_scopes() -> frozenset[str]:
- with open(_TOOL_DEFINITIONS_PATH) as definitions_file:
- definitions = json.load(definitions_file)
-
- return frozenset(
- scope for definition in definitions.values() for scope in (definition.get("required_scopes") or [])
- )
+ # Keyed merge, later files winning — same precedence as the MCP server's
+ # getToolDefinitions() ({ ...handwritten, ...generated }), so a hand-written
+ # tool overridden by a generated one doesn't leak its scopes into consent.
+ merged: dict[str, dict] = {}
+ for path in _TOOL_DEFINITIONS_PATHS:
+ with open(path) as definitions_file:
+ merged.update(json.load(definitions_file))
+
+ return frozenset(scope for definition in merged.values() for scope in (definition.get("required_scopes") or []))
def mcp_advertised_scopes() -> list[str]:
diff --git a/posthog/api/oauth/test_mcp_resource_scopes.py b/posthog/api/oauth/test_mcp_resource_scopes.py
index fe9c28dac58b..1430eec0fa3f 100644
--- a/posthog/api/oauth/test_mcp_resource_scopes.py
+++ b/posthog/api/oauth/test_mcp_resource_scopes.py
@@ -1,3 +1,7 @@
+import os
+import json
+import tempfile
+
from unittest.mock import patch
from django.test import SimpleTestCase, override_settings
@@ -50,15 +54,43 @@ class TestMcpResourceScopes(SimpleTestCase):
)
def test_advertised_scopes_derives_from_real_committed_catalog(self):
- # Guards the cross-service artifact path and shape: if
- # services/mcp/schema/generated-tool-definitions.json moves or changes
- # structure, this fails loudly instead of silently advertising nothing.
+ # Guards the cross-service artifact paths and shape: if the files under
+ # services/mcp/schema/ move or change structure, this fails loudly
+ # instead of silently advertising nothing.
_tool_required_scopes.cache_clear()
scopes = mcp_advertised_scopes()
self.assertIn("openid", scopes)
self.assertIn("notebook:read", scopes)
+ # Required only by hand-written tools (read-data-schema in
+ # tool-definitions.json) — absent if the union reads only the
+ # generated catalog.
+ self.assertIn("event_definition:read", scopes)
+ self.assertIn("property_definition:read", scopes)
self.assertGreater(len(scopes), 50)
+ def test_tool_required_scopes_merges_by_tool_name_with_generated_winning(self):
+ # A hand-written tool overridden by a generated one must not leak its
+ # scopes: the MCP server's getToolDefinitions() keyed merge drops the
+ # overridden definition, so a per-file scope union would advertise
+ # consent scopes no active tool requires.
+ with tempfile.TemporaryDirectory() as tmp_dir:
+ handwritten = os.path.join(tmp_dir, "handwritten.json")
+ generated = os.path.join(tmp_dir, "generated.json")
+ with open(handwritten, "w") as f:
+ json.dump({"tool": {"required_scopes": ["overridden:read"]}}, f)
+ with open(generated, "w") as f:
+ json.dump({"tool": {"required_scopes": ["active:read"]}}, f)
+
+ with patch(
+ "posthog.api.oauth.mcp_resource_scopes._TOOL_DEFINITIONS_PATHS",
+ (handwritten, generated),
+ ):
+ _tool_required_scopes.cache_clear()
+ try:
+ self.assertEqual(_tool_required_scopes(), frozenset({"active:read"}))
+ finally:
+ _tool_required_scopes.cache_clear()
+
def test_build_oauth_mcp_consent_context_success(self):
context = build_oauth_mcp_consent_context("https://mcp.posthog.com/mcp")