refactor(mcp): change HEAD endpoint to middleware for improved route handling

This commit is contained in:
2026-04-22 11:03:45 +05:30
parent a92546ace4
commit 3649718b77
+14 -12
View File
@@ -252,21 +252,23 @@ export async function main() {
// Health check endpoint // Health check endpoint
app.get("/health", (c) => c.json({ status: "ok" })); app.get("/health", (c) => c.json({ status: "ok" }));
// MCP HEAD endpoint - health check / availability probe // MCP HEAD handler - runs before the main handler
// Must be defined BEFORE app.all for proper route matching (specific routes first) app.use("/mcp", async (c, next) => {
app.on("HEAD", "/mcp", (_c) => { if (c.req.method === "HEAD") {
return new Response(null, { return new Response(null, {
status: 200, status: 200,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"mcp-protocol-version": "2025-03-26", "mcp-protocol-version": "2025-03-26",
}, },
}); });
}
await next();
}); });
// MCP endpoint - handles GET and POST // MCP endpoint - handles GET and POST (HEAD is handled by middleware above)
// Must be defined BEFORE inspector (which mounts at /) for proper route matching // Must be defined BEFORE inspector (which mounts at /) for proper route matching
app.all("/mcp", async (c) => { app.on(["GET", "POST"], "/mcp", async (c) => {
// Get parsed body from Hono (automatic JSON parsing) // Get parsed body from Hono (automatic JSON parsing)
let parsedBody: unknown; let parsedBody: unknown;
if (c.req.method === "POST") { if (c.req.method === "POST") {