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(aio): align clustering MCP tool scopes with endpoint scope object (#84473)

posthog[bot]ae703aeba7f2
diff --git a/frontend/src/lib/agentScopes.generated.ts b/frontend/src/lib/agentScopes.generated.ts
index 4fda486edd62..afc3ee1efcab 100644
--- a/frontend/src/lib/agentScopes.generated.ts
+++ b/frontend/src/lib/agentScopes.generated.ts
@@ -76,6 +76,8 @@ export const AGENT_USE_CASE_SCOPES = [
'integration:write',
'llm_analytics:read',
'llm_analytics:write',
+ 'ai_observability_clusters:read',
+ 'ai_observability_clusters:write',
'llm_prompt:read',
'llm_prompt:write',
'llm_provider_key:read',
diff --git a/products/ai_observability/backend/api/test/test_clustering_config.py b/products/ai_observability/backend/api/test/test_clustering_config.py
index b6105b06f1bd..2191c129746a 100644
--- a/products/ai_observability/backend/api/test/test_clustering_config.py
+++ b/products/ai_observability/backend/api/test/test_clustering_config.py
@@ -1,5 +1,6 @@
from posthog.test.base import APIBaseTest
+from parameterized import parameterized
from rest_framework import status
from products.ai_observability.backend.models.clustering_config import ClusteringConfig
@@ -100,3 +101,40 @@ class TestClusteringConfigViewSet(APIBaseTest):
response = self.client.get(f"/api/environments/{self.team.id}/llm_analytics/clustering_config/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data["event_filters"]), 2)
+
+ # The default test client uses force_login, which skips the scope check, so these
+ # cases use a real Personal API Key — the path MCP and OAuth callers take — to lock
+ # the scope the MCP tools must declare (ai_observability_clusters, not llm_analytics).
+ @parameterized.expand(
+ [
+ ("correct_read_scope", ["ai_observability_clusters:read"], status.HTTP_200_OK),
+ ("write_scope_grants_read", ["ai_observability_clusters:write"], status.HTTP_200_OK),
+ ("wrong_scope_denied", ["llm_analytics:read"], status.HTTP_403_FORBIDDEN),
+ ]
+ )
+ def test_get_config_pak_scope(self, _name: str, scopes: list[str], expected_status: int) -> None:
+ api_key = self.create_personal_api_key_with_scopes(scopes)
+ self.client.logout()
+ self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {api_key}")
+ response = self.client.get(f"/api/environments/{self.team.id}/llm_analytics/clustering_config/")
+ self.assertEqual(response.status_code, expected_status)
+
+ # set_event_filters carries its own required_scopes override rather than inheriting
+ # scope_object, so it needs separate coverage from the list action above.
+ @parameterized.expand(
+ [
+ ("correct_write_scope", ["ai_observability_clusters:write"], status.HTTP_200_OK),
+ ("read_scope_denied", ["ai_observability_clusters:read"], status.HTTP_403_FORBIDDEN),
+ ("wrong_scope_denied", ["llm_analytics:write"], status.HTTP_403_FORBIDDEN),
+ ]
+ )
+ def test_set_event_filters_pak_scope(self, _name: str, scopes: list[str], expected_status: int) -> None:
+ api_key = self.create_personal_api_key_with_scopes(scopes)
+ self.client.logout()
+ self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {api_key}")
+ response = self.client.post(
+ f"/api/environments/{self.team.id}/llm_analytics/clustering_config/set_event_filters/",
+ {"event_filters": []},
+ format="json",
+ )
+ self.assertEqual(response.status_code, expected_status)
diff --git a/products/ai_observability/backend/api/test/test_clustering_job.py b/products/ai_observability/backend/api/test/test_clustering_job.py
index a639cb78509e..246ab637d104 100644
--- a/products/ai_observability/backend/api/test/test_clustering_job.py
+++ b/products/ai_observability/backend/api/test/test_clustering_job.py
@@ -60,6 +60,41 @@ class TestClusteringJobViewSet(APIBaseTest):
self.assertEqual(response.json()["name"], "Prod Traffic")
self.assertEqual(ClusteringJob.objects.filter(team=self.team).count(), 1)
+ # The default test client uses force_login, which skips the scope check, so these
+ # cases use a real Personal API Key — the path MCP and OAuth callers take — to lock
+ # the scope the MCP tools must declare (ai_observability_clusters, not llm_analytics).
+ @parameterized.expand(
+ [
+ ("correct_read_scope", ["ai_observability_clusters:read"], status.HTTP_200_OK),
+ ("write_scope_grants_read", ["ai_observability_clusters:write"], status.HTTP_200_OK),
+ ("wrong_scope_denied", ["llm_analytics:read"], status.HTTP_403_FORBIDDEN),
+ ]
+ )
+ def test_list_pak_scope(self, _name: str, scopes: list[str], expected_status: int) -> None:
+ api_key = self.create_personal_api_key_with_scopes(scopes)
+ self.client.logout()
+ self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {api_key}")
+ response = self.client.get(self._url())
+ self.assertEqual(response.status_code, expected_status)
+
+ @parameterized.expand(
+ [
+ ("correct_write_scope", ["ai_observability_clusters:write"], status.HTTP_201_CREATED),
+ ("read_scope_denied", ["ai_observability_clusters:read"], status.HTTP_403_FORBIDDEN),
+ ("wrong_scope_denied", ["llm_analytics:write"], status.HTTP_403_FORBIDDEN),
+ ]
+ )
+ def test_create_pak_scope(self, _name: str, scopes: list[str], expected_status: int) -> None:
+ api_key = self.create_personal_api_key_with_scopes(scopes)
+ self.client.logout()
+ self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {api_key}")
+ response = self.client.post(
+ self._url(),
+ {"name": "Scoped", "analysis_level": "trace", "event_filters": [], "enabled": True},
+ format="json",
+ )
+ self.assertEqual(response.status_code, expected_status)
+
def test_create_job_with_filters(self):
filters = [{"key": "$ai_model", "value": "gpt-4", "operator": "exact", "type": "event"}]
response = self.client.post(
diff --git a/products/ai_observability/mcp/tools.yaml b/products/ai_observability/mcp/tools.yaml
index a21888183d0c..f25825bac1c9 100644
--- a/products/ai_observability/mcp/tools.yaml
+++ b/products/ai_observability/mcp/tools.yaml
@@ -54,7 +54,7 @@ tools:
operation: llm_analytics_clustering_config_list
enabled: true
scopes:
- - llm_analytics:read
+ - ai_observability_clusters:read
annotations:
readOnly: true
destructive: false
@@ -68,7 +68,7 @@ tools:
operation: llm_analytics_clustering_config_set_event_filters_create
enabled: true
scopes:
- - llm_analytics:write
+ - ai_observability_clusters:write
annotations:
readOnly: false
destructive: false
@@ -82,7 +82,7 @@ tools:
operation: llm_analytics_clustering_jobs_create
enabled: true
scopes:
- - llm_analytics:write
+ - ai_observability_clusters:write
annotations:
readOnly: false
destructive: false
@@ -96,7 +96,7 @@ tools:
operation: llm_analytics_clustering_jobs_destroy
enabled: true
scopes:
- - llm_analytics:write
+ - ai_observability_clusters:write
annotations:
readOnly: false
destructive: true
@@ -110,7 +110,7 @@ tools:
operation: llm_analytics_clustering_jobs_retrieve
enabled: true
scopes:
- - llm_analytics:read
+ - ai_observability_clusters:read
annotations:
readOnly: true
destructive: false
@@ -123,7 +123,7 @@ tools:
operation: llm_analytics_clustering_jobs_list
enabled: true
scopes:
- - llm_analytics:read
+ - ai_observability_clusters:read
annotations:
readOnly: true
destructive: false
@@ -138,7 +138,7 @@ tools:
operation: llm_analytics_clustering_jobs_partial_update
enabled: true
scopes:
- - llm_analytics:write
+ - ai_observability_clusters:write
annotations:
readOnly: false
destructive: false
diff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.json
index aa6e2f724fe1..3105058fb334 100644
--- a/services/mcp/schema/generated-tool-definitions.json
+++ b/services/mcp/schema/generated-tool-definitions.json
@@ -5771,7 +5771,7 @@
"feature": "llm_analytics",
"summary": "Get clustering config",
"title": "Get clustering config",
- "required_scopes": ["llm_analytics:read"],
+ "required_scopes": ["ai_observability_clusters:read"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
@@ -5785,7 +5785,7 @@
"feature": "llm_analytics",
"summary": "Set clustering config filters",
"title": "Set clustering config filters",
- "required_scopes": ["llm_analytics:write"],
+ "required_scopes": ["ai_observability_clusters:write"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
@@ -5799,7 +5799,7 @@
"feature": "llm_analytics",
"summary": "Create clustering job",
"title": "Create clustering job",
- "required_scopes": ["llm_analytics:write"],
+ "required_scopes": ["ai_observability_clusters:write"],
"annotations": {
"destructiveHint": false,
"idempotentHint": false,
@@ -5813,7 +5813,7 @@
"feature": "llm_analytics",
"summary": "Delete clustering job",
"title": "Delete clustering job",
- "required_scopes": ["llm_analytics:write"],
+ "required_scopes": ["ai_observability_clusters:write"],
"annotations": {
"destructiveHint": true,
"idempotentHint": true,
@@ -5827,7 +5827,7 @@
"feature": "llm_analytics",
"summary": "Get clustering job",
"title": "Get clustering job",
- "required_scopes": ["llm_analytics:read"],
+ "required_scopes": ["ai_observability_clusters:read"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
@@ -5841,7 +5841,7 @@
"feature": "llm_analytics",
"summary": "List clustering jobs",
"title": "List clustering jobs",
- "required_scopes": ["llm_analytics:read"],
+ "required_scopes": ["ai_observability_clusters:read"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
@@ -5855,7 +5855,7 @@
"feature": "llm_analytics",
"summary": "Update clustering job",
"title": "Update clustering job",
- "required_scopes": ["llm_analytics:write"],
+ "required_scopes": ["ai_observability_clusters:write"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.json
index bb2458eb7aef..dec87ce35e12 100644
--- a/services/mcp/schema/tool-definitions-all.json
+++ b/services/mcp/schema/tool-definitions-all.json
@@ -5968,7 +5968,7 @@
"feature": "llm_analytics",
"summary": "Get clustering config",
"title": "Get clustering config",
- "required_scopes": ["llm_analytics:read"],
+ "required_scopes": ["ai_observability_clusters:read"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
@@ -5982,7 +5982,7 @@
"feature": "llm_analytics",
"summary": "Set clustering config filters",
"title": "Set clustering config filters",
- "required_scopes": ["llm_analytics:write"],
+ "required_scopes": ["ai_observability_clusters:write"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
@@ -5996,7 +5996,7 @@
"feature": "llm_analytics",
"summary": "Create clustering job",
"title": "Create clustering job",
- "required_scopes": ["llm_analytics:write"],
+ "required_scopes": ["ai_observability_clusters:write"],
"annotations": {
"destructiveHint": false,
"idempotentHint": false,
@@ -6010,7 +6010,7 @@
"feature": "llm_analytics",
"summary": "Delete clustering job",
"title": "Delete clustering job",
- "required_scopes": ["llm_analytics:write"],
+ "required_scopes": ["ai_observability_clusters:write"],
"annotations": {
"destructiveHint": true,
"idempotentHint": true,
@@ -6024,7 +6024,7 @@
"feature": "llm_analytics",
"summary": "Get clustering job",
"title": "Get clustering job",
- "required_scopes": ["llm_analytics:read"],
+ "required_scopes": ["ai_observability_clusters:read"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
@@ -6038,7 +6038,7 @@
"feature": "llm_analytics",
"summary": "List clustering jobs",
"title": "List clustering jobs",
- "required_scopes": ["llm_analytics:read"],
+ "required_scopes": ["ai_observability_clusters:read"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,
@@ -6052,7 +6052,7 @@
"feature": "llm_analytics",
"summary": "Update clustering job",
"title": "Update clustering job",
- "required_scopes": ["llm_analytics:write"],
+ "required_scopes": ["ai_observability_clusters:write"],
"annotations": {
"destructiveHint": false,
"idempotentHint": true,