Skip to content

Commit ca906a8

Browse files
committed
Add test for custom hooks
1 parent 532c383 commit ca906a8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

contrib/mark3labs/mcp-go/tracing_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,35 @@ func TestIntegrationToolCallError(t *testing.T) {
254254
assert.Contains(t, toolSpan.Meta, "input")
255255
}
256256

257+
func TestWithTracingWithCustomHooks(t *testing.T) {
258+
tt := testTracer(t)
259+
defer tt.Stop()
260+
261+
customHookCalled := false
262+
customHooks := &server.Hooks{}
263+
customHooks.AddBeforeInitialize(func(ctx context.Context, id any, request *mcp.InitializeRequest) {
264+
customHookCalled = true
265+
})
266+
267+
srv := server.NewMCPServer("test-server", "1.0.0",
268+
WithTracing(&TracingConfig{Hooks: customHooks}))
269+
270+
ctx := context.Background()
271+
initRequest := `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}`
272+
273+
response := srv.HandleMessage(ctx, []byte(initRequest))
274+
assert.NotNil(t, response)
275+
276+
assert.True(t, customHookCalled, "custom hook should have been called")
277+
278+
spans := tt.WaitForLLMObsSpans(t, 1)
279+
require.Len(t, spans, 1)
280+
281+
taskSpan := spans[0]
282+
assert.Equal(t, "mcp.initialize", taskSpan.Name)
283+
assert.Equal(t, "task", taskSpan.Meta["span.kind"])
284+
}
285+
257286
// Test helpers
258287

259288
// testTracer creates a testtracer with LLMObs enabled for integration tests

0 commit comments

Comments
 (0)