From da015fb73adf41beb2b86e81adef28b22d8032ad Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 28 Apr 2026 20:53:12 +0000 Subject: [PATCH] Support Python module MCP uploads --- app/api/mcp/upload/route.ts | 90 ++++++++++++++++++++++-- app/components/McpConfigurationPanel.tsx | 12 +++- tests/mcp-configuration-page-check.mjs | 7 ++ 3 files changed, 101 insertions(+), 8 deletions(-) diff --git a/app/api/mcp/upload/route.ts b/app/api/mcp/upload/route.ts index 28fbbec..5cef51c 100644 --- a/app/api/mcp/upload/route.ts +++ b/app/api/mcp/upload/route.ts @@ -12,6 +12,7 @@ const STORE_DIR = process.env.NEMOCLAW_DASHBOARD_STATE_DIR || path.join(HOME, ". 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) +const ENTRY_MODES = new Set(["file", "python-module", "console-script"]) function slugify(value: string) { return value @@ -40,6 +41,11 @@ function parseEnv(value: unknown) { ) } +function normalizeEntryMode(value: unknown) { + const mode = String(value || "file").trim() + return ENTRY_MODES.has(mode) ? mode : "file" +} + function normalizeRelativePath(value: string) { const normalized = path.posix.normalize(value.replace(/\\/g, "/")).replace(/^\/+/, "") if (!normalized || normalized === "." || normalized.startsWith("../") || normalized.includes("/../")) { @@ -48,6 +54,18 @@ function normalizeRelativePath(value: string) { return normalized } +function validatePythonModule(value: string) { + if (!/^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$/.test(value)) { + throw new Error("python module entrypoint must look like package.module") + } +} + +function validateConsoleScript(value: string) { + if (!/^[A-Za-z0-9_.-]+$/.test(value)) { + throw new Error("console script entrypoint must be a script name, not a path") + } +} + function safeEntryPath(root: string, entrypoint: string) { const fullPath = path.resolve(root, normalizeRelativePath(entrypoint)) if (fullPath !== root && !fullPath.startsWith(`${root}${path.sep}`)) { @@ -87,6 +105,34 @@ async function resolveEntryPath(root: string, entrypoint: string) { throw new Error(`entrypoint was not found in uploaded server: ${entrypoint}`) } +async function hasProjectManifest(candidate: string) { + return await pathExists(path.join(candidate, "pyproject.toml")) + || await pathExists(path.join(candidate, "requirements.txt")) + || await pathExists(path.join(candidate, "package.json")) +} + +async function findSingleNestedProjectRoot(root: string) { + const entries = await readdir(root, { withFileTypes: true }).catch(() => []) + const visible = entries.filter((entry) => !entry.name.startsWith(".")) + if (visible.length !== 1 || !visible[0]?.isDirectory()) return null + const nested = path.join(root, visible[0].name) + return await hasProjectManifest(nested) ? nested : null +} + +async function resolveProjectRoot(root: string, entrypointPath: string | null) { + if (await hasProjectManifest(root)) return root + if (entrypointPath) { + let candidate = path.dirname(entrypointPath) + while (candidate.startsWith(root)) { + if (await hasProjectManifest(candidate)) return candidate + const parent = path.dirname(candidate) + if (parent === candidate) break + candidate = parent + } + } + return await findSingleNestedProjectRoot(root) || root +} + async function runInstallCommand(command: string, args: string[], cwd: string) { try { const { stdout, stderr } = await execFileAsync(command, args, { @@ -153,18 +199,44 @@ async function bootstrapNode(root: string, runtime: string) { } } -async function bootstrapUploadedServer(root: string, runtime: string, entrypointPath: string) { - if (await isPythonUpload(runtime, entrypointPath, root)) { +async function bootstrapUploadedServer(root: string, runtime: string, entrypoint: string) { + if (await isPythonUpload(runtime, entrypoint, root)) { const result = await bootstrapPython(root, runtime || "python3") return { command: result.command, logs: result.logs, kind: "python" } } - if (await isNodeUpload(runtime, entrypointPath, root)) { + if (await isNodeUpload(runtime, entrypoint, 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 launchCommandForUpload( + projectRoot: string, + bootstrap: Awaited>, + entryMode: string, + entrypoint: string, + entrypointPath: string | null, + extraArgs: string[], +) { + if (entryMode === "python-module") { + validatePythonModule(entrypoint) + if (bootstrap.kind !== "python") throw new Error("python module entrypoint requires a Python upload") + return { command: bootstrap.command, args: ["-m", entrypoint, ...extraArgs] } + } + if (entryMode === "console-script") { + validateConsoleScript(entrypoint) + if (bootstrap.kind === "python") { + const scriptPath = path.join(projectRoot, ".venv/bin", entrypoint) + if (!await pathExists(scriptPath)) throw new Error(`console script was not installed in the upload virtualenv: ${entrypoint}`) + return { command: scriptPath, args: extraArgs } + } + return { command: entrypoint, args: extraArgs } + } + if (!entrypointPath) throw new Error("file entrypoint was not resolved") + return { command: bootstrap.command, args: [entrypointPath, ...extraArgs] } +} + async function writeDirectoryUpload(form: FormData, root: string) { const files = form.getAll("files") const paths = form.getAll("paths").map((item) => String(item || "")) @@ -221,6 +293,7 @@ export async function POST(request: Request) { 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 entryMode = normalizeEntryMode(form.get("entryMode")) const root = path.join(UPLOAD_DIR, id) if (!runtime) throw new Error("runtime command is required") @@ -233,15 +306,17 @@ 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 = await resolveEntryPath(root, entrypoint) - const bootstrap = await bootstrapUploadedServer(root, runtime, entrypointPath) + const entrypointPath = entryMode === "file" ? await resolveEntryPath(root, entrypoint) : null + 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({ id, name, summary, transport: "stdio", - command: bootstrap.command, - args: [entrypointPath, ...parseLines(form.get("args"))], + command: launch.command, + args: launch.args, env: parseEnv(form.get("env")), tags: ["uploaded", "custom"], source: "custom", @@ -252,6 +327,7 @@ export async function POST(request: Request) { ok: true, server, uploadRoot: root, + projectRoot, dependencyInstall: { kind: bootstrap.kind, logs: bootstrap.logs, diff --git a/app/components/McpConfigurationPanel.tsx b/app/components/McpConfigurationPanel.tsx index 026624f..7d4a1e3 100644 --- a/app/components/McpConfigurationPanel.tsx +++ b/app/components/McpConfigurationPanel.tsx @@ -83,6 +83,7 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati const [uploadPaths, setUploadPaths] = useState([]) const [uploadArchive, setUploadArchive] = useState(null) const [uploadRuntime, setUploadRuntime] = useState("python3") + const [uploadEntryMode, setUploadEntryMode] = useState<"file" | "python-module" | "console-script">("file") const [uploadEntrypoint, setUploadEntrypoint] = useState("server.py") const [editingServerId, setEditingServerId] = useState(null) const [editorText, setEditorText] = useState("") @@ -304,6 +305,7 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati form.set("name", customName) form.set("summary", customSummary) form.set("runtime", uploadRuntime) + form.set("entryMode", uploadEntryMode) form.set("entrypoint", uploadEntrypoint) form.set("args", customArgs) form.set("env", customEnv) @@ -690,9 +692,17 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati Upload Runtime setUploadRuntime(event.target.value)} placeholder="python3, node, uv, uvx" className="field-control w-full px-3 py-2 text-sm font-mono" /> +
+ 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')