Skip to content

fix: McpAsyncServer: addResource use the description as the key problem (failure) to find #9

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

Merged
merged 2 commits into from
May 19, 2025
Merged
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 @@ -92,7 +92,6 @@ public class WebRxSseServerTransportProvider implements McpServerTransportProvid
* Map of active client sessions, keyed by session ID.
*/
private final ConcurrentHashMap<String, McpServerSession> sessions = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, WebRxMcpSessionTransport> sessionTransports = new ConcurrentHashMap<>();
/**
* Flag indicating if the transport is shutting down.
*/
Expand All @@ -117,9 +116,9 @@ public WebRxSseServerTransportProvider(ObjectMapper objectMapper, String message
this.sseEndpoint = sseEndpoint;
}

public void sendHeartbeat(){
for (WebRxMcpSessionTransport transport : sessionTransports.values()) {
transport.sendHeartbeat();
public void sendHeartbeat() {
for (McpServerSession session : sessions.values()) {
((WebRxMcpSessionTransport) session.getTransport()).sendHeartbeat();
}
}

Expand Down Expand Up @@ -230,14 +229,13 @@ public void handleSseConnection(Context ctx) throws Throwable{
}

Flux<SseEvent> publisher = Flux.create(sink -> {
WebRxMcpSessionTransport sessionTransport = new WebRxMcpSessionTransport(sink);
WebRxMcpSessionTransport sessionTransport = new WebRxMcpSessionTransport(ctx, sink);

McpServerSession session = sessionFactory.create(sessionTransport);
String sessionId = session.getId();

logger.debug("Created new SSE connection for session: {}", sessionId);
sessions.put(sessionId, session);
sessionTransports.put(sessionId, sessionTransport);

// Send initial endpoint event
logger.debug("Sending initial endpoint event to session: {}", sessionId);
Expand All @@ -247,7 +245,6 @@ public void handleSseConnection(Context ctx) throws Throwable{
sink.onCancel(() -> {
logger.debug("Session {} cancelled", sessionId);
sessions.remove(sessionId);
sessionTransports.remove(sessionId);
});
});

Expand Down Expand Up @@ -277,13 +274,16 @@ public void handleMessage(Context ctx) throws Throwable {
return;
}

if (Utils.isEmpty(ctx.param("sessionId"))) {
String sessionId = ctx.param("sessionId");

if (Utils.isEmpty(sessionId)) {
ctx.status(404);
ctx.render(new McpError("Session ID missing in message endpoint"));
return;
}

McpServerSession session = sessions.get(ctx.param("sessionId"));

McpServerSession session = sessions.get(sessionId);

String body = ctx.body();
try {
Expand All @@ -309,14 +309,19 @@ public void handleMessage(Context ctx) throws Throwable {
}
}

private class WebRxMcpSessionTransport implements McpServerTransport {

public class WebRxMcpSessionTransport implements McpServerTransport {
private final Context context;
private final FluxSink<SseEvent> sink;

public WebRxMcpSessionTransport(FluxSink<SseEvent> sink) {
public WebRxMcpSessionTransport(Context context,FluxSink<SseEvent> sink) {
this.context = context;
this.sink = sink;
}

public Context getContext() {
return context;
}

public void sendHeartbeat() {
sink.next(new SseEvent().comment("heartbeat"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,9 @@ public Mono<Void> addResource(McpServerFeatures.AsyncResourceSpecification resou
}

return Mono.defer(() -> {
if (this.resources.putIfAbsent(resourceSpecification.getResource().getDescription(), resourceSpecification) != null) {
if (this.resources.putIfAbsent(resourceSpecification.getResource().getUri(), resourceSpecification) != null) {
return Mono.error(new McpError(
"Resource with URI '" + resourceSpecification.getResource().getDescription() + "' already exists"));
"Resource with URI '" + resourceSpecification.getResource().getUri() + "' already exists"));
}
logger.debug("Added resource handler: {}", resourceSpecification.getResource().getUri());
if (this.serverCapabilities.getResources().getListChanged()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public McpServerSession(String id, McpServerTransport transport, InitRequestHand
this.notificationHandlers = notificationHandlers;
}

public McpServerTransport getTransport() {
return transport;
}

/**
* Retrieve the session id.
* @return session id
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<awaitility.version>4.2.0</awaitility.version>
<bnd-maven-plugin.version>5.0.1</bnd-maven-plugin.version>
<json-unit-assertj.version>2.40.1</json-unit-assertj.version>
<solon.version>3.2.1</solon.version>
<solon.version>3.3.0</solon.version>

</properties>

Expand Down
Loading