Skip to content

feat: draft search implementation #281

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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 @@ -559,6 +559,39 @@ void testToolCallSuccess(String clientType) {
mcpServer.close();
}

@ParameterizedTest(name = "{0} : {displayName} ")
@ValueSource(strings = { "httpclient", "webflux" })
void testSearchToolsSuccess(String clientType) {

var clientBuilder = clientBuilders.get(clientType);

var tool1Spec = new McpSchema.Tool("tool1", "tool1 description", emptyJsonSchema);
var searchResponse = McpSchema.SearchToolsResult.builder().tools(List.of(tool1Spec)).build();
McpServerFeatures.SyncToolSpecification tool1 = new McpServerFeatures.SyncToolSpecification(tool1Spec,
(exchange, request) -> CallToolResult.builder().textContent(List.of("CALL RESPONSE")).build());

var mcpServer = McpServer.sync(mcpServerTransportProvider)
.capabilities(ServerCapabilities.builder()
.tools(ServerCapabilities.ToolCapabilities.builder().search(true).build())
.build())
.tools(tool1)
.toolSearchHandler((exchange, request) -> searchResponse)
.build();

try (var mcpClient = clientBuilder.build()) {

InitializeResult initResult = mcpClient.initialize();
assertThat(initResult).isNotNull();

assertThat(mcpClient.searchTools(SearchToolsRequest.builder().query("test").build()).tools())
.contains(tool1.tool());

mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
}

mcpServer.close();
}

