Skip to content

MCP Server java.lang.OutOfMemoryError when handling multiple MCP client connections or reconnections #3319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
embesozzi opened this issue May 24, 2025 · 1 comment

Comments

@embesozzi
Copy link

embesozzi commented May 24, 2025

Bug description
When connecting or reconnecting multiple MCP clients, the heap size increases until it reaches the maximum limit. Just to clarify, the MCP server works well when only one client is connected.

Please keep in mind that the MCP server is protected by OAuth 2.0, as defined in the MCP specification [1], and therefore it acts as an OAuth 2.0 resource server [2]. Typically, a short-lived access token is used, making client reconnection a common scenario.

[1] https://modelcontextprotocol.io/specification/2025-03-26
[2] https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html

Environment

<properties>
    <java.version>21</java.version>
     <spring-ai.version>1.0.0</spring-ai.version>
</properties>

Steps to reproduce

  1. Launch a sample MCP Server.
  2. Simulate the connection of multiple MCP clients.
  • To simplify the test case, I just use the official MCP inspector
    npm exec @modelcontextprotocol/inspector
  • Click the reconnect button multiple times. Each time you do, you'll see the heap size increase indefinitely (simulating multiple client connections or reconnections).
Image
  1. You will how the heap is increasing and the the error:
ERROR 59459 --- [nio-8080-exec-5] o.a.coyote.http11.Http11NioProtocol      : Failed to complete processing of a request
java.lang.OutOfMemoryError: Java heap space
  • And in Java VisualVM, the heap increases until it reaches the maximum.

Image

Expected behavior
I would like a way to configure or close idle connections, or at least prevent the heap size from increasing, since this is just the reconnection of the MCP client—no operations are being performed.

@embesozzi embesozzi changed the title java.lang.OutOfMemoryError when handling multiple MCP client connections or reconnections MCP Server java.lang.OutOfMemoryError when handling multiple MCP client connections or reconnections May 25, 2025
@embesozzi
Copy link
Author

Please let me know if I can provide more information about the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant