Add inter-sandbox chat MCP server
This commit is contained in:
@@ -2,6 +2,7 @@ import { NextResponse } from "next/server"
|
||||
import { execFile, spawn } from "node:child_process"
|
||||
import { promisify } from "node:util"
|
||||
import { inspectSandbox } from "@/app/lib/openshellHost"
|
||||
import { repairOpenClawWorkspacePermissions } from "@/app/lib/sandboxPrivilegedFiles"
|
||||
import {
|
||||
commandExists,
|
||||
HOST_PATH,
|
||||
@@ -437,6 +438,11 @@ export async function POST(request: Request) {
|
||||
error: "Sandbox readiness polling produced no verification result.",
|
||||
}
|
||||
const created = readiness.verified
|
||||
const workspaceRepair = created ? await repairOpenClawWorkspacePermissions(sandboxName).catch((error) => ({
|
||||
sandboxName,
|
||||
path: "/sandbox/.openclaw/workspace",
|
||||
error: error instanceof Error ? error.message : "Failed to repair OpenClaw workspace permissions",
|
||||
})) : null
|
||||
const deviceApproval = created ? await approveOpenClawDeviceRequests(sandboxName) : null
|
||||
console.log(
|
||||
`[sandbox/create] request:complete sandbox=${sandboxName} created=${created} readinessAttempts=${readiness.attempts} deviceApproval=${deviceApproval?.approved ?? false} elapsedMs=${elapsedMs(requestStartedAt)}`,
|
||||
@@ -458,6 +464,7 @@ export async function POST(request: Request) {
|
||||
attempts: readiness.attempts,
|
||||
elapsedMs: readiness.elapsedMs,
|
||||
},
|
||||
workspaceRepair,
|
||||
deviceApproval,
|
||||
stdout: result.stdout,
|
||||
stderr: result.stderr,
|
||||
@@ -466,6 +473,8 @@ export async function POST(request: Request) {
|
||||
enableTailscale
|
||||
? "NemoClaw blueprint workflow completed with Tailscale-enabled prerequisites. Existing healthy OpenShell gateways are reused before any new gateway start is attempted."
|
||||
: "NemoClaw blueprint workflow completed in local/default mode. Existing healthy OpenShell gateways are reused before any new gateway start is attempted.",
|
||||
workspaceRepair && "note" in workspaceRepair ? workspaceRepair.note : false,
|
||||
workspaceRepair && "error" in workspaceRepair ? `OpenClaw workspace permission repair failed: ${workspaceRepair.error}` : false,
|
||||
deviceApproval?.note,
|
||||
)
|
||||
: "Blueprint command reported success, but the sandbox never reached a ready verification state afterward.",
|
||||
@@ -500,6 +509,11 @@ export async function POST(request: Request) {
|
||||
error: "Sandbox readiness polling produced no verification result.",
|
||||
}
|
||||
const created = readiness.verified
|
||||
const workspaceRepair = created ? await repairOpenClawWorkspacePermissions(sandboxName).catch((error) => ({
|
||||
sandboxName,
|
||||
path: "/sandbox/.openclaw/workspace",
|
||||
error: error instanceof Error ? error.message : "Failed to repair OpenClaw workspace permissions",
|
||||
})) : null
|
||||
const deviceApproval = created ? await approveOpenClawDeviceRequests(sandboxName) : null
|
||||
const policyPrepared = Boolean(policy)
|
||||
console.log(
|
||||
@@ -526,6 +540,7 @@ export async function POST(request: Request) {
|
||||
attempts: readiness.attempts,
|
||||
elapsedMs: readiness.elapsedMs,
|
||||
},
|
||||
workspaceRepair,
|
||||
deviceApproval,
|
||||
policyPrepared,
|
||||
note: created
|
||||
@@ -535,6 +550,8 @@ export async function POST(request: Request) {
|
||||
: (policyPrepared
|
||||
? "Custom sandbox created. Policy draft is prepared, but applying it live should be a follow-up action."
|
||||
: "Custom sandbox created."),
|
||||
workspaceRepair && "note" in workspaceRepair ? workspaceRepair.note : false,
|
||||
workspaceRepair && "error" in workspaceRepair ? `OpenClaw workspace permission repair failed: ${workspaceRepair.error}` : false,
|
||||
deviceApproval?.note,
|
||||
)
|
||||
: "Create command started, but the sandbox never reached a ready verification state afterward.",
|
||||
|
||||
@@ -64,6 +64,16 @@ export const MCP_SERVER_CATALOG: McpCatalogEntry[] = [
|
||||
env: {},
|
||||
tags: ["state", "local"],
|
||||
},
|
||||
{
|
||||
id: "inter-sandbox-chat",
|
||||
name: "Inter-Sandbox Chat",
|
||||
summary: "Give allowed sandboxes a shared roomed message board so agents can post updates and read messages from each other through the MCP broker.",
|
||||
transport: "stdio",
|
||||
command: "node",
|
||||
args: [path.join(process.cwd(), "scripts/inter-sandbox-chat-mcp.mjs")],
|
||||
env: {},
|
||||
tags: ["chat", "collaboration", "sandbox"],
|
||||
},
|
||||
{
|
||||
id: "git",
|
||||
name: "Git",
|
||||
@@ -118,6 +128,10 @@ function normalizeTransport(value: unknown): McpTransport {
|
||||
return value === "http" ? "http" : "stdio"
|
||||
}
|
||||
|
||||
function withoutUndefined<T extends Record<string, unknown>>(input: T): Partial<T> {
|
||||
return Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined)) as Partial<T>
|
||||
}
|
||||
|
||||
function normalizeInstall(input: Partial<McpServerInstall>, existing?: McpServerInstall | null): McpServerInstall {
|
||||
const name = typeof input.name === "string" && input.name.trim() ? input.name.trim() : existing?.name || "MCP Server"
|
||||
const id = slugify(typeof input.id === "string" && input.id.trim() ? input.id : existing?.id || name)
|
||||
@@ -224,16 +238,17 @@ export async function listMcpServers() {
|
||||
|
||||
export async function installMcpServer(input: Partial<McpServerInstall>) {
|
||||
const store = await readStore()
|
||||
const fromCatalog = typeof input.id === "string"
|
||||
? MCP_SERVER_CATALOG.find((entry) => entry.id === input.id)
|
||||
const cleanInput = withoutUndefined(input as Record<string, unknown>) as Partial<McpServerInstall>
|
||||
const fromCatalog = typeof cleanInput.id === "string"
|
||||
? MCP_SERVER_CATALOG.find((entry) => entry.id === cleanInput.id)
|
||||
: null
|
||||
const next = normalizeInstall(
|
||||
{
|
||||
...fromCatalog,
|
||||
...input,
|
||||
source: input.source || (fromCatalog ? "catalog" : "custom"),
|
||||
...cleanInput,
|
||||
source: cleanInput.source || (fromCatalog ? "catalog" : "custom"),
|
||||
},
|
||||
typeof input.id === "string" ? store.servers[slugify(input.id)] : null,
|
||||
typeof cleanInput.id === "string" ? store.servers[slugify(cleanInput.id)] : null,
|
||||
)
|
||||
store.servers[next.id] = next
|
||||
await writeStore(store)
|
||||
|
||||
@@ -49,3 +49,38 @@ export async function writeSandboxFilePrivileged(
|
||||
bytes: payload.byteLength,
|
||||
}
|
||||
}
|
||||
|
||||
export async function repairOpenClawWorkspacePermissions(sandboxName: string) {
|
||||
const mutableDirs = [
|
||||
"agents",
|
||||
"extensions",
|
||||
"workspace",
|
||||
"skills",
|
||||
"hooks",
|
||||
"identity",
|
||||
"devices",
|
||||
"canvas",
|
||||
"cron",
|
||||
"memory",
|
||||
"logs",
|
||||
"credentials",
|
||||
"sandbox",
|
||||
"telegram",
|
||||
]
|
||||
const script = [
|
||||
`for dir in ${mutableDirs.map(shellQuote).join(" ")}; do target="/sandbox/.openclaw-data/$dir"; link="/sandbox/.openclaw/$dir"; mkdir -p "$target"; if [ -d "$link" ] && [ ! -L "$link" ]; then cp -a "$link/." "$target/" 2>/dev/null || true; rm -rf "$link"; fi; ln -sfn "$target" "$link"; done`,
|
||||
`chown -R sandbox:sandbox /sandbox/.openclaw-data 2>/dev/null || chown -R 998:998 /sandbox/.openclaw-data`,
|
||||
`for dir in ${mutableDirs.map(shellQuote).join(" ")}; do chmod 0775 "/sandbox/.openclaw-data/$dir"; done`,
|
||||
].join(" && ")
|
||||
const result = await runDockerKubectl(
|
||||
["exec", "-n", OPENSHELL_NAMESPACE, sandboxName, "--", "sh", "-lc", script],
|
||||
)
|
||||
if (result.code !== 0) throw new Error(result.stderr || "failed to repair OpenClaw mutable paths")
|
||||
return {
|
||||
sandboxName,
|
||||
path: "/sandbox/.openclaw",
|
||||
target: "/sandbox/.openclaw-data",
|
||||
mutableDirs,
|
||||
note: "Repaired OpenClaw mutable paths so AGENTS.md, session lock, and agent state writes resolve into writable .openclaw-data storage.",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user