Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6BHcz4muRH44T3br3
71 lines
3.9 KiB
Java
71 lines
3.9 KiB
Java
package com.ankurm.codesearch;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import java.net.URI;
|
|
import java.util.Map;
|
|
|
|
import io.modelcontextprotocol.modern.McpSchema.CallToolResult;
|
|
import io.modelcontextprotocol.modern.McpSchema.Implementation;
|
|
import io.modelcontextprotocol.modern.McpSchema.TextContent;
|
|
import io.modelcontextprotocol.modern.client.McpClient;
|
|
import io.modelcontextprotocol.modern.client.McpSyncClient;
|
|
import io.modelcontextprotocol.modern.client.transport.HttpClientMcpTransport;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
/** The real SDK client against the real server: the three tools as an agent would call them, and their failures. */
|
|
class SdkClientTest {
|
|
|
|
private static String text(CallToolResult r) {
|
|
return ((TextContent) r.content().get(0)).text();
|
|
}
|
|
|
|
@Test
|
|
void anAgentSearchesThenReadsCallersThenSource() throws Exception {
|
|
try (var server = CodeSearchServer.serve(Fixtures.code(), Fixtures.hashingIndex(), 0)) {
|
|
McpSyncClient client = McpClient.sync(HttpClientMcpTransport.builder(URI.create(server.url())).build())
|
|
.clientInfo(Implementation.builder("sdk-client-test", "1").build()).build();
|
|
Transcript t = new Transcript("07-sdk-client.txt",
|
|
"An agent's session through the 2.2.0 SDK client: search, then callers, then source");
|
|
|
|
CallToolResult found = client.tool("search_code").arguments(Map.of("query", "return money to the customer", "k", 2)).call();
|
|
t.section("search_code {query: return money to the customer, k: 2}").line(text(found));
|
|
assertThat(text(found)).startsWith("1. ").contains("PaymentService#refund");
|
|
|
|
CallToolResult callers = client.tool("find_usages").arguments(Map.of("symbol", "PaymentService#refund")).call();
|
|
t.section("find_usages {symbol: PaymentService#refund}").line(text(callers));
|
|
assertThat(text(callers)).startsWith("1 usage(s) of PaymentService#refund (resolved)")
|
|
.contains("OrderService#cancelOrder").contains("[unresolved: matched by name]");
|
|
|
|
CallToolResult source = client.tool("get_source").arguments(Map.of("name", "OrderService#cancelOrder")).call();
|
|
t.section("get_source {name: OrderService#cancelOrder}").line(text(source));
|
|
assertThat(text(source)).contains("payments.refund(order.customerId(), order.total());");
|
|
|
|
t.write();
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void failuresComeBackAsToolResultsTheModelCanReadAndRecoverFrom() throws Exception {
|
|
try (var server = CodeSearchServer.serve(Fixtures.code(), Fixtures.hashingIndex(), 0)) {
|
|
McpSyncClient client = McpClient.sync(HttpClientMcpTransport.builder(URI.create(server.url())).build()).build();
|
|
Transcript t = new Transcript("08-tool-errors.txt", "Tool failures are results with isError=true, not protocol errors");
|
|
|
|
CallToolResult missing = client.tool("get_source").arguments(Map.of("name", "PaymentService#chargeback")).call();
|
|
t.section("get_source with a name that does not exist").line("isError=" + Boolean.TRUE.equals(missing.isError())).line(text(missing));
|
|
CallToolResult blank = client.tool("search_code").arguments(Map.of("query", " ")).call();
|
|
t.section("search_code with a blank query").line("isError=" + blank.isError()).line(text(blank));
|
|
CallToolResult noHits = client.tool("search_code").arguments(Map.of("query", "chargeback")).call();
|
|
t.section("search_code that matches nothing (not an error: an empty answer with advice)")
|
|
.line("isError=" + Boolean.TRUE.equals(noHits.isError())).line(text(noHits));
|
|
t.write();
|
|
|
|
assertThat(missing.isError()).isTrue();
|
|
assertThat(text(missing)).contains("Use search_code to find the exact name");
|
|
assertThat(blank.isError()).isTrue();
|
|
assertThat(noHits.isError()).isNotEqualTo(true);
|
|
}
|
|
}
|
|
}
|