Skip to content

Commit 625180d

Browse files
committed
Add docs about using custom hooks
1 parent 5ff01a9 commit 625180d

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

contrib/mark3labs/mcp-go/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ func main() {
1818
defer tracer.Stop()
1919

2020
// Do not use with `server.WithHooks(...)`, as this overwrites the tracing hooks.
21-
// Pass custom hooks via TracingConfig.Hooks instead which in turn is passed to server.WithHooks(...).
21+
// To add custom hooks alongside tracing, pass them via TracingConfig.Hooks, e.g.:
22+
// mcpgotrace.WithTracing(&mcpgotrace.TracingConfig{Hooks: customHooks})
2223
srv := server.NewMCPServer("my-server", "1.0.0",
23-
mcpgotrace.WithTracing(&mcpgotrace.TracingConfig{}))
24+
mcpgotrace.WithTracing(nil))
2425
}
2526
```
2627

contrib/mark3labs/mcp-go/example_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,33 @@
66
package mcpgo_test
77

88
import (
9+
"context"
10+
911
mcpgotrace "github.com/DataDog/dd-trace-go/contrib/mark3labs/mcp-go/v2"
1012
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
13+
"github.com/mark3labs/mcp-go/mcp"
1114
"github.com/mark3labs/mcp-go/server"
1215
)
1316

1417
func Example() {
1518
tracer.Start()
1619
defer tracer.Stop()
1720

18-
// Create server hooks and add Datadog tracing
1921
srv := server.NewMCPServer("my-server", "1.0.0",
20-
mcpgotrace.WithTracing(&mcpgotrace.TracingConfig{}))
22+
mcpgotrace.WithTracing(nil))
23+
_ = srv
24+
}
25+
26+
func Example_withCustomHooks() {
27+
tracer.Start()
28+
defer tracer.Stop()
29+
30+
customHooks := &server.Hooks{}
31+
customHooks.AddBeforeInitialize(func(ctx context.Context, id any, request *mcp.InitializeRequest) {
32+
// Your custom logic here
33+
})
34+
35+
srv := server.NewMCPServer("my-server", "1.0.0",
36+
mcpgotrace.WithTracing(&mcpgotrace.TracingConfig{Hooks: customHooks}))
2137
_ = srv
2238
}

contrib/mark3labs/mcp-go/option.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@ import (
1111

1212
// The file contains methods for easily adding tracing to a MCP server.
1313

14+
// TracingConfig holds configuration for adding tracing to an MCP server.
1415
type TracingConfig struct {
16+
// Hooks allows you to provide custom hooks that will be merged with Datadog tracing hooks.
17+
// If nil, only Datadog tracing hooks will be added and any custom hooks provided via server.WithHooks(...) will be removed.
18+
// If provided, your custom hooks will be executed alongside Datadog tracing hooks.
1519
Hooks *server.Hooks
1620
}
1721

18-
// Pass to server.NewMCPServer to add tracing to the server.
22+
// WithTracing adds Datadog tracing to an MCP server.
23+
// Pass this option to server.NewMCPServer to enable tracing.
24+
//
1925
// Do not use with `server.WithHooks(...)`, as this overwrites the hooks.
20-
// Pass custom hooks in the TracingConfig instead, which in turn is passed to server.WithHooks(...).
26+
// Instead, pass custom hooks in the TracingConfig, which will be merged with tracing hooks.
27+
//
28+
// Usage:
29+
//
30+
// // Simple usage with only tracing hooks
31+
// srv := server.NewMCPServer("my-server", "1.0.0",
32+
// WithTracing(nil))
33+
//
34+
// // With custom hooks
35+
// customHooks := &server.Hooks{}
36+
// customHooks.AddBeforeInitialize(func(ctx context.Context, id any, request *mcp.InitializeRequest) {
37+
// // Your custom logic here
38+
// })
39+
// srv := server.NewMCPServer("my-server", "1.0.0",
40+
// WithTracing(&TracingConfig{Hooks: customHooks}))
2141
func WithTracing(options *TracingConfig) server.ServerOption {
2242
return func(s *server.MCPServer) {
2343
if options == nil {

0 commit comments

Comments
 (0)