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(tasks): derive MCP OAuth scope lists from posthog.scopes (#57851)

posthog[bot]51dfd5d9263e
diff --git a/posthog/temporal/oauth.py b/posthog/temporal/oauth.py
index 9c356d5ec639..5c8309a6cf75 100644
--- a/posthog/temporal/oauth.py
+++ b/posthog/temporal/oauth.py
@@ -5,6 +5,7 @@ from django.utils import timezone
from posthog.models import OAuthAccessToken, OAuthApplication
from posthog.models.utils import generate_random_oauth_access_token
+from posthog.scopes import API_SCOPE_OBJECTS, INTERNAL_API_SCOPE_OBJECTS, OAUTH_HIDDEN_SCOPE_OBJECTS
from posthog.utils import get_instance_region
ARRAY_APP_CLIENT_ID_US = "HCWoE0aRFMYxIxFNTTwkOORn5LBjOt2GVDzwSw5W"
@@ -14,55 +15,31 @@ ARRAY_APP_CLIENT_ID_DEV = "DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ"
McpScopePreset = Literal["read_only", "full"]
-# Scopes matching the MCP server's OAUTH_SCOPES_SUPPORTED (services/mcp/src/lib/constants.ts),
-# excluding OAuth auth scopes (openid, profile, email, introspection).
-MCP_READ_SCOPES: list[str] = [
- "action:read",
- "cohort:read",
- "dashboard:read",
- "error_tracking:read",
- "event_definition:read",
- "experiment:read",
- "feature_flag:read",
- "hog_flow:read",
- "insight:read",
- "insight_variable:read",
- "llm_prompt:read",
- "llm_skill:read",
- "logs:read",
- "notebook:read",
- "organization:read",
- "tracing:read",
- "project:read",
- "property_definition:read",
- "query:read",
- "survey:read",
- "user:read",
- "warehouse_table:read",
- "warehouse_view:read",
-]
-
-MCP_WRITE_SCOPES: list[str] = [
- "action:write",
- "cohort:write",
- "dashboard:write",
- "error_tracking:write",
- "event_definition:write",
- "experiment:write",
- "feature_flag:write",
- "insight:write",
- "insight_variable:write",
- "llm_prompt:write",
- "llm_skill:write",
- "notebook:write",
- "survey:write",
-]
-
INTERNAL_SCOPES: list[str] = [
"task:write",
"llm_gateway:read",
]
+
+# Derived from posthog.scopes so the token issued to a sandboxed agent cannot
+# drift out of subset of what the MCP server advertises in
+# `services/mcp/src/lib/oauth-scopes.generated.ts` (itself generated from
+# `get_oauth_scopes_supported()` via `bin/build-mcp-oauth-scopes.py`). Scopes
+# already covered by INTERNAL_SCOPES are excluded so resolve_scopes() doesn't
+# emit duplicates.
+def _build_mcp_scopes(action: Literal["read", "write"]) -> list[str]:
+ excluded_objects = INTERNAL_API_SCOPE_OBJECTS | OAUTH_HIDDEN_SCOPE_OBJECTS
+ internal_set = set(INTERNAL_SCOPES)
+ return [
+ f"{obj}:{action}"
+ for obj in API_SCOPE_OBJECTS
+ if obj not in excluded_objects and f"{obj}:{action}" not in internal_set
+ ]
+
+
+MCP_READ_SCOPES: list[str] = _build_mcp_scopes("read")
+MCP_WRITE_SCOPES: list[str] = _build_mcp_scopes("write")
+
TOKEN_EXPIRATION_SECONDS = 60 * 60 * 6 # 6 hours
PosthogMcpScopes = McpScopePreset | list[str]
@@ -74,9 +51,12 @@ def resolve_scopes(scopes: PosthogMcpScopes = "read_only", *, include_internal_s
internal = list(INTERNAL_SCOPES) if include_internal_scopes else []
if isinstance(scopes, str):
if scopes == "full":
- return [*MCP_READ_SCOPES, *MCP_WRITE_SCOPES, *internal]
- return [*MCP_READ_SCOPES, *internal]
- return [*scopes, *internal]
+ resolved = [*MCP_READ_SCOPES, *MCP_WRITE_SCOPES, *internal]
+ else:
+ resolved = [*MCP_READ_SCOPES, *internal]
+ else:
+ resolved = [*scopes, *internal]
+ return list(dict.fromkeys(resolved))
def has_write_scopes(scopes: PosthogMcpScopes) -> bool:
diff --git a/posthog/temporal/tests/test_oauth.py b/posthog/temporal/tests/test_oauth.py
index 3e5e892c21f2..378c76a21a15 100644
--- a/posthog/temporal/tests/test_oauth.py
+++ b/posthog/temporal/tests/test_oauth.py
@@ -42,6 +42,16 @@ class TestResolveScopes(SimpleTestCase):
for scope in INTERNAL_SCOPES:
assert scope not in result
+ def test_deduplicates_overlapping_scopes(self) -> None:
+ custom = ["feature_flag:read", "feature_flag:read", "task:write", "insight:read"]
+ result = resolve_scopes(custom)
+ assert len(result) == len(set(result)), f"expected no duplicates, got {result}"
+ # task:write is in INTERNAL_SCOPES; appears once despite being in both inputs
+ assert result.count("task:write") == 1
+ assert result.count("feature_flag:read") == 1
+ # First-seen order is preserved
+ assert result.index("feature_flag:read") < result.index("task:write")
+
def test_internal_scope_objects_disjoint_from_mcp_scope_lists(self) -> None:
from posthog.scopes import INTERNAL_API_SCOPE_OBJECTS