Repository navigation
fix(server-core): require auth for MCP execution routes under legacy auth #1564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4a6aab4
f2e4711
12a2d8e
04962d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@voltagent/server-core": patch | ||
| --- | ||
|
|
||
| fix(server-core): require authentication for MCP execution routes under legacy `auth` | ||
|
|
||
| The legacy auth policy protected agent, tool, and workflow execution but left the MCP execution routes open, so an MCP client could call exposed tools, agents, and workflows without a token. `PROTECTED_ROUTES` now includes the MCP Streamable HTTP route (`/mcp/:serverId/mcp`), the SSE connection and message routes (`GET /mcp/:serverId/sse`, `POST /mcp/:serverId/messages`), MCP tool invocation, reading a single MCP prompt or resource, and the logging level route. MCP discovery is unchanged: `GET /mcp/servers`, `GET /mcp/servers/:serverId` and `GET /mcp/servers/:serverId/tools` stay public, and the prompt, resource and resource template lists stay public in the default mode (with `defaultPrivate: true` they require auth, as before). Apps that relied on unauthenticated MCP access can opt back in with `publicRoutes`. `requiresAuth()` also matches a path with a trailing slash and a `HEAD` request the same way as the route without the slash and its `GET`, so those spellings no longer reach protected handlers without a token. | ||
|
|
||
| Fixes #1493 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -122,6 +122,23 @@ export const PROTECTED_ROUTES = [ | |
| "POST /workflows/:id/executions/:executionId/replay", // Replay execution from historical step | ||
| "POST /workflows/:id/executions/:executionId/cancel", // Cancel execution | ||
|
|
||
| // ======================================== | ||
| // MCP EXECUTION (Tools, Agents, Workflows, Prompts, Resources) | ||
| // Not listed here: discovery. Server list/details and tool lists are in DEFAULT_LEGACY_PUBLIC_ROUTES; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Elysia's MCP discovery routes are not in Prompt for AI agents |
||
| // prompt, resource and resource template lists stay public unless defaultPrivate is true | ||
| // ======================================== | ||
| "/mcp/:serverId/mcp", // Streamable HTTP transport (GET/POST/DELETE) | ||
| "GET /mcp/:serverId/sse", // SSE transport connection | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
| "POST /mcp/:serverId/messages", // SSE transport messages | ||
| "POST /mcp/servers/:serverId/tools/:toolName", // Invoke MCP tool (Hono) | ||
| "POST /mcp/:serverId/tools/:toolName/invoke", // Invoke MCP tool (Elysia) | ||
| "POST /mcp/servers/:serverId/logging/level", // Set MCP logging level (Hono) | ||
| "POST /mcp/:serverId/logging/level", // Set MCP logging level (Elysia) | ||
| "GET /mcp/servers/:serverId/prompts/:promptName", // Get MCP prompt (Hono) | ||
| "GET /mcp/:serverId/prompts/:promptName", // Get MCP prompt (Elysia) | ||
| "GET /mcp/servers/:serverId/resources/contents", // Read MCP resource (Hono) | ||
| "GET /mcp/:serverId/resources/read", // Read MCP resource (Elysia) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // ======================================== | ||
| // MEMORY (User Data) | ||
| // ======================================== | ||
|
|
@@ -232,17 +249,28 @@ export function requiresAuth( | |
| publicRoutes?: string[], | ||
| defaultPrivate?: boolean, | ||
| ): boolean { | ||
| // HEAD is served by GET handlers, and a trailing slash can reach the same handler | ||
| // (Elysia's default non-strict routing), so match both against the canonical route | ||
| const upperMethod = method.toUpperCase(); | ||
| const normalizedMethod = upperMethod === "HEAD" ? "GET" : upperMethod; | ||
| const normalizedPath = path.replace(/\/+$/, "") || "/"; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| // Routes written with HEAD or a trailing slash keep matching the request as sent | ||
| const methodMatches = (routeMethod: string) => | ||
| routeMethod === upperMethod || routeMethod === normalizedMethod; | ||
| const routePathMatches = (routePath: string) => | ||
| pathMatches(path, routePath) || pathMatches(normalizedPath, routePath); | ||
|
|
||
| // Check if it's a default public route | ||
| for (const publicRoute of DEFAULT_LEGACY_PUBLIC_ROUTES) { | ||
| if (publicRoute.includes(" ")) { | ||
| // Route with method specified | ||
| const [routeMethod, routePath] = publicRoute.split(" "); | ||
| if (method.toUpperCase() === routeMethod && pathMatches(path, routePath)) { | ||
| if (methodMatches(routeMethod) && routePathMatches(routePath)) { | ||
| return false; // Public route, no auth required | ||
| } | ||
| } else { | ||
| // Route without method (any method) | ||
| if (pathMatches(path, publicRoute)) { | ||
| if (routePathMatches(publicRoute)) { | ||
| return false; // Public route, no auth required | ||
| } | ||
| } | ||
|
|
@@ -254,12 +282,12 @@ export function requiresAuth( | |
| if (publicRoute.includes(" ")) { | ||
| // Route with method specified | ||
| const [routeMethod, routePath] = publicRoute.split(" "); | ||
| if (method.toUpperCase() === routeMethod && pathMatches(path, routePath)) { | ||
| if (methodMatches(routeMethod) && routePathMatches(routePath)) { | ||
| return false; // Public route, no auth required | ||
| } | ||
| } else { | ||
| // Route without method (any method) | ||
| if (pathMatches(path, publicRoute)) { | ||
| if (routePathMatches(publicRoute)) { | ||
| return false; // Public route, no auth required | ||
| } | ||
| } | ||
|
|
@@ -271,12 +299,12 @@ export function requiresAuth( | |
| if (protectedRoute.includes(" ")) { | ||
| // Route with method specified | ||
| const [routeMethod, routePath] = protectedRoute.split(" "); | ||
| if (method.toUpperCase() === routeMethod && pathMatches(path, routePath)) { | ||
| if (methodMatches(routeMethod) && routePathMatches(routePath)) { | ||
| return true; // Protected route, auth required | ||
| } | ||
| } else { | ||
| // Route without method (any method) | ||
| if (pathMatches(path, protectedRoute)) { | ||
| if (routePathMatches(protectedRoute)) { | ||
| return true; // Protected route, auth required | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: This assertion pins a case where a
publicRoutesentry with a trailing slash does not make the slashless request public. Under non-strict routing that request reaches the same handler and gets a 401. Normalize the route pattern the same way as the request path, or drop this assertion and the test title's claim.Prompt for AI agents