Add MCP server health check
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { listBrokerServerTools } from "@/app/lib/mcpBrokerClient"
|
||||
import { listMcpServers } from "@/app/lib/mcpServerStore"
|
||||
|
||||
export async function GET() {
|
||||
const checkedAt = new Date().toISOString()
|
||||
try {
|
||||
const inventory = await listMcpServers()
|
||||
const enabledServers = inventory.servers.filter((server) => server.enabled)
|
||||
const checks = await Promise.all(enabledServers.map(async (server) => {
|
||||
const startedAt = Date.now()
|
||||
try {
|
||||
const tools = await listBrokerServerTools(server)
|
||||
return {
|
||||
id: server.id,
|
||||
name: server.name,
|
||||
transport: server.transport,
|
||||
source: server.source,
|
||||
command: server.command,
|
||||
args: server.args,
|
||||
ok: true,
|
||||
toolCount: tools.length,
|
||||
tools: tools.slice(0, 12).map((tool) => tool.name),
|
||||
durationMs: Date.now() - startedAt,
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
id: server.id,
|
||||
name: server.name,
|
||||
transport: server.transport,
|
||||
source: server.source,
|
||||
command: server.command,
|
||||
args: server.args,
|
||||
ok: false,
|
||||
toolCount: 0,
|
||||
tools: [],
|
||||
durationMs: Date.now() - startedAt,
|
||||
error: error instanceof Error ? error.message : "MCP server health check failed",
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
return NextResponse.json({
|
||||
ok: checks.every((check) => check.ok),
|
||||
checkedAt,
|
||||
installedCount: inventory.servers.length,
|
||||
enabledCount: enabledServers.length,
|
||||
checks,
|
||||
})
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
checkedAt,
|
||||
error: error instanceof Error ? error.message : "Failed to check MCP server health",
|
||||
checks: [],
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -128,10 +128,139 @@ function HealthAccordion({ sandboxes }: { sandboxes: SandboxInventoryItem[] }) {
|
||||
)
|
||||
}
|
||||
|
||||
type McpHealthCheck = {
|
||||
id: string
|
||||
name: string
|
||||
transport: string
|
||||
source: string
|
||||
command: string
|
||||
args: string[]
|
||||
ok: boolean
|
||||
toolCount: number
|
||||
tools: string[]
|
||||
durationMs: number
|
||||
error?: string
|
||||
}
|
||||
|
||||
type McpHealthResponse = {
|
||||
ok: boolean
|
||||
checkedAt: string
|
||||
installedCount: number
|
||||
enabledCount: number
|
||||
checks: McpHealthCheck[]
|
||||
error?: string
|
||||
}
|
||||
|
||||
function McpHealthAccordion() {
|
||||
const [open, setOpen] = useState(true)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [health, setHealth] = useState<McpHealthResponse | null>(null)
|
||||
const [message, setMessage] = useState("")
|
||||
|
||||
async function loadHealth() {
|
||||
try {
|
||||
setLoading(true)
|
||||
setMessage("")
|
||||
const response = await fetch("/api/mcp/health", { cache: "no-store" })
|
||||
const data = await response.json() as McpHealthResponse
|
||||
if (!response.ok) throw new Error(data.error || "Failed to check MCP server health")
|
||||
setHealth(data)
|
||||
} catch (error) {
|
||||
setMessage(error instanceof Error ? error.message : "Failed to check MCP server health")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (open && !health && !loading) loadHealth()
|
||||
}, [open, health, loading])
|
||||
|
||||
const checks = health?.checks || []
|
||||
|
||||
return (
|
||||
<section className="panel overflow-hidden">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen((current) => !current)}
|
||||
aria-expanded={open}
|
||||
className="flex w-full items-center justify-between gap-4 p-5 text-left"
|
||||
>
|
||||
<div>
|
||||
<p className="text-[10px] font-mono uppercase tracking-wider text-[var(--nvidia-green)]">Operator Checks</p>
|
||||
<h2 className="mt-1 text-sm font-semibold uppercase tracking-wider text-[var(--foreground)]">MCP Server Health</h2>
|
||||
<p className="mt-1 text-xs text-[var(--foreground-dim)]">Starts each enabled MCP server from the control host and lists its tools.</p>
|
||||
</div>
|
||||
<svg
|
||||
className={`h-4 w-4 shrink-0 text-[var(--foreground-dim)] transition-transform ${open ? "rotate-90" : ""}`}
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path strokeLinecap="square" strokeLinejoin="miter" strokeWidth={2} d="M9 5l7 7-7 7" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div className="space-y-4 border-t border-[var(--border-subtle)] p-5">
|
||||
<div className="flex items-center justify-between gap-3 max-sm:flex-col max-sm:items-start">
|
||||
<div className="text-xs text-[var(--foreground-dim)]">
|
||||
{health ? `${health.enabledCount} enabled / ${health.installedCount} installed / ${health.ok ? "healthy" : "attention needed"}` : "No health check has run yet."}
|
||||
</div>
|
||||
<button type="button" onClick={loadHealth} disabled={loading} className="action-button px-3 py-2">
|
||||
{loading ? "Checking..." : "Refresh MCP Health"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{message && <div className="rounded-sm border border-red-500/40 bg-red-500/10 p-3 text-xs text-red-300">{message}</div>}
|
||||
|
||||
{checks.length === 0 ? (
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4 text-sm text-[var(--foreground-dim)]">
|
||||
{loading ? "Checking enabled MCP servers..." : "No enabled MCP servers to check."}
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
|
||||
{checks.map((check) => (
|
||||
<div key={check.id} className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<h3 className="truncate text-sm font-mono font-semibold text-[var(--foreground)]">{check.name}</h3>
|
||||
<p className="mt-1 break-all text-[11px] font-mono text-[var(--foreground-dim)]">{check.command} {check.args.join(" ")}</p>
|
||||
</div>
|
||||
<span className={`status-chip px-2 py-1 ${check.ok ? "bg-[var(--status-running-bg)] text-[var(--status-running)]" : "bg-[var(--status-error-bg)] text-[var(--status-error)]"}`}>
|
||||
{check.ok ? "healthy" : "failed"}
|
||||
</span>
|
||||
</div>
|
||||
<p className="mt-3 text-xs text-[var(--foreground-dim)]">
|
||||
{check.ok ? `${check.toolCount} tool${check.toolCount === 1 ? "" : "s"} discovered in ${check.durationMs} ms.` : check.error || "Server did not respond."}
|
||||
</p>
|
||||
{check.tools.length > 0 && (
|
||||
<div className="mt-3 flex flex-wrap gap-2">
|
||||
{check.tools.map((tool) => (
|
||||
<span key={tool} className="rounded-sm border border-[var(--border-subtle)] px-2 py-1 text-[10px] font-mono text-[var(--foreground-dim)]">{tool}</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{health && (
|
||||
<p className="text-[11px] text-[var(--foreground-dim)]">Checked {new Date(health.checkedAt).toLocaleString()}</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
export default function HelpPanel({ sandboxes }: { sandboxes: SandboxInventoryItem[] }) {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<HealthAccordion sandboxes={sandboxes} />
|
||||
<McpHealthAccordion />
|
||||
|
||||
<section className="panel p-8">
|
||||
<div className="flex items-start justify-between gap-4 max-md:flex-col">
|
||||
|
||||
@@ -11,6 +11,7 @@ const manifestPath = path.join(root, 'app/lib/sandboxMcpManifest.ts')
|
||||
const privilegedFilesPath = path.join(root, 'app/lib/sandboxPrivilegedFiles.ts')
|
||||
const routePath = path.join(root, 'app/api/mcp/route.ts')
|
||||
const uploadRoutePath = path.join(root, 'app/api/mcp/upload/route.ts')
|
||||
const healthRoutePath = path.join(root, 'app/api/mcp/health/route.ts')
|
||||
const registryRoutePath = path.join(root, 'app/api/mcp/registry/route.ts')
|
||||
const brokerCapabilitiesRoutePath = path.join(root, 'app/api/mcp/broker/capabilities/route.ts')
|
||||
const brokerCallRoutePath = path.join(root, 'app/api/mcp/broker/call/route.ts')
|
||||
@@ -18,11 +19,12 @@ const sandboxMcpRoutePath = path.join(root, 'app/api/sandbox/[sandboxId]/mcp/rou
|
||||
const brokerUrlPath = path.join(root, 'app/lib/mcpBrokerUrl.ts')
|
||||
const middlewarePath = path.join(root, 'middleware.ts')
|
||||
const panelPath = path.join(root, 'app/components/McpConfigurationPanel.tsx')
|
||||
const helpPath = path.join(root, 'app/components/HelpPanel.tsx')
|
||||
const sidebarPath = path.join(root, 'app/components/Sidebar.tsx')
|
||||
const sandboxListPath = path.join(root, 'app/components/SandboxList.tsx')
|
||||
const pagePath = path.join(root, 'app/page.tsx')
|
||||
|
||||
const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privilegedFilesSource, routeSource, uploadRouteSource, registryRouteSource, brokerCapabilitiesRouteSource, brokerCallRouteSource, sandboxMcpRouteSource, brokerUrlSource, middlewareSource, panelSource, sidebarSource, sandboxListSource, pageSource] = await Promise.all([
|
||||
const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privilegedFilesSource, routeSource, uploadRouteSource, healthRouteSource, registryRouteSource, brokerCapabilitiesRouteSource, brokerCallRouteSource, sandboxMcpRouteSource, brokerUrlSource, middlewareSource, panelSource, helpSource, sidebarSource, sandboxListSource, pageSource] = await Promise.all([
|
||||
readFile(storePath, 'utf8'),
|
||||
readFile(brokerStorePath, 'utf8'),
|
||||
readFile(brokerClientPath, 'utf8'),
|
||||
@@ -30,6 +32,7 @@ const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privi
|
||||
readFile(privilegedFilesPath, 'utf8'),
|
||||
readFile(routePath, 'utf8'),
|
||||
readFile(uploadRoutePath, 'utf8'),
|
||||
readFile(healthRoutePath, 'utf8'),
|
||||
readFile(registryRoutePath, 'utf8'),
|
||||
readFile(brokerCapabilitiesRoutePath, 'utf8'),
|
||||
readFile(brokerCallRoutePath, 'utf8'),
|
||||
@@ -37,6 +40,7 @@ const [storeSource, brokerStoreSource, brokerClientSource, manifestSource, privi
|
||||
readFile(brokerUrlPath, 'utf8'),
|
||||
readFile(middlewarePath, 'utf8'),
|
||||
readFile(panelPath, 'utf8'),
|
||||
readFile(helpPath, 'utf8'),
|
||||
readFile(sidebarPath, 'utf8'),
|
||||
readFile(sandboxListPath, 'utf8'),
|
||||
readFile(pagePath, 'utf8'),
|
||||
@@ -91,6 +95,8 @@ assert.match(uploadRouteSource, /request\.formData\(\)/, 'MCP upload API must ac
|
||||
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, /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')
|
||||
assert.match(registryRouteSource, /registry\.modelcontextprotocol\.io/, 'MCP registry search should default to the official registry')
|
||||
assert.match(registryRouteSource, /\/v0\/servers/, 'MCP registry search should call the registry server list endpoint')
|
||||
assert.match(registryRouteSource, /normalizeRegistryEntry/, 'MCP registry search should normalize results into installable entries')
|
||||
@@ -122,6 +128,9 @@ assert.match(panelSource, /allowed \|\| hasOtherMcpAccess \? "sync" : "revoke"/,
|
||||
assert.match(panelSource, /syncAccessModeChange/, 'MCP availability changes must synchronize sandbox broker policy')
|
||||
assert.match(panelSource, /Setup Guide/, 'MCP catalog cards should link out to app setup guides')
|
||||
assert.match(panelSource, /Client JSON/, 'MCP page must surface generated client JSON')
|
||||
assert.match(helpSource, /MCP Server Health/, 'Help must expose MCP server health checks')
|
||||
assert.match(helpSource, /\/api\/mcp\/health/, 'Help MCP health check must call the health API')
|
||||
assert.match(helpSource, /Refresh MCP Health/, 'Help MCP health check must be refreshable')
|
||||
assert.match(sandboxListSource, /Allowed MCP Server Access/, 'Sandbox page must expose allowed MCP server access accordion')
|
||||
assert.match(sandboxListSource, /Sync Broker Config/, 'Sandbox MCP access controls must sync broker config')
|
||||
assert.match(sandboxListSource, /hasOtherMcpAccess/, 'Sandbox MCP access controls must revoke broker config when no MCP servers remain')
|
||||
|
||||
Reference in New Issue
Block a user