Repository navigation
Expand file tree
/
Copy pathHttpServletStatelessSharedIntegrationTests.java
More file actions
104 lines (85 loc) · 3.25 KB
/
Copy pathHttpServletStatelessSharedIntegrationTests.java
File metadata and controls
104 lines (85 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2024 - 2026 the original author or authors.
*/
package io.modelcontextprotocol.server;
import java.time.Duration;
import java.util.Map;
import java.util.stream.Stream;
import io.modelcontextprotocol.AbstractStatelessIntegrationTests;
import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.transport.HttpClientStreamableHttpTransport;
import io.modelcontextprotocol.common.McpTransportContext;
import io.modelcontextprotocol.server.McpServer.StatelessAsyncSpecification;
import io.modelcontextprotocol.server.McpServer.StatelessSyncSpecification;
import io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport;
import io.modelcontextprotocol.server.transport.TomcatTestUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleState;
import org.apache.catalina.startup.Tomcat;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.provider.Arguments;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Runs the shared stateless integration suite over the servlet stateless transport.
*/
@Timeout(15)
class HttpServletStatelessSharedIntegrationTests extends AbstractStatelessIntegrationTests {
private static final int PORT = TomcatTestUtil.findAvailablePort();
private static final String MESSAGE_ENDPOINT = "/mcp/message";
static McpTransportContextExtractor<HttpServletRequest> TEST_CONTEXT_EXTRACTOR = (request) -> McpTransportContext
.create(Map.of("important", "value"));
private Tomcat tomcat;
private HttpServletStatelessServerTransport mcpServerTransport;
static Stream<Arguments> clientsForTesting() {
return Stream.of(Arguments.of("httpclient"));
}
@BeforeEach
public void before() {
this.mcpServerTransport = HttpServletStatelessServerTransport.builder()
.contextExtractor(TEST_CONTEXT_EXTRACTOR)
.messageEndpoint(MESSAGE_ENDPOINT)
.build();
this.tomcat = TomcatTestUtil.createTomcatServer("", PORT, this.mcpServerTransport);
try {
this.tomcat.start();
assertThat(this.tomcat.getServer().getState()).isEqualTo(LifecycleState.STARTED);
}
catch (Exception e) {
throw new RuntimeException("Failed to start Tomcat", e);
}
prepareClients(PORT, MESSAGE_ENDPOINT);
}
@Override
protected void prepareClients(int port, String mcpEndpoint) {
this.clientBuilders.put("httpclient", McpClient
.sync(HttpClientStreamableHttpTransport.builder("http://localhost:" + port).endpoint(mcpEndpoint).build())
.initializationTimeout(Duration.ofHours(10))
.requestTimeout(Duration.ofHours(10)));
}
@Override
protected StatelessAsyncSpecification prepareAsyncServerBuilder() {
return McpServer.async(this.mcpServerTransport);
}
@Override
protected StatelessSyncSpecification prepareSyncServerBuilder() {
return McpServer.sync(this.mcpServerTransport);
}
@AfterEach
public void after() {
if (this.mcpServerTransport != null) {
this.mcpServerTransport.closeGracefully().block();
}
if (this.tomcat != null) {
try {
this.tomcat.stop();
this.tomcat.destroy();
}
catch (LifecycleException e) {
throw new RuntimeException("Failed to stop Tomcat", e);
}
}
}
}