Skip to content

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

fix(mcp): Instrument more auth level fields (#15111)

Ben Bachem5924903e48d8
diff --git a/web/src/features/mcp/core/run-mcp-tool.ts b/web/src/features/mcp/core/run-mcp-tool.ts
index 5c1d52cd30..f0403c19fb 100644
--- a/web/src/features/mcp/core/run-mcp-tool.ts
+++ b/web/src/features/mcp/core/run-mcp-tool.ts
@@ -1,5 +1,5 @@
import { BaseError } from "@langfuse/shared";
-import { instrumentAsync } from "@langfuse/shared/src/server";
+import { addUserToSpan, instrumentAsync } from "@langfuse/shared/src/server";
import { SpanKind, type Span } from "@opentelemetry/api";
import { UnstablePublicApiError } from "@/src/features/public-api/server/unstable-public-api-error-contract";
@@ -22,10 +22,16 @@ export const runMcpTool = async <TResult>({
instrumentAsync(
{ name: spanName, spanKind: SpanKind.INTERNAL },
async (span) => {
+ addUserToSpan(
+ {
+ projectId: context.projectId,
+ orgId: context.orgId,
+ apiKeyId: context.apiKeyId,
+ },
+ span,
+ );
+
span.setAttributes({
- "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(
diff --git a/web/src/pages/api/public/mcp/index.ts b/web/src/pages/api/public/mcp/index.ts
index 32fad78dd3..fbdf58e598 100644
--- a/web/src/pages/api/public/mcp/index.ts
+++ b/web/src/pages/api/public/mcp/index.ts
@@ -33,7 +33,7 @@ import {
} from "@/src/features/mcp/server/security";
import { formatErrorForUser } from "@/src/features/mcp/core/error-formatting";
import { type ServerContext } from "@/src/features/mcp/types";
-import { logger, redis } from "@langfuse/shared/src/server";
+import { addUserToSpan, logger, redis } from "@langfuse/shared/src/server";
import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
import { RateLimitService } from "@/src/features/public-api/server/RateLimitService";
import { prisma } from "@langfuse/shared/src/db";
@@ -101,6 +101,14 @@ export default async function handler(
);
}
+ addUserToSpan({
+ apiKeyId: authCheck.scope.apiKeyId,
+ publicKey: authCheck.scope.publicKey,
+ projectId: authCheck.scope.projectId,
+ orgId: authCheck.scope.orgId,
+ plan: authCheck.scope.plan,
+ });
+
// Check if ingestion is suspended due to usage limits
if (authCheck.scope.isIngestionSuspended) {
throw new ForbiddenError(