Configure sandbox MCP broker access

This commit is contained in:
mmckeen-nv
2026-04-29 13:01:59 -07:00
parent 6b2451e216
commit 10f5a0b6aa
11 changed files with 626 additions and 66 deletions
+23 -1
View File
@@ -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)
+47
View File
@@ -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.",
})
}