feat: add peer stats reporting (messages, tool calls, uptime, errors)

Peers self-report resource usage via set_stats; stats visible in
list_peers responses and the new mesh_stats MCP tool. CLI auto-reports
every 60s and tracks messagesIn/Out, toolCalls, uptime, and errors.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Alejandro Gutiérrez
2026-04-07 23:52:26 +01:00
parent fe9285351b
commit b3b9972e60
5 changed files with 599 additions and 6 deletions

View File

@@ -264,6 +264,12 @@ Your message mode is "${messageMode}".
server.setRequestHandler(CallToolRequestSchema, async (req) => {
const { name, arguments: args } = req.params;
// Track tool call count across all connected clients
for (const c of allClients()) {
c.incrementToolCalls();
}
if (config.meshes.length === 0) {
return text(
"No meshes joined. Run `claudemesh join https://claudemesh.com/join/<token>` first.",
@@ -913,6 +919,26 @@ Your message mode is "${messageMode}".
return text(lines.join("\n"));
}
case "mesh_stats": {
const clients = allClients();
if (clients.length === 0) return text("mesh_stats: no joined meshes", true);
const sections: string[] = [];
for (const c of clients) {
const peers = await c.listPeers();
const header = `## ${c.meshSlug}`;
const rows = peers.map((p) => {
const s = p.stats;
if (!s) return `| ${p.displayName} | - | - | - | - | - |`;
const up = s.uptime != null ? `${Math.floor(s.uptime / 60)}m` : "-";
return `| ${p.displayName} | ${s.messagesIn ?? 0} | ${s.messagesOut ?? 0} | ${s.toolCalls ?? 0} | ${up} | ${s.errors ?? 0} |`;
});
sections.push(
`${header}\n| Peer | Msgs In | Msgs Out | Tool Calls | Uptime | Errors |\n|------|---------|----------|------------|--------|--------|\n${rows.join("\n")}`,
);
}
return text(sections.join("\n\n"));
}
case "ping_mesh": {
const { priorities: pingPriorities } = (args ?? {}) as { priorities?: string[] };
const toTest = (pingPriorities ?? ["now", "next"]) as Priority[];

View File

@@ -609,6 +609,14 @@ export const TOOLS: Tool[] = [
inputSchema: { type: "object", properties: {} },
},
// --- Stats ---
{
name: "mesh_stats",
description:
"View resource usage stats for all peers: messages sent/received, tool calls, uptime, errors.",
inputSchema: { type: "object", properties: {} },
},
// --- MCP Proxy ---
{
name: "mesh_mcp_register",
@@ -669,6 +677,42 @@ export const TOOLS: Tool[] = [
},
},
// --- Simulation clock tools ---
{
name: "mesh_set_clock",
description:
"Set the simulation clock speed. x1 = real-time, x10 = 10x faster, x100 = 100x. Peers receive heartbeat ticks at the simulated rate.",
inputSchema: {
type: "object",
properties: {
speed: {
type: "number",
description: "Speed multiplier (1-100). x1 = tick every 60s, x10 = tick every 6s, x100 = tick every 600ms.",
},
},
required: ["speed"],
},
},
{
name: "mesh_pause_clock",
description:
"Pause the simulation clock. Ticks stop until resumed.",
inputSchema: { type: "object", properties: {} },
},
{
name: "mesh_resume_clock",
description:
"Resume a paused simulation clock.",
inputSchema: { type: "object", properties: {} },
},
{
name: "mesh_clock",
description:
"Get current simulation clock status: speed, tick count, simulated time.",
inputSchema: { type: "object", properties: {} },
},
// --- Diagnostics ---
{
name: "ping_mesh",