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

Function Example_elicitation

mcp/client_example_test.go:106–139  ·  view source on GitHub ↗

!-sampling !+elicitation

()

Source from the content-addressed store, hash-verified

104// !+elicitation
105
106func Example_elicitation() {
107 ctx := context.Background()
108 ct, st := mcp.NewInMemoryTransports()
109
110 s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
111 ss, err := s.Connect(ctx, st, nil)
112 if err != nil {
113 log.Fatal(err)
114 }
115 defer ss.Close()
116
117 c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, &mcp.ClientOptions{
118 ElicitationHandler: func(context.Context, *mcp.ElicitRequest) (*mcp.ElicitResult, error) {
119 return &mcp.ElicitResult{Action: "accept", Content: map[string]any{"test": "value"}}, nil
120 },
121 })
122 if _, err := c.Connect(ctx, ct, nil); err != nil {
123 log.Fatal(err)
124 }
125 res, err := ss.Elicit(ctx, &mcp.ElicitParams{
126 Message: "This should fail",
127 RequestedSchema: &jsonschema.Schema{
128 Type: "object",
129 Properties: map[string]*jsonschema.Schema{
130 "test": {Type: "string"},
131 },
132 },
133 })
134 if err != nil {
135 log.Fatal(err)
136 }
137 fmt.Println(res.Content["test"])
138 // Output: value
139}
140
141// !-elicitation

Callers

nothing calls this directly

Calls 7

ConnectMethod · 0.95
ConnectMethod · 0.95
NewInMemoryTransportsFunction · 0.92
NewServerFunction · 0.92
NewClientFunction · 0.92
ElicitMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…