forked from modelcontextprotocol/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTomcatTestUtil.java
More file actions
63 lines (50 loc) · 1.57 KB
/
TomcatTestUtil.java
File metadata and controls
63 lines (50 loc) · 1.57 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
/*
* Copyright 2025 - 2025 the original author or authors.
*/
package io.modelcontextprotocol.server.transport;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import jakarta.servlet.Servlet;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
/**
* @author Christian Tzolov
*/
public class TomcatTestUtil {
TomcatTestUtil() {
// Prevent instantiation
}
public static Tomcat createTomcatServer(String contextPath, int port, Servlet servlet) {
var tomcat = new Tomcat();
tomcat.setPort(port);
String baseDir = System.getProperty("java.io.tmpdir");
tomcat.setBaseDir(baseDir);
Context context = tomcat.addContext(contextPath, baseDir);
// Add transport servlet to Tomcat
org.apache.catalina.Wrapper wrapper = context.createWrapper();
wrapper.setName("mcpServlet");
wrapper.setServlet(servlet);
wrapper.setLoadOnStartup(1);
wrapper.setAsyncSupported(true);
context.addChild(wrapper);
context.addServletMappingDecoded("/*", "mcpServlet");
var connector = tomcat.getConnector();
connector.setAsyncTimeout(3000);
return tomcat;
}
/**
* Finds an available port on the local machine.
* @return an available port number
* @throws IllegalStateException if no available port can be found
*/
public static int findAvailablePort() {
try (final ServerSocket socket = new ServerSocket()) {
socket.bind(new InetSocketAddress(0));
return socket.getLocalPort();
}
catch (final IOException e) {
throw new IllegalStateException("Cannot bind to an available port!", e);
}
}
}