Configure sandbox MCP broker access
This commit is contained in:
@@ -5,16 +5,38 @@ import {
|
||||
verifySandboxMcpBrokerToken,
|
||||
} from "@/app/lib/mcpBrokerStore"
|
||||
import { callBrokerServerTool } from "@/app/lib/mcpBrokerClient"
|
||||
import {
|
||||
brokerMcpUnauthorizedError,
|
||||
handleBrokerMcpJsonRpcBody,
|
||||
isBrokerMcpJsonRpcBody,
|
||||
} from "@/app/lib/mcpBrokerProtocol"
|
||||
|
||||
function jsonRpcResponse(payload: unknown, status = 200) {
|
||||
return NextResponse.json(payload, {
|
||||
status,
|
||||
headers: { "Cache-Control": "no-store" },
|
||||
})
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const token = readBrokerToken(request)
|
||||
const session = token ? await verifySandboxMcpBrokerToken(token) : null
|
||||
const isMcpJsonRpc = isBrokerMcpJsonRpcBody(body)
|
||||
if (!session) {
|
||||
if (isMcpJsonRpc) return jsonRpcResponse(brokerMcpUnauthorizedError(), 401)
|
||||
return NextResponse.json({ ok: false, error: "MCP broker is unavailable for this sandbox." }, { status: 401 })
|
||||
}
|
||||
|
||||
const body = await request.json().catch(() => ({}))
|
||||
if (isMcpJsonRpc) {
|
||||
const response = await handleBrokerMcpJsonRpcBody(session, body)
|
||||
if (response.body === null) {
|
||||
return new Response(null, { status: response.status, headers: { "Cache-Control": "no-store" } })
|
||||
}
|
||||
return jsonRpcResponse(response.body, response.status)
|
||||
}
|
||||
|
||||
const requestedServerId = typeof body?.serverId === "string" ? body.serverId : ""
|
||||
const toolName = typeof body?.toolName === "string" ? body.toolName : ""
|
||||
const toolArguments = body?.arguments && typeof body.arguments === "object" && !Array.isArray(body.arguments)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import {
|
||||
readBrokerToken,
|
||||
verifySandboxMcpBrokerToken,
|
||||
} from "@/app/lib/mcpBrokerStore"
|
||||
import {
|
||||
brokerMcpUnauthorizedError,
|
||||
handleBrokerMcpJsonRpcBody,
|
||||
MCP_PROTOCOL_VERSION,
|
||||
} from "@/app/lib/mcpBrokerProtocol"
|
||||
|
||||
function jsonRpcResponse(payload: unknown, status = 200) {
|
||||
return NextResponse.json(payload, {
|
||||
status,
|
||||
headers: { "Cache-Control": "no-store" },
|
||||
})
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const parsedBody = await request.json().catch(() => null)
|
||||
const token = readBrokerToken(request)
|
||||
const session = token ? await verifySandboxMcpBrokerToken(token) : null
|
||||
if (!session) {
|
||||
return jsonRpcResponse(brokerMcpUnauthorizedError(), 401)
|
||||
}
|
||||
|
||||
const response = await handleBrokerMcpJsonRpcBody(session, parsedBody)
|
||||
if (response.body === null) {
|
||||
return new Response(null, { status: response.status, headers: { "Cache-Control": "no-store" } })
|
||||
}
|
||||
|
||||
return jsonRpcResponse(response.body, response.status)
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const token = readBrokerToken(request)
|
||||
const session = token ? await verifySandboxMcpBrokerToken(token) : null
|
||||
if (!session) {
|
||||
return jsonRpcResponse(brokerMcpUnauthorizedError(), 401)
|
||||
}
|
||||
return jsonRpcResponse({
|
||||
ok: true,
|
||||
endpoint: "mcp",
|
||||
protocol: MCP_PROTOCOL_VERSION,
|
||||
note: "POST MCP JSON-RPC messages to this endpoint.",
|
||||
})
|
||||
}
|
||||
+28
-10
@@ -6,6 +6,7 @@ import {
|
||||
updateMcpServerAccess,
|
||||
uninstallMcpServer,
|
||||
} from "@/app/lib/mcpServerStore"
|
||||
import { autoSyncSandboxMcpAccess } from "@/app/lib/mcpSandboxAutoSync"
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
@@ -23,29 +24,44 @@ export async function POST(request: Request) {
|
||||
|
||||
if (action === "uninstall") {
|
||||
const serverId = typeof body?.serverId === "string" ? body.serverId : ""
|
||||
return NextResponse.json({ server: await uninstallMcpServer(serverId), ...(await listMcpServers()) })
|
||||
const before = await listMcpServers()
|
||||
const server = await uninstallMcpServer(serverId)
|
||||
const after = await listMcpServers()
|
||||
const sandboxSync = await autoSyncSandboxMcpAccess(request, before.servers, after.servers, [server.id])
|
||||
return NextResponse.json({ server, ...after, sandboxSync })
|
||||
}
|
||||
|
||||
if (action === "enable" || action === "disable") {
|
||||
const serverId = typeof body?.serverId === "string" ? body.serverId : ""
|
||||
const before = await listMcpServers()
|
||||
const server = await setMcpServerEnabled(serverId, action === "enable")
|
||||
const after = await listMcpServers()
|
||||
const sandboxSync = await autoSyncSandboxMcpAccess(request, before.servers, after.servers, [server.id])
|
||||
return NextResponse.json({
|
||||
server: await setMcpServerEnabled(serverId, action === "enable"),
|
||||
...(await listMcpServers()),
|
||||
server,
|
||||
...after,
|
||||
sandboxSync,
|
||||
})
|
||||
}
|
||||
|
||||
if (action === "update-access") {
|
||||
const serverId = typeof body?.serverId === "string" ? body.serverId : ""
|
||||
const before = await listMcpServers()
|
||||
const server = await updateMcpServerAccess(serverId, {
|
||||
enabled: typeof body?.enabled === "boolean" ? body.enabled : undefined,
|
||||
accessMode: body?.accessMode,
|
||||
allowedSandboxIds: Array.isArray(body?.allowedSandboxIds) ? body.allowedSandboxIds : undefined,
|
||||
})
|
||||
const after = await listMcpServers()
|
||||
const sandboxSync = await autoSyncSandboxMcpAccess(request, before.servers, after.servers, [server.id])
|
||||
return NextResponse.json({
|
||||
server: await updateMcpServerAccess(serverId, {
|
||||
enabled: typeof body?.enabled === "boolean" ? body.enabled : undefined,
|
||||
accessMode: body?.accessMode,
|
||||
allowedSandboxIds: Array.isArray(body?.allowedSandboxIds) ? body.allowedSandboxIds : undefined,
|
||||
}),
|
||||
...(await listMcpServers()),
|
||||
server,
|
||||
...after,
|
||||
sandboxSync,
|
||||
})
|
||||
}
|
||||
|
||||
const before = await listMcpServers()
|
||||
const server = await installMcpServer({
|
||||
id: body?.id,
|
||||
name: body?.name,
|
||||
@@ -61,8 +77,10 @@ export async function POST(request: Request) {
|
||||
accessMode: body?.accessMode,
|
||||
allowedSandboxIds: body?.allowedSandboxIds,
|
||||
})
|
||||
const after = await listMcpServers()
|
||||
const sandboxSync = await autoSyncSandboxMcpAccess(request, before.servers, after.servers, [server.id])
|
||||
|
||||
return NextResponse.json({ server, ...(await listMcpServers()) })
|
||||
return NextResponse.json({ server, ...after, sandboxSync })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Failed to update MCP servers"
|
||||
return NextResponse.json({ error: message }, { status: 400 })
|
||||
|
||||
Reference in New Issue
Block a user