!-sampling !+elicitation
()
| 104 | // !+elicitation |
| 105 | |
| 106 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…