From a2c57d4da099cef11f1d053f07df158ecf95780a Mon Sep 17 00:00:00 2001 From: bendtherules Date: Tue, 21 Apr 2026 09:06:17 +0530 Subject: [PATCH] refactor: move MCP endpoint above inspector for proper route priority - Define /mcp endpoint before mounting inspector at / - Ensures specific routes are matched before catch-all - Prevents inspector from intercepting MCP requests --- src/mcp-server-http.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/mcp-server-http.ts b/src/mcp-server-http.ts index 3674d3e..63729e1 100644 --- a/src/mcp-server-http.ts +++ b/src/mcp-server-http.ts @@ -252,15 +252,8 @@ export async function main() { // Health check endpoint app.get("/health", (c) => c.json({ status: "ok" })); - // MCP Inspector at root path - auto-connects to /mcp - // Use environment variable for public URL, fallback to localhost for dev - const mcpPublicUrl = process.env.MCP_PUBLIC_URL || `http://localhost:${PORT}`; - mountInspector(app, { - autoConnectUrl: `${mcpPublicUrl}/mcp`, - devMode: process.env.NODE_ENV !== "production", - }); - // MCP endpoint - handles both GET and POST + // Must be defined BEFORE inspector (which mounts at /) for proper route matching app.all("/mcp", async (c) => { // Get parsed body from Hono (automatic JSON parsing) let parsedBody: unknown; @@ -289,6 +282,14 @@ export async function main() { return response; }); + // MCP Inspector at root path - auto-connects to /mcp + // Mounted AFTER /mcp so specific routes take precedence + const mcpPublicUrl = process.env.MCP_PUBLIC_URL || `http://localhost:${PORT}`; + mountInspector(app, { + autoConnectUrl: `${mcpPublicUrl}/mcp`, + devMode: process.env.NODE_ENV !== "production", + }); + // Start the server console.log(`Ask262 MCP HTTP Server running on http://0.0.0.0:${PORT}`); console.log(`MCP endpoint: POST http://0.0.0.0:${PORT}/mcp`);