-
Notifications
You must be signed in to change notification settings - Fork 493
feat(contrib/mcp-go): Simplified way to add tracing #4122
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
731de01
Simplified way to add tracing
jboolean c3c19ed
Improve docs
jboolean 12241ac
Make AddServerHooks private
jboolean 6bc0b2f
Rename file
jboolean 8384e02
Make middleware factory a private constant
jboolean 84f8d83
Add test for custom hooks
jboolean 1408989
Add docs about using custom hooks
jboolean 1c7172d
s/WithTracing/WithMCPServerTracing
jboolean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2025 Datadog, Inc. | ||
|
|
||
| package mcpgo | ||
|
|
||
| import ( | ||
| "github.com/mark3labs/mcp-go/server" | ||
| ) | ||
|
|
||
| // The file contains methods for easily adding tracing to a MCP server. | ||
|
|
||
| // TracingConfig holds configuration for adding tracing to an MCP server. | ||
| type TracingConfig struct { | ||
| // Hooks allows you to provide custom hooks that will be merged with Datadog tracing hooks. | ||
| // If nil, only Datadog tracing hooks will be added and any custom hooks provided via server.WithHooks(...) will be removed. | ||
| // If provided, your custom hooks will be executed alongside Datadog tracing hooks. | ||
| Hooks *server.Hooks | ||
| } | ||
|
|
||
| // WithMCPServerTracing adds Datadog tracing to an MCP server. | ||
| // Pass this option to server.NewMCPServer to enable tracing. | ||
| // | ||
| // Do not use with `server.WithHooks(...)`, as this overwrites the hooks. | ||
| // Instead, pass custom hooks in the TracingConfig, which will be merged with tracing hooks. | ||
| // | ||
| // Usage: | ||
| // | ||
| // // Simple usage with only tracing hooks | ||
| // srv := server.NewMCPServer("my-server", "1.0.0", | ||
| // WithMCPServerTracing(nil)) | ||
| // | ||
| // // With custom hooks | ||
| // customHooks := &server.Hooks{} | ||
| // customHooks.AddBeforeInitialize(func(ctx context.Context, id any, request *mcp.InitializeRequest) { | ||
| // // Your custom logic here | ||
| // }) | ||
| // srv := server.NewMCPServer("my-server", "1.0.0", | ||
| // WithMCPServerTracing(&TracingConfig{Hooks: customHooks})) | ||
| func WithMCPServerTracing(options *TracingConfig) server.ServerOption { | ||
| return func(s *server.MCPServer) { | ||
| if options == nil { | ||
| options = new(TracingConfig) | ||
| } | ||
|
|
||
| hooks := options.Hooks | ||
|
|
||
| // Append hooks (hooks is a private field) | ||
| if hooks == nil { | ||
| hooks = &server.Hooks{} | ||
| } | ||
| appendTracingHooks(hooks) | ||
|
|
||
| server.WithHooks(hooks)(s) | ||
|
|
||
| server.WithToolHandlerMiddleware(toolHandlerMiddleware)(s) | ||
jboolean marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In the future (next PR in stack) a boolean option to enable intent capture will be added to this config.