Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/mcp/client_example_test.go

raw symbols SCIP
1 // Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package mcp_test
6
7 import (
8 "context"
9 "fmt"
10 "log"
11
12 "github.com/google/jsonschema-go/jsonschema"
13 "github.com/modelcontextprotocol/go-sdk/mcp"
14 )
15
16 // !+roots
17
18 func Example_roots() {
19 ctx := context.Background()
20
21 // Create a client with a single root.
22 c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
23 c.AddRoots(&mcp.Root{URI: "file://a"})
24
25 // Now create a server with a handler to receive notifications about roots.
26 rootsChanged := make(chan struct{})
27 handleRootsChanged := func(ctx context.Context, req *mcp.RootsListChangedRequest) {
28 rootList, err := req.Session.ListRoots(ctx, nil)
29 if err != nil {
30 log.Fatal(err)
31 }
32 var roots []string
33 for _, root := range rootList.Roots {
34 roots = append(roots, root.URI)
35 }
36 fmt.Println(roots)
37 close(rootsChanged)
38 }
39 s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, &mcp.ServerOptions{
40 RootsListChangedHandler: handleRootsChanged,
41 })
42
43 // Connect the server and client...
44 t1, t2 := mcp.NewInMemoryTransports()
45 serverSession, err := s.Connect(ctx, t1, nil)
46 if err != nil {
47 log.Fatal(err)
48 }
49 defer serverSession.Close()
50
51 clientSession, err := c.Connect(ctx, t2, nil)
52 if err != nil {
53 log.Fatal(err)
54 }
55 defer clientSession.Close()
56
57 // ...and add a root. The server is notified about the change.
58 c.AddRoots(&mcp.Root{URI: "file://b"})
59 <-rootsChanged
60 // Output: [file://a file://b]
61 }
62
63 // !-roots
64
65 // !+sampling
66
67 func Example_sampling() {
68 ctx := context.Background()
69
70 // Create a client with a sampling handler.
71 c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, &mcp.ClientOptions{
72 CreateMessageHandler: func(_ context.Context, req *mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error) {
73 return &mcp.CreateMessageResult{
74 Content: &mcp.TextContent{
75 Text: "would have created a message",
76 },
77 }, nil
78 },
79 })
80
81 // Connect the server and client...
82 ct, st := mcp.NewInMemoryTransports()
83 s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
84 session, err := s.Connect(ctx, st, nil)
85 if err != nil {
86 log.Fatal(err)
87 }
88 defer session.Close()
89
90 if _, err := c.Connect(ctx, ct, nil); err != nil {
91 log.Fatal(err)
92 }
93
94 msg, err := session.CreateMessage(ctx, &mcp.CreateMessageParams{})
95 if err != nil {
96 log.Fatal(err)
97 }
98 fmt.Println(msg.Content.(*mcp.TextContent).Text)
99 // Output: would have created a message
100 }
101
102 // !-sampling
103
104 // !+elicitation
105
106 func Example_elicitation() {
107 ctx := context.Background()
108 ct, st := mcp.NewInMemoryTransports()
109
110 s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
111 ss, err := s.Connect(ctx, st, nil)
112 if err != nil {
113 log.Fatal(err)
114 }
115 defer ss.Close()
116
117 c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, &mcp.ClientOptions{
118 ElicitationHandler: func(context.Context, *mcp.ElicitRequest) (*mcp.ElicitResult, error) {
119 return &mcp.ElicitResult{Action: "accept", Content: map[string]any{"test": "value"}}, nil
120 },
121 })
122 if _, err := c.Connect(ctx, ct, nil); err != nil {
123 log.Fatal(err)
124 }
125 res, err := ss.Elicit(ctx, &mcp.ElicitParams{
126 Message: "This should fail",
127 RequestedSchema: &jsonschema.Schema{
128 Type: "object",
129 Properties: map[string]*jsonschema.Schema{
130 "test": {Type: "string"},
131 },
132 },
133 })
134 if err != nil {
135 log.Fatal(err)
136 }
137 fmt.Println(res.Content["test"])
138 // Output: value
139 }
140
141 // !-elicitation