mcp: add resource subscriptions (#138)
This CL adds the ability for clients to subscribe and receive updates for resources as described in https://modelcontextprotocol.io/specification/2025-06-18/server/resources#subscriptions Fixes: #23
This commit is contained in:
parent
ea6162ca1c
commit
bcbb31f072
1 changed files with 214 additions and 6 deletions
|
|
@ -748,13 +748,26 @@ Server sessions also support the spec methods `ListResources` and `ListResourceT
|
|||
|
||||
#### Subscriptions
|
||||
|
||||
ClientSessions can manage change notifications on particular resources:
|
||||
##### Client-Side Usage
|
||||
|
||||
Use the Subscribe and Unsubscribe methods on a ClientSession to start or stop receiving updates for a specific resource.
|
||||
|
||||
```go
|
||||
func (*ClientSession) Subscribe(context.Context, *SubscribeParams) error
|
||||
func (*ClientSession) Unsubscribe(context.Context, *UnsubscribeParams) error
|
||||
```
|
||||
|
||||
To process incoming update notifications, you must provide a ResourceUpdatedHandler in your ClientOptions. The SDK calls this function automatically whenever the server sends a notification for a resource you're subscribed to.
|
||||
|
||||
```go
|
||||
type ClientOptions struct {
|
||||
...
|
||||
ResourceUpdatedHandler func(context.Context, *ClientSession, *ResourceUpdatedNotificationParams)
|
||||
}
|
||||
```
|
||||
|
||||
##### Server-Side Implementation
|
||||
|
||||
The server does not implement resource subscriptions. It passes along subscription requests to the user, and supplies a method to notify clients of changes. It tracks which sessions have subscribed to which resources so the user doesn't have to.
|
||||
|
||||
If a server author wants to support resource subscriptions, they must provide handlers to be called when clients subscribe and unsubscribe. It is an error to provide only one of these handlers.
|
||||
|
|
@ -772,7 +785,7 @@ type ServerOptions struct {
|
|||
User code should call `ResourceUpdated` when a subscribed resource changes.
|
||||
|
||||
```go
|
||||
func (*Server) ResourceUpdated(context.Context, *ResourceUpdatedNotification) error
|
||||
func (*Server) ResourceUpdated(context.Context, *ResourceUpdatedNotificationParams) error
|
||||
```
|
||||
|
||||
The server routes these notifications to the server sessions that subscribed to the resource.
|
||||
|
|
|
|||
Loading…
Reference in a new issue