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