@@ -109,7 +109,129 @@ func Example_prompts() {
109109
110110## Resources
111111
112- <!-- TODO -->
112+ In MCP terms, a _ resource_ is some data referenced by a URI.
113+ MCP servers can serve resources to clients.
114+ They can register resources individually, or register a _ resource template_
115+ that uses a URI pattern to describe a collection of resources.
116+
117+
118+ ** Client-side** :
119+ Call [ ` ClientSession.ReadResource ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ReadResource )
120+ to read a resource.
121+ The SDK ensures that a read succeeds only if the URI matches a registered resource exactly,
122+ or matches the URI pattern of a resource template.
123+
124+ To list a server's resources and resource templates, use the
125+ [ ` ClientSession.Resources ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Resources )
126+ and
127+ [ ` ClientSession.ResourceTemplates ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ResourceTemplates )
128+ iterators, or the lower-level ` ListXXX ` calls (see [ pagination] ( #pagination ) ).
129+ Set
130+ [ ` ClientOptions.ResourceListChangedHandler ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.ResourceListChangedHandler )
131+ to be notified of changes in the lists of resources or resource templates.
132+
133+ Clients can be notified when the contents of a resource changes by subscribing to the resource's URI.
134+ Call
135+ [ ` ClientSession.Subscribe ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Subscribe )
136+ to subscribe to a resource
137+ and
138+ [ ` ClientSession.Unsubscribe ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Unsubscribe )
139+ to unsubscribe.
140+ Set
141+ [ ` ClientOptions.ResourceUpdatedHandler ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.ResourceUpdatedHandler )
142+ to be notified of changes to subscribed resources.
143+
144+ ** Server-side** :
145+ Use
146+ [ ` Server.AddResource ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server.AddResource )
147+ or
148+ [ ` Server.AddResourceTemplate ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server.AddResourceTemplate )
149+ to add a resource or resource template to the server along with its handler.
150+ A
151+ [ ` ResourceHandler ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ResourceHandler )
152+ maps a URI to the contents of a resource, which can include text, binary data,
153+ or both.
154+ If ` AddResource ` or ` AddResourceTemplate ` is called before a server is connected, the server will have the
155+ ` resources ` capability.
156+ The server will have the ` resources ` capability if any resource or resource template is added before the
157+ server is connected to a client, or if
158+ [ ` ServerOptions.HasResources ` ] ( https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.HasResources )
159+ is explicitly set. When a prompt is added, any clients already connected to the
160+ server will be notified via a ` notifications/resources/list_changed `
161+ notification.
162+
163+
164+ ``` go
165+ func Example_resources () {
166+ ctx := context.Background ()
167+
168+ resources := map [string ]string {
169+ " file:///a" : " a" ,
170+ " file:///dir/x" : " x" ,
171+ " file:///dir/y" : " y" ,
172+ }
173+
174+ handler := func (_ context.Context , req *mcp.ReadResourceRequest ) (*mcp.ReadResourceResult , error ) {
175+ uri := req.Params .URI
176+ c , ok := resources[uri]
177+ if !ok {
178+ return nil , mcp.ResourceNotFoundError (uri)
179+ }
180+ return &mcp.ReadResourceResult {
181+ Contents: []*mcp.ResourceContents {{URI: uri, Text: c}},
182+ }, nil
183+ }
184+
185+ // Create a server with a single resource.
186+ s := mcp.NewServer (&mcp.Implementation {Name: " server" , Version: " v0.0.1" }, nil )
187+ s.AddResource (&mcp.Resource {URI: " file:///a" }, handler)
188+ s.AddResourceTemplate (&mcp.ResourceTemplate {URITemplate: " file:///dir/{f}" }, handler)
189+
190+ // Create a client.
191+ c := mcp.NewClient (&mcp.Implementation {Name: " client" , Version: " v0.0.1" }, nil )
192+
193+ // Connect the server and client.
194+ t1 , t2 := mcp.NewInMemoryTransports ()
195+ if _ , err := s.Connect (ctx, t1, nil ); err != nil {
196+ log.Fatal (err)
197+ }
198+ cs , err := c.Connect (ctx, t2, nil )
199+ if err != nil {
200+ log.Fatal (err)
201+ }
202+ defer cs.Close ()
203+
204+ // List resources and resource templates.
205+ for r , err := range cs.Resources (ctx, nil ) {
206+ if err != nil {
207+ log.Fatal (err)
208+ }
209+ fmt.Println (r.URI )
210+ }
211+ for r , err := range cs.ResourceTemplates (ctx, nil ) {
212+ if err != nil {
213+ log.Fatal (err)
214+ }
215+ fmt.Println (r.URITemplate )
216+ }
217+
218+ // Read resources.
219+ for _ , path := range []string {" a" , " dir/x" , " b" } {
220+ res , err := cs.ReadResource (ctx, &mcp.ReadResourceParams {URI: " file:///" + path})
221+ if err != nil {
222+ fmt.Println (err)
223+ } else {
224+ fmt.Println (res.Contents [0 ].Text )
225+ }
226+ }
227+ // Output:
228+ // file:///a
229+ // file:///dir/{f}
230+ // a
231+ // x
232+ // calling "resources/read": Resource not found
233+ }
234+ ```
113235
114236## Tools
115237
0 commit comments