modelcontextprotocol / modelcontextprotocol/java-sdk

McpServerSession lifecycle doesn't support distributed services

Open
#274 17 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/server enhancement feature/session-storage
Dominant language
Java
Stars
3.7k
Forks
1.1k
Avg merge
1d 15h
Merged PRs (30d)
9

Description

This issue is closely related to #273 and could be considered as a follow up to that, but I decided to split it for convenience and readability.

Consider an MCP server written with this SDK and using the StreamableHttp transport. We deploy two replicas (A and B) of this server, with a load balancer in front.

  1. A new client application initiates a session by sending an initialize message, followed by a notifications/initialized notifications.
  2. Let's say that the Load Balancer routed both messages to replica A
  3. As a result, replica A generated a new session which Id is communicated back to the client
  4. Replica A now has an McpServerSession instance properly initialized with all the necessary client information

Problem:

  1. Next the client sends a tools/list message with the obtain sessionId, but this time the load balancer routes the message to replica B
  2. Even though I believe it should be the server's responsibility to share the session id across replicas, the McpServerSession instance that lives in replica B has never received (and will never receive) the initialize message and notifications
  3. Although it's possible to distribute all that state and simulate that handshake in replica B (I actually did it), I think this SDK should have an OOTB support for that, or at the very least, SessionFactory methods that make it easier to generate "ready to use" Sessions based on previous initializations.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading this issue alongside related issue #273, then trace the StreamableHttp session lifecycle from initialize and notifications/initialized through tools/list. Inspect the session handling and SessionFactory entry points mentioned in the issue. Done means a session initialized on replica A can be used on replica B without manually simulating the handshake.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, distributed-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.