Skip to content

Commit 26304a7

Browse files
authored
Document resource subscription support in server and client guides (#843)
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
1 parent ed02736 commit 26304a7

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

docs/client.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,49 @@ Resources represent server-side data sources that clients can access using URI t
408408
.subscribe();
409409
```
410410

411+
### Resource Subscriptions
412+
413+
When the server advertises `resources.subscribe` support, clients can subscribe to individual resources and receive a callback whenever the server pushes a `notifications/resources/updated` notification for that URI. The SDK automatically re-reads the resource on notification and delivers the updated contents to the registered consumer.
414+
415+
Register a consumer on the client builder, then subscribe/unsubscribe at any time:
416+
417+
=== "Sync API"
418+
419+
```java
420+
McpSyncClient client = McpClient.sync(transport)
421+
.resourcesUpdateConsumer(contents -> {
422+
// called with the updated resource contents after each notification
423+
System.out.println("Resource updated: " + contents);
424+
})
425+
.build();
426+
427+
client.initialize();
428+
429+
// Subscribe to a specific resource URI
430+
client.subscribeResource(new McpSchema.SubscribeRequest("custom://resource"));
431+
432+
// ... later, stop receiving updates
433+
client.unsubscribeResource(new McpSchema.UnsubscribeRequest("custom://resource"));
434+
```
435+
436+
=== "Async API"
437+
438+
```java
439+
McpAsyncClient client = McpClient.async(transport)
440+
.resourcesUpdateConsumer(contents -> Mono.fromRunnable(() -> {
441+
System.out.println("Resource updated: " + contents);
442+
}))
443+
.build();
444+
445+
client.initialize()
446+
.then(client.subscribeResource(new McpSchema.SubscribeRequest("custom://resource")))
447+
.subscribe();
448+
449+
// ... later, stop receiving updates
450+
client.unsubscribeResource(new McpSchema.UnsubscribeRequest("custom://resource"))
451+
.subscribe();
452+
```
453+
411454
### Prompt System
412455

413456
The prompt system enables interaction with server-side prompt templates. These templates can be discovered and executed with custom parameters, allowing for dynamic text generation based on predefined patterns.

docs/server.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The server supports both synchronous and asynchronous APIs, allowing for flexibl
3333
McpSyncServer syncServer = McpServer.sync(transportProvider)
3434
.serverInfo("my-server", "1.0.0")
3535
.capabilities(ServerCapabilities.builder()
36-
.resources(false, true) // Enable resource support with list changes
36+
.resources(false, true) // Resource support: subscribe=false, listChanged=true
3737
.tools(true) // Enable tool support with list changes
3838
.prompts(true) // Enable prompt support with list changes
3939
.completions() // Enable completions support
@@ -57,7 +57,7 @@ The server supports both synchronous and asynchronous APIs, allowing for flexibl
5757
McpAsyncServer asyncServer = McpServer.async(transportProvider)
5858
.serverInfo("my-server", "1.0.0")
5959
.capabilities(ServerCapabilities.builder()
60-
.resources(false, true) // Enable resource support with list changes
60+
.resources(false, true) // Resource support: subscribe=false, listChanged=true
6161
.tools(true) // Enable tool support with list changes
6262
.prompts(true) // Enable prompt support with list changes
6363
.completions() // Enable completions support
@@ -319,7 +319,7 @@ The server can be configured with various capabilities:
319319

320320
```java
321321
var capabilities = ServerCapabilities.builder()
322-
.resources(false, true) // Resource support (subscribe, listChanged)
322+
.resources(true, true) // Resource support: subscribe=true, listChanged=true
323323
.tools(true) // Tool support with list changes notifications
324324
.prompts(true) // Prompt support with list changes notifications
325325
.completions() // Enable completions support
@@ -438,6 +438,42 @@ Resources provide context to AI models by exposing data such as: File contents,
438438
);
439439
```
440440

441+
### Resource Subscriptions
442+
443+
When the `subscribe` capability is enabled, clients can subscribe to specific resources and receive targeted `notifications/resources/updated` notifications when those resources change. Only sessions that have explicitly subscribed to a given URI receive the notification — not every connected client.
444+
445+
Enable subscription support in the server capabilities:
446+
447+
```java
448+
McpSyncServer server = McpServer.sync(transportProvider)
449+
.serverInfo("my-server", "1.0.0")
450+
.capabilities(ServerCapabilities.builder()
451+
.resources(true, false) // subscribe=true, listChanged=false
452+
.build())
453+
.resources(myResourceSpec)
454+
.build();
455+
```
456+
457+
When a subscribed resource changes, notify only the interested sessions:
458+
459+
=== "Sync"
460+
461+
```java
462+
server.notifyResourcesUpdated(
463+
new McpSchema.ResourcesUpdatedNotification("custom://resource")
464+
);
465+
```
466+
467+
=== "Async"
468+
469+
```java
470+
server.notifyResourcesUpdated(
471+
new McpSchema.ResourcesUpdatedNotification("custom://resource")
472+
).subscribe();
473+
```
474+
475+
If no sessions are subscribed to the given URI the call completes immediately without sending any messages. Subscription state is automatically cleaned up when a client session closes.
476+
441477
### Resource Template Specification
442478

443479
Resource templates allow servers to expose parameterized resources using URI templates:

0 commit comments

Comments
 (0)