Skip to content

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

fix(mcp): Add user agent to mcp spans (#15078)

Ben Bachem1db2ab74f2bc
diff --git a/web/src/features/mcp/core/run-mcp-tool.ts b/web/src/features/mcp/core/run-mcp-tool.ts
index 06f5a3e88e..5c1d52cd30 100644
--- a/web/src/features/mcp/core/run-mcp-tool.ts
+++ b/web/src/features/mcp/core/run-mcp-tool.ts
@@ -26,6 +26,7 @@ export const runMcpTool = async <TResult>({
"langfuse.project.id": context.projectId,
"langfuse.org.id": context.orgId,
"mcp.api_key_id": context.apiKeyId,
+ ...(context.userAgent ? { user_agent: context.userAgent } : {}),
...Object.fromEntries(
Object.entries(attributes ?? {}).filter(
(entry): entry is [string, McpToolAttribute] =>
diff --git a/web/src/features/mcp/types.ts b/web/src/features/mcp/types.ts
index a67ff6a1e2..4a1de28e11 100644
--- a/web/src/features/mcp/types.ts
+++ b/web/src/features/mcp/types.ts
@@ -51,6 +51,9 @@ export interface ServerContext {
/** Public key used for authentication */
publicKey: string;
+ /** User agent from the MCP client's HTTP request */
+ userAgent?: string;
+
/** In-app-agent-specific MCP authorization state. */
inAppAgent?: InAppAgentContext;
}
diff --git a/web/src/pages/api/public/mcp/index.ts b/web/src/pages/api/public/mcp/index.ts
index 48ad36b465..32fad78dd3 100644
--- a/web/src/pages/api/public/mcp/index.ts
+++ b/web/src/pages/api/public/mcp/index.ts
@@ -129,6 +129,7 @@ export default async function handler(
apiKeyId: authCheck.scope.apiKeyId,
accessLevel: "project",
publicKey: authCheck.scope.publicKey,
+ userAgent: req.headers["user-agent"],
inAppAgent: getInAppAgentContext(req, authCheck.scope.isInAppAgentKey),
};