|
| 1 | +import { Controller } from "../index" |
| 2 | +import { EmptyRequest } from "@shared/proto/common" |
| 3 | +import { McpServers } from "@shared/proto/mcp" |
| 4 | +import { StreamingResponseHandler, getRequestRegistry } from "../grpc-handler" |
| 5 | +import { convertMcpServersToProtoMcpServers } from "@shared/proto-conversions/mcp/mcp-server-conversion" |
| 6 | + |
| 7 | +// Keep track of active subscriptions |
| 8 | +const activeMcpServersSubscriptions = new Set<StreamingResponseHandler>() |
| 9 | + |
| 10 | +/** |
| 11 | + * Subscribe to MCP servers events |
| 12 | + * @param controller The controller instance |
| 13 | + * @param request The empty request |
| 14 | + * @param responseStream The streaming response handler |
| 15 | + * @param requestId The ID of the request (passed by the gRPC handler) |
| 16 | + */ |
| 17 | +export async function subscribeToMcpServers( |
| 18 | + controller: Controller, |
| 19 | + request: EmptyRequest, |
| 20 | + responseStream: StreamingResponseHandler, |
| 21 | + requestId?: string, |
| 22 | +): Promise<void> { |
| 23 | + // Add this subscription to the active subscriptions |
| 24 | + activeMcpServersSubscriptions.add(responseStream) |
| 25 | + |
| 26 | + // Register cleanup when the connection is closed |
| 27 | + const cleanup = () => { |
| 28 | + activeMcpServersSubscriptions.delete(responseStream) |
| 29 | + } |
| 30 | + |
| 31 | + // Register the cleanup function with the request registry if we have a requestId |
| 32 | + if (requestId) { |
| 33 | + getRequestRegistry().registerRequest(requestId, cleanup, { type: "mcpServers_subscription" }, responseStream) |
| 34 | + } |
| 35 | + |
| 36 | + // Send initial state if available |
| 37 | + if (controller.mcpHub) { |
| 38 | + const mcpServers = controller.mcpHub.getServers() |
| 39 | + if (mcpServers.length > 0) { |
| 40 | + try { |
| 41 | + const protoServers = McpServers.create({ |
| 42 | + mcpServers: convertMcpServersToProtoMcpServers(mcpServers), |
| 43 | + }) |
| 44 | + await responseStream( |
| 45 | + protoServers, |
| 46 | + false, // Not the last message |
| 47 | + ) |
| 48 | + } catch (error) { |
| 49 | + console.error("Error sending initial MCP servers:", error) |
| 50 | + activeMcpServersSubscriptions.delete(responseStream) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Send an MCP servers update to all active subscribers |
| 58 | + * @param mcpServers The MCP servers to send |
| 59 | + */ |
| 60 | +export async function sendMcpServersUpdate(mcpServers: McpServers): Promise<void> { |
| 61 | + // Send the event to all active subscribers |
| 62 | + const promises = Array.from(activeMcpServersSubscriptions).map(async (responseStream) => { |
| 63 | + try { |
| 64 | + await responseStream( |
| 65 | + mcpServers, |
| 66 | + false, // Not the last message |
| 67 | + ) |
| 68 | + } catch (error) { |
| 69 | + console.error("Error sending MCP servers update:", error) |
| 70 | + // Remove the subscription if there was an error |
| 71 | + activeMcpServersSubscriptions.delete(responseStream) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + await Promise.all(promises) |
| 76 | +} |
0 commit comments