From bec2547446c4123358e8ab9e459bc2e16fbaad7e Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 28 Apr 2026 19:31:21 +0000 Subject: [PATCH] Support MCP server bundle uploads --- app/api/mcp/upload/route.ts | 150 +++++++++++++++++++++++ app/components/McpConfigurationPanel.tsx | 98 ++++++++++++--- tests/mcp-configuration-page-check.mjs | 17 ++- 3 files changed, 243 insertions(+), 22 deletions(-) create mode 100644 app/api/mcp/upload/route.ts diff --git a/app/api/mcp/upload/route.ts b/app/api/mcp/upload/route.ts new file mode 100644 index 0000000..a7b0ab3 --- /dev/null +++ b/app/api/mcp/upload/route.ts @@ -0,0 +1,150 @@ +import { NextResponse } from "next/server" +import { execFile } from "node:child_process" +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises" +import os from "node:os" +import path from "node:path" +import { promisify } from "node:util" +import { installMcpServer, listMcpServers } from "@/app/lib/mcpServerStore" + +const execFileAsync = promisify(execFile) +const HOME = process.env.HOME || "/tmp" +const STORE_DIR = process.env.NEMOCLAW_DASHBOARD_STATE_DIR || path.join(HOME, ".nemoclaw-dashboard") +const UPLOAD_DIR = path.join(STORE_DIR, "mcp-server-uploads") +const MAX_UPLOAD_BYTES = Number.parseInt(process.env.MCP_SERVER_UPLOAD_MAX_BYTES || String(128 * 1024 * 1024), 10) + +function slugify(value: string) { + return value + .trim() + .toLowerCase() + .replace(/[^a-z0-9-]+/g, "-") + .replace(/^-+|-+$/g, "") + .slice(0, 64) || "uploaded-server" +} + +function parseLines(value: unknown) { + return typeof value === "string" + ? value.split(/\r?\n/).map((line) => line.trim()).filter(Boolean) + : [] +} + +function parseEnv(value: unknown) { + if (typeof value !== "string") return {} + return Object.fromEntries( + parseLines(value) + .map((line) => { + const [key, ...rest] = line.split("=") + return [key?.trim() || "", rest.join("=").trim()] + }) + .filter(([key]) => Boolean(key)), + ) +} + +function normalizeRelativePath(value: string) { + const normalized = path.posix.normalize(value.replace(/\\/g, "/")).replace(/^\/+/, "") + if (!normalized || normalized === "." || normalized.startsWith("../") || normalized.includes("/../")) { + throw new Error("uploaded file path must stay inside the server directory") + } + return normalized +} + +function safeEntryPath(root: string, entrypoint: string) { + const fullPath = path.resolve(root, normalizeRelativePath(entrypoint)) + if (fullPath !== root && !fullPath.startsWith(`${root}${path.sep}`)) { + throw new Error("entrypoint must stay inside the uploaded server directory") + } + return fullPath +} + +async function writeDirectoryUpload(form: FormData, root: string) { + const files = form.getAll("files") + const paths = form.getAll("paths").map((item) => String(item || "")) + if (files.length === 0) return false + if (files.length !== paths.length) throw new Error("directory upload paths are incomplete") + + let totalBytes = 0 + for (const [index, item] of files.entries()) { + if (!(item instanceof File)) throw new Error("directory upload contains a non-file entry") + const relativePath = normalizeRelativePath(paths[index] || item.name) + const bytes = Buffer.from(await item.arrayBuffer()) + totalBytes += bytes.length + if (totalBytes > MAX_UPLOAD_BYTES) throw new Error("uploaded MCP server is too large") + const target = path.join(root, relativePath) + await mkdir(path.dirname(target), { recursive: true, mode: 0o700 }) + await writeFile(target, bytes, { mode: 0o600 }) + } + return true +} + +async function writeArchiveUpload(form: FormData, root: string) { + const archive = form.get("archive") + if (!(archive instanceof File)) return false + const bytes = Buffer.from(await archive.arrayBuffer()) + if (bytes.length > MAX_UPLOAD_BYTES) throw new Error("uploaded MCP server archive is too large") + + const tempDir = await mkdtemp(path.join(os.tmpdir(), "mcp-server-upload-")) + const archivePath = path.join(tempDir, archive.name || "server-upload") + await writeFile(archivePath, bytes, { mode: 0o600 }) + try { + if (/\.zip$/i.test(archivePath)) { + await execFileAsync("unzip", ["-q", archivePath, "-d", root], { timeout: 60000, maxBuffer: 10 * 1024 * 1024 }) + return true + } + if (/\.(tgz|tar\.gz)$/i.test(archivePath)) { + await execFileAsync("tar", ["-xzf", archivePath, "-C", root], { timeout: 60000, maxBuffer: 10 * 1024 * 1024 }) + return true + } + if (/\.tar$/i.test(archivePath)) { + await execFileAsync("tar", ["-xf", archivePath, "-C", root], { timeout: 60000, maxBuffer: 10 * 1024 * 1024 }) + return true + } + throw new Error("upload archive must be .zip, .tgz, .tar.gz, or .tar") + } finally { + await rm(tempDir, { recursive: true, force: true }) + } +} + +export async function POST(request: Request) { + try { + const form = await request.formData() + const name = String(form.get("name") || "uploaded-server").trim() + const id = slugify(String(form.get("id") || name)) + const summary = String(form.get("summary") || "Uploaded MCP server").trim() + const runtime = String(form.get("runtime") || "python3").trim() + const entrypoint = String(form.get("entrypoint") || "server.py").trim() + const root = path.join(UPLOAD_DIR, id) + + if (!runtime) throw new Error("runtime command is required") + if (!entrypoint) throw new Error("entrypoint is required") + + await rm(root, { recursive: true, force: true }) + await mkdir(root, { recursive: true, mode: 0o700 }) + + const uploadedDirectory = await writeDirectoryUpload(form, root) + const uploadedArchive = uploadedDirectory ? false : await writeArchiveUpload(form, root) + if (!uploadedDirectory && !uploadedArchive) throw new Error("choose a directory, .zip, .tgz, .tar.gz, or .tar server upload") + + const entrypointPath = safeEntryPath(root, entrypoint) + const server = await installMcpServer({ + id, + name, + summary, + transport: "stdio", + command: runtime, + args: [entrypointPath, ...parseLines(form.get("args"))], + env: parseEnv(form.get("env")), + tags: ["uploaded", "custom"], + source: "custom", + enabled: true, + }) + + return NextResponse.json({ + ok: true, + server, + uploadRoot: root, + ...(await listMcpServers()), + }) + } catch (error) { + const message = error instanceof Error ? error.message : "Failed to upload MCP server" + return NextResponse.json({ ok: false, error: message }, { status: 400 }) + } +} diff --git a/app/components/McpConfigurationPanel.tsx b/app/components/McpConfigurationPanel.tsx index 6d44c59..3be00ac 100644 --- a/app/components/McpConfigurationPanel.tsx +++ b/app/components/McpConfigurationPanel.tsx @@ -79,8 +79,11 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati const [customCommand, setCustomCommand] = useState("npx") const [customArgs, setCustomArgs] = useState("-y\n@modelcontextprotocol/server-memory") const [customEnv, setCustomEnv] = useState("") - const [uploadFileName, setUploadFileName] = useState("") - const [uploadText, setUploadText] = useState("") + const [uploadFiles, setUploadFiles] = useState([]) + const [uploadPaths, setUploadPaths] = useState([]) + const [uploadArchive, setUploadArchive] = useState(null) + const [uploadRuntime, setUploadRuntime] = useState("python3") + const [uploadEntrypoint, setUploadEntrypoint] = useState("server.py") const [editingServerId, setEditingServerId] = useState(null) const [editorText, setEditorText] = useState("") const [registrySearch, setRegistrySearch] = useState("github") @@ -209,24 +212,61 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati } } - async function chooseUploadFile(file: File | null) { - if (!file) return - setUploadFileName(file.name) - setUploadText(await file.text()) + function chooseUploadDirectory(fileList: FileList | null) { + const files = Array.from(fileList || []) + setUploadFiles(files) + setUploadPaths(files.map((file) => { + const withRelativePath = file as File & { webkitRelativePath?: string } + return withRelativePath.webkitRelativePath || file.name + })) + if (files.length > 0) setUploadArchive(null) + } + + function chooseUploadArchive(file: File | null) { + setUploadArchive(file) + if (file) { + setUploadFiles([]) + setUploadPaths([]) + } } async function uploadServer() { try { - if (!uploadText.trim()) { - setMessage("Choose a JSON MCP server file before uploading.") + if (!uploadArchive && uploadFiles.length === 0) { + setMessage("Choose a server directory or archive before uploading.") return } - const fallbackId = uploadFileName.replace(/\.[^.]+$/, "") || "uploaded-server" - await postUpdate(serverPayloadFromJson(uploadText, fallbackId), `${fallbackId} uploaded.`) - setUploadText("") - setUploadFileName("") + setSaving(true) + const form = new FormData() + form.set("id", customName) + form.set("name", customName) + form.set("summary", customSummary) + form.set("runtime", uploadRuntime) + form.set("entrypoint", uploadEntrypoint) + form.set("args", customArgs) + form.set("env", customEnv) + if (uploadArchive) { + form.set("archive", uploadArchive) + } else { + uploadFiles.forEach((file, index) => { + form.append("files", file) + form.append("paths", uploadPaths[index] || file.name) + }) + } + const response = await fetch("/api/mcp/upload", { method: "POST", body: form }) + const data = await response.json() as McpResponse + 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: {} }) + setUploadFiles([]) + setUploadPaths([]) + setUploadArchive(null) + setMessage(`${customName} uploaded.`) } catch (error) { - setMessage(error instanceof Error ? error.message : "Failed to upload MCP server JSON") + setMessage(error instanceof Error ? error.message : "Failed to upload MCP server") + } finally { + setSaving(false) } } @@ -586,6 +626,14 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati {customTransport === "http" ? "Headers" : "Env"}