|
| 1 | +<!-- Autogenerated by weave; DO NOT EDIT --> |
| 2 | +# Support for MCP client features |
| 3 | + |
| 4 | +1. [Roots](#roots) |
| 5 | +1. [Sampling](#sampling) |
| 6 | +1. [Elicitation](#elicitation) |
| 7 | + |
| 8 | +## Roots |
| 9 | + |
| 10 | +MCP allows clients to specify a set of filesystem |
| 11 | +["roots"](https://modelcontextprotocol.io/specification/2025-06-18/client/roots). |
| 12 | +The SDK supports this as follows: |
| 13 | + |
| 14 | +**Client-side**: The SDK client always has the `roots.listChanged` capability. |
| 15 | +To add roots to a client, use the |
| 16 | +[`Client.AddRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#AddRoots) |
| 17 | +and |
| 18 | +[`Client.RemoveRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Client.RemoveRoots) |
| 19 | +methods. If any servers are already [connected](protocol.md#lifecycle) to the |
| 20 | +client, a call to `AddRoot` or `RemoveRoots` will result in a |
| 21 | +`notifications/roots/list_changed` notification to each connected server. |
| 22 | + |
| 23 | +**Server-side**: To query roots from the server, use the |
| 24 | +[`ServerSession.ListRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.ListRoots) |
| 25 | +method. To receive notifications about root changes, set |
| 26 | +[`ServerOptions.RootsListChangedHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.RootsListChangedHandler). |
| 27 | + |
| 28 | +```go |
| 29 | +func Example_roots() { |
| 30 | + ctx := context.Background() |
| 31 | + |
| 32 | + // Create a client with a single root. |
| 33 | + c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil) |
| 34 | + c.AddRoots(&mcp.Root{URI: "file://a"}) |
| 35 | + |
| 36 | + // Now create a server with a handler to receive notifications about roots. |
| 37 | + rootsChanged := make(chan struct{}) |
| 38 | + handleRootsChanged := func(ctx context.Context, req *mcp.RootsListChangedRequest) { |
| 39 | + rootList, err := req.Session.ListRoots(ctx, nil) |
| 40 | + if err != nil { |
| 41 | + log.Fatal(err) |
| 42 | + } |
| 43 | + var roots []string |
| 44 | + for _, root := range rootList.Roots { |
| 45 | + roots = append(roots, root.URI) |
| 46 | + } |
| 47 | + fmt.Println(roots) |
| 48 | + close(rootsChanged) |
| 49 | + } |
| 50 | + s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, &mcp.ServerOptions{ |
| 51 | + RootsListChangedHandler: handleRootsChanged, |
| 52 | + }) |
| 53 | + |
| 54 | + // Connect the server and client... |
| 55 | + t1, t2 := mcp.NewInMemoryTransports() |
| 56 | + if _, err := s.Connect(ctx, t1, nil); err != nil { |
| 57 | + log.Fatal(err) |
| 58 | + } |
| 59 | + if _, err := c.Connect(ctx, t2, nil); err != nil { |
| 60 | + log.Fatal(err) |
| 61 | + } |
| 62 | + |
| 63 | + // ...and add a root. The server is notified about the change. |
| 64 | + c.AddRoots(&mcp.Root{URI: "file://b"}) |
| 65 | + <-rootsChanged |
| 66 | + // Output: [file://a file://b] |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## Sampling |
| 71 | + |
| 72 | +[Sampling](https://modelcontextprotocol.io/specification/2025-06-18/client/sampling) |
| 73 | +is a way for server's to leverage the client's AI capabilities. It is |
| 74 | +implemented in the SDK as follows: |
| 75 | + |
| 76 | +**Client-side**: To add the `sampling` capability to a client, set |
| 77 | +[`ClientOptions.CreateMessageHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.CreateMessageHandler). |
| 78 | +This function is invoked whenever the server requests sampling. |
| 79 | + |
| 80 | +**Server-side**: To use sampling from the server, call |
| 81 | +[`ServerSession.CreateMessage`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.CreateMessage). |
| 82 | + |
| 83 | +```go |
| 84 | +func Example_sampling() { |
| 85 | + ctx := context.Background() |
| 86 | + |
| 87 | + // Create a client with a single root. |
| 88 | + c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, &mcp.ClientOptions{ |
| 89 | + CreateMessageHandler: func(_ context.Context, req *mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error) { |
| 90 | + return &mcp.CreateMessageResult{ |
| 91 | + Content: &mcp.TextContent{ |
| 92 | + Text: "would have created a message", |
| 93 | + }, |
| 94 | + }, nil |
| 95 | + }, |
| 96 | + }) |
| 97 | + |
| 98 | + // Connect the server and client... |
| 99 | + ct, st := mcp.NewInMemoryTransports() |
| 100 | + s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 101 | + session, err := s.Connect(ctx, st, nil) |
| 102 | + if err != nil { |
| 103 | + log.Fatal(err) |
| 104 | + } |
| 105 | + defer session.Close() |
| 106 | + |
| 107 | + if _, err := c.Connect(ctx, ct, nil); err != nil { |
| 108 | + log.Fatal(err) |
| 109 | + } |
| 110 | + |
| 111 | + msg, err := session.CreateMessage(ctx, &mcp.CreateMessageParams{}) |
| 112 | + if err != nil { |
| 113 | + log.Fatal(err) |
| 114 | + } |
| 115 | + fmt.Println(msg.Content.(*mcp.TextContent).Text) |
| 116 | + // Output: would have created a message |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +## Elicitation |
| 121 | + |
| 122 | +[Elicitation](https://modelcontextprotocol.io/specification/2025-06-18/client/elicitation) |
| 123 | +allows servers to request user inputs. It is implemented in the SDK as follows: |
| 124 | + |
| 125 | +**Client-side**: To add the `elicitation` capability to a client, set |
| 126 | +[`ClientOptions.ElicitationHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.ElicitationHandler). |
| 127 | +The elicitation handler must return a result that matches the requested schema; |
| 128 | +otherwise, elicitation returns an error. |
| 129 | + |
| 130 | +**Server-side**: To use eliciation from the server, call |
| 131 | +[`ServerSession.Elicit`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.Elicit). |
| 132 | + |
| 133 | +```go |
| 134 | +func Example_elicitation() { |
| 135 | + ctx := context.Background() |
| 136 | + ct, st := mcp.NewInMemoryTransports() |
| 137 | + |
| 138 | + s := mcp.NewServer(testImpl, nil) |
| 139 | + ss, err := s.Connect(ctx, st, nil) |
| 140 | + if err != nil { |
| 141 | + log.Fatal(err) |
| 142 | + } |
| 143 | + defer ss.Close() |
| 144 | + |
| 145 | + c := mcp.NewClient(testImpl, &mcp.ClientOptions{ |
| 146 | + ElicitationHandler: func(context.Context, *mcp.ElicitRequest) (*mcp.ElicitResult, error) { |
| 147 | + return &mcp.ElicitResult{Action: "accept", Content: map[string]any{"test": "value"}}, nil |
| 148 | + }, |
| 149 | + }) |
| 150 | + if _, err := c.Connect(ctx, ct, nil); err != nil { |
| 151 | + log.Fatal(err) |
| 152 | + } |
| 153 | + res, err := ss.Elicit(ctx, &mcp.ElicitParams{ |
| 154 | + Message: "This should fail", |
| 155 | + RequestedSchema: &jsonschema.Schema{ |
| 156 | + Type: "object", |
| 157 | + Properties: map[string]*jsonschema.Schema{ |
| 158 | + "test": {Type: "string"}, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }) |
| 162 | + if err != nil { |
| 163 | + log.Fatal(err) |
| 164 | + } |
| 165 | + fmt.Println(res.Content["test"]) |
| 166 | + // Output: value |
| 167 | +} |
| 168 | +``` |
0 commit comments