Skip to content

Commit f0a5ee7

Browse files
committed
feat(contrib/mark3labs/mcp-go): set session ID on tool spans at start time
Pass llmobs.WithSessionID at StartToolSpan so LLMObs groups tool spans under their MCP session natively (in addition to the existing mcp_session_id tag). The session is read from the MCP client session attached to ctx. Inspired by changes in the internal dd-source copy of this contrib.
1 parent a850ceb commit f0a5ee7

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

contrib/mark3labs/mcp-go/tracing.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ func appendTracingHooks(hooks *server.Hooks) {
3333

3434
var toolHandlerMiddleware = func(next server.ToolHandlerFunc) server.ToolHandlerFunc {
3535
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
36-
toolSpan, ctx := llmobs.StartToolSpan(ctx, request.Params.Name, llmobs.WithIntegration(string(instrumentation.PackageMark3LabsMCPGo)))
36+
startOpts := []llmobs.StartSpanOption{
37+
llmobs.WithIntegration(string(instrumentation.PackageMark3LabsMCPGo)),
38+
}
39+
if session := server.ClientSessionFromContext(ctx); session != nil {
40+
startOpts = append(startOpts, llmobs.WithSessionID(session.SessionID()))
41+
}
42+
toolSpan, ctx := llmobs.StartToolSpan(ctx, request.Params.Name, startOpts...)
3743

3844
var result *mcp.CallToolResult
3945
var err error

contrib/mark3labs/mcp-go/tracing_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ func TestIntegrationToolCallSuccess(t *testing.T) {
178178
assert.Contains(t, initSpan.Tags, expectedTag)
179179
assert.Contains(t, toolSpan.Tags, expectedTag)
180180

181+
// Tool span carries the session ID natively so LLMObs groups it under the session.
182+
assert.Equal(t, sessionID, toolSpan.SessionID)
183+
181184
assert.Contains(t, toolSpan.Tags, "mcp_method:tools/call")
182185
assert.Contains(t, toolSpan.Tags, "mcp_tool_kind:server")
183186
assert.Contains(t, toolSpan.Tags, "mcp_tool:calculator")

0 commit comments

Comments
 (0)