fix(server-core): require auth for MCP execution routes under legacy auth

SH
5d ago
+250-7

PR Checklist

Please check if your PR fulfills the following requirements:

Bugs / Features

What is the current behavior?

With the legacy auth option, requiresAuth() protects agent, tool and workflow execution through PROTECTED_ROUTES, but none of the MCP execution routes are in that list. They fall through to defaultPrivate ?? false, so by default an MCP client can run every tool, agent and workflow an MCP server exposes, and read its prompts and resources, without a token:

  • /mcp/:serverId/mcp (Streamable HTTP, GET/POST/DELETE)
  • GET /mcp/:serverId/sse and POST /mcp/:serverId/messages (SSE transport)
  • POST /mcp/servers/:serverId/tools/:toolName (Hono) and POST /mcp/:serverId/tools/:toolName/invoke (Elysia)
  • GET /mcp/servers/:serverId/prompts/:promptName and GET /mcp/servers/:serverId/resources/contents (Hono), GET /mcp/:serverId/prompts/:promptName and GET /mcp/:serverId/resources/read (Elysia)
  • POST /mcp/servers/:serverId/logging/level (Hono) and POST /mcp/:serverId/logging/level (Elysia)

The issue lists the transport paths as /mcp/servers/:id/...; the routes actually registered are /mcp/:serverId/{mcp,sse,messages}. authNext is not affected, since it treats every non-public, non-console route as user-protected.

What is the new behavior?

These MCP routes are added to PROTECTED_ROUTES, so legacy auth answers 401 without a valid token, the same as POST /agents/:id/text.

Discovery is unchanged. GET /mcp/servers, GET /mcp/servers/:serverId and GET /mcp/servers/:serverId/tools stay public. The prompt, resource and resource template lists stay public in the default mode and require auth with defaultPrivate: true, as before; they are deliberately not added to DEFAULT_LEGACY_PUBLIC_ROUTES, so private-by-default apps don't get looser. An app that wants open MCP access can still list a route in publicRoutes.

Tests:

  • server-core/src/auth/defaults.spec.ts: transports, tool invocation, prompt and resource reads and logging level require auth; discovery and the lists stay public in the default mode; with defaultPrivate: true discovery stays public and the lists require auth; publicRoutes can still open a transport route.
  • server-hono/src/auth/middleware.spec.ts: 401 for these routes without a token, and a valid token gets through.
  • server-elysia/src/auth/middleware.spec.ts: 401 for the Elysia tool invocation, logging level, prompt and resource read routes without a token.

The legacy auth section of website/docs/api/authentication.md and a @voltagent/server-core patch changeset are included.

fixes #1493

Supersedes #1522 (same change, rebuilt as one commit on current main, with the review feedback there already applied).

Notes for reviewers

Checked against a real honoServer with legacy auth and an MCPServer exposing a tool, a prompt and a resource, calls without a token:

BeforeAfter
beforeafter

One known edge, unchanged by this PR: route matching is by segment, so for an MCP server whose id is literally servers, GET /mcp/servers/mcp and GET /mcp/servers/sse match the public GET /mcp/servers/:serverId route first. POST to those paths is still protected.

Validation (Node 24): pnpm build:all, pnpm test:all --concurrency 1, pnpm lint:ci (only the existing complexity warnings), pnpm sp lint and pnpm publint:all. Left out of test:all because they fail the same way on unchanged main here: @voltagent/resumable-streams, @voltagent/voltagent-memory and @voltagent/cloudflare-d1 (no test files, vitest exits with "No projects were found") and server-elysia/src/elysia-server-provider.spec.ts; the rest of server-elysia was run on its own with that file excluded.

The issue also mentions LocalSandbox isolation, prompt cache TTL 0 (#1531) and prompt logging; those are separate and not touched here.


Summary by cubic

Fixes an authentication gap in the legacy auth option where MCP execution routes ran without a token. Previously an MCP client could invoke exposed tools, agents, and workflows, and read prompts and resources, without authentication; now these routes are added to PROTECTED_ROUTES and return 401 without a valid token, matching other execution routes like POST /agents/:id/text.

  • Protects MCP transports (/mcp/:serverId/mcp, SSE, messages), tool invocation, single prompt/resource reads, and logging level routes for both Hono and Elysia servers.
  • Normalizes trailing-slash paths and HEAD requests to their canonical route so they no longer bypass the guard, while public routes written with HEAD or a trailing slash still match.
  • Keeps discovery public: GET /mcp/servers, GET /mcp/servers/:serverId, and GET /mcp/servers/:serverId/tools.
  • Leaves prompt, resource, and resource-template list routes public by default; with defaultPrivate: true they require auth, as before.
  • Apps that relied on unauthenticated MCP access can opt back in via publicRoutes.
  • authNext is unaffected.

Fixes #1493.

Summary by CodeRabbit

  • Bug Fixes
    • Legacy authentication now protects MCP transport connections, tool execution, logging controls, and prompt and resource reads. Requests without valid credentials receive an authentication error.
    • MCP discovery routes and prompt, resource, and resource-template lists remain public by default. With defaultPrivate: true, these lists require authentication; publicRoutes can keep selected routes accessible without it.
    • Trailing-slash paths and HEAD requests follow the authentication rules for their corresponding paths and GET requests.
  • Documentation
    • Updated the authentication guide to describe protection for MCP execution routes.