Skip to content

Commit 9e488ed

Browse files
committed
fix: add correct elicit example
1 parent 65109a7 commit 9e488ed

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

mcp/client.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,24 @@ func (c *Client) elicit(ctx context.Context, cs *ClientSession, params *ElicitPa
274274
return nil, jsonrpc2.NewError(CodeInvalidParams, err.Error())
275275
}
276276

277-
return c.opts.ElicitationHandler(ctx, cs, params)
277+
res, err := c.opts.ElicitationHandler(ctx, cs, params)
278+
if err != nil {
279+
return nil, err
280+
}
281+
282+
// Validate elicitation result content against requested schema
283+
if params.RequestedSchema != nil && res.Content != nil {
284+
resolved, err := params.RequestedSchema.Resolve(nil)
285+
if err != nil {
286+
return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("failed to resolve requested schema: %v", err))
287+
}
288+
289+
if err := resolved.Validate(res.Content); err != nil {
290+
return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("elicitation result content does not match requested schema: %v", err))
291+
}
292+
}
293+
294+
return res, nil
278295
}
279296

280297
// validateElicitSchema validates that the schema conforms to MCP elicitation schema requirements.

mcp/elicitation_example_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ func Example_elicitation() {
2626
}
2727

2828
// Create client with elicitation handler
29+
// Note: Never use elicitation for sensitive data like API keys or passwords
2930
client := mcp.NewClient(&mcp.Implementation{Name: "config-client", Version: "v1.0.0"}, &mcp.ClientOptions{
3031
ElicitationHandler: func(ctx context.Context, cs *mcp.ClientSession, params *mcp.ElicitParams) (*mcp.ElicitResult, error) {
3132
fmt.Printf("Server requests: %s\n", params.Message)
32-
33+
3334
// In a real application, this would prompt the user for input
3435
// Here we simulate user providing configuration data
3536
return &mcp.ElicitResult{
3637
Action: "accept",
3738
Content: map[string]any{
38-
"apiKey": "sk-test123",
39-
"maxRetries": float64(3),
40-
"enableLogs": true,
39+
"serverEndpoint": "https://api.example.com",
40+
"maxRetries": float64(3),
41+
"enableLogs": true,
4142
},
4243
}, nil
4344
},
@@ -52,11 +53,11 @@ func Example_elicitation() {
5253
configSchema := &jsonschema.Schema{
5354
Type: "object",
5455
Properties: map[string]*jsonschema.Schema{
55-
"apiKey": {Type: "string", Description: "API key for authentication"},
56-
"maxRetries": {Type: "number", Minimum: ptr(1.0), Maximum: ptr(10.0)},
57-
"enableLogs": {Type: "boolean", Description: "Enable debug logging"},
56+
"serverEndpoint": {Type: "string", Description: "Server endpoint URL"},
57+
"maxRetries": {Type: "number", Minimum: ptr(1.0), Maximum: ptr(10.0)},
58+
"enableLogs": {Type: "boolean", Description: "Enable debug logging"},
5859
},
59-
Required: []string{"apiKey"},
60+
Required: []string{"serverEndpoint"},
6061
}
6162

6263
result, err := serverSession.Elicit(ctx, &mcp.ElicitParams{
@@ -68,18 +69,18 @@ func Example_elicitation() {
6869
}
6970

7071
if result.Action == "accept" {
71-
fmt.Printf("Configuration received: API Key: %v, Max Retries: %.0f, Logs: %v\n",
72-
result.Content["apiKey"],
72+
fmt.Printf("Configuration received: Endpoint: %v, Max Retries: %.0f, Logs: %v\n",
73+
result.Content["serverEndpoint"],
7374
result.Content["maxRetries"],
7475
result.Content["enableLogs"])
7576
}
7677

7778
// Output:
7879
// Server requests: Please provide your configuration settings
79-
// Configuration received: API Key: sk-test123, Max Retries: 3, Logs: true
80+
// Configuration received: Endpoint: https://api.example.com, Max Retries: 3, Logs: true
8081
}
8182

8283
// ptr is a helper function to create pointers for schema constraints
8384
func ptr[T any](v T) *T {
8485
return &v
85-
}
86+
}

0 commit comments

Comments
 (0)