feat: make MCP server registrations persistent across peer disconnects
Persistent MCP servers (opt-in via `persistent: true`) survive host disconnects — they appear as offline in mcp_list and auto-restore when the host reconnects. Ephemeral servers (default) still clean up on disconnect. Offline servers return a clear error on mcp_call with time-since-disconnect info. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -196,6 +196,14 @@ interface McpRegisteredServer {
|
|||||||
/** Keyed by "meshId:serverName" */
|
/** Keyed by "meshId:serverName" */
|
||||||
const mcpRegistry = new Map<string, McpRegisteredServer>();
|
const mcpRegistry = new Map<string, McpRegisteredServer>();
|
||||||
|
|
||||||
|
/** Human-readable relative time string from an ISO timestamp. */
|
||||||
|
function relativeTimeStr(iso: string): string {
|
||||||
|
const ms = Date.now() - new Date(iso).getTime();
|
||||||
|
if (ms < 60_000) return `${Math.round(ms / 1000)}s ago`;
|
||||||
|
if (ms < 3_600_000) return `${Math.round(ms / 60_000)}m ago`;
|
||||||
|
return `${Math.round(ms / 3_600_000)}h ago`;
|
||||||
|
}
|
||||||
|
|
||||||
/** Pending MCP call forwards: callId → { resolve, timer } */
|
/** Pending MCP call forwards: callId → { resolve, timer } */
|
||||||
const mcpCallResolvers = new Map<string, {
|
const mcpCallResolvers = new Map<string, {
|
||||||
resolve: (result: { result?: unknown; error?: string }) => void;
|
resolve: (result: { result?: unknown; error?: string }) => void;
|
||||||
@@ -1236,6 +1244,34 @@ function handleConnection(ws: WebSocket): void {
|
|||||||
if (peer.meshId !== joinedConn.meshId) continue;
|
if (peer.meshId !== joinedConn.meshId) continue;
|
||||||
sendToPeer(pid, joinMsg);
|
sendToPeer(pid, joinMsg);
|
||||||
}
|
}
|
||||||
|
// Restore persistent MCP servers owned by this member
|
||||||
|
for (const [, entry] of mcpRegistry) {
|
||||||
|
if (entry.memberId === joinedConn.memberId && entry.meshId === joinedConn.meshId && !entry.online) {
|
||||||
|
entry.online = true;
|
||||||
|
entry.presenceId = presenceId;
|
||||||
|
entry.offlineSince = undefined;
|
||||||
|
entry.hostedByName = joinedConn.displayName;
|
||||||
|
// Broadcast restoration
|
||||||
|
const restoreMsg: WSPushMessage = {
|
||||||
|
type: "push",
|
||||||
|
subtype: "system",
|
||||||
|
event: "mcp_restored",
|
||||||
|
eventData: { serverName: entry.serverName, hostedBy: joinedConn.displayName },
|
||||||
|
messageId: crypto.randomUUID(),
|
||||||
|
meshId: joinedConn.meshId,
|
||||||
|
senderPubkey: "system",
|
||||||
|
priority: "low",
|
||||||
|
nonce: "",
|
||||||
|
ciphertext: "",
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
for (const [pid2, peer2] of connections) {
|
||||||
|
if (peer2.meshId !== joinedConn.meshId) continue;
|
||||||
|
sendToPeer(pid2, restoreMsg);
|
||||||
|
}
|
||||||
|
log.info("mcp_restored", { server: entry.serverName, member: joinedConn.displayName });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -2625,7 +2661,7 @@ function handleConnection(ws: WebSocket): void {
|
|||||||
description: mr.description,
|
description: mr.description,
|
||||||
tools: mr.tools,
|
tools: mr.tools,
|
||||||
hostedByName: conn.displayName,
|
hostedByName: conn.displayName,
|
||||||
persistent: !!(mr as any).persistent,
|
persistent: !!mr.persistent,
|
||||||
online: true,
|
online: true,
|
||||||
memberId: conn.memberId,
|
memberId: conn.memberId,
|
||||||
registeredAt: new Date().toISOString(),
|
registeredAt: new Date().toISOString(),
|
||||||
@@ -2700,6 +2736,8 @@ function handleConnection(ws: WebSocket): void {
|
|||||||
description: string;
|
description: string;
|
||||||
hostedBy: string;
|
hostedBy: string;
|
||||||
tools: Array<{ name: string; description: string }>;
|
tools: Array<{ name: string; description: string }>;
|
||||||
|
online: boolean;
|
||||||
|
offlineSince?: string;
|
||||||
}> = [];
|
}> = [];
|
||||||
for (const [, entry] of mcpRegistry) {
|
for (const [, entry] of mcpRegistry) {
|
||||||
if (entry.meshId !== conn.meshId) continue;
|
if (entry.meshId !== conn.meshId) continue;
|
||||||
@@ -2708,6 +2746,8 @@ function handleConnection(ws: WebSocket): void {
|
|||||||
description: entry.description,
|
description: entry.description,
|
||||||
hostedBy: entry.hostedByName,
|
hostedBy: entry.hostedByName,
|
||||||
tools: entry.tools.map((t) => ({ name: t.name, description: t.description })),
|
tools: entry.tools.map((t) => ({ name: t.name, description: t.description })),
|
||||||
|
online: entry.online,
|
||||||
|
...(entry.offlineSince ? { offlineSince: entry.offlineSince } : {}),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
sendToPeer(presenceId, {
|
sendToPeer(presenceId, {
|
||||||
@@ -2733,10 +2773,28 @@ function handleConnection(ws: WebSocket): void {
|
|||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// Check if server is offline (persistent but host disconnected)
|
||||||
|
if (!server.online) {
|
||||||
|
const ago = server.offlineSince
|
||||||
|
? ` who disconnected ${relativeTimeStr(server.offlineSince)}`
|
||||||
|
: "";
|
||||||
|
sendToPeer(presenceId, {
|
||||||
|
type: "mcp_call_result",
|
||||||
|
error: `Server '${mc.serverName}' is offline — hosted by ${server.hostedByName}${ago}. It will restore when they reconnect.`,
|
||||||
|
...(_reqId ? { _reqId } : {}),
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
// Check hosting peer is still connected
|
// Check hosting peer is still connected
|
||||||
const hostConn = connections.get(server.presenceId);
|
const hostConn = connections.get(server.presenceId);
|
||||||
if (!hostConn) {
|
if (!hostConn) {
|
||||||
mcpRegistry.delete(callKey);
|
if (server.persistent) {
|
||||||
|
server.online = false;
|
||||||
|
server.offlineSince = new Date().toISOString();
|
||||||
|
server.presenceId = "";
|
||||||
|
} else {
|
||||||
|
mcpRegistry.delete(callKey);
|
||||||
|
}
|
||||||
sendToPeer(presenceId, {
|
sendToPeer(presenceId, {
|
||||||
type: "mcp_call_result",
|
type: "mcp_call_result",
|
||||||
error: `MCP server "${mc.serverName}" host disconnected`,
|
error: `MCP server "${mc.serverName}" host disconnected`,
|
||||||
|
|||||||
@@ -737,6 +737,7 @@ export interface WSMcpRegisterMessage {
|
|||||||
serverName: string;
|
serverName: string;
|
||||||
description: string;
|
description: string;
|
||||||
tools: Array<{ name: string; description: string; inputSchema: Record<string, unknown> }>;
|
tools: Array<{ name: string; description: string; inputSchema: Record<string, unknown> }>;
|
||||||
|
persistent?: boolean;
|
||||||
_reqId?: string;
|
_reqId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -787,6 +788,8 @@ export interface WSMcpListResultMessage {
|
|||||||
description: string;
|
description: string;
|
||||||
hostedBy: string;
|
hostedBy: string;
|
||||||
tools: Array<{ name: string; description: string }>;
|
tools: Array<{ name: string; description: string }>;
|
||||||
|
online: boolean;
|
||||||
|
offlineSince?: string;
|
||||||
}>;
|
}>;
|
||||||
_reqId?: string;
|
_reqId?: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1144,27 +1144,32 @@ Your message mode is "${messageMode}".
|
|||||||
|
|
||||||
// --- MCP Proxy ---
|
// --- MCP Proxy ---
|
||||||
case "mesh_mcp_register": {
|
case "mesh_mcp_register": {
|
||||||
const { server_name, description, tools: regTools } = (args ?? {}) as {
|
const { server_name, description, tools: regTools, persistent: regPersistent } = (args ?? {}) as {
|
||||||
server_name?: string;
|
server_name?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
tools?: Array<{ name: string; description: string; inputSchema: Record<string, unknown> }>;
|
tools?: Array<{ name: string; description: string; inputSchema: Record<string, unknown> }>;
|
||||||
|
persistent?: boolean;
|
||||||
};
|
};
|
||||||
if (!server_name || !description || !regTools?.length)
|
if (!server_name || !description || !regTools?.length)
|
||||||
return text("mesh_mcp_register: `server_name`, `description`, and `tools` required", true);
|
return text("mesh_mcp_register: `server_name`, `description`, and `tools` required", true);
|
||||||
const client = allClients()[0];
|
const client = allClients()[0];
|
||||||
if (!client) return text("mesh_mcp_register: not connected", true);
|
if (!client) return text("mesh_mcp_register: not connected", true);
|
||||||
const result = await client.mcpRegister(server_name, description, regTools);
|
const result = await client.mcpRegister(server_name, description, regTools, regPersistent);
|
||||||
if (!result) return text("mesh_mcp_register: broker did not acknowledge", true);
|
if (!result) return text("mesh_mcp_register: broker did not acknowledge", true);
|
||||||
return text(`Registered MCP server "${result.serverName}" with ${result.toolCount} tool(s). Other peers can now call its tools via mesh_tool_call.`);
|
const persistLabel = regPersistent ? " (persistent — survives disconnect)" : "";
|
||||||
|
return text(`Registered MCP server "${result.serverName}" with ${result.toolCount} tool(s)${persistLabel}. Other peers can now call its tools via mesh_tool_call.`);
|
||||||
}
|
}
|
||||||
case "mesh_mcp_list": {
|
case "mesh_mcp_list": {
|
||||||
const client = allClients()[0];
|
const client = allClients()[0];
|
||||||
if (!client) return text("mesh_mcp_list: not connected", true);
|
if (!client) return text("mesh_mcp_list: not connected", true);
|
||||||
const servers = await client.mcpList();
|
const servers = await client.mcpList();
|
||||||
if (servers.length === 0) return text("No MCP servers registered in the mesh.");
|
if (servers.length === 0) return text("No MCP servers registered in the mesh.");
|
||||||
const lines = servers.map((s) => {
|
const lines = servers.map((s: any) => {
|
||||||
const toolList = s.tools.map((t) => ` - **${t.name}**: ${t.description}`).join("\n");
|
const toolList = s.tools.map((t: any) => ` - **${t.name}**: ${t.description}`).join("\n");
|
||||||
return `- **${s.name}** (hosted by ${s.hostedBy}): ${s.description}\n${toolList}`;
|
const status = s.online === false
|
||||||
|
? ` [OFFLINE${s.offlineSince ? ` since ${s.offlineSince}` : ""}]`
|
||||||
|
: "";
|
||||||
|
return `- **${s.name}** (hosted by ${s.hostedBy})${status}: ${s.description}\n${toolList}`;
|
||||||
});
|
});
|
||||||
return text(`${servers.length} MCP server(s) in mesh:\n${lines.join("\n")}`);
|
return text(`${servers.length} MCP server(s) in mesh:\n${lines.join("\n")}`);
|
||||||
}
|
}
|
||||||
@@ -1383,6 +1388,8 @@ Your message mode is "${messageMode}".
|
|||||||
content = `[system] New MCP server available: "${data.serverName}" (hosted by ${data.hostedBy}). Tools: ${tools}. Use mesh_tool_call to invoke.`;
|
content = `[system] New MCP server available: "${data.serverName}" (hosted by ${data.hostedBy}). Tools: ${tools}. Use mesh_tool_call to invoke.`;
|
||||||
} else if (eventName === "mcp_unregistered") {
|
} else if (eventName === "mcp_unregistered") {
|
||||||
content = `[system] MCP server "${data.serverName}" removed (was hosted by ${data.hostedBy})`;
|
content = `[system] MCP server "${data.serverName}" removed (was hosted by ${data.hostedBy})`;
|
||||||
|
} else if (eventName === "mcp_restored") {
|
||||||
|
content = `[system] MCP server "${data.serverName}" is back online (hosted by ${data.hostedBy})`;
|
||||||
} else {
|
} else {
|
||||||
content = `[system] ${eventName}: ${JSON.stringify(data)}`;
|
content = `[system] ${eventName}: ${JSON.stringify(data)}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -682,6 +682,10 @@ export const TOOLS: Tool[] = [
|
|||||||
},
|
},
|
||||||
description: "Tool definitions to expose",
|
description: "Tool definitions to expose",
|
||||||
},
|
},
|
||||||
|
persistent: {
|
||||||
|
type: "boolean",
|
||||||
|
description: "If true, registration survives peer disconnect. Other peers see it as 'offline' until you reconnect. Default: false",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
required: ["server_name", "description", "tools"],
|
required: ["server_name", "description", "tools"],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -931,6 +931,7 @@ export class BrokerClient {
|
|||||||
serverName: string,
|
serverName: string,
|
||||||
description: string,
|
description: string,
|
||||||
tools: Array<{ name: string; description: string; inputSchema: Record<string, unknown> }>,
|
tools: Array<{ name: string; description: string; inputSchema: Record<string, unknown> }>,
|
||||||
|
persistent?: boolean,
|
||||||
): Promise<{ serverName: string; toolCount: number } | null> {
|
): Promise<{ serverName: string; toolCount: number } | null> {
|
||||||
if (!this.ws || this.ws.readyState !== this.ws.OPEN) return null;
|
if (!this.ws || this.ws.readyState !== this.ws.OPEN) return null;
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
@@ -938,7 +939,7 @@ export class BrokerClient {
|
|||||||
this.mcpRegisterResolvers.set(reqId, { resolve, timer: setTimeout(() => {
|
this.mcpRegisterResolvers.set(reqId, { resolve, timer: setTimeout(() => {
|
||||||
if (this.mcpRegisterResolvers.delete(reqId)) resolve(null);
|
if (this.mcpRegisterResolvers.delete(reqId)) resolve(null);
|
||||||
}, 5_000) });
|
}, 5_000) });
|
||||||
this.ws!.send(JSON.stringify({ type: "mcp_register", serverName, description, tools, _reqId: reqId }));
|
this.ws!.send(JSON.stringify({ type: "mcp_register", serverName, description, tools, ...(persistent ? { persistent: true } : {}), _reqId: reqId }));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user