Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Add protected resource metadata endpoint and permissive CORS for MCP Inspector compatibility

Saif92b794afae8c
diff --git a/backend/src/ee/routes/ai/mcp-endpoint-metadata-router.ts b/backend/src/ee/routes/ai/mcp-endpoint-metadata-router.ts
index aee3369ec4..89c941e920 100644
--- a/backend/src/ee/routes/ai/mcp-endpoint-metadata-router.ts
+++ b/backend/src/ee/routes/ai/mcp-endpoint-metadata-router.ts
@@ -102,3 +102,40 @@ export const registerMcpEndpointAuthServerMetadataRouter = async (server: Fastif
}
});
};
+
+// RFC 9728 compliant: Protected Resource metadata at path-appended URL
+// MCP Inspector uses this pattern: /.well-known/oauth-protected-resource + resource path
+// GET /.well-known/oauth-protected-resource/api/v1/ai/mcp/endpoints/:endpointId/connect
+export const registerRfc9728ProtectedResourceMetadataRouter = async (server: FastifyZodProvider) => {
+ const appCfg = getConfig();
+ const siteUrl = removeTrailingSlash(appCfg.SITE_URL || "");
+ if (!siteUrl) {
+ return;
+ }
+
+ const siteHost = new URL(siteUrl).host;
+ const scopeAccess = `https://${siteHost}/mcp:access`;
+
+ server.route({
+ method: "GET",
+ url: "/api/v1/ai/mcp/endpoints/:endpointId/connect",
+ config: {
+ rateLimit: readLimit
+ },
+ schema: {
+ params: z.object({
+ endpointId: z.string().trim().min(1)
+ })
+ },
+ handler: async (req) => {
+ const { resourceUrl, authServerIssuer } = getMcpUrls(siteUrl, req.params.endpointId);
+ // Return endpoint-specific auth server so MCP Inspector uses endpoint-specific OAuth endpoints
+ return {
+ resource: resourceUrl,
+ authorization_servers: [authServerIssuer],
+ scopes_supported: ["openid", scopeAccess],
+ bearer_methods_supported: ["header"]
+ };
+ }
+ });
+};
diff --git a/backend/src/ee/routes/v1/ai-mcp-endpoint-router.ts b/backend/src/ee/routes/v1/ai-mcp-endpoint-router.ts
index a225e0d3fb..bbd4876fe1 100644
--- a/backend/src/ee/routes/v1/ai-mcp-endpoint-router.ts
+++ b/backend/src/ee/routes/v1/ai-mcp-endpoint-router.ts
@@ -575,14 +575,16 @@ export const registerAiMcpEndpointRouter = async (server: FastifyZodProvider) =>
params: z.object({
endpointId: z.string().uuid().trim().min(1)
}),
- body: z.object({
- redirect_uris: z.array(z.string()),
- token_endpoint_auth_method: z.string(),
- grant_types: z.array(z.string()),
- response_types: z.array(z.string()),
- client_name: z.string(),
- client_uri: z.string().optional()
- }),
+ body: z
+ .object({
+ redirect_uris: z.array(z.string()),
+ token_endpoint_auth_method: z.string(),
+ grant_types: z.array(z.string()),
+ response_types: z.array(z.string()),
+ client_name: z.string(),
+ client_uri: z.string().optional()
+ })
+ .passthrough(),
response: {
200: z.object({
client_id: z.string(),
@@ -733,13 +735,15 @@ export const registerAiMcpEndpointRouter = async (server: FastifyZodProvider) =>
params: z.object({
endpointId: z.string().uuid().trim().min(1)
}),
- body: z.object({
- grant_type: z.literal("authorization_code"),
- code: z.string(),
- redirect_uri: z.string().url(),
- code_verifier: z.string(),
- client_id: z.string()
- }),
+ body: z
+ .object({
+ grant_type: z.literal("authorization_code"),
+ code: z.string(),
+ redirect_uri: z.string().url(),
+ code_verifier: z.string(),
+ client_id: z.string()
+ })
+ .passthrough(),
response: {
200: z.object({
access_token: z.string(),
diff --git a/backend/src/server/app.ts b/backend/src/server/app.ts
index 60b678f638..2201ac9815 100644
--- a/backend/src/server/app.ts
+++ b/backend/src/server/app.ts
@@ -11,7 +11,7 @@ import helmet from "@fastify/helmet";
import type { FastifyRateLimitOptions } from "@fastify/rate-limit";
import ratelimiter from "@fastify/rate-limit";
import { fastifyRequestContext } from "@fastify/request-context";
-import fastify from "fastify";
+import fastify, { FastifyInstance, FastifyRequest } from "fastify";
import { Cluster, Redis } from "ioredis";
import { Knex } from "knex";
@@ -103,19 +103,37 @@ export const main = async ({
await server.register(fastifyEtag);
- await server.register<FastifyCorsOptions>(cors, {
- credentials: true,
- ...(appCfg.CORS_ALLOWED_ORIGINS?.length
- ? {
- origin: [...appCfg.CORS_ALLOWED_ORIGINS, ...(appCfg.SITE_URL ? [appCfg.SITE_URL] : [])]
+ // Dynamic CORS: MCP OAuth routes need permissive CORS for browser-based flows (MCP Inspector)
+ await server.register(
+ cors,
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ (_instance: FastifyInstance) =>
+ (req: FastifyRequest, callback: (err: Error | null, options: FastifyCorsOptions) => void) => {
+ const isMcpOAuthRoute =
+ req.url.startsWith("/.well-known/oauth-protected-resource") ||
+ req.url.startsWith("/.well-known/oauth-authorization-server") ||
+ req.url.startsWith("/mcp-endpoints/") ||
+ (req.url.includes("/ai/mcp/endpoints/") && req.url.includes("/oauth/"));
+
+ if (isMcpOAuthRoute) {
+ callback(null, { origin: true, credentials: true });
+ return;
}
- : {
- origin: appCfg.SITE_URL || true
- }),
- ...(appCfg.CORS_ALLOWED_HEADERS?.length && {
- allowedHeaders: appCfg.CORS_ALLOWED_HEADERS
- })
- });
+
+ // Default CORS config for other routes
+ const defaultOrigin = appCfg.CORS_ALLOWED_ORIGINS?.length
+ ? [...appCfg.CORS_ALLOWED_ORIGINS, ...(appCfg.SITE_URL ? [appCfg.SITE_URL] : [])]
+ : appCfg.SITE_URL || true;
+
+ callback(null, {
+ credentials: true,
+ origin: defaultOrigin,
+ ...(appCfg.CORS_ALLOWED_HEADERS?.length && {
+ allowedHeaders: appCfg.CORS_ALLOWED_HEADERS
+ })
+ });
+ }
+ );
await server.register(addErrorsToResponseSchemas);
// pull ip based on various proxy headers
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 70b69e5f4c..17521a81ff 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -6,7 +6,8 @@ import { z } from "zod";
import {
registerMcpEndpointAuthServerMetadataRouter,
- registerMcpEndpointMetadataRouter
+ registerMcpEndpointMetadataRouter,
+ registerRfc9728ProtectedResourceMetadataRouter
} from "@app/ee/routes/ai/mcp-endpoint-metadata-router";
import { registerCertificateEstRouter } from "@app/ee/routes/est/certificate-est-router";
import { registerV1EERoutes } from "@app/ee/routes/v1";
@@ -2866,6 +2867,9 @@ export const registerRoutes = async (
await server.register(registerMcpEndpointAuthServerMetadataRouter, {
prefix: "/.well-known/oauth-authorization-server"
});
+ await server.register(registerRfc9728ProtectedResourceMetadataRouter, {
+ prefix: "/.well-known/oauth-protected-resource"
+ });
// register routes for v1
await server.register(