Skip to content

fix: Support list tools pagination in tools change notification handler #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ void testListTools() {
});
}

@Test
void testListAllTools() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.listAllTools()))
.consumeNextWith(tools -> {
assertThat(tools).isNotNull().satisfies(result -> {
assertThat(result).isNotEmpty();

Tool firstTool = result.get(0);
assertThat(firstTool.name()).isNotNull();
assertThat(firstTool.description()).isNotNull();
});
})
.verifyComplete();
});
}

@Test
void testPingWithoutInitialization() {
verifyCallSucceedsWithImplicitInitialization(client -> client.ping(), "pinging the server");
Expand Down Expand Up @@ -293,6 +310,23 @@ void testListResources() {
});
}

@Test
void testListAllResources() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.listAllResources()))
.consumeNextWith(resources -> {
assertThat(resources).isNotNull().satisfies(result -> {
assertThat(result).isNotEmpty();

Resource firstResource = result.get(0);
assertThat(firstResource.uri()).isNotNull();
assertThat(firstResource.name()).isNotNull();
});
})
.verifyComplete();
});
}

@Test
void testMcpAsyncClientState() {
withClient(createMcpTransport(), mcpAsyncClient -> {
Expand Down Expand Up @@ -324,6 +358,23 @@ void testListPrompts() {
});
}

@Test
void testListAllPrompts() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.listAllPrompts()))
.consumeNextWith(prompts -> {
assertThat(prompts).isNotNull().satisfies(result -> {
assertThat(result).isNotEmpty();

Prompt firstPrompt = result.get(0);
assertThat(firstPrompt.name()).isNotNull();
assertThat(firstPrompt.description()).isNotNull();
});
})
.verifyComplete();
});
}

@Test
void testGetPromptWithoutInitialization() {
GetPromptRequest request = new GetPromptRequest("simple_prompt", Map.of());
Expand Down Expand Up @@ -439,6 +490,23 @@ void testListResourceTemplates() {
});
}

@Test
void testListAllResourceTemplates() {
withClient(createMcpTransport(), mcpAsyncClient -> {
StepVerifier.create(mcpAsyncClient.initialize().then(mcpAsyncClient.listAllResourceTemplates()))
.consumeNextWith(resourceTemplates -> {
assertThat(resourceTemplates).isNotNull().satisfies(result -> {
assertThat(result).isNotEmpty();

McpSchema.ResourceTemplate firstResourceTemplate = result.get(0);
assertThat(firstResourceTemplate.name()).isNotNull();
assertThat(firstResourceTemplate.description()).isNotNull();
});
})
.verifyComplete();
});
}

// @Test
void testResourceSubscription() {
withClient(createMcpTransport(), mcpAsyncClient -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ void testListTools() {
});
}

@Test
void testListAllTools() {
withClient(createMcpTransport(), mcpSyncClient -> {
mcpSyncClient.initialize();
List<Tool> tools = mcpSyncClient.listAllTools();

assertThat(tools).isNotNull().satisfies(result -> {
assertThat(result).isNotEmpty();

Tool firstTool = result.get(0);
assertThat(firstTool.name()).isNotNull();
assertThat(firstTool.description()).isNotNull();
});
});
}

@Test
void testCallToolsWithoutInitialization() {
verifyCallSucceedsWithImplicitInitialization(
Expand Down Expand Up @@ -320,6 +336,22 @@ void testListResources() {
});
}

@Test
void testListAllResources() {
withClient(createMcpTransport(), mcpSyncClient -> {
mcpSyncClient.initialize();
List<Resource> resources = mcpSyncClient.listAllResources();

assertThat(resources).isNotNull().satisfies(result -> {
assertThat(result).isNotEmpty();

Resource firstResource = result.get(0);
assertThat(firstResource.uri()).isNotNull();
assertThat(firstResource.name()).isNotNull();
});
});
}

