Skip to content

Fix transport session id mismatch with sessionId #176

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -261,8 +262,8 @@ private Mono<ServerResponse> handleSseConnection(ServerRequest request) {
.body(Flux.<ServerSentEvent<?>>create(sink -> {
WebFluxMcpSessionTransport sessionTransport = new WebFluxMcpSessionTransport(sink);

McpServerSession session = sessionFactory.create(sessionTransport);
String sessionId = session.getId();
String sessionId = UUID.randomUUID().toString();
McpServerSession session = sessionFactory.create(sessionId, sessionTransport);

logger.debug("Created new SSE connection for session: {}", sessionId);
sessions.put(sessionId, session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private ServerResponse handleSseConnection(ServerRequest request) {
});

WebMvcMcpSessionTransport sessionTransport = new WebMvcMcpSessionTransport(sessionId, sseBuilder);
McpServerSession session = sessionFactory.create(sessionTransport);
McpServerSession session = sessionFactory.create(sessionId, sessionTransport);
this.sessions.put(sessionId, session);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ private static class AsyncServerImpl extends McpAsyncServer {
notificationHandlers.put(McpSchema.METHOD_NOTIFICATION_ROOTS_LIST_CHANGED,
asyncRootsListChangedNotificationHandler(rootsChangeConsumers));

mcpTransportProvider.setSessionFactory(
transport -> new McpServerSession(UUID.randomUUID().toString(), requestTimeout, transport,
this::asyncInitializeRequestHandler, Mono::empty, requestHandlers, notificationHandlers));
mcpTransportProvider
.setSessionFactory((id, transport) -> new McpServerSession(id, requestTimeout, transport,
this::asyncInitializeRequestHandler, Mono::empty, requestHandlers, notificationHandlers));
}

// ---------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
writer);

// Create a new session using the session factory
McpServerSession session = sessionFactory.create(sessionTransport);
McpServerSession session = sessionFactory.create(sessionId, sessionTransport);
this.sessions.put(sessionId, session);

// Send initial endpoint event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
Expand Down Expand Up @@ -94,7 +95,8 @@ public StdioServerTransportProvider(ObjectMapper objectMapper, InputStream input
public void setSessionFactory(McpServerSession.Factory sessionFactory) {
// Create a single session for the stdio connection
var transport = new StdioMcpSessionTransport();
this.session = sessionFactory.create(transport);
String sessionId = UUID.randomUUID().toString();
this.session = sessionFactory.create(sessionId, transport);
transport.initProcessing();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,11 @@ public interface Factory {

/**
* Creates a new 1:1 representation of the client-server interaction.
* @param sessionId the id of the session.
* @param sessionTransport the transport to use for communication with the client.
* @return a new server session.
*/
McpServerSession create(McpServerTransport sessionTransport);
McpServerSession create(String sessionId, McpServerTransport sessionTransport);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.modelcontextprotocol;

import java.util.Map;
import java.util.UUID;

import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpServerSession;
Expand Down Expand Up @@ -43,7 +44,8 @@ public MockMcpServerTransport getTransport() {
@Override
public void setSessionFactory(Factory sessionFactory) {

session = sessionFactory.create(transport);
String sessionId = UUID.randomUUID().toString();
session = sessionFactory.create(sessionId, transport);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void setUp() {
sessionFactory = mock(McpServerSession.Factory.class);

// Configure mock behavior
when(sessionFactory.create(any(McpServerTransport.class))).thenReturn(mockSession);
when(sessionFactory.create(any(), any(McpServerTransport.class))).thenReturn(mockSession);
when(mockSession.closeGracefully()).thenReturn(Mono.empty());
when(mockSession.sendNotification(any(), any())).thenReturn(Mono.empty());

Expand Down Expand Up @@ -110,7 +110,7 @@ void shouldHandleIncomingMessages() throws Exception {
AtomicReference<McpSchema.JSONRPCMessage> capturedMessage = new AtomicReference<>();
CountDownLatch messageLatch = new CountDownLatch(1);

McpServerSession.Factory realSessionFactory = transport -> {
McpServerSession.Factory realSessionFactory = (id, transport) -> {
McpServerSession session = mock(McpServerSession.class);
when(session.handle(any())).thenAnswer(invocation -> {
capturedMessage.set(invocation.getArgument(0));
Expand Down