Bootstrap uploaded MCP server dependencies
This commit is contained in:
+118
-3
@@ -1,6 +1,6 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { execFile } from "node:child_process"
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"
|
||||
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"
|
||||
@@ -11,6 +11,7 @@ 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)
|
||||
const INSTALL_TIMEOUT_MS = Number.parseInt(process.env.MCP_SERVER_DEPENDENCY_INSTALL_TIMEOUT_MS || String(5 * 60 * 1000), 10)
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
@@ -55,6 +56,115 @@ function safeEntryPath(root: string, entrypoint: string) {
|
||||
return fullPath
|
||||
}
|
||||
|
||||
async function pathExists(candidate: string) {
|
||||
try {
|
||||
await access(candidate)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function findFileByName(root: string, fileName: string): Promise<string | null> {
|
||||
const entries = await readdir(root, { withFileTypes: true }).catch(() => [])
|
||||
for (const entry of entries) {
|
||||
if (entry.name === "node_modules" || entry.name === ".venv" || entry.name === ".git") continue
|
||||
const fullPath = path.join(root, entry.name)
|
||||
if (entry.isFile() && entry.name === fileName) return fullPath
|
||||
if (entry.isDirectory()) {
|
||||
const nested = await findFileByName(fullPath, fileName)
|
||||
if (nested) return nested
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
async function resolveEntryPath(root: string, entrypoint: string) {
|
||||
const direct = safeEntryPath(root, entrypoint)
|
||||
if (await pathExists(direct)) return direct
|
||||
const discovered = await findFileByName(root, path.basename(entrypoint))
|
||||
if (discovered) return discovered
|
||||
throw new Error(`entrypoint was not found in uploaded server: ${entrypoint}`)
|
||||
}
|
||||
|
||||
async function runInstallCommand(command: string, args: string[], cwd: string) {
|
||||
try {
|
||||
const { stdout, stderr } = await execFileAsync(command, args, {
|
||||
cwd,
|
||||
timeout: INSTALL_TIMEOUT_MS,
|
||||
maxBuffer: 5 * 1024 * 1024,
|
||||
env: {
|
||||
...process.env,
|
||||
PATH: `${path.join(cwd, ".venv/bin")}:${process.env.PATH || ""}`,
|
||||
},
|
||||
})
|
||||
return [String(stdout).trim(), String(stderr).trim()].filter(Boolean).join("\n")
|
||||
} catch (error: any) {
|
||||
const detail = [String(error?.stdout || "").trim(), String(error?.stderr || "").trim(), error instanceof Error ? error.message : ""]
|
||||
.filter(Boolean)
|
||||
.join("\n")
|
||||
throw new Error(detail || `dependency install command failed: ${command} ${args.join(" ")}`)
|
||||
}
|
||||
}
|
||||
|
||||
async function isPythonUpload(runtime: string, entrypointPath: string, root: string) {
|
||||
return /(^|\/)python(?:3(?:\.\d+)?)?$/i.test(runtime)
|
||||
|| /\.py$/i.test(entrypointPath)
|
||||
|| await pathExists(path.join(root, "requirements.txt"))
|
||||
|| await pathExists(path.join(root, "pyproject.toml"))
|
||||
}
|
||||
|
||||
async function isNodeUpload(runtime: string, entrypointPath: string, root: string) {
|
||||
return /(^|\/)(node|npm|npx)$/i.test(runtime)
|
||||
|| /\.(mjs|cjs|js)$/i.test(entrypointPath)
|
||||
|| await pathExists(path.join(root, "package.json"))
|
||||
}
|
||||
|
||||
async function bootstrapPython(root: string, runtime: string) {
|
||||
const logs: string[] = []
|
||||
const venvDir = path.join(root, ".venv")
|
||||
const venvPython = path.join(venvDir, "bin/python")
|
||||
if (!await pathExists(venvPython)) {
|
||||
logs.push(await runInstallCommand(runtime || "python3", ["-m", "venv", venvDir], root))
|
||||
}
|
||||
const pip = path.join(venvDir, "bin/pip")
|
||||
logs.push(await runInstallCommand(venvPython, ["-m", "pip", "install", "--upgrade", "pip"], root))
|
||||
if (await pathExists(path.join(root, "requirements.txt"))) {
|
||||
logs.push(await runInstallCommand(pip, ["install", "-r", "requirements.txt"], root))
|
||||
}
|
||||
if (await pathExists(path.join(root, "pyproject.toml"))) {
|
||||
logs.push(await runInstallCommand(pip, ["install", "-e", "."], root))
|
||||
}
|
||||
return {
|
||||
command: venvPython,
|
||||
logs: logs.filter(Boolean),
|
||||
}
|
||||
}
|
||||
|
||||
async function bootstrapNode(root: string, runtime: string) {
|
||||
const logs: string[] = []
|
||||
if (await pathExists(path.join(root, "package.json"))) {
|
||||
const hasLock = await pathExists(path.join(root, "package-lock.json"))
|
||||
logs.push(await runInstallCommand("npm", [hasLock ? "ci" : "install"], root))
|
||||
}
|
||||
return {
|
||||
command: /(^|\/)npm$/i.test(runtime) ? runtime : "node",
|
||||
logs: logs.filter(Boolean),
|
||||
}
|
||||
}
|
||||
|
||||
async function bootstrapUploadedServer(root: string, runtime: string, entrypointPath: string) {
|
||||
if (await isPythonUpload(runtime, entrypointPath, root)) {
|
||||
const result = await bootstrapPython(root, runtime || "python3")
|
||||
return { command: result.command, logs: result.logs, kind: "python" }
|
||||
}
|
||||
if (await isNodeUpload(runtime, entrypointPath, root)) {
|
||||
const result = await bootstrapNode(root, runtime || "node")
|
||||
return { command: result.command, logs: result.logs, kind: "node" }
|
||||
}
|
||||
return { command: runtime, logs: [] as string[], kind: "generic" }
|
||||
}
|
||||
|
||||
async function writeDirectoryUpload(form: FormData, root: string) {
|
||||
const files = form.getAll("files")
|
||||
const paths = form.getAll("paths").map((item) => String(item || ""))
|
||||
@@ -123,13 +233,14 @@ export async function POST(request: Request) {
|
||||
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 entrypointPath = await resolveEntryPath(root, entrypoint)
|
||||
const bootstrap = await bootstrapUploadedServer(root, runtime, entrypointPath)
|
||||
const server = await installMcpServer({
|
||||
id,
|
||||
name,
|
||||
summary,
|
||||
transport: "stdio",
|
||||
command: runtime,
|
||||
command: bootstrap.command,
|
||||
args: [entrypointPath, ...parseLines(form.get("args"))],
|
||||
env: parseEnv(form.get("env")),
|
||||
tags: ["uploaded", "custom"],
|
||||
@@ -141,6 +252,10 @@ export async function POST(request: Request) {
|
||||
ok: true,
|
||||
server,
|
||||
uploadRoot: root,
|
||||
dependencyInstall: {
|
||||
kind: bootstrap.kind,
|
||||
logs: bootstrap.logs,
|
||||
},
|
||||
...(await listMcpServers()),
|
||||
})
|
||||
} catch (error) {
|
||||
|
||||
@@ -316,7 +316,7 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati
|
||||
})
|
||||
}
|
||||
const response = await fetch("/api/mcp/upload", { method: "POST", body: form })
|
||||
const data = await response.json() as McpResponse
|
||||
const data = await response.json() as McpResponse & { dependencyInstall?: { kind?: string; logs?: string[] } }
|
||||
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 : [])
|
||||
@@ -324,7 +324,9 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati
|
||||
setUploadFiles([])
|
||||
setUploadPaths([])
|
||||
setUploadArchive(null)
|
||||
setMessage(`${customName} uploaded.`)
|
||||
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"}` : ""}.`}`)
|
||||
} catch (error) {
|
||||
setMessage(error instanceof Error ? error.message : "Failed to upload MCP server")
|
||||
} finally {
|
||||
|
||||
@@ -94,6 +94,11 @@ assert.match(routeSource, /updateMcpServerAccess/, 'MCP API must support access
|
||||
assert.match(uploadRouteSource, /request\.formData\(\)/, 'MCP upload API must accept multipart server bundles')
|
||||
assert.match(uploadRouteSource, /writeDirectoryUpload/, 'MCP upload API must support directory uploads')
|
||||
assert.match(uploadRouteSource, /writeArchiveUpload/, 'MCP upload API must support archive uploads')
|
||||
assert.match(uploadRouteSource, /bootstrapUploadedServer/, 'MCP upload API must bootstrap uploaded server dependencies')
|
||||
assert.match(uploadRouteSource, /requirements\.txt/, 'MCP upload API must install Python requirements when present')
|
||||
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, /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')
|
||||
@@ -107,6 +112,7 @@ assert.match(panelSource, /Upload Server/, 'MCP custom server accordion must sup
|
||||
assert.match(panelSource, /Choose Directory/, 'MCP custom server upload must accept directories')
|
||||
assert.match(panelSource, /Choose Archive/, 'MCP custom server upload must accept archives')
|
||||
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, /serverPayloadFromJson/, 'MCP page must parse edited server JSON')
|
||||
assert.match(panelSource, /startEditingServer/, 'MCP installed server list must expose edit state')
|
||||
|
||||
Reference in New Issue
Block a user