@Test
void testClientSessionState() {
withClient(createMcpTransport(), mcpSyncClient -> {
Expand Down Expand Up @@ -418,6 +450,22 @@ void testListResourceTemplates() {
});
}

@Test
void testListAllResourceTemplates() {
withClient(createMcpTransport(), mcpSyncClient -> {
mcpSyncClient.initialize();
List<McpSchema.ResourceTemplate> resourceTemplates = mcpSyncClient.listAllResourceTemplates();

assertThat(resourceTemplates).isNotNull().satisfies(result -> {
assertThat(result).isNotEmpty();

McpSchema.ResourceTemplate firstResourceTemplate = result.get(0);
assertThat(firstResourceTemplate.name()).isNotNull();
assertThat(firstResourceTemplate.description()).isNotNull();
});
});
}

// @Test
void testResourceSubscription() {
withClient(createMcpTransport(), mcpSyncClient -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
import java.util.function.Function;
import java.util.function.Supplier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;

import io.modelcontextprotocol.spec.McpClientSession;
import io.modelcontextprotocol.spec.McpClientSession.NotificationHandler;
import io.modelcontextprotocol.spec.McpClientSession.RequestHandler;
Expand All @@ -35,8 +39,6 @@
import io.modelcontextprotocol.spec.McpTransportSessionNotFoundException;
import io.modelcontextprotocol.util.Assert;
import io.modelcontextprotocol.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
Expand Down Expand Up @@ -656,7 +658,7 @@ public Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToo
}

/**
* Retrieves the list of all tools provided by the server.
* Retrieves the first page of tools provided by the server.
* @return A Mono that emits the list of tools result.
*/
public Mono<McpSchema.ListToolsResult> listTools() {
Expand All @@ -679,12 +681,28 @@ public Mono<McpSchema.ListToolsResult> listTools(String cursor) {
});
}

/**
* Retrieves list of all tools provided by the server after handling pagination.
* @return A Mono that emits a list of all tools
*/
public Mono<List<McpSchema.Tool>> listAllTools() {
return this.listTools().expand(result -> {
if (result.nextCursor() != null) {
return this.listTools(result.nextCursor());
}
return Mono.empty();
}).reduce(new ArrayList<>(), (allTools, result) -> {
allTools.addAll(result.tools());
return allTools;
});
}

private NotificationHandler asyncToolsChangeNotificationHandler(
List<Function<List<McpSchema.Tool>, Mono<Void>>> toolsChangeConsumers) {
// TODO: params are not used yet
return params -> this.listTools()
.flatMap(listToolsResult -> Flux.fromIterable(toolsChangeConsumers)
.flatMap(consumer -> consumer.apply(listToolsResult.tools()))
return params -> this.listAllTools()
.flatMap(allTools -> Flux.fromIterable(toolsChangeConsumers)
.flatMap(consumer -> consumer.apply(allTools))
.onErrorResume(error -> {
logger.error("Error handling tools list change notification", error);
return Mono.empty();
Expand All @@ -706,9 +724,9 @@ private NotificationHandler asyncToolsChangeNotificationHandler(
};

/**
* Retrieves the list of all resources provided by the server. Resources represent any
* kind of UTF-8 encoded data that an MCP server makes available to clients, such as
* database records, API responses, log files, and more.
* Retrieves the first page of resources provided by the server. Resources represent
* any kind of UTF-8 encoded data that an MCP server makes available to clients, such
* as database records, API responses, log files, and more.
* @return A Mono that completes with the list of resources result.
* @see McpSchema.ListResourcesResult
* @see #readResource(McpSchema.Resource)
Expand Down Expand Up @@ -737,6 +755,26 @@ public Mono<McpSchema.ListResourcesResult> listResources(String cursor) {
});
}

/**
* Retrieves the list of all resources provided by the server. Resources represent any
* kind of UTF-8 encoded data that an MCP server makes available to clients, such as
* database records, API responses, log files, and more.
* @return A Mono that completes with list of all resources.
* @see McpSchema.Resource
* @see #readResource(McpSchema.Resource)
*/
public Mono<List<McpSchema.Resource>> listAllResources() {
return this.listResources().expand(result -> {
if (result.nextCursor() != null) {
return this.listResources(result.nextCursor());
}
return Mono.empty();
}).reduce(new ArrayList<>(), (allResources, result) -> {
allResources.addAll(result.resources());
return allResources;
});
}

/**
* Reads the content of a specific resource identified by the provided Resource
* object. This method fetches the actual data that the resource represents.
Expand Down Expand Up @@ -769,7 +807,7 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceReq
}

/**
* Retrieves the list of all resource templates provided by the server. Resource
* Retrieves the first page of resource templates provided by the server. Resource
* templates allow servers to expose parameterized resources using URI templates,
* enabling dynamic resource access based on variable parameters.
* @return A Mono that completes with the list of resource templates result.
Expand Down Expand Up @@ -798,6 +836,25 @@ public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(String
});
}

/**
* Retrieves the list of all resource templates provided by the server. Resource
* templates allow servers to expose parameterized resources using URI templates,
* enabling dynamic resource access based on variable parameters.
* @return A Mono that completes with the list of all resource templates.
* @see McpSchema.ResourceTemplate
*/
public Mono<List<McpSchema.ResourceTemplate>> listAllResourceTemplates() {
return this.listResourceTemplates().expand(result -> {
if (result.nextCursor() != null) {
return this.listResourceTemplates(result.nextCursor());
}
return Mono.empty();
}).reduce(new ArrayList<>(), (allResourceTemplates, result) -> {
allResourceTemplates.addAll(result.resourceTemplates());
return allResourceTemplates;
});
}

/**
* Subscribes to changes in a specific resource. When the resource changes on the
* server, the client will receive notifications through the resources change
Expand Down Expand Up @@ -847,7 +904,7 @@ private NotificationHandler asyncResourcesChangeNotificationHandler(
};

/**
* Retrieves the list of all prompts provided by the server.
* Retrieves the first page of prompts provided by the server.
* @return A Mono that completes with the list of prompts result.
* @see McpSchema.ListPromptsResult
* @see #getPrompt(GetPromptRequest)
Expand All @@ -868,6 +925,24 @@ public Mono<ListPromptsResult> listPrompts(String cursor) {
.sendRequest(McpSchema.METHOD_PROMPT_LIST, new PaginatedRequest(cursor), LIST_PROMPTS_RESULT_TYPE_REF));
}

/**
* Retrieves the list of all prompts provided by the server.
* @return A Mono that completes with the list of all prompts.
* @see McpSchema.Prompt
* @see #getPrompt(GetPromptRequest)
*/
public Mono<List<McpSchema.Prompt>> listAllPrompts() {
return this.listPrompts().expand(result -> {
if (result.nextCursor() != null) {
return this.listPrompts(result.nextCursor());
}
return Mono.empty();
}).reduce(new ArrayList<>(), (allPrompts, result) -> {
allPrompts.addAll(result.prompts());
return allPrompts;
});
}

/**
* Retrieves a specific prompt by its ID. This provides the complete prompt template
* including all parameters and instructions for generating AI content.
Expand Down
Loading