Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(mcp): Add `.well-known/mcp.json` route (#14720)

Ben Bachemf1eef1b87128
diff --git a/web/next.config.mjs b/web/next.config.mjs
index 46d062574c..2f93d92437 100644
--- a/web/next.config.mjs
+++ b/web/next.config.mjs
@@ -120,6 +120,15 @@ const nextConfig = {
},
output: "standalone",
+ async rewrites() {
+ return [
+ {
+ source: "/.well-known/mcp.json",
+ destination: "/api/well-known/mcp.json",
+ },
+ ];
+ },
+
async headers() {
return [
{
diff --git a/web/src/features/mcp/server/security.ts b/web/src/features/mcp/server/security.ts
index 4275a3c6ce..481f5a4dc6 100644
--- a/web/src/features/mcp/server/security.ts
+++ b/web/src/features/mcp/server/security.ts
@@ -1,9 +1,9 @@
import { env } from "@/src/env.mjs";
+import { getBaseUrl } from "@/src/utils/base-url";
import { ForbiddenError } from "@langfuse/shared";
import { type NextApiRequest, type NextApiResponse } from "next";
const LOCALHOST_HOSTNAMES = ["localhost", "127.0.0.1", "[::1]"] as const;
-const LOCALHOST_HOST_PATTERN = /^(localhost|127\.0\.0\.1|\[::1\])(?::|\/|$)/i;
function parseAllowedMcpHostEntry(
entry: string,
@@ -39,12 +39,7 @@ function parseAllowedMcpHostEntry(
}
function getAllowedMcpOriginsAndHostnames() {
- const rawBaseUrl = env.NEXTAUTH_URL;
- const baseUrl = new URL(
- /^https?:\/\//i.test(rawBaseUrl)
- ? rawBaseUrl
- : `${LOCALHOST_HOST_PATTERN.test(rawBaseUrl) ? "http" : "https"}://${rawBaseUrl}`,
- );
+ const baseUrl = getBaseUrl();
const allowedHostnames = new Set([baseUrl.hostname.toLowerCase()]);
const allowedOrigins = new Set([baseUrl.origin.toLowerCase()]);
diff --git a/web/src/pages/api/well-known/mcp.json.ts b/web/src/pages/api/well-known/mcp.json.ts
new file mode 100644
index 0000000000..970e6ee4d6
--- /dev/null
+++ b/web/src/pages/api/well-known/mcp.json.ts
@@ -0,0 +1,31 @@
+import { type NextApiRequest, type NextApiResponse } from "next";
+import { getProductBaseUrl } from "@/src/utils/base-url";
+
+export default function handler(req: NextApiRequest, res: NextApiResponse) {
+ if (req.method !== "GET") {
+ res.setHeader("Allow", "GET");
+ res.status(405).end("Method Not Allowed");
+ return;
+ }
+ const baseUrl = getProductBaseUrl();
+ const payload = {
+ $schema:
+ "https://static.modelcontextprotocol.io/schemas/2025-10-17/server.schema.json",
+ title: "Langfuse",
+ description: "Use Langfuse over MCP.",
+ websiteUrl: "https://langfuse.com",
+ repository: {
+ url: "https://github.com/langfuse/langfuse",
+ source: "github",
+ },
+ remotes: [
+ {
+ type: "streamable-http",
+ url: new URL("api/public/mcp", baseUrl).toString(),
+ },
+ ],
+ };
+
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
+ res.status(200).send(`${JSON.stringify(payload, null, 2)}\n`);
+}
diff --git a/web/src/utils/base-url.ts b/web/src/utils/base-url.ts
new file mode 100644
index 0000000000..f3556f864e
--- /dev/null
+++ b/web/src/utils/base-url.ts
@@ -0,0 +1,27 @@
+import { env } from "@/src/env.mjs";
+
+const LOCALHOST_HOST_PATTERN = /^(localhost|127\.0\.0\.1|\[::1\])(?::|\/|$)/i;
+
+export const getBaseUrl = () => {
+ const rawBaseUrl = env.NEXTAUTH_URL;
+
+ return new URL(
+ /^https?:\/\//i.test(rawBaseUrl)
+ ? rawBaseUrl
+ : `${LOCALHOST_HOST_PATTERN.test(rawBaseUrl) ? "http" : "https"}://${rawBaseUrl}`,
+ );
+};
+
+export const getProductBaseUrl = () => {
+ const baseUrl = getBaseUrl();
+
+ baseUrl.pathname = baseUrl.pathname.replace(/\/api\/auth\/?$/, "/");
+ baseUrl.search = "";
+ baseUrl.hash = "";
+
+ if (!baseUrl.pathname.endsWith("/")) {
+ baseUrl.pathname = `${baseUrl.pathname}/`;
+ }
+
+ return baseUrl;
+};
diff --git a/web/src/utils/product-url.ts b/web/src/utils/product-url.ts
index fe899f14fa..61ef8859fb 100644
--- a/web/src/utils/product-url.ts
+++ b/web/src/utils/product-url.ts
@@ -1,13 +1,11 @@
import type { FilterState } from "@langfuse/shared";
-import { env } from "@/src/env.mjs";
import { encodeFiltersGeneric } from "@/src/features/filters/lib/filter-query-encoding";
+import { getProductBaseUrl } from "@/src/utils/base-url";
import {
rangeToString,
type TABLE_AGGREGATION_OPTIONS,
} from "@/src/utils/date-range-utils";
-const LOCALHOST_HOST_PATTERN = /^(localhost|127\.0\.0\.1|\[::1\])(?::|\/|$)/i;
-
type ProductPathQuery = Record<string, string | string[] | null | undefined>;
type TracesPathTimeRange =
@@ -42,21 +40,6 @@ type TracesPathParams = {
timeRange?: TracesPathTimeRange;
};
-const getProductBaseUrl = () => {
- const rawBaseUrl = env.NEXTAUTH_URL;
- const baseUrl = new URL(
- /^https?:\/\//i.test(rawBaseUrl)
- ? rawBaseUrl
- : `${LOCALHOST_HOST_PATTERN.test(rawBaseUrl) ? "http" : "https"}://${rawBaseUrl}`,
- );
-
- baseUrl.pathname = baseUrl.pathname.replace(/\/api\/auth\/?$/, "/");
- baseUrl.search = "";
- baseUrl.hash = "";
-
- return baseUrl;
-};
-
export const appendProductPathQuery = (
path: string,
query: ProductPathQuery,