Skip to content
Merged
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
21 changes: 0 additions & 21 deletions mcp-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,6 @@
</dependency>

<!-- Test-scoped dependencies for integration tests -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty-http</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package io.modelcontextprotocol.client.transport;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.time.Duration;
Expand All @@ -30,11 +31,8 @@
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import reactor.core.publisher.Mono;
import reactor.core.publisher.Sinks;
import reactor.test.StepVerifier;

import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.util.UriComponentsBuilder;
import static io.modelcontextprotocol.util.McpJsonMapperUtils.JSON_MAPPER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
Expand Down Expand Up @@ -78,8 +76,6 @@ static class TestHttpClientSseClientTransport extends HttpClientSseClientTranspo

private final AtomicInteger inboundMessageCount = new AtomicInteger(0);

private Sinks.Many<ServerSentEvent<String>> events = Sinks.many().unicast().onBackpressureBuffer();

public TestHttpClientSseClientTransport(final String baseUri,
SseMessageEndpointValidator sseMessageEndpointValidator) {
super(HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build(),
Expand All @@ -91,13 +87,7 @@ public int getInboundMessageCount() {
return inboundMessageCount.get();
}

public void simulateEndpointEvent(String jsonMessage) {
events.tryEmitNext(ServerSentEvent.<String>builder().event("endpoint").data(jsonMessage).build());
inboundMessageCount.incrementAndGet();
}

public void simulateMessageEvent(String jsonMessage) {
events.tryEmitNext(ServerSentEvent.<String>builder().event("message").data(jsonMessage).build());
inboundMessageCount.incrementAndGet();
}

Expand Down Expand Up @@ -333,7 +323,7 @@ void testCustomizeClient() {
}

@Test
void testRequestCustomizer() {
void testRequestCustomizer() throws URISyntaxException {
var mockCustomizer = mock(McpSyncHttpClientRequestCustomizer.class);

// Create a transport with the customizer
Expand All @@ -348,8 +338,7 @@ void testRequestCustomizer() {
.verifyComplete();

// Verify the customizer was called
verify(mockCustomizer).customize(any(), eq("GET"),
eq(UriComponentsBuilder.fromUriString(host).path("/sse").build().toUri()), isNull(), eq(context));
verify(mockCustomizer).customize(any(), eq("GET"), eq(new URI(host + "/sse")), isNull(), eq(context));
clearInvocations(mockCustomizer);

// Send test message
Expand All @@ -373,7 +362,7 @@ void testRequestCustomizer() {
}

@Test
void testAsyncRequestCustomizer() {
void testAsyncRequestCustomizer() throws URISyntaxException {
var mockCustomizer = mock(McpAsyncHttpClientRequestCustomizer.class);
when(mockCustomizer.customize(any(), any(), any(), any(), any()))
.thenAnswer(invocation -> Mono.just(invocation.getArguments()[0]));
Expand All @@ -390,8 +379,7 @@ void testAsyncRequestCustomizer() {
.verifyComplete();

// Verify the customizer was called
verify(mockCustomizer).customize(any(), eq("GET"),
eq(UriComponentsBuilder.fromUriString(host).path("/sse").build().toUri()), isNull(), eq(context));
verify(mockCustomizer).customize(any(), eq("GET"), eq(new URI(host + "/sse")), isNull(), eq(context));
clearInvocations(mockCustomizer);

// Send test message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.client.RestClient;
import static io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport.APPLICATION_JSON;
import static io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport.TEXT_EVENT_STREAM;
import static io.modelcontextprotocol.util.McpJsonMapperUtils.JSON_MAPPER;
Expand Down Expand Up @@ -140,12 +137,18 @@ void testToolCallSuccess() {
Tool.builder("tool1", EMPTY_JSON_SCHEMA).title("tool1 description").build(),
(transportContext, request) -> {
// perform a blocking call to a remote service
String response = RestClient.create()
.get()
.uri("https://raw.githubusercontent.com/modelcontextprotocol/java-sdk/refs/heads/main/README.md")
.retrieve()
.body(String.class);
assertThat(response).isNotBlank();
try {
HttpResponse<String> response = HttpClient.newHttpClient()
.send(HttpRequest.newBuilder()
.uri(URI.create(
"https://raw.githubusercontent.com/modelcontextprotocol/java-sdk/refs/heads/main/README.md"))
.GET()
.build(), HttpResponse.BodyHandlers.ofString());
assertThat(response.body()).isNotBlank();
}
catch (Exception e) {
throw new RuntimeException(e);
}
return callResponse;
});

Expand Down Expand Up @@ -714,34 +717,19 @@ void testThrownMcpErrorAndJsonRpcError() throws Exception {

mcpServer.addTool(toolSpec);

McpSchema.CallToolRequest callToolRequest = McpSchema.CallToolRequest.builder("test")
.arguments(Map.of())
var request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PORT + CUSTOM_MESSAGE_ENDPOINT))
.header("Content-Type", APPLICATION_JSON)
.header("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM)
.header(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26)
.POST(HttpRequest.BodyPublishers.ofString("""
{"jsonrpc":"2.0","id":"test","method":"tools/call","params":{"name":"test","arguments":{}}}"""))
.build();
McpSchema.JSONRPCRequest jsonrpcRequest = new McpSchema.JSONRPCRequest(McpSchema.METHOD_TOOLS_CALL, "test",
callToolRequest);

MockHttpServletRequest request = new MockHttpServletRequest("POST", CUSTOM_MESSAGE_ENDPOINT);
MockHttpServletResponse response = new MockHttpServletResponse();

byte[] content = JSON_MAPPER.writeValueAsBytes(jsonrpcRequest);
request.setContent(content);
request.addHeader("Content-Type", "application/json");
request.addHeader("Content-Length", Integer.toString(content.length));
request.addHeader("Content-Length", Integer.toString(content.length));
request.addHeader("Accept", APPLICATION_JSON + ", " + TEXT_EVENT_STREAM);
request.addHeader("Content-Type", APPLICATION_JSON);
request.addHeader("Cache-Control", "no-cache");
request.addHeader(HttpHeaders.PROTOCOL_VERSION, ProtocolVersions.MCP_2025_03_26);

mcpStatelessServerTransport.service(request, response);

McpSchema.JSONRPCResponse jsonrpcResponse = JSON_MAPPER.readValue(response.getContentAsByteArray(),
McpSchema.JSONRPCResponse.class);
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());

assertThat(jsonrpcResponse).isNotNull();
assertThat(jsonrpcResponse.error()).isNotNull();
assertThat(jsonrpcResponse.error().code()).isEqualTo(ErrorCodes.INTERNAL_ERROR);
assertThat(jsonrpcResponse.error().message()).isEqualTo("testing");
assertThatJson(response.body()).inPath("error.code").isEqualTo(ErrorCodes.INTERNAL_ERROR);
assertThatJson(response.body()).inPath("error.message").isEqualTo("testing");
}

@Test
Expand Down
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
<jackson-annotations.version>2.21</jackson-annotations.version>
<jackson2.version>2.21.1</jackson2.version>
<jackson3.version>3.1.4</jackson3.version>
<springframework.version>6.2.19</springframework.version>

<!-- plugin versions -->
<maven-compiler-plugin.version>3.16.0</maven-compiler-plugin.version>
Expand Down
Loading