From fa31c3f66e352dc604ac3c7ce10b431343b7d558 Mon Sep 17 00:00:00 2001 From: bendtherules Date: Mon, 27 Apr 2026 19:03:49 +0530 Subject: [PATCH] feat(langfuse): enhance trace output structure with encapsulated result data --- src/mcp-server-http.ts | 24 +++++++++++++++++++----- src/mcp-server-stdio.ts | 6 +++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/mcp-server-http.ts b/src/mcp-server-http.ts index 9ec9189..d9baa4a 100644 --- a/src/mcp-server-http.ts +++ b/src/mcp-server-http.ts @@ -38,8 +38,10 @@ import { createEmbeddings } from "./lib/embeddings-factory.js"; import { LogOperation, logger } from "./lib/logger.js"; import { trace } from "@opentelemetry/api"; import { + extractMcpToolInfo, LANGFUSE_OBSERVATION_INPUT_ATTR, LANGFUSE_OBSERVATION_OUTPUT_ATTR, + LANGFUSE_TRACE_INPUT_ATTR, LANGFUSE_TRACE_NAME_ATTR, LANGFUSE_TRACE_OUTPUT_ATTR, TRACE_NAME_HTTP, @@ -111,7 +113,7 @@ async function createMcpServer() { .getActiveSpan() ?.setAttribute( LANGFUSE_TRACE_OUTPUT_ATTR, - JSON.stringify(result), + JSON.stringify({ output: result }), ); trace .getActiveSpan() @@ -159,7 +161,7 @@ async function createMcpServer() { .getActiveSpan() ?.setAttribute( LANGFUSE_TRACE_OUTPUT_ATTR, - JSON.stringify(result), + JSON.stringify({ output: result }), ); trace .getActiveSpan() @@ -204,7 +206,7 @@ async function createMcpServer() { .getActiveSpan() ?.setAttribute( LANGFUSE_TRACE_OUTPUT_ATTR, - JSON.stringify(result), + JSON.stringify({ output: result }), ); trace .getActiveSpan() @@ -298,15 +300,27 @@ export async function main() { } } + // Extract MCP tool info for trace-level input + const mcpInfo = extractMcpToolInfo(parsedBody); + // Handle request within trace context (passing trace ID from header if available) const sessionMetadata = getSessionMetadata("http"); return await withSpan( "mcp_http_request", { [LANGFUSE_TRACE_NAME_ATTR]: TRACE_NAME_HTTP, + [LANGFUSE_TRACE_INPUT_ATTR]: JSON.stringify({ + http_method: c.req.method, + mcp_method: mcpInfo.method, + tool: mcpInfo.tool, + input: mcpInfo.input, + client_ip: clientIp, + }), [LANGFUSE_OBSERVATION_INPUT_ATTR]: JSON.stringify({ - method: c.req.method, - endpoint: "/mcp", + http_method: c.req.method, + mcp_method: mcpInfo.method, + tool: mcpInfo.tool, + input: mcpInfo.input, client_ip: clientIp, }), method: c.req.method, diff --git a/src/mcp-server-stdio.ts b/src/mcp-server-stdio.ts index 1f25649..7216eed 100644 --- a/src/mcp-server-stdio.ts +++ b/src/mcp-server-stdio.ts @@ -179,7 +179,7 @@ export async function main() { .getActiveSpan() ?.setAttribute( LANGFUSE_TRACE_OUTPUT_ATTR, - JSON.stringify(result), + JSON.stringify({ output: result }), ); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], @@ -237,7 +237,7 @@ export async function main() { .getActiveSpan() ?.setAttribute( LANGFUSE_TRACE_OUTPUT_ATTR, - JSON.stringify(result), + JSON.stringify({ output: result }), ); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], @@ -289,7 +289,7 @@ export async function main() { .getActiveSpan() ?.setAttribute( LANGFUSE_TRACE_OUTPUT_ATTR, - JSON.stringify(result), + JSON.stringify({ output: result }), ); const isError = result.error !== undefined; const text = isError ? result.error : JSON.stringify(result, null, 2);