moflo 4.8.19 → 4.8.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (241) hide show
  1. package/.claude/guidance/shipped/moflo.md +45 -0
  2. package/.claude/helpers/statusline.cjs +1 -1
  3. package/.claude/workflow-state.json +9 -0
  4. package/package.json +2 -2
  5. package/src/@claude-flow/cli/dist/src/init/statusline-generator.js +1 -1
  6. package/src/@claude-flow/cli/dist/src/services/agentic-flow-bridge.js +5 -3
  7. package/src/@claude-flow/cli/package.json +1 -1
  8. package/src/@claude-flow/memory/dist/agent-memory-scope.d.ts +131 -0
  9. package/src/@claude-flow/memory/dist/agent-memory-scope.js +223 -0
  10. package/src/@claude-flow/memory/dist/agent-memory-scope.test.d.ts +8 -0
  11. package/src/@claude-flow/memory/dist/agent-memory-scope.test.js +466 -0
  12. package/src/@claude-flow/memory/dist/agentdb-adapter.d.ts +165 -0
  13. package/src/@claude-flow/memory/dist/agentdb-adapter.js +806 -0
  14. package/src/@claude-flow/memory/dist/agentdb-backend.d.ts +212 -0
  15. package/src/@claude-flow/memory/dist/agentdb-backend.js +842 -0
  16. package/src/@claude-flow/memory/dist/agentdb-backend.test.d.ts +7 -0
  17. package/src/@claude-flow/memory/dist/agentdb-backend.test.js +258 -0
  18. package/src/@claude-flow/memory/dist/application/commands/delete-memory.command.d.ts +65 -0
  19. package/src/@claude-flow/memory/dist/application/commands/delete-memory.command.js +129 -0
  20. package/src/@claude-flow/memory/dist/application/commands/store-memory.command.d.ts +48 -0
  21. package/src/@claude-flow/memory/dist/application/commands/store-memory.command.js +72 -0
  22. package/src/@claude-flow/memory/dist/application/index.d.ts +12 -0
  23. package/src/@claude-flow/memory/dist/application/index.js +15 -0
  24. package/src/@claude-flow/memory/dist/application/queries/search-memory.query.d.ts +72 -0
  25. package/src/@claude-flow/memory/dist/application/queries/search-memory.query.js +143 -0
  26. package/src/@claude-flow/memory/dist/application/services/memory-application-service.d.ts +121 -0
  27. package/src/@claude-flow/memory/dist/application/services/memory-application-service.js +190 -0
  28. package/src/@claude-flow/memory/dist/auto-memory-bridge.d.ts +226 -0
  29. package/src/@claude-flow/memory/dist/auto-memory-bridge.js +709 -0
  30. package/src/@claude-flow/memory/dist/auto-memory-bridge.test.d.ts +8 -0
  31. package/src/@claude-flow/memory/dist/auto-memory-bridge.test.js +757 -0
  32. package/src/@claude-flow/memory/dist/benchmark.test.d.ts +2 -0
  33. package/src/@claude-flow/memory/dist/benchmark.test.js +277 -0
  34. package/src/@claude-flow/memory/dist/cache-manager.d.ts +134 -0
  35. package/src/@claude-flow/memory/dist/cache-manager.js +407 -0
  36. package/src/@claude-flow/memory/dist/controller-registry.d.ts +216 -0
  37. package/src/@claude-flow/memory/dist/controller-registry.js +893 -0
  38. package/src/@claude-flow/memory/dist/controller-registry.test.d.ts +14 -0
  39. package/src/@claude-flow/memory/dist/controller-registry.test.js +593 -0
  40. package/src/@claude-flow/memory/dist/database-provider.d.ts +87 -0
  41. package/src/@claude-flow/memory/dist/database-provider.js +372 -0
  42. package/src/@claude-flow/memory/dist/database-provider.test.d.ts +7 -0
  43. package/src/@claude-flow/memory/dist/database-provider.test.js +287 -0
  44. package/src/@claude-flow/memory/dist/domain/entities/memory-entry.d.ts +143 -0
  45. package/src/@claude-flow/memory/dist/domain/entities/memory-entry.js +226 -0
  46. package/src/@claude-flow/memory/dist/domain/index.d.ts +11 -0
  47. package/src/@claude-flow/memory/dist/domain/index.js +12 -0
  48. package/src/@claude-flow/memory/dist/domain/repositories/memory-repository.interface.d.ts +102 -0
  49. package/src/@claude-flow/memory/dist/domain/repositories/memory-repository.interface.js +11 -0
  50. package/src/@claude-flow/memory/dist/domain/services/memory-domain-service.d.ts +105 -0
  51. package/src/@claude-flow/memory/dist/domain/services/memory-domain-service.js +297 -0
  52. package/src/@claude-flow/memory/dist/hnsw-index.d.ts +111 -0
  53. package/src/@claude-flow/memory/dist/hnsw-index.js +781 -0
  54. package/src/@claude-flow/memory/dist/hnsw-lite.d.ts +23 -0
  55. package/src/@claude-flow/memory/dist/hnsw-lite.js +168 -0
  56. package/src/@claude-flow/memory/dist/index.d.ts +204 -0
  57. package/src/@claude-flow/memory/dist/index.js +358 -0
  58. package/src/@claude-flow/memory/dist/infrastructure/index.d.ts +17 -0
  59. package/src/@claude-flow/memory/dist/infrastructure/index.js +16 -0
  60. package/src/@claude-flow/memory/dist/infrastructure/repositories/hybrid-memory-repository.d.ts +66 -0
  61. package/src/@claude-flow/memory/dist/infrastructure/repositories/hybrid-memory-repository.js +409 -0
  62. package/src/@claude-flow/memory/dist/learning-bridge.d.ts +137 -0
  63. package/src/@claude-flow/memory/dist/learning-bridge.js +335 -0
  64. package/src/@claude-flow/memory/dist/learning-bridge.test.d.ts +8 -0
  65. package/src/@claude-flow/memory/dist/learning-bridge.test.js +578 -0
  66. package/src/@claude-flow/memory/dist/memory-graph.d.ts +100 -0
  67. package/src/@claude-flow/memory/dist/memory-graph.js +333 -0
  68. package/src/@claude-flow/memory/dist/memory-graph.test.d.ts +8 -0
  69. package/src/@claude-flow/memory/dist/memory-graph.test.js +609 -0
  70. package/src/@claude-flow/memory/dist/migration.d.ts +68 -0
  71. package/src/@claude-flow/memory/dist/migration.js +513 -0
  72. package/src/@claude-flow/memory/dist/persistent-sona.d.ts +144 -0
  73. package/src/@claude-flow/memory/dist/persistent-sona.js +332 -0
  74. package/src/@claude-flow/memory/dist/query-builder.d.ts +211 -0
  75. package/src/@claude-flow/memory/dist/query-builder.js +438 -0
  76. package/src/@claude-flow/memory/dist/rvf-backend.d.ts +51 -0
  77. package/src/@claude-flow/memory/dist/rvf-backend.js +481 -0
  78. package/src/@claude-flow/memory/dist/rvf-learning-store.d.ts +139 -0
  79. package/src/@claude-flow/memory/dist/rvf-learning-store.js +295 -0
  80. package/src/@claude-flow/memory/dist/rvf-migration.d.ts +45 -0
  81. package/src/@claude-flow/memory/dist/rvf-migration.js +234 -0
  82. package/src/@claude-flow/memory/dist/sqljs-backend.d.ts +127 -0
  83. package/src/@claude-flow/memory/dist/sqljs-backend.js +600 -0
  84. package/src/@claude-flow/memory/dist/types.d.ts +484 -0
  85. package/src/@claude-flow/memory/dist/types.js +58 -0
  86. package/src/@claude-flow/shared/dist/core/config/defaults.d.ts +41 -0
  87. package/src/@claude-flow/shared/dist/core/config/defaults.js +186 -0
  88. package/src/@claude-flow/shared/dist/core/config/index.d.ts +8 -0
  89. package/src/@claude-flow/shared/dist/core/config/index.js +12 -0
  90. package/src/@claude-flow/shared/dist/core/config/loader.d.ts +45 -0
  91. package/src/@claude-flow/shared/dist/core/config/loader.js +222 -0
  92. package/src/@claude-flow/shared/dist/core/config/schema.d.ts +1134 -0
  93. package/src/@claude-flow/shared/dist/core/config/schema.js +158 -0
  94. package/src/@claude-flow/shared/dist/core/config/validator.d.ts +92 -0
  95. package/src/@claude-flow/shared/dist/core/config/validator.js +147 -0
  96. package/src/@claude-flow/shared/dist/core/event-bus.d.ts +31 -0
  97. package/src/@claude-flow/shared/dist/core/event-bus.js +197 -0
  98. package/src/@claude-flow/shared/dist/core/index.d.ts +15 -0
  99. package/src/@claude-flow/shared/dist/core/index.js +19 -0
  100. package/src/@claude-flow/shared/dist/core/interfaces/agent.interface.d.ts +200 -0
  101. package/src/@claude-flow/shared/dist/core/interfaces/agent.interface.js +6 -0
  102. package/src/@claude-flow/shared/dist/core/interfaces/coordinator.interface.d.ts +310 -0
  103. package/src/@claude-flow/shared/dist/core/interfaces/coordinator.interface.js +7 -0
  104. package/src/@claude-flow/shared/dist/core/interfaces/event.interface.d.ts +224 -0
  105. package/src/@claude-flow/shared/dist/core/interfaces/event.interface.js +46 -0
  106. package/src/@claude-flow/shared/dist/core/interfaces/index.d.ts +10 -0
  107. package/src/@claude-flow/shared/dist/core/interfaces/index.js +15 -0
  108. package/src/@claude-flow/shared/dist/core/interfaces/memory.interface.d.ts +298 -0
  109. package/src/@claude-flow/shared/dist/core/interfaces/memory.interface.js +7 -0
  110. package/src/@claude-flow/shared/dist/core/interfaces/task.interface.d.ts +185 -0
  111. package/src/@claude-flow/shared/dist/core/interfaces/task.interface.js +6 -0
  112. package/src/@claude-flow/shared/dist/core/orchestrator/event-coordinator.d.ts +35 -0
  113. package/src/@claude-flow/shared/dist/core/orchestrator/event-coordinator.js +101 -0
  114. package/src/@claude-flow/shared/dist/core/orchestrator/health-monitor.d.ts +60 -0
  115. package/src/@claude-flow/shared/dist/core/orchestrator/health-monitor.js +166 -0
  116. package/src/@claude-flow/shared/dist/core/orchestrator/index.d.ts +46 -0
  117. package/src/@claude-flow/shared/dist/core/orchestrator/index.js +64 -0
  118. package/src/@claude-flow/shared/dist/core/orchestrator/lifecycle-manager.d.ts +56 -0
  119. package/src/@claude-flow/shared/dist/core/orchestrator/lifecycle-manager.js +195 -0
  120. package/src/@claude-flow/shared/dist/core/orchestrator/session-manager.d.ts +83 -0
  121. package/src/@claude-flow/shared/dist/core/orchestrator/session-manager.js +193 -0
  122. package/src/@claude-flow/shared/dist/core/orchestrator/task-manager.d.ts +49 -0
  123. package/src/@claude-flow/shared/dist/core/orchestrator/task-manager.js +253 -0
  124. package/src/@claude-flow/shared/dist/events/domain-events.d.ts +282 -0
  125. package/src/@claude-flow/shared/dist/events/domain-events.js +165 -0
  126. package/src/@claude-flow/shared/dist/events/event-store.d.ts +126 -0
  127. package/src/@claude-flow/shared/dist/events/event-store.js +432 -0
  128. package/src/@claude-flow/shared/dist/events/event-store.test.d.ts +8 -0
  129. package/src/@claude-flow/shared/dist/events/event-store.test.js +297 -0
  130. package/src/@claude-flow/shared/dist/events/example-usage.d.ts +10 -0
  131. package/src/@claude-flow/shared/dist/events/example-usage.js +193 -0
  132. package/src/@claude-flow/shared/dist/events/index.d.ts +21 -0
  133. package/src/@claude-flow/shared/dist/events/index.js +22 -0
  134. package/src/@claude-flow/shared/dist/events/projections.d.ts +177 -0
  135. package/src/@claude-flow/shared/dist/events/projections.js +421 -0
  136. package/src/@claude-flow/shared/dist/events/rvf-event-log.d.ts +82 -0
  137. package/src/@claude-flow/shared/dist/events/rvf-event-log.js +340 -0
  138. package/src/@claude-flow/shared/dist/events/state-reconstructor.d.ts +101 -0
  139. package/src/@claude-flow/shared/dist/events/state-reconstructor.js +263 -0
  140. package/src/@claude-flow/shared/dist/events.d.ts +80 -0
  141. package/src/@claude-flow/shared/dist/events.js +249 -0
  142. package/src/@claude-flow/shared/dist/hooks/example-usage.d.ts +42 -0
  143. package/src/@claude-flow/shared/dist/hooks/example-usage.js +351 -0
  144. package/src/@claude-flow/shared/dist/hooks/executor.d.ts +100 -0
  145. package/src/@claude-flow/shared/dist/hooks/executor.js +267 -0
  146. package/src/@claude-flow/shared/dist/hooks/hooks.test.d.ts +9 -0
  147. package/src/@claude-flow/shared/dist/hooks/hooks.test.js +322 -0
  148. package/src/@claude-flow/shared/dist/hooks/index.d.ts +52 -0
  149. package/src/@claude-flow/shared/dist/hooks/index.js +51 -0
  150. package/src/@claude-flow/shared/dist/hooks/registry.d.ts +133 -0
  151. package/src/@claude-flow/shared/dist/hooks/registry.js +277 -0
  152. package/src/@claude-flow/shared/dist/hooks/safety/bash-safety.d.ts +105 -0
  153. package/src/@claude-flow/shared/dist/hooks/safety/bash-safety.js +481 -0
  154. package/src/@claude-flow/shared/dist/hooks/safety/file-organization.d.ts +144 -0
  155. package/src/@claude-flow/shared/dist/hooks/safety/file-organization.js +328 -0
  156. package/src/@claude-flow/shared/dist/hooks/safety/git-commit.d.ts +158 -0
  157. package/src/@claude-flow/shared/dist/hooks/safety/git-commit.js +450 -0
  158. package/src/@claude-flow/shared/dist/hooks/safety/index.d.ts +17 -0
  159. package/src/@claude-flow/shared/dist/hooks/safety/index.js +17 -0
  160. package/src/@claude-flow/shared/dist/hooks/session-hooks.d.ts +234 -0
  161. package/src/@claude-flow/shared/dist/hooks/session-hooks.js +334 -0
  162. package/src/@claude-flow/shared/dist/hooks/task-hooks.d.ts +163 -0
  163. package/src/@claude-flow/shared/dist/hooks/task-hooks.js +326 -0
  164. package/src/@claude-flow/shared/dist/hooks/types.d.ts +267 -0
  165. package/src/@claude-flow/shared/dist/hooks/types.js +62 -0
  166. package/src/@claude-flow/shared/dist/hooks/verify-exports.test.d.ts +9 -0
  167. package/src/@claude-flow/shared/dist/hooks/verify-exports.test.js +93 -0
  168. package/src/@claude-flow/shared/dist/index.d.ts +20 -0
  169. package/src/@claude-flow/shared/dist/index.js +50 -0
  170. package/src/@claude-flow/shared/dist/mcp/connection-pool.d.ts +98 -0
  171. package/src/@claude-flow/shared/dist/mcp/connection-pool.js +364 -0
  172. package/src/@claude-flow/shared/dist/mcp/index.d.ts +69 -0
  173. package/src/@claude-flow/shared/dist/mcp/index.js +84 -0
  174. package/src/@claude-flow/shared/dist/mcp/server.d.ts +166 -0
  175. package/src/@claude-flow/shared/dist/mcp/server.js +593 -0
  176. package/src/@claude-flow/shared/dist/mcp/session-manager.d.ts +136 -0
  177. package/src/@claude-flow/shared/dist/mcp/session-manager.js +335 -0
  178. package/src/@claude-flow/shared/dist/mcp/tool-registry.d.ts +178 -0
  179. package/src/@claude-flow/shared/dist/mcp/tool-registry.js +439 -0
  180. package/src/@claude-flow/shared/dist/mcp/transport/http.d.ts +104 -0
  181. package/src/@claude-flow/shared/dist/mcp/transport/http.js +476 -0
  182. package/src/@claude-flow/shared/dist/mcp/transport/index.d.ts +102 -0
  183. package/src/@claude-flow/shared/dist/mcp/transport/index.js +238 -0
  184. package/src/@claude-flow/shared/dist/mcp/transport/stdio.d.ts +104 -0
  185. package/src/@claude-flow/shared/dist/mcp/transport/stdio.js +263 -0
  186. package/src/@claude-flow/shared/dist/mcp/transport/websocket.d.ts +133 -0
  187. package/src/@claude-flow/shared/dist/mcp/transport/websocket.js +396 -0
  188. package/src/@claude-flow/shared/dist/mcp/types.d.ts +438 -0
  189. package/src/@claude-flow/shared/dist/mcp/types.js +54 -0
  190. package/src/@claude-flow/shared/dist/plugin-interface.d.ts +544 -0
  191. package/src/@claude-flow/shared/dist/plugin-interface.js +23 -0
  192. package/src/@claude-flow/shared/dist/plugin-loader.d.ts +139 -0
  193. package/src/@claude-flow/shared/dist/plugin-loader.js +434 -0
  194. package/src/@claude-flow/shared/dist/plugin-registry.d.ts +183 -0
  195. package/src/@claude-flow/shared/dist/plugin-registry.js +457 -0
  196. package/src/@claude-flow/shared/dist/plugins/index.d.ts +10 -0
  197. package/src/@claude-flow/shared/dist/plugins/index.js +10 -0
  198. package/src/@claude-flow/shared/dist/plugins/official/hive-mind-plugin.d.ts +106 -0
  199. package/src/@claude-flow/shared/dist/plugins/official/hive-mind-plugin.js +241 -0
  200. package/src/@claude-flow/shared/dist/plugins/official/index.d.ts +10 -0
  201. package/src/@claude-flow/shared/dist/plugins/official/index.js +10 -0
  202. package/src/@claude-flow/shared/dist/plugins/official/maestro-plugin.d.ts +121 -0
  203. package/src/@claude-flow/shared/dist/plugins/official/maestro-plugin.js +355 -0
  204. package/src/@claude-flow/shared/dist/plugins/types.d.ts +93 -0
  205. package/src/@claude-flow/shared/dist/plugins/types.js +9 -0
  206. package/src/@claude-flow/shared/dist/resilience/bulkhead.d.ts +105 -0
  207. package/src/@claude-flow/shared/dist/resilience/bulkhead.js +206 -0
  208. package/src/@claude-flow/shared/dist/resilience/circuit-breaker.d.ts +132 -0
  209. package/src/@claude-flow/shared/dist/resilience/circuit-breaker.js +233 -0
  210. package/src/@claude-flow/shared/dist/resilience/index.d.ts +19 -0
  211. package/src/@claude-flow/shared/dist/resilience/index.js +19 -0
  212. package/src/@claude-flow/shared/dist/resilience/rate-limiter.d.ts +168 -0
  213. package/src/@claude-flow/shared/dist/resilience/rate-limiter.js +314 -0
  214. package/src/@claude-flow/shared/dist/resilience/retry.d.ts +91 -0
  215. package/src/@claude-flow/shared/dist/resilience/retry.js +159 -0
  216. package/src/@claude-flow/shared/dist/security/index.d.ts +10 -0
  217. package/src/@claude-flow/shared/dist/security/index.js +12 -0
  218. package/src/@claude-flow/shared/dist/security/input-validation.d.ts +73 -0
  219. package/src/@claude-flow/shared/dist/security/input-validation.js +201 -0
  220. package/src/@claude-flow/shared/dist/security/secure-random.d.ts +92 -0
  221. package/src/@claude-flow/shared/dist/security/secure-random.js +142 -0
  222. package/src/@claude-flow/shared/dist/services/index.d.ts +7 -0
  223. package/src/@claude-flow/shared/dist/services/index.js +7 -0
  224. package/src/@claude-flow/shared/dist/services/v3-progress.service.d.ts +124 -0
  225. package/src/@claude-flow/shared/dist/services/v3-progress.service.js +402 -0
  226. package/src/@claude-flow/shared/dist/types/agent.types.d.ts +137 -0
  227. package/src/@claude-flow/shared/dist/types/agent.types.js +6 -0
  228. package/src/@claude-flow/shared/dist/types/index.d.ts +11 -0
  229. package/src/@claude-flow/shared/dist/types/index.js +17 -0
  230. package/src/@claude-flow/shared/dist/types/mcp.types.d.ts +266 -0
  231. package/src/@claude-flow/shared/dist/types/mcp.types.js +7 -0
  232. package/src/@claude-flow/shared/dist/types/memory.types.d.ts +236 -0
  233. package/src/@claude-flow/shared/dist/types/memory.types.js +7 -0
  234. package/src/@claude-flow/shared/dist/types/swarm.types.d.ts +186 -0
  235. package/src/@claude-flow/shared/dist/types/swarm.types.js +65 -0
  236. package/src/@claude-flow/shared/dist/types/task.types.d.ts +178 -0
  237. package/src/@claude-flow/shared/dist/types/task.types.js +32 -0
  238. package/src/@claude-flow/shared/dist/types.d.ts +197 -0
  239. package/src/@claude-flow/shared/dist/types.js +21 -0
  240. package/src/@claude-flow/shared/dist/utils/secure-logger.d.ts +69 -0
  241. package/src/@claude-flow/shared/dist/utils/secure-logger.js +208 -0
