: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-store): simplify how installation actions resolve scopes (#130391)
Jake Ruth3bd31fcfa289
diff --git a/products/mcp_store/backend/presentation/views.py b/products/mcp_store/backend/presentation/views.pyindex 8e6944a2ac4d..4ea2b3a888c5 100644--- a/products/mcp_store/backend/presentation/views.py+++ b/products/mcp_store/backend/presentation/views.py@@ -766,31 +766,11 @@ class MCPServerInstallationViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSetlookup_field = "id"permission_classes = [IsAuthenticated, DenyMCPBuiltInAgentOAuth, DenyWorkloadAccessToken]- # Installations are user-scoped or shared (safely_get_queryset returns both),- # so write actions don't need project admin access.- # Return project:read so AccessControlPermission requires "member" not "admin".- _USER_SCOPED_ACTIONS = {- "destroy",- "partial_update",- "install_custom",- "install_template",- "update_tool_approval",- "refresh_tools",- # share/unshare enforce their own owner/admin checks in the action bodies.- "share",- "unshare",- }-# Mutations restricted to the credential owner (installer) on shared rows.# Tool approval is included: it gates which tools autonomous runs may execute# under the owner's credential, so a non-owner must not be able to flip it._OWNER_ONLY_ACTIONS = {"destroy", "partial_update", "refresh_tools", "update_tool_approval"}- def dangerously_get_required_scopes(self, request: Any, view: Any) -> list[str] | None:- if self.action in self._USER_SCOPED_ACTIONS:- return ["project:read"]- return None-def safely_get_queryset(self, queryset: QuerySet) -> QuerySet:return (queryset.filter(diff --git a/products/mcp_store/backend/test/test_api.py b/products/mcp_store/backend/test/test_api.pyindex 18bcfa9fbed8..b529e0e65a28 100644--- a/products/mcp_store/backend/test/test_api.py+++ b/products/mcp_store/backend/test/test_api.py@@ -1179,9 +1179,9 @@ class TestMCPGatewayWriteScopeAPI(APIBaseTest):membership.level = OrganizationMembership.Level.ADMINmembership.save()- def _read_only_oauth_client(self) -> APIClient:+ def _oauth_client(self, scope: str = "project:read") -> APIClient:application = OAuthApplication.objects.create(- name="Read-only MCP gateway",+ name="Scoped MCP client",client_type=OAuthApplication.CLIENT_CONFIDENTIAL,authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,redirect_uris="https://example.com/callback",@@ -1192,9 +1192,9 @@ class TestMCPGatewayWriteScopeAPI(APIBaseTest):access_token = OAuthAccessToken.objects.create(user=self.user,application=application,- token="pha_read_only_mcp_gateway",+ token=f"pha_mcp_{scope.replace(':', '_').replace(' ', '_')}",expires=timezone.now() + timedelta(hours=1),- scope="project:read",+ scope=scope,scoped_teams=[self.team.id],)client = APIClient()@@ -1230,12 +1230,63 @@ class TestMCPGatewayWriteScopeAPI(APIBaseTest):url = f"/api/projects/{self.team.id}/mcp_gateway/service_accounts/{account.id}/access/"data = {"gateway_server_id": str(server.id), "enabled": True}- response = self._read_only_oauth_client().post(url, data=data, format="json")+ response = self._oauth_client().post(url, data=data, format="json")assert response.status_code == status.HTTP_403_FORBIDDENassert not MCPToolPolicy.objects.for_team(self.team.id).filter(gateway_server=server).exists()assert not MCPServiceAccountServerAccess.objects.for_team(self.team.id).filter(gateway_server=server).exists()+ def _installation_with_tool(self) -> tuple[MCPServerInstallation, MCPServerInstallationTool]:+ installation = MCPServerInstallation.objects.create(+ team=self.team,+ user=self.user,+ display_name="Scoped server",+ url="https://mcp.scoped.example.com/mcp",+ auth_type="api_key",+ scope="personal",+ )+ tool = MCPServerInstallationTool.objects.create(+ installation=installation, tool_name="search", last_seen_at=timezone.now()+ )+ return installation, tool++ @parameterized.expand([("install_custom",), ("update_tool_approval",), ("destroy",)])+ def test_read_only_oauth_token_cannot_change_installations(self, action_name: str) -> None:+ installation, tool = self._installation_with_tool()+ base = f"/api/environments/{self.team.id}/mcp_server_installations/"+ client = self._oauth_client()++ if action_name == "install_custom":+ with ALLOW_URL:+ response = client.post(+ f"{base}install_custom/",+ data={"name": "Custom", "url": "https://mcp.custom-scope.example.com/mcp", "auth_type": "api_key"},+ format="json",+ )+ elif action_name == "update_tool_approval":+ response = client.patch(+ f"{base}{installation.id}/tools/{tool.tool_name}/", data={"approval_state": "approved"}, format="json"+ )+ else:+ response = client.delete(f"{base}{installation.id}/")++ assert response.status_code == status.HTTP_403_FORBIDDEN+ assert "project:write" in response.json()["detail"]+ assert MCPServerInstallation.objects.filter(team=self.team).count() == 1+ tool.refresh_from_db()+ assert tool.approval_state == "needs_approval"++ def test_write_scoped_oauth_token_can_install_custom(self) -> None:+ with ALLOW_URL:+ response = self._oauth_client("project:read project:write").post(+ f"/api/environments/{self.team.id}/mcp_server_installations/install_custom/",+ data={"name": "Custom", "url": "https://mcp.custom-scope.example.com/mcp", "auth_type": "api_key"},+ format="json",+ )++ assert response.status_code == status.HTTP_201_CREATED+ assert MCPServerInstallation.objects.filter(team=self.team, user=self.user).count() == 1+class TestMCPServiceAccountAPI(APIBaseTest):def _api_url(self, suffix: str = "") -> str: