agentchat-mcp 0.2.0 → 0.2.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentchat-mcp",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "AgentChat MCP plugin for Claude Code — join the AI Agent social network",
5
5
  "type": "module",
6
6
  "bin": {
@@ -37,6 +37,7 @@
37
37
  },
38
38
  "files": [
39
39
  "src/server.ts",
40
+ "src/heartbeat.ts",
40
41
  "README.md"
41
42
  ]
42
43
  }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Heartbeat / dead-connection detection — extracted for testability.
3
+ *
4
+ * HeartbeatMonitor sends periodic pings and watches for pong replies.
5
+ * If no pong arrives within `pongTimeout` ms it forces a reconnect.
6
+ */
7
+
8
+ export interface HeartbeatDeps {
9
+ /** Send a ping message over the wire */
10
+ sendPing: () => void;
11
+ /** Force-close the current connection and reconnect */
12
+ reconnect: () => void;
13
+ /** Current WS ready-state (matches WebSocket.OPEN / CLOSED constants) */
14
+ getReadyState: () => number;
15
+ }
16
+
17
+ /** WebSocket readyState constants (same as the spec) */
18
+ export const WS_OPEN = 1;
19
+ export const WS_CLOSED = 3;
20
+
21
+ export class HeartbeatMonitor {
22
+ private lastPong: number;
23
+ private timer: ReturnType<typeof setInterval> | null = null;
24
+
25
+ constructor(
26
+ private deps: HeartbeatDeps,
27
+ /** How often to send a ping (ms) */
28
+ public readonly pingInterval: number = 30_000,
29
+ /** Max time without a pong before we consider connection dead (ms) */
30
+ public readonly pongTimeout: number = 90_000,
31
+ ) {
32
+ this.lastPong = Date.now();
33
+ }
34
+
35
+ /** Record that a pong (or any alive signal like auth_ok) was received */
36
+ receivedPong() {
37
+ this.lastPong = Date.now();
38
+ }
39
+
40
+ /** Start the periodic heartbeat check */
41
+ start() {
42
+ this.stop();
43
+ this.lastPong = Date.now();
44
+ this.timer = setInterval(() => this.tick(), this.pingInterval);
45
+ }
46
+
47
+ /** Stop the heartbeat timer */
48
+ stop() {
49
+ if (this.timer) {
50
+ clearInterval(this.timer);
51
+ this.timer = null;
52
+ }
53
+ }
54
+
55
+ /** Exposed for testing — runs one heartbeat cycle */
56
+ tick() {
57
+ const state = this.deps.getReadyState();
58
+
59
+ if (state === WS_OPEN) {
60
+ if (Date.now() - this.lastPong > this.pongTimeout) {
61
+ // No pong in too long — force reconnect
62
+ this.deps.reconnect();
63
+ return;
64
+ }
65
+ this.deps.sendPing();
66
+ return;
67
+ }
68
+
69
+ if (state === WS_CLOSED) {
70
+ // Connection dead but onclose may not have fired
71
+ this.deps.reconnect();
72
+ }
73
+ }
74
+ }
package/src/server.ts CHANGED
@@ -679,7 +679,7 @@ function connectWS() {
679
679
  }
680
680
 
681
681
  // Heartbeat with dead-connection detection (15s ping, 45s timeout for faster recovery)
682
- import { HeartbeatMonitor, WS_OPEN, WS_CLOSED } from "./heartbeat.js";
682
+ import { HeartbeatMonitor, WS_OPEN, WS_CLOSED } from "./heartbeat.ts";
683
683
 
684
684
  const heartbeat = new HeartbeatMonitor({
685
685
  sendPing: () => {