From 198ca4cf9563500c6b00c6cd3beaa0ac67e45d7e Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 28 Apr 2026 20:19:28 +0000 Subject: [PATCH 01/11] Auto-sync MCP broker config from grants --- app/components/McpConfigurationPanel.tsx | 76 +++++++++++++++++++++--- tests/mcp-configuration-page-check.mjs | 4 ++ 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/app/components/McpConfigurationPanel.tsx b/app/components/McpConfigurationPanel.tsx index 3be00ac..c316ed7 100644 --- a/app/components/McpConfigurationPanel.tsx +++ b/app/components/McpConfigurationPanel.tsx @@ -134,8 +134,10 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati setServers(Array.isArray(data.servers) ? data.servers : []) setConfig(data.config || { mcpServers: {} }) setMessage(success) + return true } catch (error) { setMessage(error instanceof Error ? error.message : "Failed to update MCP configuration") + return false } finally { setSaving(false) } @@ -150,12 +152,72 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati current.delete(sandbox.id) current.delete(sandbox.name) if (allowed) current.add(sandbox.id) + const hasOtherMcpAccess = servers.some((candidate) => candidate.id !== server.id && sandboxAllowed(candidate, sandbox)) return postUpdate({ action: "update-access", serverId: server.id, accessMode: "allow_only", allowedSandboxIds: Array.from(current), - }, `${server.name} sandbox access updated.`) + }, `${server.name} sandbox access updated.`).then((updated) => { + if (!updated) return + return syncSandboxBrokerConfig(sandbox, allowed || hasOtherMcpAccess ? "sync" : "revoke") + }) + } + + async function syncSandboxBrokerConfig(sandbox: McpSandbox, action: "sync" | "revoke" = "sync") { + try { + setSaving(true) + const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/mcp`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ sandboxName: sandbox.name, action }), + }) + const data = await response.json() + if (!response.ok) throw new Error(data.error || "Failed to sync MCP broker config") + setMessage(data.note || (action === "revoke" ? `${sandbox.name} MCP broker config revoked.` : `${sandbox.name} MCP broker config synced.`)) + } catch (error) { + setMessage(error instanceof Error ? error.message : "Failed to sync MCP broker config") + } finally { + setSaving(false) + } + } + + async function syncAccessModeChange(server: McpServerInstall, accessMode: string) { + const updated = await postUpdate({ + action: "update-access", + serverId: server.id, + accessMode, + }, `${server.name} availability updated.`) + if (!updated) return + + if (accessMode === "allow_all") { + await Promise.all(sandboxes.map((sandbox) => syncSandboxBrokerConfig(sandbox))) + return + } + + if (accessMode === "disabled") { + await Promise.all(sandboxes + .filter((sandbox) => sandboxAllowed(server, sandbox)) + .map((sandbox) => { + const hasOtherMcpAccess = servers.some((candidate) => candidate.id !== server.id && sandboxAllowed(candidate, sandbox)) + return syncSandboxBrokerConfig(sandbox, hasOtherMcpAccess ? "sync" : "revoke") + })) + } + } + + async function setServerEnabled(server: McpServerInstall, enabled: boolean) { + const updated = await postUpdate({ + action: "update-access", + serverId: server.id, + enabled, + }, `${server.name} ${enabled ? "enabled" : "disabled"}.`) + if (!updated) return + + const affectedSandboxes = sandboxes.filter((sandbox) => sandboxAllowed(server, sandbox)) + await Promise.all(affectedSandboxes.map((sandbox) => { + const hasOtherMcpAccess = servers.some((candidate) => candidate.id !== server.id && sandboxAllowed(candidate, sandbox)) + return syncSandboxBrokerConfig(sandbox, enabled || hasOtherMcpAccess ? "sync" : "revoke") + })) } function editableServerJson(server: McpServerInstall) { @@ -378,14 +440,14 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati
+
+ Upload Launch Mode + +
Upload Entrypoint - setUploadEntrypoint(event.target.value)} placeholder="server.py, src/server.py, index.js" className="field-control w-full px-3 py-2 text-sm font-mono" /> + setUploadEntrypoint(event.target.value)} placeholder={uploadEntryMode === "python-module" ? "isaac_mcp_poc.server" : uploadEntryMode === "console-script" ? "isaac-mcp-poc" : "server.py, src/server.py, index.js"} className="field-control w-full px-3 py-2 text-sm font-mono" />
diff --git a/tests/mcp-configuration-page-check.mjs b/tests/mcp-configuration-page-check.mjs index 044ae50..eb3c0a1 100644 --- a/tests/mcp-configuration-page-check.mjs +++ b/tests/mcp-configuration-page-check.mjs @@ -99,6 +99,10 @@ assert.match(uploadRouteSource, /requirements\.txt/, 'MCP upload API must instal assert.match(uploadRouteSource, /pyproject\.toml/, 'MCP upload API must install Python projects when present') assert.match(uploadRouteSource, /package\.json/, 'MCP upload API must install Node dependencies when present') assert.match(uploadRouteSource, /\.venv/, 'MCP upload API must isolate Python uploads in a per-server virtualenv') +assert.match(uploadRouteSource, /entryMode/, 'MCP upload API must support non-file launch modes') +assert.match(uploadRouteSource, /python-module/, 'MCP upload API must support Python module launch') +assert.match(uploadRouteSource, /console-script/, 'MCP upload API must support installed console script launch') +assert.match(uploadRouteSource, /resolveProjectRoot/, 'MCP upload API must find project manifests inside uploaded directories') assert.match(uploadRouteSource, /installMcpServer/, 'MCP upload API must install uploaded server bundles') assert.match(healthRouteSource, /listBrokerServerTools/, 'MCP health API must inspect server tools through the broker client') assert.match(healthRouteSource, /enabledServers/, 'MCP health API must check enabled MCP servers') @@ -114,6 +118,9 @@ assert.match(panelSource, /Choose Archive/, 'MCP custom server upload must accep assert.match(panelSource, /\/api\/mcp\/upload/, 'MCP custom server upload must call the upload API') assert.match(panelSource, /dependencyInstall/, 'MCP custom server upload must surface dependency bootstrap results') assert.match(panelSource, /uploadEntrypoint/, 'MCP custom server upload must collect an entrypoint') +assert.match(panelSource, /uploadEntryMode/, 'MCP custom server upload must collect a launch mode') +assert.match(panelSource, /Python module/, 'MCP custom server upload must expose Python module launch mode') +assert.match(panelSource, /Console script/, 'MCP custom server upload must expose console script launch mode') assert.match(panelSource, /serverPayloadFromJson/, 'MCP page must parse edited server JSON') assert.match(panelSource, /startEditingServer/, 'MCP installed server list must expose edit state') assert.match(panelSource, /Server JSON/, 'MCP installed server editor must render an inline JSON file editor') From e86358c719e026830ffb157a47d53775b0145b6d Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 28 Apr 2026 21:02:06 +0000 Subject: [PATCH 05/11] Add MCP server preflight checks --- app/api/mcp/preflight/route.ts | 45 +++++++++ app/api/mcp/upload/route.ts | 27 +++++- app/components/McpConfigurationPanel.tsx | 69 +++++++++++++- app/lib/mcpPreflight.ts | 113 +++++++++++++++++++++++ tests/mcp-configuration-page-check.mjs | 15 ++- 5 files changed, 265 insertions(+), 4 deletions(-) create mode 100644 app/api/mcp/preflight/route.ts create mode 100644 app/lib/mcpPreflight.ts diff --git a/app/api/mcp/preflight/route.ts b/app/api/mcp/preflight/route.ts new file mode 100644 index 0000000..8c03768 --- /dev/null +++ b/app/api/mcp/preflight/route.ts @@ -0,0 +1,45 @@ +import { NextResponse } from "next/server" +import { preflightMcpServer } from "@/app/lib/mcpPreflight" +import { listMcpServers, type McpServerInstall } from "@/app/lib/mcpServerStore" + +function normalizeServer(body: any, installed?: McpServerInstall | null): McpServerInstall { + const now = new Date().toISOString() + const server = installed || body || {} + return { + id: String(body?.id || server.id || body?.serverId || "preflight-server"), + name: String(body?.name || server.name || body?.id || body?.serverId || "Preflight Server"), + summary: String(body?.summary || server.summary || ""), + websiteUrl: typeof body?.websiteUrl === "string" ? body.websiteUrl : server.websiteUrl, + transport: body?.transport === "http" || server.transport === "http" ? "http" : "stdio", + command: String(body?.command || server.command || ""), + args: Array.isArray(body?.args) ? body.args.map(String) : Array.isArray(server.args) ? server.args.map(String) : [], + env: body?.env && typeof body.env === "object" && !Array.isArray(body.env) ? body.env : server.env || {}, + tags: Array.isArray(body?.tags) ? body.tags.map(String) : Array.isArray(server.tags) ? server.tags.map(String) : ["custom"], + installedAt: server.installedAt || now, + updatedAt: now, + source: body?.source === "catalog" || body?.source === "registry" || body?.source === "custom" ? body.source : server.source || "custom", + enabled: true, + accessMode: "disabled", + allowedSandboxIds: [], + } +} + +export async function POST(request: Request) { + try { + const body = await request.json().catch(() => ({})) + let installed: McpServerInstall | null = null + if (typeof body?.serverId === "string" && body.serverId.trim()) { + const inventory = await listMcpServers() + installed = inventory.servers.find((server) => server.id === body.serverId.trim()) || null + if (!installed) throw new Error("MCP server is not installed") + } + + const server = normalizeServer(body, installed) + if (!server.command) throw new Error("server command is required") + const preflight = await preflightMcpServer(server) + return NextResponse.json({ ok: preflight.ok, serverId: server.id, preflight }) + } catch (error) { + const message = error instanceof Error ? error.message : "Failed to preflight MCP server" + return NextResponse.json({ ok: false, error: message }, { status: /required|installed/.test(message) ? 400 : 500 }) + } +} diff --git a/app/api/mcp/upload/route.ts b/app/api/mcp/upload/route.ts index 5cef51c..c51d096 100644 --- a/app/api/mcp/upload/route.ts +++ b/app/api/mcp/upload/route.ts @@ -4,6 +4,7 @@ import { access, mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises import os from "node:os" import path from "node:path" import { promisify } from "node:util" +import { preflightMcpServer } from "@/app/lib/mcpPreflight" import { installMcpServer, listMcpServers } from "@/app/lib/mcpServerStore" const execFileAsync = promisify(execFile) @@ -310,7 +311,7 @@ export async function POST(request: Request) { const projectRoot = await resolveProjectRoot(root, entrypointPath) const bootstrap = await bootstrapUploadedServer(projectRoot, runtime, entrypoint) const launch = await launchCommandForUpload(projectRoot, bootstrap, entryMode, entrypoint, entrypointPath, parseLines(form.get("args"))) - const server = await installMcpServer({ + const candidate = { id, name, summary, @@ -321,6 +322,29 @@ export async function POST(request: Request) { tags: ["uploaded", "custom"], source: "custom", enabled: true, + } as const satisfies { + id: string + name: string + summary: string + transport: "stdio" + command: string + args: string[] + env: Record + tags: string[] + source: "custom" + enabled: boolean + } + const preflight = await preflightMcpServer({ + ...candidate, + installedAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + accessMode: "disabled", + allowedSandboxIds: [], + }) + const server = await installMcpServer({ + ...candidate, + enabled: preflight.ok, + tags: preflight.ok ? candidate.tags : [...candidate.tags, "preflight-failed"], }) return NextResponse.json({ @@ -332,6 +356,7 @@ export async function POST(request: Request) { kind: bootstrap.kind, logs: bootstrap.logs, }, + preflight, ...(await listMcpServers()), }) } catch (error) { diff --git a/app/components/McpConfigurationPanel.tsx b/app/components/McpConfigurationPanel.tsx index 7d4a1e3..cae5c2e 100644 --- a/app/components/McpConfigurationPanel.tsx +++ b/app/components/McpConfigurationPanel.tsx @@ -32,6 +32,21 @@ type McpResponse = { error?: string } +type McpPreflightResult = { + ok: boolean + checkedAt: string + toolCount: number + tools: string[] + durationMs: number + error?: string + diagnosis: { + summary: string + likelyCauses: string[] + suggestedFixes: string[] + confidence: "low" | "medium" | "high" + } +} + type McpSandbox = { id: string name: string @@ -92,6 +107,7 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati const [registryLoading, setRegistryLoading] = useState(false) const [registryPage, setRegistryPage] = useState(1) const [lastRegistrySearch, setLastRegistrySearch] = useState("") + const [preflightResults, setPreflightResults] = useState>({}) const [catalogOpen, setCatalogOpen] = useState(false) const [customOpen, setCustomOpen] = useState(false) @@ -293,6 +309,38 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati } } + function summarizePreflight(preflight: McpPreflightResult) { + if (preflight.ok) { + return `${preflight.diagnosis.summary}${preflight.tools.length > 0 ? ` Tools: ${preflight.tools.join(", ")}.` : ""}` + } + const causes = preflight.diagnosis.likelyCauses.length > 0 + ? `\nLikely: ${preflight.diagnosis.likelyCauses.join(" ")}` + : "" + const fixes = preflight.diagnosis.suggestedFixes.length > 0 + ? `\nTry: ${preflight.diagnosis.suggestedFixes.join(" ")}` + : "" + return `${preflight.diagnosis.summary}\n${preflight.error || "MCP tool discovery failed."}${causes}${fixes}` + } + + async function preflightServer(server: McpServerInstall) { + try { + setSaving(true) + const response = await fetch("/api/mcp/preflight", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ serverId: server.id }), + }) + const data = await response.json() as { preflight?: McpPreflightResult; error?: string } + if (!response.ok || !data.preflight) throw new Error(data.error || "Failed to preflight MCP server") + setPreflightResults((current) => ({ ...current, [server.id]: data.preflight as McpPreflightResult })) + setMessage(`${server.name} preflight ${data.preflight.ok ? "passed" : "failed"}.\n${summarizePreflight(data.preflight)}`) + } catch (error) { + setMessage(error instanceof Error ? error.message : "Failed to preflight MCP server") + } finally { + setSaving(false) + } + } + async function uploadServer() { try { if (!uploadArchive && uploadFiles.length === 0) { @@ -318,17 +366,23 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati }) } const response = await fetch("/api/mcp/upload", { method: "POST", body: form }) - const data = await response.json() as McpResponse & { dependencyInstall?: { kind?: string; logs?: string[] } } + const data = await response.json() as McpResponse & { dependencyInstall?: { kind?: string; logs?: string[] }; preflight?: McpPreflightResult; server?: McpServerInstall } if (!response.ok) throw new Error(data.error || "Failed to upload MCP server") setCatalog(Array.isArray(data.catalog) ? data.catalog : catalog) setServers(Array.isArray(data.servers) ? data.servers : []) setConfig(data.config || { mcpServers: {} }) + if (data.preflight && data.server?.id) { + setPreflightResults((current) => ({ ...current, [data.server!.id]: data.preflight as McpPreflightResult })) + } setUploadFiles([]) setUploadPaths([]) setUploadArchive(null) const installKind = data.dependencyInstall?.kind || "generic" const installLogCount = data.dependencyInstall?.logs?.filter(Boolean).length || 0 - setMessage(`${customName} uploaded. ${installKind === "generic" ? "No dependency bootstrap was needed." : `${installKind} dependencies bootstrapped${installLogCount > 0 ? ` with ${installLogCount} install step${installLogCount === 1 ? "" : "s"}` : ""}.`}`) + const preflightNote = data.preflight + ? `\nPreflight ${data.preflight.ok ? "passed" : "failed; server saved disabled"}.\n${summarizePreflight(data.preflight)}` + : "" + setMessage(`${customName} uploaded. ${installKind === "generic" ? "No dependency bootstrap was needed." : `${installKind} dependencies bootstrapped${installLogCount > 0 ? ` with ${installLogCount} install step${installLogCount === 1 ? "" : "s"}` : ""}.`}${preflightNote}`) } catch (error) { setMessage(error instanceof Error ? error.message : "Failed to upload MCP server") } finally { @@ -785,6 +839,9 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati + @@ -792,6 +849,14 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati Remove
+ {preflightResults[server.id] && ( +
+
+ Preflight {preflightResults[server.id].ok ? "passed" : "failed"} / {preflightResults[server.id].durationMs} ms +
+

{summarizePreflight(preflightResults[server.id])}

+
+ )} {editingServerId === server.id && (
diff --git a/app/lib/mcpPreflight.ts b/app/lib/mcpPreflight.ts new file mode 100644 index 0000000..48b2a37 --- /dev/null +++ b/app/lib/mcpPreflight.ts @@ -0,0 +1,113 @@ +import { listBrokerServerTools } from "./mcpBrokerClient" +import type { McpServerInstall } from "./mcpServerStore" + +export type McpPreflightResult = { + ok: boolean + checkedAt: string + toolCount: number + tools: string[] + durationMs: number + error?: string + diagnosis: { + summary: string + likelyCauses: string[] + suggestedFixes: string[] + confidence: "low" | "medium" | "high" + } +} + +function commandLine(server: Pick) { + return [server.command, ...(server.args || [])].filter(Boolean).join(" ") +} + +function diagnoseFailure(server: Pick, error: string): McpPreflightResult["diagnosis"] { + const joinedArgs = (server.args || []).join(" ") + const command = commandLine(server) + const likelyCauses: string[] = [] + const suggestedFixes: string[] = [] + let confidence: McpPreflightResult["diagnosis"]["confidence"] = "medium" + + if (/attempted relative import with no known parent package/i.test(error)) { + confidence = "high" + likelyCauses.push("The Python server was launched as a raw file even though it uses package-relative imports.") + suggestedFixes.push("Switch the upload launch mode to Python module and use an entrypoint like package_name.server.") + suggestedFixes.push("Alternatively use Console script if pyproject.toml defines a [project.scripts] entry.") + } + + if (/unexpected keyword argument ['\"]description['\"]/i.test(error) || /FastMCP\.__init__\(\).*description/i.test(error)) { + confidence = "high" + likelyCauses.push("The server code targets a different MCP SDK FastMCP constructor than the one installed in the upload environment.") + suggestedFixes.push("Remove the description= argument from FastMCP(...) or pin the mcp dependency to a version that supports it.") + } + + if (/No module named ['\"][^'\"]+['\"]|ModuleNotFoundError/i.test(error)) { + confidence = confidence === "high" ? "high" : "medium" + likelyCauses.push("A Python dependency is missing from requirements.txt or pyproject.toml.") + suggestedFixes.push("Add the missing package to project dependencies, then upload again so the virtualenv is rebuilt.") + } + + if (/command not found|ENOENT|spawn .* ENOENT/i.test(error)) { + confidence = "high" + likelyCauses.push("The configured command is not available on the controller host PATH.") + suggestedFixes.push("Use an installed command, an absolute path, or upload a bundled server so OpenShell Control can create the runtime environment.") + } + + if (/ECONNREFUSED|ENOTFOUND|404 Not Found|fetch failed|connect/i.test(error) && server.transport === "http") { + likelyCauses.push("The remote MCP endpoint did not answer as a streamable HTTP MCP server.") + suggestedFixes.push("Confirm the endpoint URL points at the MCP route, not just the service root.") + } + + if (/^-m\s+\S+/.test(joinedArgs) || joinedArgs.includes(" -m ")) { + suggestedFixes.push("Confirm the Python module path is importable after dependency installation.") + } + + if (likelyCauses.length === 0) { + likelyCauses.push("The server process did not complete MCP initialization and tool discovery.") + suggestedFixes.push("Run the command manually on the controller host and check stderr for startup errors.") + if (/\.py(\s|$)/.test(command)) { + suggestedFixes.push("If this is a Python package, try Python module or Console script launch mode instead of File.") + } + confidence = "low" + } + + return { + summary: `Preflight could not list MCP tools for: ${command}`, + likelyCauses, + suggestedFixes, + confidence, + } +} + +export async function preflightMcpServer(server: McpServerInstall): Promise { + const checkedAt = new Date().toISOString() + const startedAt = Date.now() + try { + const tools = await listBrokerServerTools(server) + return { + ok: true, + checkedAt, + toolCount: tools.length, + tools: tools.slice(0, 12).map((tool) => tool.name), + durationMs: Date.now() - startedAt, + diagnosis: { + summary: tools.length > 0 + ? `Preflight listed ${tools.length} MCP tool${tools.length === 1 ? "" : "s"}.` + : "Preflight connected but the server did not report any tools.", + likelyCauses: [], + suggestedFixes: tools.length > 0 ? [] : ["Confirm this server is expected to expose tools rather than only resources or prompts."], + confidence: "high", + }, + } + } catch (error) { + const message = error instanceof Error ? error.message : "MCP server preflight failed" + return { + ok: false, + checkedAt, + toolCount: 0, + tools: [], + durationMs: Date.now() - startedAt, + error: message, + diagnosis: diagnoseFailure(server, message), + } + } +} diff --git a/tests/mcp-configuration-page-check.mjs b/tests/mcp-configuration-page-check.mjs index eb3c0a1..a349468 100644 --- a/tests/mcp-configuration-page-check.mjs +++ b/tests/mcp-configuration-page-check.mjs @@ -11,12 +11,14 @@ const manifestPath = path.join(root, 'app/lib/sandboxMcpManifest.ts') const privilegedFilesPath = path.join(root, 'app/lib/sandboxPrivilegedFiles.ts') const routePath = path.join(root, 'app/api/mcp/route.ts') const uploadRoutePath = path.join(root, 'app/api/mcp/upload/route.ts') +const preflightRoutePath = path.join(root, 'app/api/mcp/preflight/route.ts') const healthRoutePath = path.join(root, 'app/api/mcp/health/route.ts') const registryRoutePath = path.join(root, 'app/api/mcp/registry/route.ts') const brokerCapabilitiesRoutePath = path.join(root, 'app/api/mcp/broker/capabilities/route.ts') const brokerCallRoutePath = path.join(root, 'app/api/mcp/broker/call/route.ts') const sandboxMcpRoutePath = path.join(root, 'app/api/sandbox/[sandboxId]/mcp/route.ts') const brokerUrlPath = path.join(root, 'app/lib/mcpBrokerUrl.ts') +const preflightLibPath = path.join(root, 'app/lib/mcpPreflight.ts') const middlewarePath = path.join(root, 'middleware.ts') const panelPath = path.join(root, 'app/components/McpConfigurationPanel.tsx') const helpPath = path.join(root, 'app/components/HelpPanel.tsx') @@ -24,7 +26,7 @@ const sidebarPath = path.join(root, 'app/components/Sidebar.tsx') const sandboxListPath = path.join(root, 'app/components/SandboxList.tsx') const pagePath = path.join(root, 'app/page.tsx') -const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privilegedFilesSource, routeSource, uploadRouteSource, healthRouteSource, registryRouteSource, brokerCapabilitiesRouteSource, brokerCallRouteSource, sandboxMcpRouteSource, brokerUrlSource, middlewareSource, panelSource, helpSource, sidebarSource, sandboxListSource, pageSource] = await Promise.all([ +const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privilegedFilesSource, routeSource, uploadRouteSource, preflightRouteSource, healthRouteSource, registryRouteSource, brokerCapabilitiesRouteSource, brokerCallRouteSource, sandboxMcpRouteSource, brokerUrlSource, preflightLibSource, middlewareSource, panelSource, helpSource, sidebarSource, sandboxListSource, pageSource] = await Promise.all([ readFile(storePath, 'utf8'), readFile(brokerStorePath, 'utf8'), readFile(brokerClientPath, 'utf8'), @@ -32,12 +34,14 @@ const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privi readFile(privilegedFilesPath, 'utf8'), readFile(routePath, 'utf8'), readFile(uploadRoutePath, 'utf8'), + readFile(preflightRoutePath, 'utf8'), readFile(healthRoutePath, 'utf8'), readFile(registryRoutePath, 'utf8'), readFile(brokerCapabilitiesRoutePath, 'utf8'), readFile(brokerCallRoutePath, 'utf8'), readFile(sandboxMcpRoutePath, 'utf8'), readFile(brokerUrlPath, 'utf8'), + readFile(preflightLibPath, 'utf8'), readFile(middlewarePath, 'utf8'), readFile(panelPath, 'utf8'), readFile(helpPath, 'utf8'), @@ -103,7 +107,14 @@ assert.match(uploadRouteSource, /entryMode/, 'MCP upload API must support non-fi assert.match(uploadRouteSource, /python-module/, 'MCP upload API must support Python module launch') assert.match(uploadRouteSource, /console-script/, 'MCP upload API must support installed console script launch') assert.match(uploadRouteSource, /resolveProjectRoot/, 'MCP upload API must find project manifests inside uploaded directories') +assert.match(uploadRouteSource, /preflightMcpServer/, 'MCP upload API must preflight uploaded server bundles') +assert.match(uploadRouteSource, /preflight-failed/, 'MCP upload API must mark failed preflight installs') assert.match(uploadRouteSource, /installMcpServer/, 'MCP upload API must install uploaded server bundles') +assert.match(preflightRouteSource, /export async function POST/, 'MCP preflight API must expose a POST check endpoint') +assert.match(preflightRouteSource, /preflightMcpServer/, 'MCP preflight API must call the shared preflight helper') +assert.match(preflightLibSource, /listBrokerServerTools/, 'MCP preflight must validate servers through SDK tool discovery') +assert.match(preflightLibSource, /attempted relative import/, 'MCP preflight assistant must recognize Python package launch failures') +assert.match(preflightLibSource, /FastMCP/, 'MCP preflight assistant must recognize MCP SDK compatibility failures') assert.match(healthRouteSource, /listBrokerServerTools/, 'MCP health API must inspect server tools through the broker client') assert.match(healthRouteSource, /enabledServers/, 'MCP health API must check enabled MCP servers') assert.match(registryRouteSource, /registry\.modelcontextprotocol\.io/, 'MCP registry search should default to the official registry') @@ -121,6 +132,8 @@ assert.match(panelSource, /uploadEntrypoint/, 'MCP custom server upload must col assert.match(panelSource, /uploadEntryMode/, 'MCP custom server upload must collect a launch mode') assert.match(panelSource, /Python module/, 'MCP custom server upload must expose Python module launch mode') assert.match(panelSource, /Console script/, 'MCP custom server upload must expose console script launch mode') +assert.match(panelSource, /preflightServer/, 'MCP page must let operators preflight installed servers') +assert.match(panelSource, /Preflight/, 'MCP page must show preflight controls and results') assert.match(panelSource, /serverPayloadFromJson/, 'MCP page must parse edited server JSON') assert.match(panelSource, /startEditingServer/, 'MCP installed server list must expose edit state') assert.match(panelSource, /Server JSON/, 'MCP installed server editor must render an inline JSON file editor') From 2f91d8b706bbdc51be0ed1704d32e2bcdf63ae2c Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 28 Apr 2026 21:13:22 +0000 Subject: [PATCH 06/11] Make sandbox restart non-destructive --- app/api/sandbox/[sandboxId]/restart/route.ts | 103 +++++++++++-------- app/components/SandboxList.tsx | 14 +-- tests/sandbox-lifecycle-check.mjs | 7 +- 3 files changed, 73 insertions(+), 51 deletions(-) diff --git a/app/api/sandbox/[sandboxId]/restart/route.ts b/app/api/sandbox/[sandboxId]/restart/route.ts index 2118ac7..fae403c 100644 --- a/app/api/sandbox/[sandboxId]/restart/route.ts +++ b/app/api/sandbox/[sandboxId]/restart/route.ts @@ -1,12 +1,11 @@ import { NextResponse } from "next/server" -import { spawn } from "node:child_process" -import { applySandboxInferenceProfile } from "@/app/lib/sandboxInferenceApply" -import { getSandboxInferenceConfig } from "@/app/lib/sandboxInferenceStore" +import { execFile } from "node:child_process" +import { promisify } from "node:util" +import { OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands" import { inspectSandbox, resolveSandboxRef } from "@/app/lib/openshellHost" -const DOCKER_BIN = process.env.DOCKER_BIN || "docker" -const OPENSHELL_CLUSTER_CONTAINER = process.env.OPENSHELL_CLUSTER_CONTAINER || "openshell-cluster-nemoclaw" -const OPENSHELL_SANDBOX_NAMESPACE = process.env.OPENSHELL_SANDBOX_NAMESPACE || "openshell" +const execFileAsync = promisify(execFile) +const SANDBOX_DASHBOARD_REMOTE_PORT = Number.parseInt(process.env.OPENCLAW_SANDBOX_DASHBOARD_REMOTE_PORT || "18789", 10) function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)) @@ -18,24 +17,25 @@ function validateSandboxName(value: string) { return value } -async function runKubectl(args: string[]) { - return await new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve, reject) => { - const child = spawn(DOCKER_BIN, ["exec", OPENSHELL_CLUSTER_CONTAINER, "kubectl", ...args], { - stdio: ["ignore", "pipe", "pipe"], - }) - let stdout = "" - let stderr = "" - child.stdout.on("data", (chunk) => { stdout += String(chunk) }) - child.stderr.on("data", (chunk) => { stderr += String(chunk) }) - child.on("error", reject) - child.on("close", (code) => resolve({ stdout: stdout.trim(), stderr: stderr.trim(), code })) +async function runOpenShell(args: string[], timeout = 30000) { + const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, { + env: hostCommandEnv({ + OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw", + }), + timeout, + maxBuffer: 4 * 1024 * 1024, }) + return { stdout: String(stdout).trim(), stderr: String(stderr).trim() } +} + +async function runSandboxShell(sandboxName: string, script: string, timeout = 30000) { + return runOpenShell(["sandbox", "exec", "-n", sandboxName, "--", "sh", "-lc", script], timeout) } async function waitForSandboxReady(sandboxName: string, timeoutMs: number, intervalMs: number) { const startedAt = Date.now() let attempts = 0 - let lastError: string | null = null + let lastError = "" while (Date.now() - startedAt < timeoutMs) { attempts += 1 @@ -54,46 +54,67 @@ async function waitForSandboxReady(sandboxName: string, timeoutMs: number, inter return { ready: false, attempts, elapsedMs: Date.now() - startedAt, lastError } } +function restartOpenClawGatewayScript() { + return [ + `port=${SANDBOX_DASHBOARD_REMOTE_PORT}`, + "for p in /proc/[0-9]*; do", + " cmd=$(tr '\\0' ' ' < \"$p/cmdline\" 2>/dev/null || true)", + " case \"$cmd\" in", + " *'openclaw gateway run'*) kill \"${p##*/}\" 2>/dev/null || true ;;", + " esac", + "done", + "sleep 1", + "if command -v openclaw >/dev/null 2>&1; then openclaw_bin=$(command -v openclaw);", + "elif [ -x /usr/local/bin/openclaw ]; then openclaw_bin=/usr/local/bin/openclaw;", + "else echo 'openclaw command not found in sandbox' >&2; exit 127; fi", + "nohup \"$openclaw_bin\" gateway run --allow-unconfigured --bind loopback --port \"$port\" >/tmp/gateway.log 2>&1 &", + "for i in 1 2 3 4 5 6 7 8 9 10; do", + " curl -fsS --max-time 2 \"http://127.0.0.1:$port/\" >/dev/null 2>&1 && exit 0", + " sleep 1", + "done", + "echo 'OpenClaw gateway did not answer after restart. Last log lines:' >&2", + "tail -40 /tmp/gateway.log >&2 2>/dev/null || true", + "exit 1", + ].join("\n") +} + export async function POST( _request: Request, - { params }: { params: Promise<{ sandboxId: string }> } + { params }: { params: Promise<{ sandboxId: string }> }, ) { const startedAt = Date.now() try { const { sandboxId } = await params const resolved = await resolveSandboxRef(sandboxId) const sandboxName = validateSandboxName(resolved.name) - const deleted = await runKubectl(["delete", "pod", sandboxName, "-n", OPENSHELL_SANDBOX_NAMESPACE, "--wait=false"]) - if (deleted.code !== 0 && !/not found/i.test(`${deleted.stdout}\n${deleted.stderr}`)) { - throw new Error(deleted.stderr || deleted.stdout || "Failed to restart sandbox pod") + const readiness = await waitForSandboxReady(sandboxName, 15000, 1000) + if (!readiness.ready) { + return NextResponse.json({ + ok: false, + restarted: false, + sandboxId: resolved.id, + sandboxName, + readiness, + elapsedMs: Date.now() - startedAt, + note: "Sandbox was not Ready, so the dashboard runtime was not restarted.", + }, { status: 409 }) } - const readiness = await waitForSandboxReady(sandboxName, 90000, 2000) - let inferenceApply = null - if (readiness.ready) { - const inferenceConfig = await getSandboxInferenceConfig(resolved.id) - if (inferenceConfig.routes.some((route) => route.enabled)) { - inferenceApply = await applySandboxInferenceProfile(resolved.id, sandboxName) - } - } + const runtime = await runSandboxShell(sandboxName, restartOpenClawGatewayScript(), 45000) return NextResponse.json({ - ok: readiness.ready, - restarted: readiness.ready, + ok: true, + restarted: true, + restartMode: "openclaw-runtime", sandboxId: resolved.id, sandboxName, - deletion: deleted, readiness, - inferenceApply, + runtime, elapsedMs: Date.now() - startedAt, - note: readiness.ready - ? inferenceApply - ? "Sandbox pod restarted, OpenShell reports it Ready, and the saved inference profile was reapplied." - : "Sandbox pod restarted and OpenShell reports it Ready." - : "Restart was requested, but OpenShell has not reported the sandbox Ready yet.", - }, { status: readiness.ready ? 200 : 202 }) + note: "OpenClaw runtime restarted inside the sandbox. The sandbox pod was not deleted.", + }) } catch (error) { - const message = error instanceof Error ? error.message : "Failed to restart sandbox" + const message = error instanceof Error ? error.message : "Failed to restart sandbox runtime" return NextResponse.json({ ok: false, restarted: false, error: message }, { status: /required|invalid/.test(message) ? 400 : 500 }) } } diff --git a/app/components/SandboxList.tsx b/app/components/SandboxList.tsx index a769824..8cc9b13 100644 --- a/app/components/SandboxList.tsx +++ b/app/components/SandboxList.tsx @@ -213,20 +213,20 @@ export default function SandboxList({ } } - const restartSandbox = async () => { + const restartSandboxRuntime = async () => { if (!selectedSandbox || restartInProgress) return try { setRestartInProgress(true) - setDashboardMessage(`Restarting sandbox ${selectedSandbox.name}...`) + setDashboardMessage(`Restarting OpenClaw runtime for ${selectedSandbox.name}...`) const response = await fetch(`/api/sandbox/${encodeURIComponent(selectedSandbox.id)}/restart`, { method: 'POST', }) const data = await response.json() - if (!response.ok) throw new Error(data.error || data.note || 'Failed to restart sandbox') - setDashboardMessage(data.note || `Sandbox ${selectedSandbox.name} restarted.`) + if (!response.ok) throw new Error(data.error || data.note || 'Failed to restart OpenClaw runtime') + setDashboardMessage(data.note || `OpenClaw runtime restarted for ${selectedSandbox.name}.`) await onInventoryRefresh() } catch (error) { - setDashboardMessage(error instanceof Error ? error.message : 'Failed to restart sandbox.') + setDashboardMessage(error instanceof Error ? error.message : 'Failed to restart OpenClaw runtime.') } finally { setRestartInProgress(false) } @@ -538,11 +538,11 @@ export default function SandboxList({ Start OpenClaw Gateway Dashboard +
+ )}
) : (

Inventory Unavailable

{error}

+ {gatewayTrustError && ( +
+

Gateway Trust Mismatch

+

+ OpenShell reached the gateway, but certificate verification failed. This usually means the gateway regenerated mTLS material while this controller still has stale trust files. +

+ +
+ )} ) : sandboxes.length === 0 ? (
diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index a9a9078..298b270 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -14,6 +14,7 @@ const sandboxInferenceApplyRoutePath = path.join(root, 'app/api/sandbox/[sandbox const sandboxInferenceApplyLibPath = path.join(root, 'app/lib/sandboxInferenceApply.ts') const sandboxInferenceStorePath = path.join(root, 'app/lib/sandboxInferenceStore.ts') const sandboxRestartRoutePath = path.join(root, 'app/api/sandbox/[sandboxId]/restart/route.ts') +const gatewayRepairRoutePath = path.join(root, 'app/api/openshell/gateway/repair/route.ts') const hookPath = path.join(root, 'app/hooks/useSandboxInventory.ts') const pagePath = path.join(root, 'app/page.tsx') const configPanelPath = path.join(root, 'app/components/ConfigurationPanel.tsx') @@ -33,6 +34,7 @@ const sandboxInferenceApplyRouteSource = await readFile(sandboxInferenceApplyRou const sandboxInferenceApplyLibSource = await readFile(sandboxInferenceApplyLibPath, 'utf8') const sandboxInferenceStoreSource = await readFile(sandboxInferenceStorePath, 'utf8') const sandboxRestartRouteSource = await readFile(sandboxRestartRoutePath, 'utf8') +const gatewayRepairRouteSource = await readFile(gatewayRepairRoutePath, 'utf8') const hookSource = await readFile(hookPath, 'utf8') const pageSource = await readFile(pagePath, 'utf8') const configPanelSource = await readFile(configPanelPath, 'utf8') @@ -110,6 +112,13 @@ assert.match(sandboxRestartRouteSource, /restartMode: "openclaw-runtime"/, 'rest assert.match(sandboxRestartRouteSource, /waitForSandboxReady\(sandboxName, 15000, 1000\)/, 'restart route must verify OpenShell reports the sandbox ready before runtime restart') assert.match(sandboxListSource, /Restart OpenClaw Runtime/, 'sandbox details must expose a scoped runtime restart button') assert.match(sandboxListSource, /\/api\/sandbox\/\$\{encodeURIComponent\(selectedSandbox\.id\)\}\/restart/, 'restart button must call the sandbox restart endpoint') +assert.match(gatewayRepairRouteSource, /gateway", "start", "--name", gateway/, 'gateway repair route must restart the selected OpenShell gateway') +assert.match(gatewayRepairRouteSource, /backupOpenShellConfig/, 'gateway repair route must back up OpenShell config before repair') +assert.match(gatewayRepairRouteSource, /\["sandbox", "list"\]/, 'gateway repair route must verify sandbox inventory after repair') +assert.match(pageSource, /BadSignature\|invalid peer certificate\|transport error\|certificate signature/, 'dashboard must detect gateway certificate trust failures') +assert.match(pageSource, /Repair Gateway Trust/, 'inventory error UI must expose a guarded gateway trust repair button') +assert.match(pageSource, /api\/openshell\/gateway\/repair/, 'gateway trust repair button must call the repair endpoint') +assert.match(pageSource, /It does not destroy sandboxes/, 'gateway trust repair warning must clarify sandboxes are not destroyed') assert.doesNotMatch(sandboxListSource, /setInterval\(loadPermissionFeeds/, 'sandbox list must not poll every sandbox permission feed') assert.match(sandboxListSource, /loadPermissionFeed\(selectedSandbox\)/, 'policy drawer must load the selected sandbox permission feed on demand') assert.match(sandboxListSource, / Date: Tue, 28 Apr 2026 21:51:26 +0000 Subject: [PATCH 08/11] Handle gateway failures during sandbox delete --- app/api/sandbox/delete/route.ts | 27 ++++++++++++++++++++++++--- app/page.tsx | 6 +++--- tests/sandbox-lifecycle-check.mjs | 5 ++++- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/api/sandbox/delete/route.ts b/app/api/sandbox/delete/route.ts index d1fcfc0..95a1bd9 100644 --- a/app/api/sandbox/delete/route.ts +++ b/app/api/sandbox/delete/route.ts @@ -78,11 +78,28 @@ function isSandboxNotFound(output: string) { return /sandbox not found|status:\s*NotFound|not present in the live OpenShell gateway/i.test(output) } +function isGatewayRecoverable(output: string) { + return /Unknown gateway|Deploy it first|transport error|tcp connect error|Connection refused|invalid peer certificate|BadSignature/i.test(output) +} + +function isValidNemoClawSandboxName(value: string) { + return /^[a-z][a-z0-9-]*[a-z0-9]$|^[a-z]$/.test(value) +} + function isNemoClawSandboxNotRegistered(output: string, sandboxName: string) { return new RegExp(`Unknown command:\\s*${sandboxName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, "i").test(output) } async function cleanupNemoClawSandbox(sandboxName: string) { + if (!isValidNemoClawSandboxName(sandboxName)) { + return { + ok: true as const, + stdout: "", + stderr: "", + note: "Skipped NemoClaw registry cleanup because the target is not a valid NemoClaw sandbox name.", + } + } + const startedAt = Date.now() console.log(`[sandbox/delete] nemoclaw-cleanup:start sandbox=${sandboxName}`) try { @@ -169,7 +186,6 @@ export async function POST(request: Request) { const result = await deleteSandbox(target.sandboxName) const deleteOutput = [result.error, result.stdout, result.stderr].filter(Boolean).join("\n") const openShellAlreadyGone = !result.ok && isSandboxNotFound(deleteOutput) - const cleanup = await cleanupNemoClawSandbox(target.sandboxName) if (!result.ok && !openShellAlreadyGone) { return NextResponse.json({ @@ -183,10 +199,15 @@ export async function POST(request: Request) { error: result.error, stdout: result.stdout, stderr: result.stderr, - nemoclaw: cleanup, - }, { status: 500 }) + recoverableGateway: isGatewayRecoverable(deleteOutput), + note: isGatewayRecoverable(deleteOutput) + ? "OpenShell gateway is unavailable or its trust state is broken. Repair gateway trust, then retry delete." + : "OpenShell delete failed before NemoClaw cleanup ran.", + }, { status: isGatewayRecoverable(deleteOutput) ? 503 : 500 }) } + const cleanup = await cleanupNemoClawSandbox(target.sandboxName) + if (!cleanup.ok) { return NextResponse.json({ ok: false, diff --git a/app/page.tsx b/app/page.tsx index aa5c3e7..2d5883e 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -95,7 +95,7 @@ export default function Dashboard() { const sleep = (ms: number) => new Promise((resolve) => window.setTimeout(resolve, ms)) - const gatewayTrustError = Boolean(error && /BadSignature|invalid peer certificate|transport error|certificate signature/i.test(error)) + const gatewayTrustError = Boolean(error && /BadSignature|invalid peer certificate|transport error|certificate signature|Unknown gateway|Deploy it first|Connection refused|tcp connect error/i.test(error)) const repairGatewayTrust = async () => { if (gatewayRepairing) return @@ -346,7 +346,7 @@ export default function Dashboard() {

Gateway Trust Mismatch

- OpenShell reached the gateway, but certificate verification failed. This usually means the gateway regenerated mTLS material while this controller still has stale trust files. + OpenShell could not use the selected gateway. This usually means the gateway stopped, was undeployed, or regenerated mTLS material while this controller still has stale trust files.

- -
+ + {securityOpen && ( +
+ {servers.length === 0 ? ( +
+ Install an MCP server before configuring sandbox access.
- -
-
- Availability - -
-
- Allowed Sandboxes - {sandboxes.length === 0 ? ( -
- No sandboxes detected. -
- ) : ( -
- {sandboxes.map((sandbox) => ( - - ))} + + + {server.name} + + {server.enabled ? "enabled" : "disabled"} + + + {server.command} {server.args.join(" ")} + + {expanded ? "-" : "+"} + + + {expanded && ( +
+
+ + +
+
+
+ Availability + +
+
+ Allowed Sandboxes + {sandboxes.length === 0 ? ( +
No sandboxes detected.
+ ) : ( +
+ {sandboxes.map((sandbox) => ( + + ))} +
+ )} +
+
)}
-
-
- ))} -
-
- -
-
-
-

Registry Search

-

Search the Official MCP Registry and install compatible stdio or remote HTTP servers.

+ ) + })}
- - Open Registry - -
-
- setRegistrySearch(event.target.value)} - onKeyDown={(event) => { - if (event.key === "Enter") searchRegistry() - }} - placeholder="Search GitHub, Slack, filesystem, Postgres..." - className="field-control min-w-0 flex-1 px-3 py-2 text-sm" - /> - -
- {registryResults.length > 0 && ( - <> -
-

- Showing {((registryPage - 1) * REGISTRY_PAGE_SIZE) + 1}-{Math.min(registryPage * REGISTRY_PAGE_SIZE, registryResults.length)} of {registryResults.length} - {lastRegistrySearch ? ` for "${lastRegistrySearch}"` : ""} -

-
- - - {registryPage} / {registryPageCount} - - -
-
-
- {pagedRegistryResults.map((entry, index) => { - const installed = installedIds.has(entry.id) - return ( -
-
-
-

{entry.name}

-

{entry.summary}

-
- {entry.transport} -
-
- {entry.command} {entry.args.join(" ")} -
-
-

{entry.tags.join(" / ")}

- -
-
- ) - })} -
- )}
- {catalogOpen && ( -
- {catalog.map((entry) => { - const installed = installedIds.has(entry.id) - return ( -
-
-
-

{entry.name}

-

{entry.summary}

- {entry.websiteUrl && ( - - Setup Guide - - )} -
- {entry.transport} -
-
- {entry.command} {entry.args.join(" ")} -
-
-

{entry.tags.join(" / ")}

- + {repoOpen && ( +
+
+
+ Available Registries + {selectedRegistry && ( + Open + )} +
+ {registries.length === 0 ? ( +
+ Add an MCP Registry +
+ ) : ( +
+ {registries.map((registry) => { + const selected = registry.id === selectedRegistryId + return ( + + ) + })} +
+ )} + {selectedRegistry && ( + + )} +
+ +
+
+
+
Registry NameName shown in the registry selector.
+ setRegistryName(event.target.value)} placeholder="Company MCP Repo" className="field-control w-full px-3 py-2 text-sm" /> +
+
+
Registry URLRoot URL for a registry that exposes /v0/servers.
+ setRegistryUrl(event.target.value)} placeholder="https://registry.example.com" className="field-control w-full px-3 py-2 text-sm font-mono" /> +
+
+
LLM Registry AssistDescribe a registry and the running LLM endpoint drafts the fields.
+