@@ -0,0 +1,133 @@
1
+ /**
2
+ * V3 MCP WebSocket Transport
3
+ *
4
+ * Standalone WebSocket transport for MCP communication:
5
+ * - Native WebSocket server without HTTP dependency
6
+ * - Binary message support for efficiency
7
+ * - Heartbeat/ping-pong for connection health
8
+ * - Automatic reconnection handling
9
+ *
10
+ * Performance Targets:
11
+ * - Message latency: <3ms
12
+ * - Connection overhead: <10ms
13
+ */
14
+ import { EventEmitter } from 'events';
15
+ import { WebSocket } from 'ws';
16
+ import { ITransport, TransportType, MCPNotification, RequestHandler, NotificationHandler, TransportHealthStatus, ILogger, AuthConfig } from '../types.js';
17
+ /**
18
+ * WebSocket Transport Configuration
19
+ */
20
+ export interface WebSocketTransportConfig {
21
+ host: string;
22
+ port: number;
23
+ path?: string;
24
+ maxConnections?: number;
25
+ heartbeatInterval?: number;
26
+ heartbeatTimeout?: number;
27
+ maxMessageSize?: number;
28
+ auth?: AuthConfig;
29
+ enableBinaryMode?: boolean;
30
+ }
31
+ /**
32
+ * Client connection info
33
+ */
34
+ interface ClientConnection {
35
+ id: string;
36
+ ws: WebSocket;
37
+ createdAt: Date;
38
+ lastActivity: Date;
39
+ messageCount: number;
40
+ isAlive: boolean;
41
+ isAuthenticated: boolean;
42
+ }
43
+ /**
44
+ * WebSocket Transport Implementation
45
+ */
46
+ export declare class WebSocketTransport extends EventEmitter implements ITransport {
47
+ private readonly logger;
48
+ private readonly config;
49
+ readonly type: TransportType;
50
+ private requestHandler?;
51
+ private notificationHandler?;
52
+ private server?;
53
+ private wss?;
54
+ private clients;
55
+ private heartbeatTimer?;
56
+ private running;
57
+ private connectionCounter;
58
+ private messagesReceived;
59
+ private messagesSent;
60
+ private errors;
61
+ private totalConnections;
62
+ constructor(logger: ILogger, config: WebSocketTransportConfig);
63
+ /**
64
+ * Start the transport
65
+ */
66
+ start(): Promise<void>;
67
+ /**
68
+ * Stop the transport
69
+ */
70
+ stop(): Promise<void>;
71
+ /**
72
+ * Register request handler
73
+ */
74
+ onRequest(handler: RequestHandler): void;
75
+ /**
76
+ * Register notification handler
77
+ */
78
+ onNotification(handler: NotificationHandler): void;
79
+ /**
80
+ * Get health status
81
+ */
82
+ getHealthStatus(): Promise<TransportHealthStatus>;
83
+ /**
84
+ * Send notification to all connected clients
85
+ */
86
+ sendNotification(notification: MCPNotification): Promise<void>;
87
+ /**
88
+ * Send notification to specific client
89
+ */
90
+ sendToClient(clientId: string, notification: MCPNotification): Promise<boolean>;
91
+ /**
92
+ * Get connected clients
93
+ */
94
+ getClients(): string[];
95
+ /**
96
+ * Get client info
97
+ */
98
+ getClientInfo(clientId: string): ClientConnection | undefined;
99
+ /**
100
+ * Disconnect specific client
101
+ */
102
+ disconnectClient(clientId: string, reason?: string): boolean;
103
+ /**
104
+ * Setup WebSocket handlers
105
+ */
106
+ private setupWebSocketHandlers;
107
+ /**
108
+ * Handle incoming message
109
+ */
110
+ private handleMessage;
111
+ /**
112
+ * Parse incoming message
113
+ */
114
+ private parseMessage;
115
+ /**
116
+ * Serialize outgoing message
117
+ */
118
+ private serializeMessage;
119
+ /**
120
+ * Start heartbeat interval
121
+ */
122
+ private startHeartbeat;
123
+ /**
124
+ * Stop heartbeat interval
125
+ */
126
+ private stopHeartbeat;
127
+ }
128
+ /**
129
+ * Create WebSocket transport
130
+ */
131
+ export declare function createWebSocketTransport(logger: ILogger, config: WebSocketTransportConfig): WebSocketTransport;
132
+ export {};
133
+ //# sourceMappingURL=websocket.d.ts.map
@@ -0,0 +1,396 @@
1
+ /**
2
+ * V3 MCP WebSocket Transport
3
+ *
4
+ * Standalone WebSocket transport for MCP communication:
5
+ * - Native WebSocket server without HTTP dependency
6
+ * - Binary message support for efficiency
7
+ * - Heartbeat/ping-pong for connection health
8
+ * - Automatic reconnection handling
9
+ *
10
+ * Performance Targets:
11
+ * - Message latency: <3ms
12
+ * - Connection overhead: <10ms
13
+ */
14
+ import { EventEmitter } from 'events';
15
+ import { WebSocketServer, WebSocket } from 'ws';
16
+ import { createServer } from 'http';
17
+ /**
18
+ * WebSocket Transport Implementation
19
+ */
20
+ export class WebSocketTransport extends EventEmitter {
21
+ logger;
22
+ config;
23
+ type = 'websocket';
24
+ requestHandler;
25
+ notificationHandler;
26
+ server;
27
+ wss;
28
+ clients = new Map();
29
+ heartbeatTimer;
30
+ running = false;
31
+ connectionCounter = 0;
32
+ // Statistics
33
+ messagesReceived = 0;
34
+ messagesSent = 0;
35
+ errors = 0;
36
+ totalConnections = 0;
37
+ constructor(logger, config) {
38
+ super();
39
+ this.logger = logger;
40
+ this.config = config;
41
+ }
42
+ /**
43
+ * Start the transport
44
+ */
45
+ async start() {
46
+ if (this.running) {
47
+ throw new Error('WebSocket transport already running');
48
+ }
49
+ this.logger.info('Starting WebSocket transport', {
50
+ host: this.config.host,
51
+ port: this.config.port,
52
+ path: this.config.path || '/ws',
53
+ });
54
+ // Create HTTP server for WebSocket upgrade
55
+ this.server = createServer((req, res) => {
56
+ // Simple HTTP response for non-WebSocket requests
57
+ res.writeHead(426, { 'Content-Type': 'text/plain' });
58
+ res.end('Upgrade Required - WebSocket connection expected');
59
+ });
60
+ // Create WebSocket server
61
+ this.wss = new WebSocketServer({
62
+ server: this.server,
63
+ path: this.config.path || '/ws',
64
+ maxPayload: this.config.maxMessageSize || 10 * 1024 * 1024,
65
+ perMessageDeflate: true, // Enable compression
66
+ });
67
+ this.setupWebSocketHandlers();
68
+ this.startHeartbeat();
69
+ // Start server
70
+ await new Promise((resolve, reject) => {
71
+ this.server.listen(this.config.port, this.config.host, () => {
72
+ resolve();
73
+ });
74
+ this.server.on('error', reject);
75
+ });
76
+ this.running = true;
77
+ this.logger.info('WebSocket transport started', {
78
+ url: `ws://${this.config.host}:${this.config.port}${this.config.path || '/ws'}`,
79
+ });
80
+ }
81
+ /**
82
+ * Stop the transport
83
+ */
84
+ async stop() {
85
+ if (!this.running) {
86
+ return;
87
+ }
88
+ this.logger.info('Stopping WebSocket transport');
89
+ this.running = false;
90
+ this.stopHeartbeat();
91
+ // Close all client connections
92
+ for (const client of this.clients.values()) {
93
+ try {
94
+ client.ws.close(1000, 'Server shutting down');
95
+ }
96
+ catch {
97
+ // Ignore errors
98
+ }
99
+ }
100
+ this.clients.clear();
101
+ // Close WebSocket server
102
+ if (this.wss) {
103
+ this.wss.close();
104
+ this.wss = undefined;
105
+ }
106
+ // Close HTTP server
107
+ if (this.server) {
108
+ await new Promise((resolve) => {
109
+ this.server.close(() => resolve());
110
+ });
111
+ this.server = undefined;
112
+ }
113
+ this.logger.info('WebSocket transport stopped');
114
+ }
115
+ /**
116
+ * Register request handler
117
+ */
118
+ onRequest(handler) {
119
+ this.requestHandler = handler;
120
+ }
121
+ /**
122
+ * Register notification handler
123
+ */
124
+ onNotification(handler) {
125
+ this.notificationHandler = handler;
126
+ }
127
+ /**
128
+ * Get health status
129
+ */
130
+ async getHealthStatus() {
131
+ return {
132
+ healthy: this.running,
133
+ metrics: {
134
+ messagesReceived: this.messagesReceived,
135
+ messagesSent: this.messagesSent,
136
+ errors: this.errors,
137
+ activeConnections: this.clients.size,
138
+ totalConnections: this.totalConnections,
139
+ },
140
+ };
141
+ }
142
+ /**
143
+ * Send notification to all connected clients
144
+ */
145
+ async sendNotification(notification) {
146
+ const message = this.serializeMessage(notification);
147
+ for (const client of this.clients.values()) {
148
+ try {
149
+ if (client.ws.readyState === WebSocket.OPEN) {
150
+ client.ws.send(message);
151
+ this.messagesSent++;
152
+ }
153
+ }
154
+ catch (error) {
155
+ this.logger.error('Failed to send notification', { clientId: client.id, error });
156
+ this.errors++;
157
+ }
158
+ }
159
+ }
160
+ /**
161
+ * Send notification to specific client
162
+ */
163
+ async sendToClient(clientId, notification) {
164
+ const client = this.clients.get(clientId);
165
+ if (!client || client.ws.readyState !== WebSocket.OPEN) {
166
+ return false;
167
+ }
168
+ try {
169
+ client.ws.send(this.serializeMessage(notification));
170
+ this.messagesSent++;
171
+ return true;
172
+ }
173
+ catch (error) {
174
+ this.logger.error('Failed to send to client', { clientId, error });
175
+ this.errors++;
176
+ return false;
177
+ }
178
+ }
179
+ /**
180
+ * Get connected clients
181
+ */
182
+ getClients() {
183
+ return Array.from(this.clients.keys());
184
+ }
185
+ /**
186
+ * Get client info
187
+ */
188
+ getClientInfo(clientId) {
189
+ return this.clients.get(clientId);
190
+ }
191
+ /**
192
+ * Disconnect specific client
193
+ */
194
+ disconnectClient(clientId, reason = 'Disconnected by server') {
195
+ const client = this.clients.get(clientId);
196
+ if (!client) {
197
+ return false;
198
+ }
199
+ try {
200
+ client.ws.close(1000, reason);
201
+ return true;
202
+ }
203
+ catch {
204
+ return false;
205
+ }
206
+ }
207
+ /**
208
+ * Setup WebSocket handlers
209
+ */
210
+ setupWebSocketHandlers() {
211
+ if (!this.wss)
212
+ return;
213
+ this.wss.on('connection', (ws, req) => {
214
+ // Check max connections
215
+ if (this.config.maxConnections && this.clients.size >= this.config.maxConnections) {
216
+ this.logger.warn('Max connections reached, rejecting client');
217
+ ws.close(1013, 'Server at capacity');
218
+ return;
219
+ }
220
+ const clientId = `client-${++this.connectionCounter}`;
221
+ const client = {
222
+ id: clientId,
223
+ ws,
224
+ createdAt: new Date(),
225
+ lastActivity: new Date(),
226
+ messageCount: 0,
227
+ isAlive: true,
228
+ isAuthenticated: !this.config.auth?.enabled,
229
+ };
230
+ this.clients.set(clientId, client);
231
+ this.totalConnections++;
232
+ this.logger.info('Client connected', {
233
+ id: clientId,
234
+ total: this.clients.size,
235
+ });
236
+ // Setup message handler
237
+ ws.on('message', async (data) => {
238
+ await this.handleMessage(client, data);
239
+ });
240
+ // Setup pong handler for heartbeat
241
+ ws.on('pong', () => {
242
+ client.isAlive = true;
243
+ });
244
+ // Setup close handler
245
+ ws.on('close', (code, reason) => {
246
+ this.clients.delete(clientId);
247
+ this.logger.info('Client disconnected', {
248
+ id: clientId,
249
+ code,
250
+ reason: reason.toString(),
251
+ total: this.clients.size,
252
+ });
253
+ this.emit('client:disconnected', clientId);
254
+ });
255
+ // Setup error handler
256
+ ws.on('error', (error) => {
257
+ this.logger.error('Client error', { id: clientId, error });
258
+ this.errors++;
259
+ this.clients.delete(clientId);
260
+ });
261
+ this.emit('client:connected', clientId);
262
+ });
263
+ }
264
+ /**
265
+ * Handle incoming message
266
+ */
267
+ async handleMessage(client, data) {
268
+ client.lastActivity = new Date();
269
+ client.messageCount++;
270
+ this.messagesReceived++;
271
+ try {
272
+ const message = this.parseMessage(data);
273
+ // Check authentication for non-authenticated clients
274
+ if (!client.isAuthenticated && this.config.auth?.enabled) {
275
+ if (message.method !== 'authenticate') {
276
+ client.ws.send(this.serializeMessage({
277
+ jsonrpc: '2.0',
278
+ id: message.id || null,
279
+ error: { code: -32001, message: 'Authentication required' },
280
+ }));
281
+ return;
282
+ }
283
+ }
284
+ if (message.jsonrpc !== '2.0') {
285
+ client.ws.send(this.serializeMessage({
286
+ jsonrpc: '2.0',
287
+ id: message.id || null,
288
+ error: { code: -32600, message: 'Invalid JSON-RPC version' },
289
+ }));
290
+ return;
291
+ }
292
+ if (message.id === undefined) {
293
+ // Notification
294
+ if (this.notificationHandler) {
295
+ await this.notificationHandler(message);
296
+ }
297
+ }
298
+ else {
299
+ // Request
300
+ if (!this.requestHandler) {
301
+ client.ws.send(this.serializeMessage({
302
+ jsonrpc: '2.0',
303
+ id: message.id,
304
+ error: { code: -32603, message: 'No request handler' },
305
+ }));
306
+ return;
307
+ }
308
+ const startTime = performance.now();
309
+ const response = await this.requestHandler(message);
310
+ const duration = performance.now() - startTime;
311
+ this.logger.debug('Request processed', {
312
+ clientId: client.id,
313
+ method: message.method,
314
+ duration: `${duration.toFixed(2)}ms`,
315
+ });
316
+ client.ws.send(this.serializeMessage(response));
317
+ this.messagesSent++;
318
+ }
319
+ }
320
+ catch (error) {
321
+ this.errors++;
322
+ this.logger.error('Message handling error', { clientId: client.id, error });
323
+ try {
324
+ client.ws.send(this.serializeMessage({
325
+ jsonrpc: '2.0',
326
+ id: null,
327
+ error: { code: -32700, message: 'Parse error' },
328
+ }));
329
+ }
330
+ catch {
331
+ // Ignore send errors
332
+ }
333
+ }
334
+ }
335
+ /**
336
+ * Parse incoming message
337
+ */
338
+ parseMessage(data) {
339
+ if (this.config.enableBinaryMode && Buffer.isBuffer(data)) {
340
+ // Could implement binary protocol here
341
+ return JSON.parse(data.toString());
342
+ }
343
+ return JSON.parse(data.toString());
344
+ }
345
+ /**
346
+ * Serialize outgoing message
347
+ */
348
+ serializeMessage(message) {
349
+ if (this.config.enableBinaryMode) {
350
+ // Could implement binary protocol here
351
+ return JSON.stringify(message);
352
+ }
353
+ return JSON.stringify(message);
354
+ }
355
+ /**
356
+ * Start heartbeat interval
357
+ */
358
+ startHeartbeat() {
359
+ const interval = this.config.heartbeatInterval || 30000; // 30 seconds
360
+ const timeout = this.config.heartbeatTimeout || 10000; // 10 seconds
361
+ this.heartbeatTimer = setInterval(() => {
362
+ for (const client of this.clients.values()) {
363
+ if (!client.isAlive) {
364
+ // Client didn't respond to last ping
365
+ this.logger.warn('Client heartbeat timeout', { id: client.id });
366
+ client.ws.terminate();
367
+ this.clients.delete(client.id);
368
+ continue;
369
+ }
370
+ client.isAlive = false;
371
+ try {
372
+ client.ws.ping();
373
+ }
374
+ catch {
375
+ // Ignore ping errors
376
+ }
377
+ }
378
+ }, interval);
379
+ }
380
+ /**
381
+ * Stop heartbeat interval
382
+ */
383
+ stopHeartbeat() {
384
+ if (this.heartbeatTimer) {
385
+ clearInterval(this.heartbeatTimer);
386
+ this.heartbeatTimer = undefined;
387
+ }
388
+ }
389
+ }
390
+ /**
391
+ * Create WebSocket transport
392
+ */
393
+ export function createWebSocketTransport(logger, config) {
394
+ return new WebSocketTransport(logger, config);
395
+ }
396
+ //# sourceMappingURL=websocket.js.map