Skip to content

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

fix(in-app-agent): derive the MCP endpoint from its own base URL (#16837)

Lysander Kieselb794a5cfa9fd
diff --git a/.env.prod.example b/.env.prod.example
index 5777527a4a..cb5481b3df 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -336,6 +336,13 @@ LANGFUSE_ENABLE_BLOB_STORAGE_FILE_LOG=true
# Without an egress connector, AWS gives the sandbox public internet access
# LANGFUSE_IN_APP_AGENT_SANDBOX_AWS_LAMBDA_MICROVM_EGRESS_NETWORK_CONNECTOR_ARN=
+# Base URL for the worker's in-app agent MCP calls, worker only. Defaults to
+# NEXTAUTH_URL. Set this when the worker cannot reach the public URL, rather
+# than redirecting NEXTAUTH_URL, which also builds links for users in emails
+# and Slack messages. Any host used here needs a LANGFUSE_MCP_ALLOWED_HOSTS
+# entry below.
+# LANGFUSE_MCP_BASE_URL=http://langfuse-web:3000
+
# Comma-separated additional hostnames/origins accepted by the MCP endpoint.
# Set to * to disable Host and Origin validation. This is not recommended.
# LANGFUSE_MCP_ALLOWED_HOSTS=internal-langfuse.example.com,https://api.example.com
diff --git a/docker-compose.yml b/docker-compose.yml
index 821a5668f4..c6de23d816 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -25,6 +25,10 @@ services:
ENCRYPTION_KEY: ${ENCRYPTION_KEY:-0000000000000000000000000000000000000000000000000000000000000000} # CHANGEME: generate via `openssl rand -hex 32`
TELEMETRY_ENABLED: ${TELEMETRY_ENABLED:-true}
LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES: ${LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES:-false}
+ # Lets the worker reach the web service internally for in-app agent MCP
+ # calls without redirecting NEXTAUTH_URL, which also builds links for
+ # users. Pair it with LANGFUSE_MCP_ALLOWED_HOSTS on langfuse-web.
+ LANGFUSE_MCP_BASE_URL: ${LANGFUSE_MCP_BASE_URL:-}
LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST: ${LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST:-}
LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS: ${LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS:-}
LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS: ${LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS:-}
@@ -118,6 +122,9 @@ services:
LANGFUSE_INIT_USER_NAME: ${LANGFUSE_INIT_USER_NAME:-}
LANGFUSE_INIT_USER_PASSWORD: ${LANGFUSE_INIT_USER_PASSWORD:-}
LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT: ${LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT:-}
+ # Hostnames allowed to reach the MCP endpoint, beyond NEXTAUTH_URL's own
+ # host. Required when the worker uses LANGFUSE_MCP_BASE_URL.
+ LANGFUSE_MCP_ALLOWED_HOSTS: ${LANGFUSE_MCP_ALLOWED_HOSTS:-}
# In-app agent settings.
LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_USER: ${LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_USER:-}
LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_ORG: ${LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_ORG:-}
diff --git a/worker/src/env.ts b/worker/src/env.ts
index efec00b92e..ff00284f6c 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -16,6 +16,14 @@ const EnvSchema = z.object({
.default(3030),
NEXTAUTH_URL: z.string().optional(),
+ // Base URL for the in-app agent's MCP calls. Defaults to NEXTAUTH_URL.
+ // Set this instead of redirecting NEXTAUTH_URL when the worker should reach
+ // web internally: NEXTAUTH_URL also builds user-facing links in emails and
+ // Slack messages, which must stay externally resolvable.
+ // Web validates the Host header of MCP requests, so any hostname used here
+ // other than NEXTAUTH_URL's own must be listed in LANGFUSE_MCP_ALLOWED_HOSTS
+ // on web, or the requests are rejected.
+ LANGFUSE_MCP_BASE_URL: z.url().optional(),
NEXT_PUBLIC_BASE_PATH: z.string().optional(),
NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: z
diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
index 774c1f0e96..a04a4a05a4 100644
--- a/worker/src/features/in-app-agent/executeInAppAgentRun.ts
+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
@@ -60,6 +60,7 @@ import {
import { createInAppAgentSandbox } from "./runtime/sandbox";
import { createAgUiStream } from "./runtime/agent";
import { getInAppAgentPromptClient } from "./runtime/promptClient";
+import { resolveLangfuseMcpUrl } from "./resolveLangfuseMcpUrl";
import type { AgUiRunAgentInput } from "./runtime/types";
import { env } from "../../env";
@@ -735,20 +736,18 @@ function findPersistedApprovalRequest(
}
function getLangfuseMcpUrl(): string {
- if (!env.NEXTAUTH_URL) {
+ const url = resolveLangfuseMcpUrl({
+ mcpBaseUrl: env.LANGFUSE_MCP_BASE_URL,
+ nextAuthUrl: env.NEXTAUTH_URL,
+ });
+
+ if (!url) {
throw new InAppAgentRunInitError(
- "NEXTAUTH_URL must be configured to derive the MCP endpoint",
+ "LANGFUSE_MCP_BASE_URL or NEXTAUTH_URL must be configured to derive the MCP endpoint",
);
}
- const rawUrl = env.NEXTAUTH_URL.replace(/\/api\/auth\/?$/, "");
- const baseUrl = new URL(rawUrl);
-
- baseUrl.pathname = `${baseUrl.pathname.replace(/\/$/, "")}/api/public/mcp`;
- baseUrl.search = "";
- baseUrl.hash = "";
-
- return baseUrl.toString();
+ return url;
}
function buildTracingConfig(params: {
diff --git a/worker/src/features/in-app-agent/resolveLangfuseMcpUrl.test.ts b/worker/src/features/in-app-agent/resolveLangfuseMcpUrl.test.ts
new file mode 100644
index 0000000000..653a6d87db
--- /dev/null
+++ b/worker/src/features/in-app-agent/resolveLangfuseMcpUrl.test.ts
@@ -0,0 +1,32 @@
+import { describe, expect, it } from "vitest";
+
+import { resolveLangfuseMcpUrl } from "./resolveLangfuseMcpUrl";
+
+describe("resolveLangfuseMcpUrl", () => {
+ it("prefers the MCP base URL so NEXTAUTH_URL can stay externally resolvable", () => {
+ expect(
+ resolveLangfuseMcpUrl({
+ mcpBaseUrl: "http://langfuse-web:3000",
+ nextAuthUrl: "https://langfuse.example.com",
+ }),
+ ).toBe("http://langfuse-web:3000/api/public/mcp");
+ });
+
+ it("falls back to NEXTAUTH_URL, dropping the auth suffix and keeping a base path", () => {
+ expect(
+ resolveLangfuseMcpUrl({ nextAuthUrl: "https://example.com/api/auth" }),
+ ).toBe("https://example.com/api/public/mcp");
+ expect(
+ resolveLangfuseMcpUrl({
+ nextAuthUrl: "https://example.com/langfuse/api/auth/",
+ }),
+ ).toBe("https://example.com/langfuse/api/public/mcp");
+ expect(
+ resolveLangfuseMcpUrl({ nextAuthUrl: "https://example.com/langfuse/" }),
+ ).toBe("https://example.com/langfuse/api/public/mcp");
+ });
+
+ it("returns null when neither is configured", () => {
+ expect(resolveLangfuseMcpUrl({})).toBeNull();
+ });
+});
diff --git a/worker/src/features/in-app-agent/resolveLangfuseMcpUrl.ts b/worker/src/features/in-app-agent/resolveLangfuseMcpUrl.ts
new file mode 100644
index 0000000000..e55638a8ee
--- /dev/null
+++ b/worker/src/features/in-app-agent/resolveLangfuseMcpUrl.ts
@@ -0,0 +1,34 @@
+/**
+ * Derives the Langfuse MCP endpoint the in-app agent calls from the configured
+ * base URLs, or returns null when neither is set.
+ *
+ * LANGFUSE_MCP_BASE_URL wins over NEXTAUTH_URL so a deployment can point the
+ * worker at an internal address without redirecting NEXTAUTH_URL. The worker
+ * also builds links for users out of NEXTAUTH_URL — batch export emails, Slack
+ * notifications, blob storage integration settings, usage threshold emails — so
+ * pointing that at a cluster-internal Service makes all of them unreachable.
+ *
+ * Web validates the Host header of MCP requests, so an internal hostname used
+ * here must also be listed in LANGFUSE_MCP_ALLOWED_HOSTS on web.
+ *
+ * Kept free of env and error-type coupling so the precedence and URL
+ * normalisation are directly testable.
+ */
+export function resolveLangfuseMcpUrl(params: {
+ mcpBaseUrl?: string;
+ nextAuthUrl?: string;
+}): string | null {
+ const configuredUrl = params.mcpBaseUrl ?? params.nextAuthUrl;
+
+ if (!configuredUrl) {
+ return null;
+ }
+
+ const baseUrl = new URL(configuredUrl.replace(/\/api\/auth\/?$/, ""));
+
+ baseUrl.pathname = `${baseUrl.pathname.replace(/\/$/, "")}/api/public/mcp`;
+ baseUrl.search = "";
+ baseUrl.hash = "";
+
+ return baseUrl.toString();
+}