-
Notifications
You must be signed in to change notification settings - Fork 794
Using MCP server with existing Tomcat hosted Java application #490
-
Pre-submission Checklist
- I have checked that this question would not be more appropriate as an issue in a specific repository
- I have searched existing discussions and documentation for answers
Question Category
- Protocol Specification
- SDK Usage
- Server Implementation
- General Implementation
- Documentation
- Other
Your Question
I have an existing Java application deployed on a Tomcat server, and I want to embed an MCP server into it using the official [MCP Java SDK] (https://github.com/modelcontextprotocol/java-sdk).
The SDK provides APIs to create a server that supports multiple transport types, such as HttpServletStreamableServerTransportProvider. Each of these transport types is itself implemented as a servlet.
Ideally, these transport types should be extensible so that I could subclass one of them within my application. However, the current SDK does not allow extending the transport classes.
Is there an alternative approach to integrate an MCP server into an existing Tomcat-hosted Java application?
Beta Was this translation helpful? Give feedback.
All reactions
You can implement a servlet that extends HTTPServlet and then in your class's service method delegate service call to one of the transport types available from MCP SDK.
Pseudocode -
class myServlet extends HTTPServlet {
private transient HttpServletStreamableServerTransportProvider transport;
myServlet () {
transport = HttpServletStreamableServerTransportProvider.builder().mcpEndpoint("/mcp").objectMapper(objMapper).build();
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
transport.service(request, response);
}
}
Replies: 2 comments 1 reply
-
I'm also interested in understanding the decision to make that class private and non-extensible.
I'm facing a similar issue where my existing application is unable to route to the WebServlet, for some reason, it cannot be discovered.
I forked the project and changed the access modifiers to make the classes public and extensible.
You could also replicate the transport class and implement it yourself.
Beta Was this translation helpful? Give feedback.
All reactions
-
You can implement a servlet that extends HTTPServlet and then in your class's service method delegate service call to one of the transport types available from MCP SDK.
Pseudocode -
class myServlet extends HTTPServlet {
private transient HttpServletStreamableServerTransportProvider transport;
myServlet () {
transport = HttpServletStreamableServerTransportProvider.builder().mcpEndpoint("/mcp").objectMapper(objMapper).build();
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
transport.service(request, response);
}
}
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
I implemented a very basic Hello World MCP server - https://github.com/i23098/mcp-server-java
It would work fine with other servlets...
I just have an issue when redeploying it - #650
Beta Was this translation helpful? Give feedback.