Files
multica/server/pkg/db/queries/workspace_mcp.sql
T
2c0912b6ec MUL-5421: make workspace MCP servers an explicit per-agent assignment (#6976)
* feat: make workspace MCP servers an explicit per-agent assignment

#6961 shipped workspace MCP as a shared document every agent inherited
automatically. That is the wrong shape: a workspace MCP server should behave
like a workspace skill — a library entry that reaches an agent only when
someone gives it to that agent, with a per-agent on/off toggle. Creating one
now gives it to nobody, so nothing propagates implicitly.

Model (mirrors `skill` + `agent_skill`):

  - `workspace_mcp_server` — the library, keyed by id so renaming is safe
  - `agent_mcp_server` — the assignment, carrying its own `enabled` toggle
  - the agent's own `mcp_config` stays what it was: servers private to it

At claim time the effective set is runtime servers < (assigned AND enabled
workspace servers + the agent's own), with the agent's own entry winning a name
collision. 318 drops `workspace.mcp_config`: leaving it would keep a second,
silently inherited source alive. No data migration — converting each workspace
document into library entries would ASSIGN them to every agent, which is the
behaviour being removed, and the column shipped in the same unreleased cycle.

The write-only secret contract, the legacy-container normalization, and the
transport guards from the previous round are all preserved; the resolver is
re-pointed at the assignment list rather than a workspace document.

UI: workspace Settings → MCP is now a library (adding says so, and renaming is
allowed because assignments key off the id); the agent's MCP tab replaces the
auto "Inherited from workspace" list with the servers actually assigned to it,
a picker to add more from the library, and a per-assignment toggle.

Co-authored-by: multica-agent <[email protected]>

* fix: fence the MCP writes, stop laundering unknown transports, gate on canEdit

Three findings from review:

1. Neither new table carries a foreign key (repo rule), so nothing in the
   database catches a write landing after the row it depends on is gone. Create
   and assign now run in a transaction that joins the workspace teardown fence
   (#5219), and assign additionally holds the target server FOR SHARE while
   DeleteWorkspaceMcpServer holds it FOR UPDATE — so an assignment either lands
   before the delete and is swept, or waits and finds the server gone. Driven
   by two two-transaction race tests, verified to fail without the locks.

2. `mcpTransportOf` classified `{"type":"websocket","url":"wss://…"}` as http,
   because an unrecognised type fell through to the url check. That told the
   settings UI the guided form could edit the entry, and saving from it rewrites
   the protocol to `type: "http"` — the failure the client-side guard was added
   to prevent, reintroduced from the server. An explicit type is now returned
   verbatim; inference from command / url happens only when none was declared,
   which is lossless.

3. The agent MCP tab showed the picker, toggles, and remove button to members
   who cannot edit the agent, where every click 403s. It now takes `canEdit`
   like SkillsTab and hides the write affordances, keeping the inventory
   readable.

318 keeps dropping `workspace.mcp_config` with no data migration and no
expand-contract window, now recorded as an explicit maintainer decision (Bohan,
2026-08-14): 314 merged in this same cycle and was never deployed, so there is
no stored document to preserve and no old binary reading the column. The
comment spells out what this file would have to become if that ever stops
holding.

Co-authored-by: multica-agent <[email protected]>

---------

Co-authored-by: Bohan-J <[email protected]>
Co-authored-by: multica-agent <[email protected]>
2026-08-14 19:49:37 +08:00

87 lines
3.2 KiB
SQL

-- name: ListWorkspaceMcpServers :many
-- The workspace's MCP library. Returns `config` because the callers that need
-- it (claim assembly, the write path's read-modify-write) run server-side; the
-- HTTP layer projects it down to the non-secret inventory.
SELECT * FROM workspace_mcp_server
WHERE workspace_id = $1
ORDER BY name ASC;
-- name: LockWorkspaceMcpServerForShare :one
-- Taken by the assignment writer before it inserts. A shared row lock
-- conflicts with the exclusive lock DeleteWorkspaceMcpServer takes, so an
-- assignment can never land in the window between that delete sweeping the
-- bindings and committing — the junction has no FK to catch the orphan.
SELECT id FROM workspace_mcp_server
WHERE id = $1 AND workspace_id = $2
FOR SHARE;
-- name: LockWorkspaceMcpServerForUpdate :one
-- The delete half of the same protocol: take the row exclusively first, so a
-- concurrent assignment either lands before this transaction (and is swept) or
-- waits and then finds the server gone.
SELECT id FROM workspace_mcp_server
WHERE id = $1 AND workspace_id = $2
FOR UPDATE;
-- name: GetWorkspaceMcpServer :one
SELECT * FROM workspace_mcp_server
WHERE id = $1 AND workspace_id = $2;
-- name: CreateWorkspaceMcpServer :one
INSERT INTO workspace_mcp_server (workspace_id, name, config, created_by)
VALUES ($1, $2, $3, $4)
RETURNING *;
-- name: UpdateWorkspaceMcpServer :one
-- Full replace of one entry. The name can change here (unlike the previous
-- document model) because bindings key off the id, so a rename cannot orphan
-- the agents that use it.
UPDATE workspace_mcp_server SET
name = COALESCE(sqlc.narg('name'), name),
config = COALESCE(sqlc.narg('config'), config),
updated_at = now()
WHERE id = $1 AND workspace_id = $2
RETURNING *;
-- name: DeleteWorkspaceMcpServer :execrows
DELETE FROM workspace_mcp_server
WHERE id = $1 AND workspace_id = $2;
-- name: DeleteAgentMcpServersByServer :exec
-- Bindings carry no FK, so removing a library entry must sweep them explicitly
-- in the same transaction as the delete.
DELETE FROM agent_mcp_server WHERE server_id = $1;
-- name: ListAgentMcpServers :many
-- Every workspace server bound to this agent, enabled or not, for the agent
-- settings list. `enabled` comes from the binding.
SELECT s.id, s.workspace_id, s.name, s.config, s.created_at, s.updated_at, ams.enabled
FROM workspace_mcp_server s
JOIN agent_mcp_server ams ON ams.server_id = s.id
WHERE ams.agent_id = $1
ORDER BY s.name ASC;
-- name: ListEnabledAgentMcpServers :many
-- Claim path: only bound AND enabled servers reach the runtime. An unbound
-- workspace server is invisible to this agent — creating one gives it to
-- nobody until someone adds it here.
SELECT s.name, s.config
FROM workspace_mcp_server s
JOIN agent_mcp_server ams ON ams.server_id = s.id
WHERE ams.agent_id = $1 AND ams.enabled = TRUE
ORDER BY s.name ASC;
-- name: AddAgentMcpServer :exec
INSERT INTO agent_mcp_server (agent_id, server_id)
VALUES ($1, $2)
ON CONFLICT DO NOTHING;
-- name: SetAgentMcpServerEnabled :execrows
UPDATE agent_mcp_server
SET enabled = $3
WHERE agent_id = $1 AND server_id = $2;
-- name: RemoveAgentMcpServer :execrows
DELETE FROM agent_mcp_server
WHERE agent_id = $1 AND server_id = $2;