Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using MCP server with existing Tomcat hosted Java application #490

Answered by skanikdale
skanikdale asked this question in Q&A
Discussion options

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?

You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
1 reply
Comment options

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);
}

}

Answer selected by skanikdale
Comment options

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

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /