22// Use of this source code is governed by an MIT-style
33// license that can be found in the LICENSE file.
44
5- //lint:file-ignore SA1019 examples exercise deprecated SEP-2577 APIs (roots, sampling) for demonstration.
6-
75package mcp_test
86
97import (
@@ -20,26 +18,27 @@ import (
2018func Example_roots () {
2119 ctx := context .Background ()
2220
23- // Create a client with a single root .
21+ // Create a client with two roots .
2422 c := mcp .NewClient (& mcp.Implementation {Name : "client" , Version : "v0.0.1" }, nil )
25- c .AddRoots (& mcp.Root {URI : "file://a" })
26-
27- // Now create a server with a handler to receive notifications about roots.
28- rootsChanged := make (chan struct {})
29- handleRootsChanged := func (ctx context.Context , req * mcp.RootsListChangedRequest ) {
30- rootList , err := req .Session .ListRoots (ctx , nil )
31- if err != nil {
32- log .Fatal (err )
23+ c .AddRoots (& mcp.Root {URI : "file://a" }, & mcp.Root {URI : "file://b" })
24+
25+ // Create a server with a tool that requests roots via the multi round-trip
26+ // pattern (SEP-2322): server-to-client requests are no longer sent as
27+ // standalone JSON-RPC calls on protocol version >= 2026-07-28.
28+ s := mcp .NewServer (& mcp.Implementation {Name : "server" , Version : "v0.0.1" }, nil )
29+ mcp .AddTool (s , & mcp.Tool {Name : "roots" }, func (_ context.Context , req * mcp.CallToolRequest , _ struct {}) (* mcp.CallToolResult , any , error ) {
30+ if len (req .Params .InputResponses ) == 0 {
31+ return & mcp.CallToolResult {
32+ InputRequests : mcp.InputRequestMap {"roots" : & mcp.ListRootsParams {}},
33+ }, nil , nil
3334 }
35+ rootList := req .Params .InputResponses ["roots" ].(* mcp.ListRootsResult )
3436 var roots []string
3537 for _ , root := range rootList .Roots {
3638 roots = append (roots , root .URI )
3739 }
3840 fmt .Println (roots )
39- close (rootsChanged )
40- }
41- s := mcp .NewServer (& mcp.Implementation {Name : "server" , Version : "v0.0.1" }, & mcp.ServerOptions {
42- RootsListChangedHandler : handleRootsChanged ,
41+ return & mcp.CallToolResult {}, nil , nil
4342 })
4443
4544 // Connect the server and client...
@@ -56,9 +55,11 @@ func Example_roots() {
5655 }
5756 defer clientSession .Close ()
5857
59- // ...and add a root. The server is notified about the change.
60- c .AddRoots (& mcp.Root {URI : "file://b" })
61- <- rootsChanged
58+ // ...and call the tool. The client's multi round-trip driver fulfils the
59+ // embedded roots/list request and retries the call.
60+ if _ , err := clientSession .CallTool (ctx , & mcp.CallToolParams {Name : "roots" }); err != nil {
61+ log .Fatal (err )
62+ }
6263 // Output: [file://a file://b]
6364}
6465
@@ -82,22 +83,35 @@ func Example_sampling() {
8283
8384 // Connect the server and client...
8485 ct , st := mcp .NewInMemoryTransports ()
86+ // Create a server with a tool that requests sampling via the multi
87+ // round-trip pattern (SEP-2322): server-to-client requests are no longer
88+ // sent as standalone JSON-RPC calls on protocol version >= 2026-07-28.
8589 s := mcp .NewServer (& mcp.Implementation {Name : "server" , Version : "v0.0.1" }, nil )
90+ mcp .AddTool (s , & mcp.Tool {Name : "sample" }, func (_ context.Context , req * mcp.CallToolRequest , _ struct {}) (* mcp.CallToolResult , any , error ) {
91+ if len (req .Params .InputResponses ) == 0 {
92+ return & mcp.CallToolResult {
93+ InputRequests : mcp.InputRequestMap {"msg" : & mcp.CreateMessageParams {}},
94+ }, nil , nil
95+ }
96+ msg := req .Params .InputResponses ["msg" ].(* mcp.CreateMessageWithToolsResult )
97+ return & mcp.CallToolResult {Content : msg .Content }, nil , nil
98+ })
8699 session , err := s .Connect (ctx , st , nil )
87100 if err != nil {
88101 log .Fatal (err )
89102 }
90103 defer session .Close ()
91104
92- if _ , err := c .Connect (ctx , ct , nil ); err != nil {
105+ clientSession , err := c .Connect (ctx , ct , nil )
106+ if err != nil {
93107 log .Fatal (err )
94108 }
95109
96- msg , err := session . CreateMessage (ctx , & mcp.CreateMessageParams { })
110+ res , err := clientSession . CallTool (ctx , & mcp.CallToolParams { Name : "sample" })
97111 if err != nil {
98112 log .Fatal (err )
99113 }
100- fmt .Println (msg .Content .(* mcp.TextContent ).Text )
114+ fmt .Println (res .Content [ 0 ] .(* mcp.TextContent ).Text )
101115 // Output: would have created a message
102116}
103117
@@ -109,7 +123,28 @@ func Example_elicitation() {
109123 ctx := context .Background ()
110124 ct , st := mcp .NewInMemoryTransports ()
111125
126+ // Create a server with a tool that requests elicitation via the multi
127+ // round-trip pattern (SEP-2322): server-to-client requests are no longer
128+ // sent as standalone JSON-RPC calls on protocol version >= 2026-07-28.
112129 s := mcp .NewServer (& mcp.Implementation {Name : "server" , Version : "v0.0.1" }, nil )
130+ mcp .AddTool (s , & mcp.Tool {Name : "ask" }, func (_ context.Context , req * mcp.CallToolRequest , _ struct {}) (* mcp.CallToolResult , any , error ) {
131+ if len (req .Params .InputResponses ) == 0 {
132+ return & mcp.CallToolResult {
133+ InputRequests : mcp.InputRequestMap {"input" : & mcp.ElicitParams {
134+ Message : "This should fail" ,
135+ RequestedSchema : & jsonschema.Schema {
136+ Type : "object" ,
137+ Properties : map [string ]* jsonschema.Schema {
138+ "test" : {Type : "string" },
139+ },
140+ },
141+ }},
142+ }, nil , nil
143+ }
144+ res := req .Params .InputResponses ["input" ].(* mcp.ElicitResult )
145+ fmt .Println (res .Content ["test" ])
146+ return & mcp.CallToolResult {}, nil , nil
147+ })
113148 ss , err := s .Connect (ctx , st , nil )
114149 if err != nil {
115150 log .Fatal (err )
@@ -121,22 +156,13 @@ func Example_elicitation() {
121156 return & mcp.ElicitResult {Action : "accept" , Content : map [string ]any {"test" : "value" }}, nil
122157 },
123158 })
124- if _ , err := c .Connect (ctx , ct , nil ); err != nil {
159+ clientSession , err := c .Connect (ctx , ct , nil )
160+ if err != nil {
125161 log .Fatal (err )
126162 }
127- res , err := ss .Elicit (ctx , & mcp.ElicitParams {
128- Message : "This should fail" ,
129- RequestedSchema : & jsonschema.Schema {
130- Type : "object" ,
131- Properties : map [string ]* jsonschema.Schema {
132- "test" : {Type : "string" },
133- },
134- },
135- })
136- if err != nil {
163+ if _ , err := clientSession .CallTool (ctx , & mcp.CallToolParams {Name : "ask" }); err != nil {
137164 log .Fatal (err )
138165 }
139- fmt .Println (res .Content ["test" ])
140166 // Output: value
141167}
142168
0 commit comments