Skip to content

Commit 4d1336a

Browse files
committed
internal/mcp: add resource subscriptions
Handle resource subscriptions by delegating to the user. We don't need to use the word "Resource" in the handlers because the Subscribe and Unsubscribe methods in the spec don't use that name. If the spec adds another kind of subscription, they will pick a new name. The need for two handlers is dictated by backward compatibility. We must pass the RPC params to the subscription handler, which means we need different signatures for subscribe and unsubscribe. Change-Id: I045abaf52dc9c3e96181331728d50cf36cc8ed91 Reviewed-on: https://go-review.googlesource.com/c/tools/+/671895 Reviewed-by: Sam Thanawalla <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Bypass: Jonathan Amsterdam <[email protected]>
1 parent 6dfeba5 commit 4d1336a

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

internal/mcp/design/design.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -891,20 +891,46 @@ func (*Server) RemoveResourceTemplates(names ...string)
891891
Resource templates don't have unique identifiers, so removing a name will remove all
892892
resource templates with that name.
893893
894-
Clients call `ListResources` to list the available resources, `ReadResource` to read
895-
one of them, and `ListResourceTemplates` to list the templates:
896-
897-
```go
898-
func (*ClientSession) ListResources(context.Context, *ListResourcesParams) (*ListResourcesResult, error)
899-
func (*ClientSession) ListResourceTemplates(context.Context, *ListResourceTemplatesParams) (*ListResourceTemplatesResult, error)
900-
func (*ClientSession) ReadResource(context.Context, *ReadResourceParams) (*ReadResourceResult, error)
901-
```
894+
Servers support all of the resource-related spec methods:
895+
- `ListResources` and `ListResourceTemplates` for listings.
896+
- `ReadResource` to get the contents of a resource.
897+
- `Subscribe` and `Unsubscribe` to manage subscriptions on resources.
902898
903899
`ReadResource` checks the incoming URI against the server's list of
904900
resources and resource templates to make sure it matches one of them,
905901
then returns the result of calling the associated reader function.
906902
907-
<!-- TODO: subscriptions -->
903+
#### Subscriptions
904+
905+
ClientSessions can manage change notifications on particular resources:
906+
```go
907+
func (*ClientSession) Subscribe(context.Context, *SubscribeParams) error
908+
func (*ClientSession) Unsubscribe(context.Context, *UnsubscribeParams) error
909+
```
910+
911+
The server does not implement resource subscriptions. It passes along
912+
subscription requests to the user, and supplies a method to notify clients of
913+
changes. It tracks which sessions have subscribed to which resources so the
914+
user doesn't have to.
915+
916+
If a server author wants to support resource subscriptions, they must provide handlers
917+
to be called when clients subscribe and unsubscribe. It is an error to provide only
918+
one of these handlers.
919+
```go
920+
type ServerOptions struct {
921+
...
922+
// Function called when a client session subscribes to a resource.
923+
SubscribeHandler func(context.Context, *SubscribeParams) error
924+
// Function called when a client session unsubscribes from a resource.
925+
UnsubscribeHandler func(context.Context, *UnsubscribeParams) error
926+
}
927+
```
928+
929+
User code should call `ResourceUpdated` when a subscribed resource changes.
930+
```go
931+
func (*Server) ResourceUpdated(context.Context, *ResourceUpdatedNotification) error
932+
```
933+
The server routes these notifications to the server sessions that subscribed to the resource.
908934
909935
### ListChanged notifications
910936

0 commit comments

Comments
 (0)