@ParameterizedTest(name = "{0} : {displayName} ")
@ValueSource(strings = { "httpclient", "webflux" })
void testToolListChangeHandlingSuccess(String clientType) {
Expand Down Expand Up @@ -631,6 +664,119 @@ void testToolListChangeHandlingSuccess(String clientType) {
mcpServer.close();
}

@ParameterizedTest(name = "{0} : {displayName} ")
@ValueSource(strings = { "httpclient", "webflux" })
void testSearchResourcesSuccess(String clientType) {

var clientBuilder = clientBuilders.get(clientType);

var tool1Spec = new McpSchema.Tool("tool1", "tool1 description", emptyJsonSchema);

var resource = new Resource("uri://test", "test", "test", "text/plain", null);
var searchResponse = McpSchema.SearchResourcesResult.builder().resources(List.of(resource)).build();

McpServerFeatures.SyncToolSpecification tool1 = new McpServerFeatures.SyncToolSpecification(tool1Spec,
(exchange, request) -> CallToolResult.builder().textContent(List.of("CALL RESPONSE")).build());

var mcpServer = McpServer.sync(mcpServerTransportProvider)
.capabilities(ServerCapabilities.builder()
.tools(ServerCapabilities.ToolCapabilities.builder().build())
.resources(ServerCapabilities.ResourceCapabilities.builder().search(true).build())
.build())
.tools(tool1)
.resourceSearchHandler((exchange, request) -> searchResponse)
.build();

try (var mcpClient = clientBuilder.build()) {

InitializeResult initResult = mcpClient.initialize();
assertThat(initResult).isNotNull();

assertThat(mcpClient.searchResources(SearchResourcesRequest.builder().query("test").build()).resources())
.contains(resource);

mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
}

mcpServer.close();
}

@ParameterizedTest(name = "{0} : {displayName} ")
@ValueSource(strings = { "httpclient", "webflux" })
void testSearchResourceTemplatesSuccess(String clientType) {

var clientBuilder = clientBuilders.get(clientType);

var tool1Spec = new McpSchema.Tool("tool1", "tool1 description", emptyJsonSchema);

var resourceTemplate = new ResourceTemplate("uri://test", "test", "test", "text/plain", null);
var searchResponse = McpSchema.SearchResourceTemplatesResult.builder()
.resourceTemplates(List.of(resourceTemplate))
.build();

McpServerFeatures.SyncToolSpecification tool1 = new McpServerFeatures.SyncToolSpecification(tool1Spec,
(exchange, request) -> CallToolResult.builder().textContent(List.of("CALL RESPONSE")).build());

var mcpServer = McpServer.sync(mcpServerTransportProvider)
.capabilities(ServerCapabilities.builder()
.tools(ServerCapabilities.ToolCapabilities.builder().build())
.resources(ServerCapabilities.ResourceCapabilities.builder().search(true).build())
.build())
.tools(tool1)
.resourceTemplateSearchHandler((exchange, request) -> searchResponse)
.build();

try (var mcpClient = clientBuilder.build()) {

InitializeResult initResult = mcpClient.initialize();
assertThat(initResult).isNotNull();

assertThat(mcpClient.searchResourceTemplates(SearchResourceTemplatesRequest.builder().query("test").build())
.resourceTemplates()).contains(resourceTemplate);

mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
}

mcpServer.close();
}

@ParameterizedTest(name = "{0} : {displayName} ")
@ValueSource(strings = { "httpclient", "webflux" })
void testSearchPromptsSuccess(String clientType) {

var clientBuilder = clientBuilders.get(clientType);

var tool1Spec = new McpSchema.Tool("tool1", "tool1 description", emptyJsonSchema);

var prompt = new Prompt("test", "test", List.of());
var searchResponse = McpSchema.SearchPromptsResult.builder().prompts(List.of(prompt)).build();

McpServerFeatures.SyncToolSpecification tool1 = new McpServerFeatures.SyncToolSpecification(tool1Spec,
(exchange, request) -> CallToolResult.builder().textContent(List.of("CALL RESPONSE")).build());

var mcpServer = McpServer.sync(mcpServerTransportProvider)
.capabilities(ServerCapabilities.builder()
.tools(ServerCapabilities.ToolCapabilities.builder().build())
.prompts(ServerCapabilities.PromptCapabilities.builder().search(true).build())
.build())
.tools(tool1)
.promptSearchHandler((exchange, request) -> searchResponse)
.build();

try (var mcpClient = clientBuilder.build()) {

InitializeResult initResult = mcpClient.initialize();
assertThat(initResult).isNotNull();

assertThat(mcpClient.searchPrompts(SearchPromptsRequest.builder().query("test").build()).prompts())
.contains(prompt);

mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
}

mcpServer.close();
}

@ParameterizedTest(name = "{0} : {displayName} ")
@ValueSource(strings = { "httpclient", "webflux" })
void testInitialize(String clientType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,145 @@ void testToolListChangeHandlingSuccess() {
mcpServer.close();
}

// ---------------------------------------
// Search Tests
// ---------------------------------------
@Test
void testSearchToolsSuccess() {

var tool1Spec = new McpSchema.Tool("tool1", "tool1 description", emptyJsonSchema);
var searchResponse = McpSchema.SearchToolsResult.builder().tools(List.of(tool1Spec)).build();
McpServerFeatures.SyncToolSpecification tool1 = new McpServerFeatures.SyncToolSpecification(tool1Spec,
(exchange, request) -> CallToolResult.builder().textContent(List.of("CALL RESPONSE")).build());

var mcpServer = McpServer.sync(mcpServerTransportProvider)
.capabilities(ServerCapabilities.builder()
.tools(ServerCapabilities.ToolCapabilities.builder().search(true).build())
.build())
.tools(tool1)
.toolSearchHandler((exchange, request) -> searchResponse)
.build();

try (var mcpClient = clientBuilder.build()) {

InitializeResult initResult = mcpClient.initialize();
assertThat(initResult).isNotNull();

assertThat(mcpClient.searchTools(McpSchema.SearchToolsRequest.builder().query("test").build()).tools())
.contains(tool1.tool());

mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
}

mcpServer.close();
}

@Test
void testSearchResourcesSuccess() {

var tool1Spec = new McpSchema.Tool("tool1", "tool1 description", emptyJsonSchema);

var resource = new McpSchema.Resource("uri://test", "test", "test", "text/plain", null);
var searchResponse = McpSchema.SearchResourcesResult.builder().resources(List.of(resource)).build();

McpServerFeatures.SyncToolSpecification tool1 = new McpServerFeatures.SyncToolSpecification(tool1Spec,
(exchange, request) -> CallToolResult.builder().textContent(List.of("CALL RESPONSE")).build());

var mcpServer = McpServer.sync(mcpServerTransportProvider)
.capabilities(ServerCapabilities.builder()
.tools(ServerCapabilities.ToolCapabilities.builder().build())
.resources(ServerCapabilities.ResourceCapabilities.builder().search(true).build())
.build())
.tools(tool1)
.resourceSearchHandler((exchange, request) -> searchResponse)
.build();

try (var mcpClient = clientBuilder.build()) {

InitializeResult initResult = mcpClient.initialize();
assertThat(initResult).isNotNull();

assertThat(mcpClient.searchResources(McpSchema.SearchResourcesRequest.builder().query("test").build())
.resources()).contains(resource);

mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
}

mcpServer.close();
}

@Test
void testSearchResourceTemplatesSuccess() {

var tool1Spec = new McpSchema.Tool("tool1", "tool1 description", emptyJsonSchema);

var resourceTemplate = new McpSchema.ResourceTemplate("uri://test", "test", "test", "text/plain", null);
var searchResponse = McpSchema.SearchResourceTemplatesResult.builder()
.resourceTemplates(List.of(resourceTemplate))
.build();

McpServerFeatures.SyncToolSpecification tool1 = new McpServerFeatures.SyncToolSpecification(tool1Spec,
(exchange, request) -> CallToolResult.builder().textContent(List.of("CALL RESPONSE")).build());

var mcpServer = McpServer.sync(mcpServerTransportProvider)
.capabilities(ServerCapabilities.builder()
.tools(ServerCapabilities.ToolCapabilities.builder().build())
.resources(ServerCapabilities.ResourceCapabilities.builder().search(true).build())
.build())
.tools(tool1)
.resourceTemplateSearchHandler((exchange, request) -> searchResponse)
.build();

try (var mcpClient = clientBuilder.build()) {

InitializeResult initResult = mcpClient.initialize();
assertThat(initResult).isNotNull();

assertThat(mcpClient
.searchResourceTemplates(McpSchema.SearchResourceTemplatesRequest.builder().query("test").build())
.resourceTemplates()).contains(resourceTemplate);

mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
}

mcpServer.close();
}

@Test
void testSearchPromptsSuccess() {

var tool1Spec = new McpSchema.Tool("tool1", "tool1 description", emptyJsonSchema);

var prompt = new McpSchema.Prompt("test", "test", List.of());
var searchResponse = McpSchema.SearchPromptsResult.builder().prompts(List.of(prompt)).build();

McpServerFeatures.SyncToolSpecification tool1 = new McpServerFeatures.SyncToolSpecification(tool1Spec,
(exchange, request) -> CallToolResult.builder().textContent(List.of("CALL RESPONSE")).build());

var mcpServer = McpServer.sync(mcpServerTransportProvider)
.capabilities(ServerCapabilities.builder()
.tools(ServerCapabilities.ToolCapabilities.builder().build())
.prompts(ServerCapabilities.PromptCapabilities.builder().search(true).build())
.build())
.tools(tool1)
.promptSearchHandler((exchange, request) -> searchResponse)
.build();

try (var mcpClient = clientBuilder.build()) {

InitializeResult initResult = mcpClient.initialize();
assertThat(initResult).isNotNull();

assertThat(
mcpClient.searchPrompts(McpSchema.SearchPromptsRequest.builder().query("test").build()).prompts())
.contains(prompt);

mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
}

mcpServer.close();
}

@Test
void testInitialize() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,4 +838,63 @@ public Mono<McpSchema.CompleteResult> completeCompletion(McpSchema.CompleteReque
.sendRequest(McpSchema.METHOD_COMPLETION_COMPLETE, completeRequest, COMPLETION_COMPLETE_RESULT_TYPE_REF));
}

// --------------------------
// Search
// --------------------------

private static final TypeReference<McpSchema.SearchToolsResult> SEARCH_TOOLS_RESULT_TYPE_REF = new TypeReference<>() {
};

private static final TypeReference<McpSchema.SearchResourcesResult> SEARCH_RESOURCES_RESULT_TYPE_REF = new TypeReference<>() {
};

private static final TypeReference<McpSchema.SearchResourceTemplatesResult> SEARCH_RESOURCE_TEMPLATES_RESULT_TYPE_REF = new TypeReference<>() {
};

private static final TypeReference<McpSchema.SearchPromptsResult> SEARCH_PROMPTS_RESULT_TYPE_REF = new TypeReference<>() {
};

public Mono<McpSchema.SearchToolsResult> searchTools(McpSchema.SearchToolsRequest searchToolsRequest) {
return this.withInitializationCheck("searching tools", initializedResult -> {
if (this.serverCapabilities.tools() == null || !this.serverCapabilities.tools().search()) {
return Mono.error(new McpError("Server does not provide the tool search capability"));
}
return this.mcpSession.sendRequest(McpSchema.METHOD_TOOLS_SEARCH, searchToolsRequest,
SEARCH_TOOLS_RESULT_TYPE_REF);
});
}

public Mono<McpSchema.SearchResourcesResult> searchResources(
McpSchema.SearchResourcesRequest searchResourcesRequest) {
return this.withInitializationCheck("searching resources", initializedResult -> {
if (this.serverCapabilities.resources() == null || !this.serverCapabilities.resources().search()) {
return Mono.error(new McpError("Server does not provide the resource search capability"));
}
return this.mcpSession.sendRequest(McpSchema.METHOD_RESOURCES_SEARCH, searchResourcesRequest,
SEARCH_RESOURCES_RESULT_TYPE_REF);
});
}

public Mono<McpSchema.SearchResourceTemplatesResult> searchResourceTemplates(
McpSchema.SearchResourceTemplatesRequest searchResourceTemplatesRequest) {
return this.withInitializationCheck("searching resource templates", initializedResult -> {
if (this.serverCapabilities.resources() == null || !this.serverCapabilities.resources().search()) {
// shares a capability with resources
return Mono.error(new McpError("Server does not provide the resource search capability"));
}
return this.mcpSession.sendRequest(McpSchema.METHOD_RESOURCES_TEMPLATES_SEARCH,
searchResourceTemplatesRequest, SEARCH_RESOURCE_TEMPLATES_RESULT_TYPE_REF);
});
}

public Mono<McpSchema.SearchPromptsResult> searchPrompts(McpSchema.SearchPromptsRequest searchPromptsRequest) {
return this.withInitializationCheck("searching prompts", initializedResult -> {
if (this.serverCapabilities.prompts() == null || !this.serverCapabilities.prompts().search()) {
return Mono.error(new McpError("Server does not provide the tool search capability"));
}
return this.mcpSession.sendRequest(McpSchema.METHOD_PROMPT_SEARCH, searchPromptsRequest,
SEARCH_PROMPTS_RESULT_TYPE_REF);
});
}

}
Loading