-
Notifications
You must be signed in to change notification settings - Fork 488
feat(contrib/mcp-go): Trace MCP session initializations with MLObs spans #4101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: jb/contrib-mcp-go-base
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,11 @@ package mcpgo // import "github.com/DataDog/dd-trace-go/contrib/mark3labs/mcp-go | |
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "time" | ||
|
|
||
| "github.com/DataDog/dd-trace-go/v2/instrumentation" | ||
| "github.com/DataDog/dd-trace-go/v2/llmobs" | ||
| "github.com/jellydator/ttlcache/v3" | ||
|
|
||
| "github.com/mark3labs/mcp-go/mcp" | ||
| "github.com/mark3labs/mcp-go/server" | ||
|
|
@@ -22,6 +24,27 @@ func init() { | |
| instr = instrumentation.Load(instrumentation.PackageMark3LabsMcpGo) | ||
| } | ||
|
|
||
| type hooks struct { | ||
| spanCache *ttlcache.Cache[any, llmobs.Span] | ||
| } | ||
|
|
||
| type textIOAnnotator interface { | ||
| AnnotateTextIO(input, output string, opts ...llmobs.AnnotateOption) | ||
| } | ||
|
|
||
| // AddServerHooks appends Datadog tracing hooks to an existing server.Hooks object. | ||
| // Returns a cleanup function that should be called upon server shutdown. | ||
| func AddServerHooks(hooks *server.Hooks) func() { | ||
| ddHooks := newHooks() | ||
| hooks.AddBeforeInitialize(ddHooks.onBeforeInitialize) | ||
| hooks.AddAfterInitialize(ddHooks.onAfterInitialize) | ||
| hooks.AddOnError(ddHooks.onError) | ||
|
|
||
| return func() { | ||
| ddHooks.stop() | ||
| } | ||
| } | ||
|
|
||
| func NewToolHandlerMiddleware() server.ToolHandlerMiddleware { | ||
| return func(next server.ToolHandlerFunc) server.ToolHandlerFunc { | ||
| return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
|
|
@@ -48,3 +71,63 @@ func NewToolHandlerMiddleware() server.ToolHandlerMiddleware { | |
| } | ||
| } | ||
| } | ||
|
|
||
| func newHooks() *hooks { | ||
| spanCache := ttlcache.New[any, llmobs.Span]( | ||
| ttlcache.WithTTL[any, llmobs.Span](5 * time.Minute), | ||
| ) | ||
| spanCache.OnEviction(func(ctx context.Context, reason ttlcache.EvictionReason, item *ttlcache.Item[any, llmobs.Span]) { | ||
| if span := item.Value(); span != nil { | ||
| if reason == ttlcache.EvictionReasonExpired { | ||
| span.Finish() | ||
| } | ||
| } | ||
| }) | ||
| go spanCache.Start() | ||
|
|
||
| return &hooks{ | ||
| spanCache: spanCache, | ||
| } | ||
| } | ||
|
|
||
| func (h *hooks) onBeforeInitialize(ctx context.Context, id any, request *mcp.InitializeRequest) { | ||
| taskSpan, _ := llmobs.StartTaskSpan(ctx, "mcp.initialize", llmobs.WithIntegration("mark3labs/mcp-go")) | ||
| h.spanCache.Set(id, taskSpan, ttlcache.DefaultTTL) | ||
| } | ||
|
|
||
| func (h *hooks) onAfterInitialize(ctx context.Context, id any, request *mcp.InitializeRequest, result *mcp.InitializeResult) { | ||
| finishSpanWithIO(h, id, request, result) | ||
| } | ||
|
|
||
| func (h *hooks) onError(ctx context.Context, id any, method mcp.MCPMethod, message any, err error) { | ||
| if method == mcp.MethodInitialize { | ||
| if item := h.spanCache.Get(id); item != nil { | ||
| span := item.Value() | ||
| if annotator, ok := span.(textIOAnnotator); ok { | ||
| inputJSON, _ := json.Marshal(message) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this expected to happen often? I think at least we should log something (debug if this is expected to happen somewhat frequently, or warn if not). also, should we annotate the input with the error or some special message to let customers know something went wrong directly while viewing the span? |
||
| annotator.AnnotateTextIO(string(inputJSON), err.Error()) | ||
| span.Finish(llmobs.WithError(err)) | ||
| } | ||
| h.spanCache.Delete(id) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (h *hooks) stop() { | ||
| h.spanCache.Stop() | ||
| } | ||
|
|
||
| func finishSpanWithIO[Req any, Res any](h *hooks, id any, request Req, result Res) { | ||
| if item := h.spanCache.Get(id); item != nil { | ||
| span := item.Value() | ||
| if annotator, ok := span.(textIOAnnotator); ok { | ||
| inputJSON, _ := json.Marshal(request) | ||
| resultJSON, _ := json.Marshal(result) | ||
| outputText := string(resultJSON) | ||
|
|
||
| annotator.AnnotateTextIO(string(inputJSON), outputText) | ||
| span.Finish() | ||
| } | ||
| h.spanCache.Delete(id) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also finish spans that were evicted for other reasons?