最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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());
            }
        });
    }
发布评论

评论列表(0)

  1. 暂无评论