Add MCP broker access controls
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"
|
||||
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
|
||||
import type { McpServerInstall } from "./mcpServerStore"
|
||||
|
||||
const MCP_BROKER_REQUEST_TIMEOUT_MS = Number.parseInt(process.env.MCP_BROKER_REQUEST_TIMEOUT_MS || "45000", 10)
|
||||
|
||||
function cleanEnv(env: Record<string, string>) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(env).filter(([, value]) => typeof value === "string" && value.length > 0),
|
||||
)
|
||||
}
|
||||
|
||||
function inheritedEnv() {
|
||||
return Object.fromEntries(
|
||||
Object.entries(process.env).filter((entry): entry is [string, string] => typeof entry[1] === "string"),
|
||||
)
|
||||
}
|
||||
|
||||
function transportForServer(server: McpServerInstall) {
|
||||
if (server.transport === "http") {
|
||||
return new StreamableHTTPClientTransport(new URL(server.command), {
|
||||
requestInit: {
|
||||
headers: cleanEnv(server.env),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return new StdioClientTransport({
|
||||
command: server.command,
|
||||
args: server.args,
|
||||
env: {
|
||||
...inheritedEnv(),
|
||||
...cleanEnv(server.env),
|
||||
},
|
||||
stderr: "pipe",
|
||||
})
|
||||
}
|
||||
|
||||
async function withMcpClient<T>(server: McpServerInstall, fn: (client: Client) => Promise<T>) {
|
||||
const client = new Client({
|
||||
name: "openshell-control-mcp-broker",
|
||||
version: "0.1.0",
|
||||
})
|
||||
const transport = transportForServer(server)
|
||||
|
||||
try {
|
||||
await client.connect(transport, { timeout: MCP_BROKER_REQUEST_TIMEOUT_MS })
|
||||
return await fn(client)
|
||||
} finally {
|
||||
await client.close().catch(() => null)
|
||||
}
|
||||
}
|
||||
|
||||
export async function listBrokerServerTools(server: McpServerInstall) {
|
||||
return withMcpClient(server, async (client) => {
|
||||
const result = await client.listTools({}, { timeout: MCP_BROKER_REQUEST_TIMEOUT_MS })
|
||||
return result.tools.map((tool) => ({
|
||||
name: tool.name,
|
||||
title: tool.title,
|
||||
description: tool.description,
|
||||
inputSchema: tool.inputSchema,
|
||||
annotations: tool.annotations,
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
export async function callBrokerServerTool(
|
||||
server: McpServerInstall,
|
||||
toolName: string,
|
||||
toolArguments: Record<string, unknown>,
|
||||
) {
|
||||
return withMcpClient(server, async (client) => (
|
||||
client.callTool(
|
||||
{
|
||||
name: toolName,
|
||||
arguments: toolArguments,
|
||||
},
|
||||
undefined,
|
||||
{ timeout: MCP_BROKER_REQUEST_TIMEOUT_MS },
|
||||
)
|
||||
))
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { createHash, randomBytes, timingSafeEqual } from "node:crypto"
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { listMcpServers, sandboxCanAccessMcpServer } from "./mcpServerStore"
|
||||
|
||||
export type SandboxMcpBrokerSession = {
|
||||
sandboxId: string
|
||||
sandboxName: string | null
|
||||
tokenHash: string
|
||||
enabled: boolean
|
||||
createdAt: string
|
||||
rotatedAt: string
|
||||
expiresAt: string | null
|
||||
}
|
||||
|
||||
type StoreShape = {
|
||||
sessions: Record<string, SandboxMcpBrokerSession>
|
||||
}
|
||||
|
||||
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-broker-sessions.json")
|
||||
const DEFAULT_TOKEN_TTL_HOURS = Number.parseInt(process.env.MCP_BROKER_TOKEN_TTL_HOURS || "168", 10)
|
||||
|
||||
function emptyStore(): StoreShape {
|
||||
return { sessions: {} }
|
||||
}
|
||||
|
||||
async function readStore(): Promise<StoreShape> {
|
||||
try {
|
||||
const raw = await readFile(STORE_PATH, "utf8")
|
||||
const parsed = JSON.parse(raw)
|
||||
return parsed && typeof parsed === "object" && parsed.sessions && typeof parsed.sessions === "object"
|
||||
? parsed
|
||||
: emptyStore()
|
||||
} catch {
|
||||
return emptyStore()
|
||||
}
|
||||
}
|
||||
|
||||
async function writeStore(store: StoreShape) {
|
||||
await mkdir(STORE_DIR, { recursive: true, mode: 0o700 })
|
||||
await writeFile(STORE_PATH, JSON.stringify(store, null, 2), { mode: 0o600 })
|
||||
}
|
||||
|
||||
function tokenHash(token: string) {
|
||||
return createHash("sha256").update(token).digest("hex")
|
||||
}
|
||||
|
||||
function safeEqual(left: string, right: string) {
|
||||
const leftBuffer = Buffer.from(left)
|
||||
const rightBuffer = Buffer.from(right)
|
||||
return leftBuffer.length === rightBuffer.length && timingSafeEqual(leftBuffer, rightBuffer)
|
||||
}
|
||||
|
||||
function expiryFromNow() {
|
||||
if (!Number.isFinite(DEFAULT_TOKEN_TTL_HOURS) || DEFAULT_TOKEN_TTL_HOURS <= 0) return null
|
||||
return new Date(Date.now() + DEFAULT_TOKEN_TTL_HOURS * 60 * 60 * 1000).toISOString()
|
||||
}
|
||||
|
||||
export async function rotateSandboxMcpBrokerSession(sandboxId: string, sandboxName?: string | null) {
|
||||
const store = await readStore()
|
||||
const token = `osmcp_${randomBytes(32).toString("base64url")}`
|
||||
const now = new Date().toISOString()
|
||||
const existing = store.sessions[sandboxId]
|
||||
const session: SandboxMcpBrokerSession = {
|
||||
sandboxId,
|
||||
sandboxName: sandboxName || existing?.sandboxName || null,
|
||||
tokenHash: tokenHash(token),
|
||||
enabled: true,
|
||||
createdAt: existing?.createdAt || now,
|
||||
rotatedAt: now,
|
||||
expiresAt: expiryFromNow(),
|
||||
}
|
||||
store.sessions[sandboxId] = session
|
||||
await writeStore(store)
|
||||
return { session, token }
|
||||
}
|
||||
|
||||
export async function verifySandboxMcpBrokerToken(token: string) {
|
||||
const store = await readStore()
|
||||
const hash = tokenHash(token)
|
||||
const session = Object.values(store.sessions).find((candidate) => safeEqual(candidate.tokenHash, hash))
|
||||
if (!session || !session.enabled) return null
|
||||
if (session.expiresAt && Date.parse(session.expiresAt) <= Date.now()) return null
|
||||
return session
|
||||
}
|
||||
|
||||
export async function listAllowedBrokerServers(session: SandboxMcpBrokerSession) {
|
||||
const inventory = await listMcpServers()
|
||||
return inventory.servers.filter((server) => sandboxCanAccessMcpServer(server, session.sandboxId, session.sandboxName))
|
||||
}
|
||||
|
||||
export function readBrokerToken(request: Request) {
|
||||
const authorization = request.headers.get("authorization") || ""
|
||||
const bearer = authorization.match(/^Bearer\s+(.+)$/i)?.[1]?.trim()
|
||||
return bearer || request.headers.get("x-openshell-mcp-token")?.trim() || ""
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
|
||||
export type McpTransport = "stdio" | "http"
|
||||
|
||||
export type McpCatalogEntry = {
|
||||
id: string
|
||||
name: string
|
||||
summary: string
|
||||
websiteUrl?: string
|
||||
transport: McpTransport
|
||||
command: string
|
||||
args: string[]
|
||||
env: Record<string, string>
|
||||
tags: string[]
|
||||
}
|
||||
|
||||
export type McpServerInstall = McpCatalogEntry & {
|
||||
installedAt: string
|
||||
updatedAt: string
|
||||
source: "catalog" | "custom" | "registry"
|
||||
enabled: boolean
|
||||
accessMode: "disabled" | "allow_all" | "allow_only"
|
||||
allowedSandboxIds: string[]
|
||||
}
|
||||
|
||||
type StoreShape = {
|
||||
servers: Record<string, McpServerInstall>
|
||||
}
|
||||
|
||||
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-servers.json")
|
||||
|
||||
export const MCP_SERVER_CATALOG: McpCatalogEntry[] = [
|
||||
{
|
||||
id: "blender-mcp",
|
||||
name: "Blender MCP",
|
||||
summary: "Connect AI clients to Blender for natural-language 3D scene inspection, creation, and editing. Requires the Blender MCP add-on in Blender.",
|
||||
websiteUrl: "https://blendermcp.org/",
|
||||
transport: "stdio",
|
||||
command: "uvx",
|
||||
args: ["blender-mcp"],
|
||||
env: {},
|
||||
tags: ["blender", "3d", "creative"],
|
||||
},
|
||||
{
|
||||
id: "filesystem",
|
||||
name: "Filesystem",
|
||||
summary: "Expose selected local directories to MCP clients through a stdio server.",
|
||||
transport: "stdio",
|
||||
command: "npx",
|
||||
args: ["-y", "@modelcontextprotocol/server-filesystem", "/sandbox"],
|
||||
env: {},
|
||||
tags: ["files", "local"],
|
||||
},
|
||||
{
|
||||
id: "memory",
|
||||
name: "Memory",
|
||||
summary: "Provide a local knowledge graph for persistent notes and relationships.",
|
||||
transport: "stdio",
|
||||
command: "npx",
|
||||
args: ["-y", "@modelcontextprotocol/server-memory"],
|
||||
env: {},
|
||||
tags: ["state", "local"],
|
||||
},
|
||||
{
|
||||
id: "git",
|
||||
name: "Git",
|
||||
summary: "Inspect and operate on a local git repository from an MCP client.",
|
||||
transport: "stdio",
|
||||
command: "uvx",
|
||||
args: ["mcp-server-git", "--repository", process.cwd()],
|
||||
env: {},
|
||||
tags: ["repo", "developer"],
|
||||
},
|
||||
{
|
||||
id: "fetch",
|
||||
name: "Fetch",
|
||||
summary: "Retrieve web content for MCP clients that need URL context.",
|
||||
transport: "stdio",
|
||||
command: "uvx",
|
||||
args: ["mcp-server-fetch"],
|
||||
env: {},
|
||||
tags: ["web", "utility"],
|
||||
},
|
||||
]
|
||||
|
||||
function emptyStore(): StoreShape {
|
||||
return { servers: {} }
|
||||
}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9-]+/g, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 64)
|
||||
}
|
||||
|
||||
function normalizeStringArray(value: unknown) {
|
||||
if (!Array.isArray(value)) return []
|
||||
return value.map((item) => String(item ?? "").trim()).filter(Boolean)
|
||||
}
|
||||
|
||||
function normalizeEnv(value: unknown) {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return {}
|
||||
return Object.fromEntries(
|
||||
Object.entries(value)
|
||||
.map(([key, envValue]) => [key.trim(), String(envValue ?? "").trim()])
|
||||
.filter(([key]) => Boolean(key)),
|
||||
)
|
||||
}
|
||||
|
||||
function normalizeTransport(value: unknown): McpTransport {
|
||||
return value === "http" ? "http" : "stdio"
|
||||
}
|
||||
|
||||
function normalizeInstall(input: Partial<McpServerInstall>, existing?: McpServerInstall | null): McpServerInstall {
|
||||
const name = typeof input.name === "string" && input.name.trim() ? input.name.trim() : existing?.name || "MCP Server"
|
||||
const id = slugify(typeof input.id === "string" && input.id.trim() ? input.id : existing?.id || name)
|
||||
if (!id) throw new Error("server id is required")
|
||||
|
||||
const command = typeof input.command === "string" ? input.command.trim() : existing?.command || ""
|
||||
if (!command) throw new Error("server command is required")
|
||||
|
||||
const now = new Date().toISOString()
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
summary: typeof input.summary === "string" ? input.summary.trim() : existing?.summary || "",
|
||||
websiteUrl: typeof input.websiteUrl === "string" ? input.websiteUrl.trim() : existing?.websiteUrl,
|
||||
transport: normalizeTransport(input.transport ?? existing?.transport),
|
||||
command,
|
||||
args: normalizeStringArray(input.args ?? existing?.args),
|
||||
env: normalizeEnv(input.env ?? existing?.env),
|
||||
tags: normalizeStringArray(input.tags ?? existing?.tags),
|
||||
installedAt: existing?.installedAt || now,
|
||||
updatedAt: now,
|
||||
source: input.source === "custom" || input.source === "registry" ? input.source : existing?.source || "catalog",
|
||||
enabled: input.enabled ?? existing?.enabled ?? true,
|
||||
accessMode: input.accessMode === "allow_all" || input.accessMode === "allow_only" || input.accessMode === "disabled"
|
||||
? input.accessMode
|
||||
: existing?.accessMode || "disabled",
|
||||
allowedSandboxIds: normalizeStringArray(input.allowedSandboxIds ?? existing?.allowedSandboxIds),
|
||||
}
|
||||
}
|
||||
|
||||
async function readStore(): Promise<StoreShape> {
|
||||
try {
|
||||
const raw = await readFile(STORE_PATH, "utf8")
|
||||
const parsed = JSON.parse(raw)
|
||||
return parsed && typeof parsed === "object" && parsed.servers && typeof parsed.servers === "object"
|
||||
? parsed
|
||||
: emptyStore()
|
||||
} catch {
|
||||
return emptyStore()
|
||||
}
|
||||
}
|
||||
|
||||
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 function buildMcpClientConfig(servers: McpServerInstall[]) {
|
||||
return {
|
||||
mcpServers: Object.fromEntries(
|
||||
servers
|
||||
.filter((server) => server.enabled)
|
||||
.map((server) => [
|
||||
server.id,
|
||||
server.transport === "http"
|
||||
? {
|
||||
url: server.command,
|
||||
...(Object.keys(server.env).length > 0 ? { headers: server.env } : {}),
|
||||
}
|
||||
: {
|
||||
command: server.command,
|
||||
args: server.args,
|
||||
...(Object.keys(server.env).length > 0 ? { env: server.env } : {}),
|
||||
},
|
||||
]),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
export function sandboxCanAccessMcpServer(server: McpServerInstall, sandboxId: string, sandboxName?: string | null) {
|
||||
if (!server.enabled || server.accessMode === "disabled") return false
|
||||
if (server.accessMode === "allow_all") return true
|
||||
const allowed = new Set(server.allowedSandboxIds)
|
||||
return allowed.has(sandboxId) || Boolean(sandboxName && allowed.has(sandboxName))
|
||||
}
|
||||
|
||||
export async function listMcpServers() {
|
||||
const store = await readStore()
|
||||
const servers = Object.values(store.servers).sort((a, b) => a.name.localeCompare(b.name))
|
||||
return {
|
||||
catalog: MCP_SERVER_CATALOG,
|
||||
servers,
|
||||
config: buildMcpClientConfig(servers),
|
||||
}
|
||||
}
|
||||
|
||||
export async function installMcpServer(input: Partial<McpServerInstall>) {
|
||||
const store = await readStore()
|
||||
const fromCatalog = typeof input.id === "string"
|
||||
? MCP_SERVER_CATALOG.find((entry) => entry.id === input.id)
|
||||
: null
|
||||
const next = normalizeInstall(
|
||||
{
|
||||
...fromCatalog,
|
||||
...input,
|
||||
source: input.source || (fromCatalog ? "catalog" : "custom"),
|
||||
},
|
||||
typeof input.id === "string" ? store.servers[slugify(input.id)] : null,
|
||||
)
|
||||
store.servers[next.id] = next
|
||||
await writeStore(store)
|
||||
return next
|
||||
}
|
||||
|
||||
export async function setMcpServerEnabled(serverId: string, enabled: boolean) {
|
||||
const store = await readStore()
|
||||
const id = slugify(serverId)
|
||||
const existing = store.servers[id]
|
||||
if (!existing) throw new Error("MCP server is not installed")
|
||||
store.servers[id] = normalizeInstall({ ...existing, enabled }, existing)
|
||||
await writeStore(store)
|
||||
return store.servers[id]
|
||||
}
|
||||
|
||||
export async function updateMcpServerAccess(
|
||||
serverId: string,
|
||||
input: {
|
||||
enabled?: boolean
|
||||
accessMode?: McpServerInstall["accessMode"]
|
||||
allowedSandboxIds?: string[]
|
||||
},
|
||||
) {
|
||||
const store = await readStore()
|
||||
const id = slugify(serverId)
|
||||
const existing = store.servers[id]
|
||||
if (!existing) throw new Error("MCP server is not installed")
|
||||
store.servers[id] = normalizeInstall({
|
||||
...existing,
|
||||
enabled: input.enabled ?? existing.enabled,
|
||||
accessMode: input.accessMode ?? existing.accessMode,
|
||||
allowedSandboxIds: input.allowedSandboxIds ?? existing.allowedSandboxIds,
|
||||
}, existing)
|
||||
await writeStore(store)
|
||||
return store.servers[id]
|
||||
}
|
||||
|
||||
export async function uninstallMcpServer(serverId: string) {
|
||||
const store = await readStore()
|
||||
const id = slugify(serverId)
|
||||
if (!store.servers[id]) throw new Error("MCP server is not installed")
|
||||
delete store.servers[id]
|
||||
await writeStore(store)
|
||||
return { id }
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import { rotateSandboxMcpBrokerSession } from "./mcpBrokerStore"
|
||||
import { uploadSandboxFile } from "./sandboxFiles"
|
||||
|
||||
export const SANDBOX_MCP_MANIFEST_PATH = "/sandbox/openshell_control_mcp.md"
|
||||
|
||||
type SandboxRef = {
|
||||
id: string
|
||||
name?: string | null
|
||||
}
|
||||
|
||||
type BrokerHandoffOptions = {
|
||||
brokerBaseUrl: string
|
||||
rotateToken?: boolean
|
||||
}
|
||||
|
||||
function normalizeBrokerBaseUrl(value: string) {
|
||||
return value.replace(/\/+$/, "")
|
||||
}
|
||||
|
||||
export async function buildSandboxMcpBrokerHandoff(
|
||||
sandbox: SandboxRef,
|
||||
options: BrokerHandoffOptions,
|
||||
) {
|
||||
const { session, token } = await rotateSandboxMcpBrokerSession(sandbox.id, sandbox.name)
|
||||
const brokerBaseUrl = normalizeBrokerBaseUrl(options.brokerBaseUrl)
|
||||
const now = new Date().toISOString()
|
||||
|
||||
return {
|
||||
session,
|
||||
token,
|
||||
markdown: [
|
||||
"# OpenShell Control MCP Broker",
|
||||
"",
|
||||
`Generated: ${now}`,
|
||||
`Sandbox: ${sandbox.name || sandbox.id}`,
|
||||
`Sandbox ID: ${sandbox.id}`,
|
||||
"",
|
||||
"This sandbox does not receive a list of MCP servers. MCP access is brokered by OpenShell Control, and the control plane enforces which tools are available on every request.",
|
||||
"",
|
||||
"## Broker Endpoints",
|
||||
"",
|
||||
`- Capabilities: \`${brokerBaseUrl}/capabilities\``,
|
||||
`- Call: \`${brokerBaseUrl}/call\``,
|
||||
"",
|
||||
"## Authentication",
|
||||
"",
|
||||
"Send this token as an Authorization bearer token or x-openshell-mcp-token header:",
|
||||
"",
|
||||
"```",
|
||||
token,
|
||||
"```",
|
||||
"",
|
||||
"## Rules",
|
||||
"",
|
||||
"- Do not assume any MCP server exists unless the broker returns it as available.",
|
||||
"- Do not attempt to connect directly to MCP servers.",
|
||||
"- Do not invent credentials. The broker owns server credentials and launch details.",
|
||||
"- If access changes, ask the operator to issue a new broker config.",
|
||||
"",
|
||||
].join("\n"),
|
||||
}
|
||||
}
|
||||
|
||||
export async function syncSandboxMcpManifest(
|
||||
sandbox: SandboxRef,
|
||||
options: BrokerHandoffOptions,
|
||||
) {
|
||||
const handoff = await buildSandboxMcpBrokerHandoff(sandbox, options)
|
||||
const uploaded = await uploadSandboxFile(
|
||||
sandbox.id,
|
||||
SANDBOX_MCP_MANIFEST_PATH,
|
||||
"openshell_control_mcp.md",
|
||||
Buffer.from(handoff.markdown, "utf8"),
|
||||
)
|
||||
|
||||
return {
|
||||
path: uploaded.path,
|
||||
sandboxName: uploaded.sandboxName,
|
||||
bytes: uploaded.bytes,
|
||||
brokerSession: {
|
||||
sandboxId: handoff.session.sandboxId,
|
||||
sandboxName: handoff.session.sandboxName,
|
||||
enabled: handoff.session.enabled,
|
||||
rotatedAt: handoff.session.rotatedAt,
|
||||
expiresAt: handoff.session.expiresAt,
|
||||
},
|
||||
markdown: handoff.markdown,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user