MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestSamplingWithTools_ToolUse

Function TestSamplingWithTools_ToolUse

mcp/sampling_test.go:17–95  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func 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"},

Callers

nothing calls this directly

Calls 7

ConnectMethod · 0.95
ConnectMethod · 0.95
NewInMemoryTransportsFunction · 0.85
NewClientFunction · 0.85
NewServerFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…