Files
DeepTutor/deeptutor/api/routers/mcp_settings.py
Bingxi Zhao (Frank) 740ec413a0 release: v1.5.7
Per-account MCP Services and CLI Apps — the two ways a deployment plugs in
tools DeepTutor never shipped — plus the isolation that makes them safe on a
shared install.

- MCP Services: a 45-entry curated store of hosted servers each account
  installs for itself, OAuth 2.1 (PKCE + dynamic client registration) for the
  servers that require it, static keys held by reference and resolved in memory
  at connect time, stdio refused for self-service. Settings no longer
  configures MCP; /settings/mcp redirects to the store.
- CLI Apps: 101 tools snapshotted from the two CLI-Anything registries.
  Installs are admin-only, the registry's install_cmd is parsed into an argv we
  build rather than executed, npm install scripts are forced off, and 66
  first-party entries are pinned to one reviewed commit. Apps land in
  data/cli-apps, one environment each, mounted read-only into the runner.
- Credentials that authorise a person move to data/system, the one branch the
  exec sandbox never mounts: Codex tokens relocate themselves on first use
  (refusing to follow a symlink), per-user MCP configs and keys land beside
  them.
- Grants gain a cli_apps whitelist with the same deny-by-default posture as
  MCP tools; a per-turn registry view keeps one owner's provider tools out of
  the process-global registry and refuses an unauthorised tool at execute, not
  just in the manifest.
- The composer reports what filled this turn's context window, a gutter rail
  jumps back through long conversations, and the workspace collapses to a
  drawer on small screens.
- The composer keeps focus after sending and across tab switches (#720);
  released images carry provenance and SBOM attestations (#722).

README prose, all ten translated READMEs, and the docs-site pages that named
Settings → MCP are updated to match. Release notes:
assets/releases/ver1-5-7.md
2026-07-31 00:25:48 +08:00

134 lines
4.4 KiB
Python

"""
MCP Settings API Router
=======================
Manage the deployment-global MCP server registry: read/update the config,
inspect live connection status, and probe a server before saving.
Mounted at ``/api/v1/settings/mcp``. Admin-gated: the registry is
deployment-global state, and a stdio server's ``command`` runs on the host
as the app user — letting non-admins edit it would be privilege escalation.
Per-user MCP access is granted through the multi-user grant whitelist
(``mcp_tools``), not by sharing this registry.
"""
from __future__ import annotations
from typing import Any
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field, ValidationError
from deeptutor.api.routers.auth import require_admin
from deeptutor.core.i18n import t
from deeptutor.services.mcp import (
MCPConfig,
MCPServerConfig,
get_mcp_manager,
load_mcp_config,
save_mcp_config,
validate_mcp_url,
)
from deeptutor.services.mcp.manager import probe_server
router = APIRouter(dependencies=[Depends(require_admin)])
class MCPSettingsPayload(BaseModel):
servers: dict[str, MCPServerConfig] = Field(default_factory=dict)
def _validate_servers(config: MCPConfig) -> None:
for name, cfg in config.servers.items():
transport = cfg.resolved_type()
if transport is None:
raise HTTPException(
status_code=400,
detail=t("mcp.configure_command_or_url", name=name),
)
if transport in {"sse", "streamableHttp"}:
ok, error = validate_mcp_url(cfg.url)
if not ok:
raise HTTPException(
status_code=400, detail=t("mcp.server_error", name=name, error=error)
)
@router.get("")
async def get_mcp_settings() -> dict[str, Any]:
config = load_mcp_config()
manager = get_mcp_manager()
await manager.ensure_started()
return {
"servers": {name: cfg.model_dump(mode="json") for name, cfg in config.servers.items()},
"status": manager.status(),
}
@router.put("")
async def update_mcp_settings(payload: MCPSettingsPayload) -> dict[str, Any]:
try:
config = MCPConfig(servers=payload.servers)
except (ValidationError, ValueError) as exc:
raise HTTPException(status_code=400, detail=str(exc))
_validate_servers(config)
save_mcp_config(config)
manager = get_mcp_manager()
await manager.reload()
return {"status": manager.status()}
@router.put("/servers/{name}")
async def upsert_mcp_server(name: str, cfg: MCPServerConfig) -> dict[str, Any]:
"""Upsert one server, leaving every other entry byte-identical.
The whole-map ``PUT`` above cannot express "change this one": a client has to
send back everything it read, so it silently drops any field it does not
model (a hand-written ``disabled_tools`` blocklist) and overwrites whatever
a second administrator saved in between.
"""
config = load_mcp_config()
servers = dict(config.servers)
servers[name] = cfg
try:
updated = MCPConfig(servers=servers)
except (ValidationError, ValueError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
_validate_servers(MCPConfig(servers={name: cfg}))
save_mcp_config(updated)
manager = get_mcp_manager()
await manager.reload()
return {
"servers": {key: value.model_dump(mode="json") for key, value in updated.servers.items()},
"status": manager.status(),
}
@router.delete("/servers/{name}")
async def delete_mcp_server(name: str) -> dict[str, Any]:
config = load_mcp_config()
servers = {key: value for key, value in config.servers.items() if key != name}
updated = MCPConfig(servers=servers)
save_mcp_config(updated)
manager = get_mcp_manager()
await manager.reload()
return {
"servers": {key: value.model_dump(mode="json") for key, value in updated.servers.items()},
"status": manager.status(),
}
@router.post("/test")
async def test_mcp_server(cfg: MCPServerConfig) -> dict[str, Any]:
transport = cfg.resolved_type()
if transport is None:
raise HTTPException(
status_code=400,
detail=t("mcp.configure_before_testing"),
)
if transport in {"sse", "streamableHttp"}:
ok, error = validate_mcp_url(cfg.url)
if not ok:
raise HTTPException(status_code=400, detail=error)
return await probe_server(cfg)