Skip to content

Commit 5aefa05

Browse files
committed
Expose resourcesUpdateConsumer() in sync client
`resourcesUpdateConsumer()` was missing from the sync client. Add it.
1 parent fa9dac8 commit 5aefa05

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

mcp-core/src/main/java/io/modelcontextprotocol/client/McpClient.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,8 @@
44

55
package io.modelcontextprotocol.client;
66

7-
import java.time.Duration;
8-
import java.util.ArrayList;
9-
import java.util.HashMap;
10-
import java.util.List;
11-
import java.util.Map;
12-
import java.util.function.Consumer;
13-
import java.util.function.Function;
14-
import java.util.function.Supplier;
15-
16-
import io.modelcontextprotocol.json.schema.JsonSchemaValidator;
177
import io.modelcontextprotocol.common.McpTransportContext;
8+
import io.modelcontextprotocol.json.schema.JsonSchemaValidator;
189
import io.modelcontextprotocol.spec.McpClientTransport;
1910
import io.modelcontextprotocol.spec.McpSchema;
2011
import io.modelcontextprotocol.spec.McpSchema.ClientCapabilities;
@@ -28,6 +19,15 @@
2819
import io.modelcontextprotocol.util.Assert;
2920
import reactor.core.publisher.Mono;
3021

22+
import java.time.Duration;
23+
import java.util.ArrayList;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.function.Consumer;
28+
import java.util.function.Function;
29+
import java.util.function.Supplier;
30+
3131
/**
3232
* Factory class for creating Model Context Protocol (MCP) clients. MCP is a protocol that
3333
* enables AI models to interact with external tools and resources through a standardized
@@ -346,6 +346,22 @@ public SyncSpec resourcesChangeConsumer(Consumer<List<McpSchema.Resource>> resou
346346
return this;
347347
}
348348

349+
/**
350+
* Adds a consumer to be notified when a specific resource is updated. This allows
351+
* the client to react to changes in individual resources, such as updates to
352+
* their content or metadata.
353+
* @param resourcesUpdateConsumer A consumer function that processes the updated
354+
* resource and returns a Mono indicating the completion of the processing. Must
355+
* not be null.
356+
* @return This builder instance for method chaining.
357+
* @throws IllegalArgumentException If the resourcesUpdateConsumer is null.
358+
*/
359+
public SyncSpec resourcesUpdateConsumer(Consumer<List<McpSchema.ResourceContents>> resourcesUpdateConsumer) {
360+
Assert.notNull(resourcesUpdateConsumer, "Resources update consumer must not be null");
361+
this.resourcesUpdateConsumers.add(resourcesUpdateConsumer);
362+
return this;
363+
}
364+
349365
/**
350366
* Adds a consumer to be notified when the available prompts change. This allows
351367
* the client to react to changes in the server's prompt templates, such as new

mcp-core/src/test/java/io/modelcontextprotocol/client/AbstractMcpSyncClientTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,13 @@ void testNotificationHandlers() {
536536
AtomicBoolean toolsNotificationReceived = new AtomicBoolean(false);
537537
AtomicBoolean resourcesNotificationReceived = new AtomicBoolean(false);
538538
AtomicBoolean promptsNotificationReceived = new AtomicBoolean(false);
539+
AtomicBoolean resourcesUpdatedNotificationReceived = new AtomicBoolean(false);
539540

540541
withClient(createMcpTransport(),
541542
builder -> builder.toolsChangeConsumer(tools -> toolsNotificationReceived.set(true))
542543
.resourcesChangeConsumer(resources -> resourcesNotificationReceived.set(true))
543-
.promptsChangeConsumer(prompts -> promptsNotificationReceived.set(true)),
544+
.promptsChangeConsumer(prompts -> promptsNotificationReceived.set(true))
545+
.resourcesUpdateConsumer(resources -> resourcesUpdatedNotificationReceived.set(true)),
544546
client -> {
545547

546548
assertThatCode(() -> {

mcp-test/src/main/java/io/modelcontextprotocol/client/AbstractMcpSyncClientTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,13 @@ void testNotificationHandlers() {
535535
AtomicBoolean toolsNotificationReceived = new AtomicBoolean(false);
536536
AtomicBoolean resourcesNotificationReceived = new AtomicBoolean(false);
537537
AtomicBoolean promptsNotificationReceived = new AtomicBoolean(false);
538+
AtomicBoolean resourcesUpdatedNotificationReceived = new AtomicBoolean(false);
538539

539540
withClient(createMcpTransport(),
540541
builder -> builder.toolsChangeConsumer(tools -> toolsNotificationReceived.set(true))
541542
.resourcesChangeConsumer(resources -> resourcesNotificationReceived.set(true))
542-
.promptsChangeConsumer(prompts -> promptsNotificationReceived.set(true)),
543+
.promptsChangeConsumer(prompts -> promptsNotificationReceived.set(true))
544+
.resourcesUpdateConsumer(resources -> resourcesUpdatedNotificationReceived.set(true)),
543545
client -> {
544546

545547
assertThatCode(() -> {

0 commit comments

Comments
 (0)