(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func TestSamplingWithTools_ToolUse(t *testing.T) { |
| 18 | ctx := context.Background() |
| 19 | ct, st := NewInMemoryTransports() |
| 20 | |
| 21 | // Track what the client received |
| 22 | var gotParams *CreateMessageWithToolsParams |
| 23 | result := &CreateMessageWithToolsResult{ |
| 24 | Model: "test-model", |
| 25 | Role: "assistant", |
| 26 | Content: []Content{ |
| 27 | &ToolUseContent{ |
| 28 | ID: "tool_call_1", |
| 29 | Name: "calculator", |
| 30 | Input: map[string]any{"x": 1.0, "y": 2.0}, |
| 31 | }, |
| 32 | }, |
| 33 | StopReason: "toolUse", |
| 34 | } |
| 35 | |
| 36 | // Client with tools capability, using CreateMessageWithToolsHandler |
| 37 | client := NewClient(testImpl, &ClientOptions{ |
| 38 | CreateMessageWithToolsHandler: func(_ context.Context, req *CreateMessageWithToolsRequest) (*CreateMessageWithToolsResult, error) { |
| 39 | gotParams = req.Params |
| 40 | return result, nil |
| 41 | }, |
| 42 | Capabilities: &ClientCapabilities{ |
| 43 | Sampling: &SamplingCapabilities{Tools: &SamplingToolsCapabilities{}}, |
| 44 | }, |
| 45 | }) |
| 46 | |
| 47 | server := NewServer(testImpl, nil) |
| 48 | ss, err := server.Connect(ctx, st, nil) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | defer ss.Close() |
| 53 | |
| 54 | cs, err := client.Connect(ctx, ct, nil) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | defer cs.Close() |
| 59 | |
| 60 | // Server sends CreateMessageWithTools |
| 61 | params := &CreateMessageWithToolsParams{ |
| 62 | MaxTokens: 1000, |
| 63 | Messages: []*SamplingMessageV2{ |
| 64 | {Role: "user", Content: []Content{&TextContent{Text: "Calculate 1+2"}}}, |
| 65 | }, |
| 66 | Tools: []*Tool{ |
| 67 | { |
| 68 | Name: "calculator", |
| 69 | Description: "A calculator", |
| 70 | InputSchema: map[string]any{ |
| 71 | "type": "object", |
| 72 | "properties": map[string]any{ |
| 73 | "x": map[string]any{"type": "number"}, |
| 74 | "y": map[string]any{"type": "number"}, |
nothing calls this directly
no test coverage detected
searching dependent graphs…