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.

chore(tasks): cleaner mcp scope defined on run source (#61538)

Alessandro Pogliaghi37d71256923b
diff --git a/products/tasks/backend/api.py b/products/tasks/backend/api.py
index bb184a7aa51e..583e322afbff 100644
--- a/products/tasks/backend/api.py
+++ b/products/tasks/backend/api.py
@@ -42,6 +42,7 @@ from posthog.permissions import APIScopePermission
from posthog.rate_limit import CodeInviteThrottle
from posthog.renderers import ServerSentEventRenderer
from posthog.storage import object_storage
+from posthog.temporal.oauth import PosthogMcpScopes
from products.slack_app.backend.models import SlackThreadTaskMapping
@@ -341,6 +342,14 @@ def _slack_repo_research_payload(
}
+_FULL_MCP_RUN_SOURCES: frozenset[RunSource | None] = frozenset({None, RunSource.MANUAL})
+
+
+def _resolve_posthog_mcp_scopes(task_run: TaskRun) -> PosthogMcpScopes:
+ run_source = parse_run_state(task_run.state).run_source
+ return "full" if run_source in _FULL_MCP_RUN_SOURCES else "read_only"
+
+
class TaskViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
"""
API for managing tasks within a project. Tasks represent units of work to be performed by an agent.
@@ -735,6 +744,7 @@ class TaskViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
run_id=str(task_run.id),
team_id=task.team.id,
user_id=getattr(self.request.user, "id", None),
+ posthog_mcp_scopes=_resolve_posthog_mcp_scopes(task_run),
)
logger.info(f"Workflow trigger completed for task {task.id}, run {task_run.id}")
except Exception as e:
@@ -1638,6 +1648,7 @@ class TaskRunViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
run_id=str(task_run.id),
team_id=task.team.id,
user_id=getattr(self.request.user, "id", None),
+ posthog_mcp_scopes=_resolve_posthog_mcp_scopes(task_run),
)
logger.info("Workflow trigger completed for task %s, run %s", task.id, task_run.id)
except Exception as e:
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index 2f5f91afb79a..7f0768324a0a 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -1097,12 +1097,33 @@ class TestTaskAPI(BaseTaskAPITest):
run_id=run_id,
team_id=task.team.id,
user_id=self.user.id,
+ posthog_mcp_scopes="full",
)
self.assertEqual(latest_run["task"], str(task.id))
self.assertEqual(latest_run["status"], "queued")
self.assertEqual(latest_run["environment"], "cloud")
+ @parameterized.expand(
+ [
+ ("run_source_omitted", None, "full"),
+ ("manual", {"run_source": "manual"}, "full"),
+ ("signal_report", {"run_source": "signal_report"}, "read_only"),
+ ]
+ )
+ @patch("products.tasks.backend.api.execute_task_processing_workflow")
+ def test_run_endpoint_resolves_mcp_scope_from_run_source(self, _name, payload, expected_scope, mock_workflow):
+ task = self.create_task()
+
+ response = self.client.post(
+ f"/api/projects/@current/tasks/{task.id}/run/",
+ payload,
+ format="json",
+ )
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+ self.assertEqual(mock_workflow.call_args.kwargs["posthog_mcp_scopes"], expected_scope)
+
@patch("products.tasks.backend.api.execute_task_processing_workflow")
def test_run_endpoint_persists_sandbox_environment_id(self, mock_workflow):
task = self.create_task(created_by=self.user)
@@ -1465,6 +1486,7 @@ class TestTaskAPI(BaseTaskAPITest):
run_id=str(task_run.id),
team_id=task.team.id,
user_id=self.user.id,
+ posthog_mcp_scopes="full",
)
@patch("products.tasks.backend.api.execute_task_processing_workflow")
@@ -1545,6 +1567,7 @@ class TestTaskAPI(BaseTaskAPITest):
run_id=str(task_run.id),
team_id=task.team.id,
user_id=self.user.id,
+ posthog_mcp_scopes="full",
)
@parameterized.expand(