spring ai - JAVA MCP SDK WebFlux Server Transport sendMessage is Broadcasts? - Stack Overflow

Why is this place broadcast to all sessions, if one MCPClient initiates the call, other McPclients will

Why is this place broadcast to all sessions, if one MCPClient initiates the call, other McPclients will also receive the call results of other clients? thanks for help

    /**
     * Broadcasts a message to all connected clients through their SSE connections. The
     * message is serialized to JSON and sent as an SSE event with type "message". If any
     * errors occur during sending to a particular client, they are logged but don't
     * prevent sending to other clients.
     * @param message The JSON-RPC message to broadcast to all connected clients
     * @return A Mono that completes when the broadcast attempt is finished
     */
    @Override
    public Mono<Void> sendMessage(McpSchema.JSONRPCMessage message) {
        return Mono.fromRunnable(() -> {
            if (sessions.isEmpty()) {
                logger.debug("No active sessions to broadcast message to");
                return;
            }

            try {
                String jsonText = objectMapper.writeValueAsString(message);
                logger.debug("Attempting to broadcast message to {} active sessions", sessions.size());

                sessions.values().forEach(session -> {
                    try {
                        session.sseBuilder.id(session.id).event(MESSAGE_EVENT_TYPE).data(jsonText);
                    }
                    catch (Exception e) {
                        logger.error("Failed to send message to session {}: {}", session.id, e.getMessage());
                        session.sseBuilder.error(e);
                    }
                });
            }
            catch (IOException e) {
                logger.error("Failed to serialize message: {}", e.getMessage());
            }
        });
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745172004a4614983.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信