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

Function main

examples/server/elicitation/main.go:16–81  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14)
15
16func main() {
17 ctx := context.Background()
18 clientTransport, serverTransport := mcp.NewInMemoryTransports()
19
20 // Create server
21 server := mcp.NewServer(&mcp.Implementation{Name: "config-server", Version: "v1.0.0"}, nil)
22
23 serverSession, err := server.Connect(ctx, serverTransport, nil)
24 if err != nil {
25 log.Fatal(err)
26 }
27
28 // Create client with elicitation handler
29 // Note: Never use elicitation for sensitive data like API keys or passwords
30 client := mcp.NewClient(&mcp.Implementation{Name: "config-client", Version: "v1.0.0"}, &mcp.ClientOptions{
31 ElicitationHandler: func(ctx context.Context, request *mcp.ElicitRequest) (*mcp.ElicitResult, error) {
32 fmt.Printf("Server requests: %s\n", request.Params.Message)
33
34 // In a real application, this would prompt the user for input
35 // Here we simulate user providing configuration data
36 return &mcp.ElicitResult{
37 Action: "accept",
38 Content: map[string]any{
39 "serverEndpoint": "https://api.example.com",
40 "maxRetries": float64(3),
41 "enableLogs": true,
42 },
43 }, nil
44 },
45 })
46
47 _, err = client.Connect(ctx, clientTransport, nil)
48 if err != nil {
49 log.Fatal(err)
50 }
51
52 // Server requests user configuration via elicitation
53 configSchema := &jsonschema.Schema{
54 Type: "object",
55 Properties: map[string]*jsonschema.Schema{
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"},
59 },
60 Required: []string{"serverEndpoint"},
61 }
62
63 result, err := serverSession.Elicit(ctx, &mcp.ElicitParams{
64 Message: "Please provide your configuration settings",
65 RequestedSchema: configSchema,
66 })
67 if err != nil {
68 log.Fatal(err)
69 }
70
71 if result.Action == "accept" {
72 fmt.Printf("Configuration received: Endpoint: %v, Max Retries: %.0f, Logs: %v\n",
73 result.Content["serverEndpoint"],

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
ptrFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…