|
| 1 | +/** |
| 2 | + * MedCore MCP endpoint — Model Context Protocol over Streamable HTTP |
| 3 | + * (stateless mode, JSON responses). |
| 4 | + * |
| 5 | + * Connect from any MCP client, e.g. Claude Code: |
| 6 | + * claude mcp add --transport http medcore https://medcore-research-builder.vercel.app/api/mcp |
| 7 | + * |
| 8 | + * Tool logic lives in lib/mcp/server.ts; this file is only the JSON-RPC 2.0 |
| 9 | + * transport: initialize / ping / tools/list / tools/call, notifications get |
| 10 | + * 202, batches and SSE streams are not used (every call here is a single |
| 11 | + * request/response). |
| 12 | + */ |
| 13 | + |
| 14 | +import { NextResponse } from "next/server"; |
| 15 | +import { enforceRateLimit, safeJson } from "../_utils"; |
| 16 | +import { |
| 17 | + MCP_PROTOCOL_VERSION, |
| 18 | + MCP_TOOLS, |
| 19 | + SERVER_INFO, |
| 20 | + SERVER_INSTRUCTIONS, |
| 21 | + callMcpTool, |
| 22 | +} from "@/lib/mcp/server"; |
| 23 | + |
| 24 | +export const runtime = "nodejs"; |
| 25 | +export const maxDuration = 60; |
| 26 | + |
| 27 | +type JsonRpcRequest = { |
| 28 | + jsonrpc?: string; |
| 29 | + id?: string | number | null; |
| 30 | + method?: string; |
| 31 | + params?: Record<string, unknown>; |
| 32 | +}; |
| 33 | + |
| 34 | +function rpcResult(id: string | number | null, result: unknown) { |
| 35 | + return NextResponse.json({ jsonrpc: "2.0", id, result }); |
| 36 | +} |
| 37 | + |
| 38 | +function rpcError(id: string | number | null, code: number, message: string, status = 200) { |
| 39 | + return NextResponse.json({ jsonrpc: "2.0", id, error: { code, message } }, { status }); |
| 40 | +} |
| 41 | + |
| 42 | +export async function POST(req: Request) { |
| 43 | + let body: JsonRpcRequest; |
| 44 | + try { |
| 45 | + body = await safeJson<JsonRpcRequest>(req, "llm"); |
| 46 | + } catch { |
| 47 | + return rpcError(null, -32700, "Parse error: body must be a single JSON-RPC 2.0 message.", 400); |
| 48 | + } |
| 49 | + if (Array.isArray(body)) { |
| 50 | + return rpcError(null, -32600, "Batch requests are not supported.", 400); |
| 51 | + } |
| 52 | + const method = typeof body?.method === "string" ? body.method : ""; |
| 53 | + const hasId = body && "id" in body && body.id !== undefined && body.id !== null; |
| 54 | + const id = hasId ? (body.id as string | number) : null; |
| 55 | + const params = (body?.params || {}) as Record<string, unknown>; |
| 56 | + |
| 57 | + // Notifications (initialized, cancelled, …) need no response body. |
| 58 | + if (!hasId) { |
| 59 | + return new NextResponse(null, { status: 202 }); |
| 60 | + } |
| 61 | + |
| 62 | + switch (method) { |
| 63 | + case "initialize": { |
| 64 | + const requested = typeof params.protocolVersion === "string" ? params.protocolVersion : ""; |
| 65 | + return rpcResult(id, { |
| 66 | + protocolVersion: requested === "2024-11-05" ? requested : MCP_PROTOCOL_VERSION, |
| 67 | + capabilities: { tools: { listChanged: false } }, |
| 68 | + serverInfo: SERVER_INFO, |
| 69 | + instructions: SERVER_INSTRUCTIONS, |
| 70 | + }); |
| 71 | + } |
| 72 | + case "ping": |
| 73 | + return rpcResult(id, {}); |
| 74 | + case "tools/list": |
| 75 | + return rpcResult(id, { tools: MCP_TOOLS }); |
| 76 | + case "tools/call": { |
| 77 | + const name = typeof params.name === "string" ? params.name : ""; |
| 78 | + if (!name) return rpcError(id, -32602, "'name' is required."); |
| 79 | + // Reference verification fans out to several upstream APIs — hold it to |
| 80 | + // the stricter verify budget; everything else shares the search tier. |
| 81 | + const tier = name === "verify_references" ? "verify" : "search"; |
| 82 | + const limited = await enforceRateLimit(req, tier); |
| 83 | + if (limited) { |
| 84 | + return rpcError(id, -32000, "Rate limit exceeded — please wait and try again.", 429); |
| 85 | + } |
| 86 | + const args = (params.arguments || {}) as Record<string, unknown>; |
| 87 | + const result = await callMcpTool(name, args); |
| 88 | + return rpcResult(id, result); |
| 89 | + } |
| 90 | + case "resources/list": |
| 91 | + return rpcResult(id, { resources: [] }); |
| 92 | + case "prompts/list": |
| 93 | + return rpcResult(id, { prompts: [] }); |
| 94 | + default: |
| 95 | + return rpcError(id, -32601, `Method '${method}' not found.`); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Stateless server: no server-initiated SSE stream, no session to delete. |
| 100 | +export async function GET() { |
| 101 | + return NextResponse.json( |
| 102 | + { error: "This MCP endpoint is stateless — POST JSON-RPC messages instead." }, |
| 103 | + { status: 405, headers: { Allow: "POST" } }, |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +export async function DELETE() { |
| 108 | + return new NextResponse(null, { status: 405, headers: { Allow: "POST" } }); |
| 109 | +} |
0 commit comments