Add MCP registry wizard preflight flow

This commit is contained in:
Codex
2026-04-29 15:32:28 +00:00
parent 42561a7eff
commit ce4303a602
8 changed files with 955 additions and 223 deletions
+253
View File
@@ -0,0 +1,253 @@
import { readdir, readFile, stat, writeFile } from "node:fs/promises"
import path from "node:path"
import { getSandboxInferenceConfig } from "./sandboxInferenceStore"
import type { McpPreflightResult } from "./mcpPreflight"
import type { McpServerInstall } from "./mcpServerStore"
export type McpPreflightRepairChange = {
type: "file" | "launch"
path?: string
summary: string
}
export type McpPreflightRepairResult = {
attempted: boolean
ok: boolean
provider: "openai-compatible"
model: string
baseUrl: string
summary: string
changes: McpPreflightRepairChange[]
error?: string
updatedServer?: Pick<McpServerInstall, "command" | "args" | "env">
}
type RepairContext = {
uploadRoot: string
projectRoot: string
server: McpServerInstall
preflight: McpPreflightResult
dependencyLogs?: string[]
sandboxId?: string | null
}
type LlmRepairResponse = {
summary?: string
files?: Array<{ path?: string; content?: string; summary?: string }>
launch?: {
command?: string
args?: string[]
env?: Record<string, string>
summary?: string
}
}
const MAX_CONTEXT_FILE_BYTES = Number.parseInt(process.env.MCP_PREFLIGHT_REPAIR_MAX_FILE_BYTES || String(40 * 1024), 10)
const MAX_CONTEXT_FILES = Number.parseInt(process.env.MCP_PREFLIGHT_REPAIR_MAX_FILES || "18", 10)
const MAX_REPAIR_FILE_BYTES = Number.parseInt(process.env.MCP_PREFLIGHT_REPAIR_MAX_OUTPUT_FILE_BYTES || String(512 * 1024), 10)
const REQUEST_TIMEOUT_MS = Number.parseInt(process.env.MCP_PREFLIGHT_REPAIR_TIMEOUT_MS || "60000", 10)
const DEFAULT_MODEL = process.env.MCP_PREFLIGHT_LLM_MODEL || process.env.OPENAI_MODEL || "gpt-4o-mini"
const DEFAULT_BASE_URL = process.env.MCP_PREFLIGHT_LLM_BASE_URL || process.env.OPENAI_BASE_URL || process.env.VLLM_BASE_URL || "http://localhost:8000/v1"
function cleanBaseUrl(value: string) {
return value.replace(/\/+$/, "")
}
function relativeInside(root: string, candidate: string) {
const fullPath = path.resolve(root, candidate)
if (fullPath !== root && !fullPath.startsWith(`${root}${path.sep}`)) {
throw new Error("repair path must stay inside the uploaded server")
}
return fullPath
}
function normalizeRepairPath(value: string) {
const normalized = path.posix.normalize(value.replace(/\\/g, "/")).replace(/^\/+/, "")
if (!normalized || normalized === "." || normalized.startsWith("../") || normalized.includes("/../")) {
throw new Error("repair file path must be relative to the uploaded server")
}
return normalized
}
function isLikelyUsefulFile(filePath: string) {
if (/(^|\/)(node_modules|\.venv|\.git|dist|build|__pycache__)(\/|$)/.test(filePath)) return false
return /\.(py|js|mjs|cjs|ts|json|toml|txt|md|yaml|yml)$/i.test(filePath)
|| /(^|\/)(requirements\.txt|package\.json|pyproject\.toml)$/.test(filePath)
}
async function collectFiles(root: string, directory = root): Promise<string[]> {
const entries = await readdir(directory, { withFileTypes: true }).catch(() => [])
const files: string[] = []
for (const entry of entries) {
const fullPath = path.join(directory, entry.name)
const relative = path.relative(root, fullPath).replace(/\\/g, "/")
if (entry.isDirectory()) {
if (/(^|\/)(node_modules|\.venv|\.git|dist|build|__pycache__)(\/|$)/.test(relative)) continue
files.push(...await collectFiles(root, fullPath))
} else if (entry.isFile()) {
if (!isLikelyUsefulFile(relative)) continue
files.push(relative)
}
}
return files
}
async function readContextFiles(root: string) {
const files = (await collectFiles(root)).slice(0, MAX_CONTEXT_FILES)
const context: Array<{ path: string; content: string; truncated: boolean }> = []
for (const file of files) {
const fullPath = relativeInside(root, file)
const stats = await stat(fullPath).catch(() => null)
if (!stats || stats.size > MAX_CONTEXT_FILE_BYTES * 2) continue
const content = await readFile(fullPath, "utf8").catch(() => "")
context.push({
path: file,
content: content.length > MAX_CONTEXT_FILE_BYTES ? content.slice(0, MAX_CONTEXT_FILE_BYTES) : content,
truncated: content.length > MAX_CONTEXT_FILE_BYTES,
})
}
return context
}
async function resolveRepairRoute(sandboxId?: string | null) {
if (sandboxId) {
const config = await getSandboxInferenceConfig(sandboxId)
const enabled = config.routes.filter((route) => route.enabled)
const primary = enabled.find((route) => route.id === config.primaryRouteId) || enabled[0]
if (primary?.model) {
return {
model: process.env.MCP_PREFLIGHT_LLM_MODEL || primary.model,
baseUrl: cleanBaseUrl(DEFAULT_BASE_URL),
}
}
}
return {
model: DEFAULT_MODEL,
baseUrl: cleanBaseUrl(DEFAULT_BASE_URL),
}
}
function extractJsonObject(value: string) {
const trimmed = value.trim()
if (trimmed.startsWith("{")) return trimmed
const match = trimmed.match(/\{[\s\S]*\}/)
if (!match) throw new Error("repair model did not return JSON")
return match[0]
}
async function requestRepairPlan(context: RepairContext, files: Awaited<ReturnType<typeof readContextFiles>>) {
const route = await resolveRepairRoute(context.sandboxId)
const controller = new AbortController()
const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS)
try {
const response = await fetch(`${route.baseUrl}/chat/completions`, {
method: "POST",
signal: controller.signal,
headers: {
"Content-Type": "application/json",
...(process.env.MCP_PREFLIGHT_LLM_API_KEY || process.env.OPENAI_API_KEY
? { Authorization: `Bearer ${process.env.MCP_PREFLIGHT_LLM_API_KEY || process.env.OPENAI_API_KEY}` }
: {}),
},
body: JSON.stringify({
model: route.model,
temperature: 0.1,
response_format: { type: "json_object" },
messages: [
{
role: "system",
content: [
"You repair uploaded Model Context Protocol stdio servers.",
"Return only JSON with optional files and launch keys.",
"Only modify files that are included in the context.",
"Do not invent dependencies unless required by the observed error.",
"Prefer minimal, runnable fixes that let MCP initialize and list tools.",
].join(" "),
},
{
role: "user",
content: JSON.stringify({
server: {
command: context.server.command,
args: context.server.args,
envKeys: Object.keys(context.server.env || {}),
},
preflight: context.preflight,
dependencyLogs: context.dependencyLogs || [],
files,
expectedJson: {
summary: "short explanation",
files: [{ path: "relative/path.py", content: "complete replacement content", summary: "what changed" }],
launch: { command: context.server.command, args: context.server.args, env: context.server.env, summary: "optional launch change" },
},
}),
},
],
}),
})
const data = await response.json().catch(() => null)
if (!response.ok) throw new Error(data?.error?.message || data?.error || `repair model request failed (${response.status})`)
const content = data?.choices?.[0]?.message?.content
if (typeof content !== "string" || !content.trim()) throw new Error("repair model returned an empty response")
return {
route,
plan: JSON.parse(extractJsonObject(content)) as LlmRepairResponse,
}
} finally {
clearTimeout(timeout)
}
}
function normalizeLaunch(server: McpServerInstall, launch?: LlmRepairResponse["launch"]) {
if (!launch || typeof launch !== "object") return null
const command = typeof launch.command === "string" && launch.command.trim() ? launch.command.trim() : server.command
const args = Array.isArray(launch.args)
? launch.args.map((arg) => String(arg ?? "").trim()).filter(Boolean)
: server.args
const env = launch.env && typeof launch.env === "object" && !Array.isArray(launch.env)
? Object.fromEntries(Object.entries(launch.env).map(([key, value]) => [key.trim(), String(value ?? "").trim()]).filter(([key]) => Boolean(key)))
: server.env
if (command === server.command && JSON.stringify(args) === JSON.stringify(server.args) && JSON.stringify(env) === JSON.stringify(server.env)) {
return null
}
return { command, args, env, summary: launch.summary || "Adjusted launch metadata." }
}
export async function repairUploadedMcpServerWithLlm(context: RepairContext): Promise<McpPreflightRepairResult> {
const files = await readContextFiles(context.projectRoot)
const { route, plan } = await requestRepairPlan(context, files)
const changes: McpPreflightRepairChange[] = []
for (const file of Array.isArray(plan.files) ? plan.files : []) {
if (typeof file?.path !== "string" || typeof file?.content !== "string") continue
if (Buffer.byteLength(file.content, "utf8") > MAX_REPAIR_FILE_BYTES) {
throw new Error(`repair output for ${file.path} is too large`)
}
const relative = normalizeRepairPath(file.path)
if (!files.some((contextFile) => contextFile.path === relative)) {
throw new Error(`repair model tried to edit a file outside provided context: ${relative}`)
}
await writeFile(relativeInside(context.projectRoot, relative), file.content, { mode: 0o600 })
changes.push({ type: "file", path: relative, summary: file.summary || `Updated ${relative}` })
}
const launch = normalizeLaunch(context.server, plan.launch)
if (launch) {
changes.push({ type: "launch", summary: launch.summary })
}
return {
attempted: true,
ok: changes.length > 0,
provider: "openai-compatible",
model: route.model,
baseUrl: route.baseUrl,
summary: plan.summary || (changes.length > 0 ? "Applied LLM-assisted MCP repair." : "The repair model did not suggest changes."),
changes,
updatedServer: launch ? {
command: launch.command,
args: launch.args,
env: launch.env,
} : undefined,
}
}
+94
View File
@@ -0,0 +1,94 @@
import { mkdir, readFile, writeFile } from "node:fs/promises"
import path from "node:path"
export type McpRegistry = {
id: string
name: string
baseUrl: string
description: string
addedAt: string
}
type StoreShape = {
registries: McpRegistry[]
}
const HOME = process.env.HOME || "/tmp"
const STORE_DIR = process.env.NEMOCLAW_DASHBOARD_STATE_DIR || path.join(HOME, ".nemoclaw-dashboard")
const STORE_PATH = path.join(STORE_DIR, "mcp-registries.json")
export const DEFAULT_MCP_REGISTRY: McpRegistry = {
id: "official",
name: "Official MCP Registry",
baseUrl: process.env.MCP_REGISTRY_BASE_URL || "https://registry.modelcontextprotocol.io",
description: "The public Model Context Protocol registry.",
addedAt: "default",
}
function slugify(value: string) {
return value
.trim()
.toLowerCase()
.replace(/[^a-z0-9-]+/g, "-")
.replace(/^-+|-+$/g, "")
.slice(0, 64) || "mcp-registry"
}
function normalizeRegistry(input: Partial<McpRegistry>, existing?: McpRegistry | null): McpRegistry {
const name = typeof input.name === "string" && input.name.trim() ? input.name.trim() : existing?.name || "MCP Registry"
const rawUrl = typeof input.baseUrl === "string" && input.baseUrl.trim() ? input.baseUrl.trim() : existing?.baseUrl || ""
if (!rawUrl) throw new Error("registry URL is required")
const parsed = new URL(rawUrl)
parsed.pathname = parsed.pathname.replace(/\/+$/, "")
parsed.search = ""
parsed.hash = ""
return {
id: slugify(input.id || existing?.id || name),
name,
baseUrl: parsed.toString().replace(/\/+$/, ""),
description: typeof input.description === "string" ? input.description.trim() : existing?.description || "",
addedAt: existing?.addedAt || new Date().toISOString(),
}
}
async function readStore(): Promise<StoreShape | null> {
try {
const raw = await readFile(STORE_PATH, "utf8")
const parsed = JSON.parse(raw)
return parsed && typeof parsed === "object" && Array.isArray(parsed.registries)
? parsed
: null
} catch {
return null
}
}
async function writeStore(store: StoreShape) {
await mkdir(STORE_DIR, { recursive: true, mode: 0o700 })
await writeFile(STORE_PATH, JSON.stringify(store, null, 2), { mode: 0o600 })
}
export async function listMcpRegistries() {
const store = await readStore()
return store?.registries || [DEFAULT_MCP_REGISTRY]
}
export async function saveMcpRegistry(input: Partial<McpRegistry>) {
const store = await readStore() || { registries: [DEFAULT_MCP_REGISTRY] }
const id = slugify(input.id || input.name || "mcp-registry")
const existing = store.registries.find((registry) => registry.id === id)
const next = normalizeRegistry({ ...input, id }, existing)
const filtered = store.registries.filter((registry) => registry.id !== next.id)
store.registries = [next, ...filtered]
await writeStore(store)
return next
}
export async function deleteMcpRegistry(registryId: string) {
const id = slugify(registryId)
const store = await readStore() || { registries: [DEFAULT_MCP_REGISTRY] }
store.registries = store.registries.filter((registry) => registry.id !== id)
await writeStore(store)
return { id }
}