Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/octop/api/routers/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
RESOURCE_CONNECTOR,
GroupAccess,
)
from octop.infra.users.permissions import user_has_permission
from octop.infra.utils.locale import resolve_request_locale
from octop.infra.utils.ulid import new_ulid

Expand Down Expand Up @@ -292,7 +293,9 @@ async def _begin_oauth_flow(
ErrorCode.CONNECTOR_INVALID_CREDENTIALS,
f"OAuth for {kind} is not available",
)
elif target_type != "custom_mcp":
elif target_type == "custom_mcp":
_assert_custom_mcp_permission(user)
else:
raise OctopError(ErrorCode.CONNECTOR_INVALID_CREDENTIALS, "unsupported oauth target")

state = secrets.token_urlsafe(24)
Expand Down Expand Up @@ -607,6 +610,15 @@ def _assert_can_manage_connector(inst: Any, user: Any) -> None:
raise OctopError(ErrorCode.FORBIDDEN, "not your connector instance")


def _assert_custom_mcp_permission(user: Any) -> None:
if not user_has_permission(user, "connector_custom"):
raise OctopError(
ErrorCode.FORBIDDEN,
"permission required",
details={"permission": "connector_custom"},
)


def _raise_name_taken(name: str) -> None:
raise OctopError(
ErrorCode.CONNECTOR_NAME_TAKEN,
Expand Down Expand Up @@ -704,7 +716,7 @@ async def list_instances(

@router.get("/connectors/custom-mcp", summary="Get custom MCP servers")
async def get_custom_mcp(
user: Any = Depends(current_user),
user: Any = Depends(require_permission("connector_custom")),
server: Any = Depends(get_server),
) -> dict[str, Any]:
"""Return the user's custom MCP server map (langchain-mcp-adapters shape)."""
Expand All @@ -715,7 +727,7 @@ async def get_custom_mcp(
@router.put("/connectors/custom-mcp", summary="Save custom MCP servers")
async def put_custom_mcp(
body: CustomMcpPutBody,
user: Any = Depends(current_user),
user: Any = Depends(require_permission("connector_custom")),
server: Any = Depends(get_server),
) -> dict[str, Any]:
"""Replace the user's custom MCP servers document and reload agents."""
Expand Down Expand Up @@ -749,7 +761,7 @@ async def put_custom_mcp(
async def patch_custom_mcp_server(
server_name: str,
body: CustomMcpServerPatchBody,
user: Any = Depends(current_user),
user: Any = Depends(require_permission("connector_custom")),
server: Any = Depends(get_server),
) -> dict[str, Any]:
"""Update ``enabled`` and/or ``default_open`` for one custom MCP server."""
Expand All @@ -775,7 +787,7 @@ async def patch_custom_mcp_server(
@router.post("/connectors/custom-mcp/test", summary="Probe a custom MCP server")
async def test_custom_mcp(
body: CustomMcpTestBody,
user: Any = Depends(current_user),
user: Any = Depends(require_permission("connector_custom")),
server: Any = Depends(get_server),
) -> dict[str, Any]:
"""Probe connectivity for one custom MCP server (saved name or inline spec)."""
Expand Down Expand Up @@ -939,6 +951,7 @@ async def patch_instance(
"""Edit a connector instance without replacing its identity."""
custom_target = _resolve_custom_target(instance_id, user=user, server=server)
if custom_target is not None:
_assert_custom_mcp_permission(user)
custom_user_id, synthetic_name = custom_target
svc = _connector_service(server)
if body.status is None and body.default_open is None and body.shared is None:
Expand Down Expand Up @@ -1081,6 +1094,7 @@ async def delete_instance(
"""Disconnect and delete stored credentials for a connector instance."""
custom_target = _resolve_custom_target(instance_id, user=user, server=server)
if custom_target is not None:
_assert_custom_mcp_permission(user)
custom_user_id, synthetic_name = custom_target
svc = _connector_service(server)
servers = dict(svc.get_custom_servers(custom_user_id))
Expand Down Expand Up @@ -1161,6 +1175,7 @@ async def test_instance(
"""Probe the connector with stored credentials and return success or error details."""
custom_target = _resolve_custom_target(instance_id, user=user, server=server)
if custom_target is not None:
_assert_custom_mcp_permission(user)
custom_user_id, synthetic_name = custom_target
svc = _connector_service(server)
saved = svc.get_custom_servers(custom_user_id)
Expand Down
130 changes: 130 additions & 0 deletions tests/integration/test_connectors_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import os
import re
import sys
from html import escape
from pathlib import Path
from unittest.mock import AsyncMock, patch
Expand Down Expand Up @@ -663,6 +664,135 @@ async def test_patch_custom_mcp_server_default_open_only(env):
assert "default_open" not in patch_off.json()["servers"]["linear"]


async def test_custom_mcp_routes_require_connector_custom_permission(env):
c, _, admin_auth, _ = env
user_auth = await create_user(
c,
admin_auth,
username="custom_mcp_routes_denied",
permissions=[],
)

listed = await c.get("/api/connectors/custom-mcp", headers=user_auth)
oauth = await c.post(
"/api/connectors/oauth/start",
headers=user_auth,
json={"target": {"type": "custom_mcp", "server_name": "linear"}},
)
saved = await c.put(
"/api/connectors/custom-mcp",
headers=user_auth,
json={
"servers": {
"linear": {
"transport": "streamable_http",
"url": "https://mcp.example.com/mcp",
}
}
},
)

assert [listed.status_code, oauth.status_code, saved.status_code] == [403, 403, 403]


async def test_custom_mcp_patch_and_delete_require_connector_custom_permission(env):
c, server, admin_auth, _ = env
user_auth = await create_user(
c,
admin_auth,
username="custom_mcp_mutation_denied",
permissions=[],
)
user_id = await resolve_user_id(c, admin_auth, "custom_mcp_mutation_denied")
svc = _connector_service(server)
svc.put_custom_servers(
user_id,
{
"linear": {
"transport": "streamable_http",
"url": "https://mcp.example.com/mcp",
}
},
)

direct_patch = await c.patch(
"/api/connectors/custom-mcp/servers/linear",
headers=user_auth,
json={"enabled": False},
)
instance_patch = await c.patch(
"/api/connector-instances/custom:linear",
headers=user_auth,
json={"status": "disabled"},
)
deleted = await c.delete("/api/connector-instances/custom:linear", headers=user_auth)

assert [direct_patch.status_code, instance_patch.status_code, deleted.status_code] == [
403,
403,
403,
]
assert "linear" in svc.get_custom_servers(user_id)


async def test_custom_mcp_probe_requires_permission_before_starting_stdio(env, tmp_path: Path):
c, _, admin_auth, _ = env
user_auth = await create_user(
c,
admin_auth,
username="custom_mcp_probe_denied",
permissions=[],
)
marker = tmp_path / "stdio-started"
script = f"from pathlib import Path; Path({str(marker)!r}).write_text('started')"

response = await c.post(
"/api/connectors/custom-mcp/test",
headers=user_auth,
json={
"server": {
"transport": "stdio",
"command": sys.executable,
"args": ["-c", script],
}
},
)

assert (response.status_code, marker.exists()) == (403, False)


async def test_custom_mcp_instance_probe_requires_permission_before_starting_stdio(
env, tmp_path: Path
):
c, server, admin_auth, _ = env
user_auth = await create_user(
c,
admin_auth,
username="custom_mcp_instance_probe_denied",
permissions=[],
)
user_id = await resolve_user_id(c, admin_auth, "custom_mcp_instance_probe_denied")
marker = tmp_path / "instance-stdio-started"
script = f"from pathlib import Path; Path({str(marker)!r}).write_text('started')"
_connector_service(server).put_custom_servers(
user_id,
{
"linear": {
"transport": "stdio",
"command": sys.executable,
"args": ["-c", script],
}
},
)

response = await c.post(
"/api/connector-instances/custom:linear/test",
headers=user_auth,
)

assert (response.status_code, marker.exists()) == (403, False)


async def test_shared_custom_mcp_is_visible_with_collision_safe_name(env):
c, _, admin_auth, _ = env
put = await c.put(
Expand Down
Loading