Support Python module MCP uploads
This commit is contained in:
@@ -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 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 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 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) {
|
function slugify(value: string) {
|
||||||
return value
|
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) {
|
function normalizeRelativePath(value: string) {
|
||||||
const normalized = path.posix.normalize(value.replace(/\\/g, "/")).replace(/^\/+/, "")
|
const normalized = path.posix.normalize(value.replace(/\\/g, "/")).replace(/^\/+/, "")
|
||||||
if (!normalized || normalized === "." || normalized.startsWith("../") || normalized.includes("/../")) {
|
if (!normalized || normalized === "." || normalized.startsWith("../") || normalized.includes("/../")) {
|
||||||
@@ -48,6 +54,18 @@ function normalizeRelativePath(value: string) {
|
|||||||
return normalized
|
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) {
|
function safeEntryPath(root: string, entrypoint: string) {
|
||||||
const fullPath = path.resolve(root, normalizeRelativePath(entrypoint))
|
const fullPath = path.resolve(root, normalizeRelativePath(entrypoint))
|
||||||
if (fullPath !== root && !fullPath.startsWith(`${root}${path.sep}`)) {
|
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}`)
|
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) {
|
async function runInstallCommand(command: string, args: string[], cwd: string) {
|
||||||
try {
|
try {
|
||||||
const { stdout, stderr } = await execFileAsync(command, args, {
|
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) {
|
async function bootstrapUploadedServer(root: string, runtime: string, entrypoint: string) {
|
||||||
if (await isPythonUpload(runtime, entrypointPath, root)) {
|
if (await isPythonUpload(runtime, entrypoint, root)) {
|
||||||
const result = await bootstrapPython(root, runtime || "python3")
|
const result = await bootstrapPython(root, runtime || "python3")
|
||||||
return { command: result.command, logs: result.logs, kind: "python" }
|
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")
|
const result = await bootstrapNode(root, runtime || "node")
|
||||||
return { command: result.command, logs: result.logs, kind: "node" }
|
return { command: result.command, logs: result.logs, kind: "node" }
|
||||||
}
|
}
|
||||||
return { command: runtime, logs: [] as string[], kind: "generic" }
|
return { command: runtime, logs: [] as string[], kind: "generic" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function launchCommandForUpload(
|
||||||
|
projectRoot: string,
|
||||||
|
bootstrap: Awaited<ReturnType<typeof bootstrapUploadedServer>>,
|
||||||
|
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) {
|
async function writeDirectoryUpload(form: FormData, root: string) {
|
||||||
const files = form.getAll("files")
|
const files = form.getAll("files")
|
||||||
const paths = form.getAll("paths").map((item) => String(item || ""))
|
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 summary = String(form.get("summary") || "Uploaded MCP server").trim()
|
||||||
const runtime = String(form.get("runtime") || "python3").trim()
|
const runtime = String(form.get("runtime") || "python3").trim()
|
||||||
const entrypoint = String(form.get("entrypoint") || "server.py").trim()
|
const entrypoint = String(form.get("entrypoint") || "server.py").trim()
|
||||||
|
const entryMode = normalizeEntryMode(form.get("entryMode"))
|
||||||
const root = path.join(UPLOAD_DIR, id)
|
const root = path.join(UPLOAD_DIR, id)
|
||||||
|
|
||||||
if (!runtime) throw new Error("runtime command is required")
|
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)
|
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")
|
if (!uploadedDirectory && !uploadedArchive) throw new Error("choose a directory, .zip, .tgz, .tar.gz, or .tar server upload")
|
||||||
|
|
||||||
const entrypointPath = await resolveEntryPath(root, entrypoint)
|
const entrypointPath = entryMode === "file" ? await resolveEntryPath(root, entrypoint) : null
|
||||||
const bootstrap = await bootstrapUploadedServer(root, runtime, entrypointPath)
|
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 server = await installMcpServer({
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
summary,
|
summary,
|
||||||
transport: "stdio",
|
transport: "stdio",
|
||||||
command: bootstrap.command,
|
command: launch.command,
|
||||||
args: [entrypointPath, ...parseLines(form.get("args"))],
|
args: launch.args,
|
||||||
env: parseEnv(form.get("env")),
|
env: parseEnv(form.get("env")),
|
||||||
tags: ["uploaded", "custom"],
|
tags: ["uploaded", "custom"],
|
||||||
source: "custom",
|
source: "custom",
|
||||||
@@ -252,6 +327,7 @@ export async function POST(request: Request) {
|
|||||||
ok: true,
|
ok: true,
|
||||||
server,
|
server,
|
||||||
uploadRoot: root,
|
uploadRoot: root,
|
||||||
|
projectRoot,
|
||||||
dependencyInstall: {
|
dependencyInstall: {
|
||||||
kind: bootstrap.kind,
|
kind: bootstrap.kind,
|
||||||
logs: bootstrap.logs,
|
logs: bootstrap.logs,
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati
|
|||||||
const [uploadPaths, setUploadPaths] = useState<string[]>([])
|
const [uploadPaths, setUploadPaths] = useState<string[]>([])
|
||||||
const [uploadArchive, setUploadArchive] = useState<File | null>(null)
|
const [uploadArchive, setUploadArchive] = useState<File | null>(null)
|
||||||
const [uploadRuntime, setUploadRuntime] = useState("python3")
|
const [uploadRuntime, setUploadRuntime] = useState("python3")
|
||||||
|
const [uploadEntryMode, setUploadEntryMode] = useState<"file" | "python-module" | "console-script">("file")
|
||||||
const [uploadEntrypoint, setUploadEntrypoint] = useState("server.py")
|
const [uploadEntrypoint, setUploadEntrypoint] = useState("server.py")
|
||||||
const [editingServerId, setEditingServerId] = useState<string | null>(null)
|
const [editingServerId, setEditingServerId] = useState<string | null>(null)
|
||||||
const [editorText, setEditorText] = useState("")
|
const [editorText, setEditorText] = useState("")
|
||||||
@@ -304,6 +305,7 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati
|
|||||||
form.set("name", customName)
|
form.set("name", customName)
|
||||||
form.set("summary", customSummary)
|
form.set("summary", customSummary)
|
||||||
form.set("runtime", uploadRuntime)
|
form.set("runtime", uploadRuntime)
|
||||||
|
form.set("entryMode", uploadEntryMode)
|
||||||
form.set("entrypoint", uploadEntrypoint)
|
form.set("entrypoint", uploadEntrypoint)
|
||||||
form.set("args", customArgs)
|
form.set("args", customArgs)
|
||||||
form.set("env", customEnv)
|
form.set("env", customEnv)
|
||||||
@@ -690,9 +692,17 @@ export default function McpConfigurationPanel({ sandboxes = [] }: McpConfigurati
|
|||||||
<FieldLabel>Upload Runtime</FieldLabel>
|
<FieldLabel>Upload Runtime</FieldLabel>
|
||||||
<input value={uploadRuntime} onChange={(event) => setUploadRuntime(event.target.value)} placeholder="python3, node, uv, uvx" className="field-control w-full px-3 py-2 text-sm font-mono" />
|
<input value={uploadRuntime} onChange={(event) => setUploadRuntime(event.target.value)} placeholder="python3, node, uv, uvx" className="field-control w-full px-3 py-2 text-sm font-mono" />
|
||||||
</div>
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<FieldLabel>Upload Launch Mode</FieldLabel>
|
||||||
|
<select value={uploadEntryMode} onChange={(event) => setUploadEntryMode(event.target.value as "file" | "python-module" | "console-script")} className="field-control w-full px-3 py-2 text-sm font-mono">
|
||||||
|
<option value="file">File</option>
|
||||||
|
<option value="python-module">Python module</option>
|
||||||
|
<option value="console-script">Console script</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<FieldLabel>Upload Entrypoint</FieldLabel>
|
<FieldLabel>Upload Entrypoint</FieldLabel>
|
||||||
<input value={uploadEntrypoint} onChange={(event) => 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" />
|
<input value={uploadEntrypoint} onChange={(event) => 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" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-5 flex flex-wrap items-center gap-3">
|
<div className="mt-5 flex flex-wrap items-center gap-3">
|
||||||
|
|||||||
@@ -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, /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, /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, /\.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(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, /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(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, /\/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, /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, /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, /serverPayloadFromJson/, 'MCP page must parse edited server JSON')
|
||||||
assert.match(panelSource, /startEditingServer/, 'MCP installed server list must expose edit state')
|
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')
|
assert.match(panelSource, /Server JSON/, 'MCP installed server editor must render an inline JSON file editor')
|
||||||
|
|||||||
Reference in New Issue
Block a user