Skip to content

Commit 87d139d

Browse files
committed
fix: add correct elicit example
1 parent 903792c commit 87d139d

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

mcp/client.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,24 @@ func (c *Client) elicit(ctx context.Context, req *ElicitRequest) (*ElicitResult,
287287
return nil, jsonrpc2.NewError(CodeInvalidParams, err.Error())
288288
}
289289

290-
return c.opts.ElicitationHandler(ctx, req)
290+
res, err := c.opts.ElicitationHandler(ctx, req)
291+
if err != nil {
292+
return nil, err
293+
}
294+
295+
// Validate elicitation result content against requested schema
296+
if req.Params.RequestedSchema != nil && res.Content != nil {
297+
resolved, err := req.Params.RequestedSchema.Resolve(nil)
298+
if err != nil {
299+
return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("failed to resolve requested schema: %v", err))
300+
}
301+
302+
if err := resolved.Validate(res.Content); err != nil {
303+
return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("elicitation result content does not match requested schema: %v", err))
304+
}
305+
}
306+
307+
return res, nil
291308
}
292309

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

mcp/elicitation_example_test.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,31 @@ func Example_elicitation() {
2020
// Create server
2121
server := mcp.NewServer(&mcp.Implementation{Name: "config-server", Version: "v1.0.0"}, nil)
2222

23-
serverSession, err := server.Connect(ctx, serverTransport)
23+
serverSession, err := server.Connect(ctx, serverTransport, nil)
2424
if err != nil {
2525
log.Fatal(err)
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{
30-
ElicitationHandler: func(ctx context.Context, cs *mcp.ClientSession, params *mcp.ElicitParams) (*mcp.ElicitResult, error) {
31-
fmt.Printf("Server requests: %s\n", params.Message)
32-
31+
ElicitationHandler: func(ctx context.Context, request *mcp.ElicitRequest) (*mcp.ElicitResult, error) {
32+
fmt.Printf("Server requests: %s\n", request.Params.Message)
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
},
4445
})
4546

46-
_, err = client.Connect(ctx, clientTransport)
47+
_, err = client.Connect(ctx, clientTransport, nil)
4748
if err != nil {
4849
log.Fatal(err)
4950
}
@@ -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)