agenthub-multiagent-mcp 1.1.4 → 1.3.0

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/src/heartbeat.ts CHANGED
@@ -1,8 +1,13 @@
1
1
  /**
2
2
  * Background heartbeat manager with enhanced logging for pending tasks/messages
3
+ * and automatic reconnection on failures.
3
4
  */
4
5
 
5
6
  import { ApiClient } from "./client.js";
7
+ import { AgentState } from "./state.js";
8
+
9
+ // Reconnection callback type
10
+ export type ReconnectCallback = () => Promise<AgentState | null>;
6
11
 
7
12
  export class HeartbeatManager {
8
13
  private client: ApiClient;
@@ -13,16 +18,34 @@ export class HeartbeatManager {
13
18
  private lastUnreadMessages = 0;
14
19
  private heartbeatCount = 0;
15
20
 
21
+ // Retry state
22
+ private consecutiveFailures = 0;
23
+ private backoffMs = 1000; // Start at 1 second
24
+ private readonly maxBackoffMs = 30000; // Cap at 30 seconds
25
+ private readonly failuresBeforeReconnect = 3;
26
+ private reconnectCallback?: ReconnectCallback;
27
+ private isReconnecting = false;
28
+
16
29
  constructor(client: ApiClient) {
17
30
  this.client = client;
18
31
  }
19
32
 
33
+ /**
34
+ * Set callback for reconnection attempts
35
+ */
36
+ setReconnectCallback(callback: ReconnectCallback): void {
37
+ this.reconnectCallback = callback;
38
+ }
39
+
20
40
  start(agentId: string): void {
21
41
  this.stop(); // Clear any existing interval
22
42
  this.agentId = agentId;
23
43
  this.heartbeatCount = 0;
24
44
  this.lastPendingTasks = 0;
25
45
  this.lastUnreadMessages = 0;
46
+ this.consecutiveFailures = 0;
47
+ this.backoffMs = 1000;
48
+ this.isReconnecting = false;
26
49
 
27
50
  // Send heartbeat every 30 seconds
28
51
  this.intervalId = setInterval(async () => {
@@ -31,12 +54,12 @@ export class HeartbeatManager {
31
54
  try {
32
55
  await this.sendHeartbeat();
33
56
  } catch (error) {
34
- console.error("Heartbeat failed:", error);
57
+ await this.handleHeartbeatFailure(error);
35
58
  }
36
59
  }, 30_000);
37
60
 
38
61
  // Send initial heartbeat
39
- this.sendHeartbeat();
62
+ this.sendHeartbeat().catch((error) => this.handleHeartbeatFailure(error));
40
63
  }
41
64
 
42
65
  stop(): void {
@@ -57,42 +80,102 @@ export class HeartbeatManager {
57
80
 
58
81
  setStatus(status: "online" | "busy"): void {
59
82
  this.status = status;
60
- this.sendHeartbeat();
83
+ this.sendHeartbeat().catch((error) => this.handleHeartbeatFailure(error));
61
84
  }
62
85
 
63
86
  private async sendHeartbeat(): Promise<void> {
64
87
  if (!this.agentId) return;
65
88
 
66
- try {
67
- const response = await this.client.heartbeat(this.agentId, this.status);
68
- this.heartbeatCount++;
89
+ const response = await this.client.heartbeat(this.agentId, this.status);
69
90
 
70
- const { pending_tasks_count, unread_messages_count } = response;
91
+ // Success - reset failure state
92
+ this.consecutiveFailures = 0;
93
+ this.backoffMs = 1000;
94
+ this.heartbeatCount++;
71
95
 
72
- // Log if there are new pending tasks
73
- if (pending_tasks_count > 0 && pending_tasks_count !== this.lastPendingTasks) {
74
- console.error(`\nšŸ”” [AgentHub] You have ${pending_tasks_count} pending task(s) waiting! Use get_pending_tasks to view them.\n`);
75
- }
96
+ const { pending_tasks_count, unread_messages_count } = response;
76
97
 
77
- // Log if there are new unread messages
78
- if (unread_messages_count > 0 && unread_messages_count !== this.lastUnreadMessages) {
79
- console.error(`\nšŸ“¬ [AgentHub] You have ${unread_messages_count} unread message(s)! Use check_inbox to read them.\n`);
98
+ // Log if there are new pending tasks
99
+ if (pending_tasks_count > 0 && pending_tasks_count !== this.lastPendingTasks) {
100
+ console.error(`\nšŸ”” [AgentHub] You have ${pending_tasks_count} pending task(s) waiting! Use get_pending_tasks to view them.\n`);
101
+ }
102
+
103
+ // Log if there are new unread messages
104
+ if (unread_messages_count > 0 && unread_messages_count !== this.lastUnreadMessages) {
105
+ console.error(`\nšŸ“¬ [AgentHub] You have ${unread_messages_count} unread message(s)! Use check_inbox to read them.\n`);
106
+ }
107
+
108
+ // Periodic reminder every 5 heartbeats (2.5 minutes) if there are pending items
109
+ if (this.heartbeatCount % 5 === 0) {
110
+ if (pending_tasks_count > 0 || unread_messages_count > 0) {
111
+ const parts = [];
112
+ if (pending_tasks_count > 0) parts.push(`${pending_tasks_count} pending task(s)`);
113
+ if (unread_messages_count > 0) parts.push(`${unread_messages_count} unread message(s)`);
114
+ console.error(`\nā° [AgentHub] Reminder: You have ${parts.join(" and ")}.\n`);
80
115
  }
116
+ }
117
+
118
+ this.lastPendingTasks = pending_tasks_count;
119
+ this.lastUnreadMessages = unread_messages_count;
120
+ }
121
+
122
+ private async handleHeartbeatFailure(error: unknown): Promise<void> {
123
+ this.consecutiveFailures++;
124
+ console.error(`[AgentHub] Heartbeat failed (attempt ${this.consecutiveFailures}):`, error);
81
125
 
82
- // Periodic reminder every 5 heartbeats (2.5 minutes) if there are pending items
83
- if (this.heartbeatCount % 5 === 0) {
84
- if (pending_tasks_count > 0 || unread_messages_count > 0) {
85
- const parts = [];
86
- if (pending_tasks_count > 0) parts.push(`${pending_tasks_count} pending task(s)`);
87
- if (unread_messages_count > 0) parts.push(`${unread_messages_count} unread message(s)`);
88
- console.error(`\nā° [AgentHub] Reminder: You have ${parts.join(" and ")}.\n`);
89
- }
126
+ // Check if we should attempt full reconnection
127
+ if (this.consecutiveFailures >= this.failuresBeforeReconnect && !this.isReconnecting) {
128
+ await this.attemptReconnection();
129
+ } else {
130
+ // Schedule retry with exponential backoff
131
+ this.scheduleRetry();
132
+ }
133
+ }
134
+
135
+ private scheduleRetry(): void {
136
+ console.error(`[AgentHub] Retrying heartbeat in ${this.backoffMs}ms...`);
137
+
138
+ setTimeout(async () => {
139
+ if (!this.agentId) return;
140
+
141
+ try {
142
+ await this.sendHeartbeat();
143
+ console.error("[AgentHub] Heartbeat recovered!");
144
+ } catch (error) {
145
+ await this.handleHeartbeatFailure(error);
90
146
  }
147
+ }, this.backoffMs);
148
+
149
+ // Increase backoff for next failure (exponential with cap)
150
+ this.backoffMs = Math.min(this.backoffMs * 2, this.maxBackoffMs);
151
+ }
152
+
153
+ private async attemptReconnection(): Promise<void> {
154
+ if (!this.reconnectCallback) {
155
+ console.error("[AgentHub] No reconnect callback configured, continuing retries...");
156
+ this.scheduleRetry();
157
+ return;
158
+ }
159
+
160
+ this.isReconnecting = true;
161
+ console.error("[AgentHub] Attempting full reconnection...");
91
162
 
92
- this.lastPendingTasks = pending_tasks_count;
93
- this.lastUnreadMessages = unread_messages_count;
163
+ try {
164
+ const state = await this.reconnectCallback();
165
+ if (state) {
166
+ this.agentId = state.agent_id;
167
+ this.consecutiveFailures = 0;
168
+ this.backoffMs = 1000;
169
+ this.isReconnecting = false;
170
+ console.error(`[AgentHub] Reconnected successfully as ${state.agent_id}`);
171
+ } else {
172
+ throw new Error("No state available for reconnection");
173
+ }
94
174
  } catch (error) {
95
- console.error("Heartbeat failed:", error);
175
+ console.error("[AgentHub] Reconnection failed:", error);
176
+ this.isReconnecting = false;
177
+ this.consecutiveFailures = 0; // Reset to try again after more failures
178
+ this.scheduleRetry();
96
179
  }
97
180
  }
98
181
 
package/src/index.ts CHANGED
@@ -10,11 +10,14 @@ import {
10
10
  import { ApiClient } from "./client.js";
11
11
  import { registerTools, handleToolCall } from "./tools/index.js";
12
12
  import { HeartbeatManager } from "./heartbeat.js";
13
+ import { WebSocketClient } from "./websocket.js";
14
+ import * as state from "./state.js";
13
15
 
14
16
  // Environment configuration
15
- const AGENTHUB_URL = process.env.AGENTHUB_URL || "http://localhost:8787";
17
+ const AGENTHUB_URL = process.env.AGENTHUB_URL || "https://agenthub.contetial.com";
16
18
  const AGENTHUB_API_KEY = process.env.AGENTHUB_API_KEY || "";
17
19
  const AGENTHUB_AGENT_ID = process.env.AGENTHUB_AGENT_ID || "";
20
+ const AGENTHUB_AUTO_RECONNECT = process.env.AGENTHUB_AUTO_RECONNECT !== "false"; // Enabled by default
18
21
 
19
22
  // Validate configuration
20
23
  if (!AGENTHUB_API_KEY) {
@@ -28,14 +31,61 @@ const client = new ApiClient(AGENTHUB_URL, AGENTHUB_API_KEY);
28
31
  // Initialize heartbeat manager
29
32
  const heartbeat = new HeartbeatManager(client);
30
33
 
31
- // Track current agent ID
34
+ // Initialize WebSocket client for push notifications
35
+ const wsClient = new WebSocketClient(AGENTHUB_URL, AGENTHUB_API_KEY);
36
+
37
+ // Track current agent ID and working directory
32
38
  let currentAgentId = AGENTHUB_AGENT_ID;
39
+ const workingDir = process.cwd();
40
+
41
+ /**
42
+ * Attempt to reconnect using stored state
43
+ */
44
+ async function attemptReconnect(): Promise<state.AgentState | null> {
45
+ const existingState = state.loadState(workingDir);
46
+ if (!existingState) {
47
+ return null;
48
+ }
49
+
50
+ const owner = state.getCurrentOwner();
51
+ if (existingState.owner !== owner) {
52
+ console.error("[AgentHub] State file belongs to different user, skipping auto-reconnect");
53
+ return null;
54
+ }
55
+
56
+ try {
57
+ const result = await client.reconnectAgent(
58
+ existingState.agent_id,
59
+ existingState.token,
60
+ owner,
61
+ undefined, // model - keep existing
62
+ existingState.name
63
+ );
64
+
65
+ if (result.reconnected) {
66
+ currentAgentId = result.agent_id;
67
+ // Update state with any changes
68
+ state.saveState(workingDir, {
69
+ ...existingState,
70
+ name: result.name,
71
+ });
72
+ return existingState;
73
+ }
74
+ } catch (error) {
75
+ console.error("[AgentHub] Reconnect attempt failed:", error);
76
+ }
77
+
78
+ return null;
79
+ }
80
+
81
+ // Set up reconnect callback for heartbeat manager
82
+ heartbeat.setReconnectCallback(attemptReconnect);
33
83
 
34
84
  // Create MCP server
35
85
  const server = new Server(
36
86
  {
37
87
  name: "agenthub",
38
- version: "1.1.4",
88
+ version: "1.2.0",
39
89
  },
40
90
  {
41
91
  capabilities: {
@@ -61,9 +111,14 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
61
111
  setCurrentAgentId: (id: string) => {
62
112
  currentAgentId = id;
63
113
  heartbeat.start(id);
114
+ wsClient.connect(id);
115
+ },
116
+ stopHeartbeat: () => {
117
+ heartbeat.stop();
118
+ wsClient.close();
64
119
  },
65
- stopHeartbeat: () => heartbeat.stop(),
66
120
  getWorkingDir: () => process.cwd(),
121
+ getPushedItems: () => wsClient.getPushedItems(),
67
122
  });
68
123
 
69
124
  return {
@@ -91,11 +146,13 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
91
146
  // Graceful shutdown
92
147
  process.on("SIGINT", () => {
93
148
  heartbeat.stop();
149
+ wsClient.close();
94
150
  process.exit(0);
95
151
  });
96
152
 
97
153
  process.on("SIGTERM", () => {
98
154
  heartbeat.stop();
155
+ wsClient.close();
99
156
  process.exit(0);
100
157
  });
101
158
 
@@ -105,15 +162,31 @@ async function main() {
105
162
  await server.connect(transport);
106
163
  console.error("AgentHub MCP server running");
107
164
 
108
- // Auto-register if agent ID is provided
165
+ // Auto-register if agent ID is provided via environment
109
166
  if (AGENTHUB_AGENT_ID) {
110
167
  try {
111
168
  await client.registerAgent(AGENTHUB_AGENT_ID);
112
169
  heartbeat.start(AGENTHUB_AGENT_ID);
170
+ wsClient.connect(AGENTHUB_AGENT_ID);
113
171
  console.error(`Auto-registered as agent: ${AGENTHUB_AGENT_ID}`);
114
172
  } catch (error) {
115
173
  console.error(`Failed to auto-register: ${error}`);
116
174
  }
175
+ } else if (AGENTHUB_AUTO_RECONNECT) {
176
+ // Try to auto-reconnect from stored state
177
+ const existingState = state.loadState(workingDir);
178
+ if (existingState) {
179
+ console.error(`[AgentHub] Found existing state for agent: ${existingState.agent_id}`);
180
+ const reconnected = await attemptReconnect();
181
+ if (reconnected) {
182
+ heartbeat.start(reconnected.agent_id);
183
+ wsClient.connect(reconnected.agent_id);
184
+ const displayName = reconnected.name || reconnected.agent_id;
185
+ console.error(`[AgentHub] Auto-reconnected as: ${displayName}`);
186
+ } else {
187
+ console.error("[AgentHub] Auto-reconnect failed, manual registration required");
188
+ }
189
+ }
117
190
  }
118
191
  }
119
192
 
package/src/state.ts CHANGED
@@ -20,6 +20,7 @@ const PBKDF2_ITERATIONS = 100000;
20
20
 
21
21
  export interface AgentState {
22
22
  agent_id: string;
23
+ name?: string;
23
24
  owner: string;
24
25
  token: string;
25
26
  last_task?: string;