|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2025 Datadog, Inc. |
| 5 | + |
| 6 | +package mcpgo |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "encoding/json" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer" |
| 14 | + "github.com/mark3labs/mcp-go/mcp" |
| 15 | + "github.com/mark3labs/mcp-go/server" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func TestIntentCapture(t *testing.T) { |
| 21 | + mt := mocktracer.Start() |
| 22 | + defer mt.Stop() |
| 23 | + |
| 24 | + srv := server.NewMCPServer("test-server", "1.0.0", WithTracing(&TracingConfig{IntentCaptureEnabled: true})) |
| 25 | + |
| 26 | + var receivedArgs map[string]any |
| 27 | + calcTool := mcp.NewTool("calculator", |
| 28 | + mcp.WithDescription("A simple calculator"), |
| 29 | + mcp.WithString("operation", mcp.Required(), mcp.Description("The operation to perform")), |
| 30 | + mcp.WithNumber("x", mcp.Required(), mcp.Description("First number")), |
| 31 | + mcp.WithNumber("y", mcp.Required(), mcp.Description("Second number"))) |
| 32 | + |
| 33 | + srv.AddTool(calcTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 34 | + receivedArgs = request.Params.Arguments.(map[string]any) |
| 35 | + return mcp.NewToolResultText(`{"result":8}`), nil |
| 36 | + }) |
| 37 | + |
| 38 | + ctx := context.Background() |
| 39 | + |
| 40 | + listResp := srv.HandleMessage(ctx, []byte(`{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}`)) |
| 41 | + var listResult map[string]interface{} |
| 42 | + json.Unmarshal(json.RawMessage(mustMarshal(listResp)), &listResult) |
| 43 | + |
| 44 | + result := listResult["result"].(map[string]interface{}) |
| 45 | + tools := result["tools"].([]interface{}) |
| 46 | + require.Len(t, tools, 1) |
| 47 | + |
| 48 | + tool := tools[0].(map[string]interface{}) |
| 49 | + schema := tool["inputSchema"].(map[string]interface{}) |
| 50 | + props := schema["properties"].(map[string]interface{}) |
| 51 | + |
| 52 | + assert.Contains(t, props, "operation") |
| 53 | + assert.Contains(t, props, "x") |
| 54 | + assert.Contains(t, props, "y") |
| 55 | + assert.Contains(t, props, "ddtrace") |
| 56 | + |
| 57 | + // Ensure ddtrace is added to schema |
| 58 | + ddtraceSchema := props["ddtrace"].(map[string]interface{}) |
| 59 | + assert.Equal(t, "object", ddtraceSchema["type"]) |
| 60 | + ddtraceProps := ddtraceSchema["properties"].(map[string]interface{}) |
| 61 | + intentSchema := ddtraceProps["intent"].(map[string]interface{}) |
| 62 | + assert.Equal(t, "string", intentSchema["type"]) |
| 63 | + assert.Equal(t, intentPrompt, intentSchema["description"]) |
| 64 | + |
| 65 | + required := schema["required"].([]interface{}) |
| 66 | + assert.Contains(t, required, "operation") |
| 67 | + assert.Contains(t, required, "x") |
| 68 | + assert.Contains(t, required, "y") |
| 69 | + |
| 70 | + session := &mockSession{id: "test"} |
| 71 | + session.Initialize() |
| 72 | + ctx = srv.WithContext(ctx, session) |
| 73 | + |
| 74 | + srv.HandleMessage(ctx, []byte(`{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"calculator","arguments":{"operation":"add","x":5,"y":3,"ddtrace":{"intent":"test"}}}}`)) |
| 75 | + |
| 76 | + // Ensure ddtrace is removed in tool call |
| 77 | + require.NotNil(t, receivedArgs) |
| 78 | + assert.Equal(t, "add", receivedArgs["operation"]) |
| 79 | + assert.Equal(t, float64(5), receivedArgs["x"]) |
| 80 | + assert.Equal(t, float64(3), receivedArgs["y"]) |
| 81 | + assert.NotContains(t, receivedArgs, "ddtrace") |
| 82 | +} |
| 83 | + |
| 84 | +func mustMarshal(v interface{}) []byte { |
| 85 | + b, _ := json.Marshal(v) |
| 86 | + return b |
| 87 | +} |
0 commit comments