!-roots !+sampling
()
| 65 | // !+sampling |
| 66 | |
| 67 | func Example_sampling() { |
| 68 | ctx := context.Background() |
| 69 | |
| 70 | // Create a client with a sampling handler. |
| 71 | c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, &mcp.ClientOptions{ |
| 72 | CreateMessageHandler: func(_ context.Context, req *mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error) { |
| 73 | return &mcp.CreateMessageResult{ |
| 74 | Content: &mcp.TextContent{ |
| 75 | Text: "would have created a message", |
| 76 | }, |
| 77 | }, nil |
| 78 | }, |
| 79 | }) |
| 80 | |
| 81 | // Connect the server and client... |
| 82 | ct, st := mcp.NewInMemoryTransports() |
| 83 | s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 84 | session, err := s.Connect(ctx, st, nil) |
| 85 | if err != nil { |
| 86 | log.Fatal(err) |
| 87 | } |
| 88 | defer session.Close() |
| 89 | |
| 90 | if _, err := c.Connect(ctx, ct, nil); err != nil { |
| 91 | log.Fatal(err) |
| 92 | } |
| 93 | |
| 94 | msg, err := session.CreateMessage(ctx, &mcp.CreateMessageParams{}) |
| 95 | if err != nil { |
| 96 | log.Fatal(err) |
| 97 | } |
| 98 | fmt.Println(msg.Content.(*mcp.TextContent).Text) |
| 99 | // Output: would have created a message |
| 100 | } |
| 101 | |
| 102 | // !-sampling |
| 103 |
nothing calls this directly
no test coverage detected
searching dependent graphs…