Skip to content

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

Closed
@embesozzi

Description

@embesozzi

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.

Activity

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[/+] on May 25, 2025
embesozzi

embesozzi commented on May 26, 2025

@embesozzi
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      MCP Server java.lang.OutOfMemoryError when handling multiple MCP client connections or reconnections · Issue #3319 · spring-projects/spring-ai