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

Function testSamplingHandler

conformance/everything-server/main.go:306–336  ·  view source on GitHub ↗
(ctx context.Context, req *mcp.CallToolRequest, input samplingInput)

Source from the content-addressed store, hash-verified

304}
305
306func testSamplingHandler(ctx context.Context, req *mcp.CallToolRequest, input samplingInput) (*mcp.CallToolResult, any, error) {
307 // Request LLM completion from the client
308 result, err := req.Session.CreateMessage(ctx, &mcp.CreateMessageParams{
309 Messages: []*mcp.SamplingMessage{
310 {
311 Role: "user",
312 Content: &mcp.TextContent{
313 Text: input.Prompt,
314 },
315 },
316 },
317 MaxTokens: 100,
318 })
319 if err != nil {
320 return nil, nil, fmt.Errorf("sampling failed: %w", err)
321 }
322
323 // Extract the text response from the result
324 var responseText string
325 if tc, ok := result.Content.(*mcp.TextContent); ok {
326 responseText = tc.Text
327 } else {
328 responseText = "(non-text response)"
329 }
330
331 return &mcp.CallToolResult{
332 Content: []mcp.Content{
333 &mcp.TextContent{Text: fmt.Sprintf("LLM response: %s", responseText)},
334 },
335 }, nil, nil
336}
337
338type elicitationInput struct {
339 Message string `json:"message" jsonschema:"The message to show the user"`

Callers

nothing calls this directly

Calls 1

CreateMessageMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…