|
| 1 | +// Files under `api/` become HTTP endpoints automatically — this is `/api/mcp`. |
| 2 | +import { VercelRequest, VercelResponse } from "@vercel/node"; |
| 3 | + |
| 4 | +export default async function handler(req: VercelRequest, res: VercelResponse) { |
| 5 | + res.setHeader("Access-Control-Allow-Origin", "*"); |
| 6 | + res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS"); |
| 7 | + res.setHeader( |
| 8 | + "Access-Control-Allow-Headers", |
| 9 | + "Content-Type, Accept, Mcp-Session-Id, Mcp-Protocol-Version" |
| 10 | + ); |
| 11 | + res.setHeader("Access-Control-Expose-Headers", "Mcp-Session-Id"); |
| 12 | + |
| 13 | + if (req.method === "OPTIONS") { |
| 14 | + return res.status(204).end(); |
| 15 | + } |
| 16 | + |
| 17 | + if (req.method !== "POST") { |
| 18 | + res.setHeader("Allow", "POST, OPTIONS"); |
| 19 | + return res.status(405).end("Method Not Allowed"); |
| 20 | + } |
| 21 | + |
| 22 | + // Get environment variables |
| 23 | + const { MCP_WORKSPACE_ID, SEARCH_API_TOKEN } = process.env; |
| 24 | + |
| 25 | + if (!MCP_WORKSPACE_ID || !SEARCH_API_TOKEN) { |
| 26 | + return res.status(500).json({ error: "MCP service is not configured." }); |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + // Forward the JSON-RPC body unchanged with the API key injected, so we |
| 31 | + // don't need to know any MCP methods — new upstream tools just work. |
| 32 | + const apiResponse = await fetch( |
| 33 | + `https://api.cloud.deepset.ai/api/v2/workspaces/${MCP_WORKSPACE_ID}/mcp`, |
| 34 | + { |
| 35 | + method: "POST", |
| 36 | + headers: { |
| 37 | + "Content-Type": "application/json", |
| 38 | + Accept: |
| 39 | + (req.headers.accept as string) || |
| 40 | + "application/json, text/event-stream", |
| 41 | + "X-Client-Source": "haystack-docs", |
| 42 | + Authorization: `Bearer ${SEARCH_API_TOKEN}`, |
| 43 | + // Forward MCP session id so upstream can correlate requests. |
| 44 | + ...(req.headers["mcp-session-id"] && { |
| 45 | + "Mcp-Session-Id": req.headers["mcp-session-id"] as string, |
| 46 | + }), |
| 47 | + // Forward the protocol version the client negotiated. |
| 48 | + ...(req.headers["mcp-protocol-version"] && { |
| 49 | + "Mcp-Protocol-Version": req.headers[ |
| 50 | + "mcp-protocol-version" |
| 51 | + ] as string, |
| 52 | + }), |
| 53 | + }, |
| 54 | + body: JSON.stringify(req.body), |
| 55 | + } |
| 56 | + ); |
| 57 | + |
| 58 | + // Pass the response through as-is (status, content-type, raw body). |
| 59 | + const text = await apiResponse.text(); |
| 60 | + res.status(apiResponse.status); |
| 61 | + const contentType = apiResponse.headers.get("content-type"); |
| 62 | + if (contentType) res.setHeader("Content-Type", contentType); |
| 63 | + // Surface the session id back to the client (browsers can read it because |
| 64 | + // it's in Access-Control-Expose-Headers above). |
| 65 | + const sessionId = apiResponse.headers.get("mcp-session-id"); |
| 66 | + if (sessionId) res.setHeader("Mcp-Session-Id", sessionId); |
| 67 | + return res.send(text); |
| 68 | + } catch (error) { |
| 69 | + console.error("MCP proxy error:", error); |
| 70 | + return res.status(502).json({ error: "Failed to reach MCP upstream." }); |
| 71 | + } |
| 72 | +} |
0 commit comments