@sym-bot/mesh-channel 0.1.19 → 0.1.21
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/.claude-plugin/plugin.json +33 -33
- package/.mcp.json +14 -14
- package/CHANGELOG.md +218 -170
- package/README.md +192 -137
- package/SECURITY.md +89 -89
- package/bin/install.js +350 -199
- package/package.json +32 -32
- package/server.js +325 -325
package/server.js
CHANGED
|
@@ -1,325 +1,325 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
// Subcommand dispatch: `sym-mesh-channel init` runs the installer.
|
|
5
|
-
if (process.argv[2] === 'init') {
|
|
6
|
-
require('./bin/install.js');
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* sym-mesh-channel — MCP server that makes Claude Code a peer node on the SYM mesh.
|
|
12
|
-
*
|
|
13
|
-
* Architecture (MMP Section 13.9: Local Event Interface):
|
|
14
|
-
* SymNode (own identity, own SVAF field weights) → relay → mesh
|
|
15
|
-
* MCP channel notifications → Claude Code (real-time push)
|
|
16
|
-
* MCP tools → SymNode methods (send, observe, recall)
|
|
17
|
-
*
|
|
18
|
-
* This is a PEER NODE, not a client of the daemon. It has its own identity,
|
|
19
|
-
* its own relay connection, and its own SVAF evaluation with engineering-domain
|
|
20
|
-
* field weights. Per MMP Section 3: every participant is a peer.
|
|
21
|
-
*
|
|
22
|
-
* Copyright (c) 2026 SYM.BOT Ltd. Apache 2.0 License.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
|
|
26
|
-
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
|
|
27
|
-
const {
|
|
28
|
-
CallToolRequestSchema,
|
|
29
|
-
ListToolsRequestSchema,
|
|
30
|
-
} = require('@modelcontextprotocol/sdk/types.js');
|
|
31
|
-
const { SymNode } = require('@sym-bot/sym');
|
|
32
|
-
|
|
33
|
-
// ── Engineering-domain field weights (SVAF α_f) ──────────────
|
|
34
|
-
|
|
35
|
-
const FIELD_WEIGHTS = {
|
|
36
|
-
focus: 2.0, // code, architecture, technical decisions
|
|
37
|
-
issue: 2.0, // bugs, blockers, technical debt
|
|
38
|
-
intent: 1.5, // what needs building
|
|
39
|
-
motivation: 1.0, // why it matters
|
|
40
|
-
commitment: 1.5, // deadlines, dependencies
|
|
41
|
-
perspective: 0.5, // viewpoint — low for engineering
|
|
42
|
-
mood: 0.8, // user fatigue affects code quality
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// ── SymNode — full peer on the mesh ──────────────────────────
|
|
46
|
-
|
|
47
|
-
// Default: hostname-based identity, unique per machine. The old default
|
|
48
|
-
// ('claude-code-mac') caused ghost-peer bugs when another machine ran
|
|
49
|
-
// without SYM_NODE_NAME set — both machines claimed the same name with
|
|
50
|
-
// different nodeIds, creating phantom peers that absorbed messages.
|
|
51
|
-
const NODE_NAME = process.env.SYM_NODE_NAME || `claude-${require('os').hostname().toLowerCase().replace(/[^a-z0-9-]/g, '-')}`;
|
|
52
|
-
|
|
53
|
-
const node = new SymNode({
|
|
54
|
-
name: NODE_NAME,
|
|
55
|
-
cognitiveProfile: 'Engineering node. Code, architecture, debugging, technical decisions.',
|
|
56
|
-
svafFieldWeights: FIELD_WEIGHTS,
|
|
57
|
-
svafFreshnessSeconds: 7200, // 2hr — session-length context
|
|
58
|
-
relay: process.env.SYM_RELAY_URL || null,
|
|
59
|
-
relayToken: process.env.SYM_RELAY_TOKEN || null,
|
|
60
|
-
silent: true,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Identity collision (added in @sym-bot/sym 0.3.68): the relay told us
|
|
64
|
-
// another process is holding our nodeId. Don't try to reconnect — that
|
|
65
|
-
// caused the peer-flap loop documented in v0.1.2/v0.1.3 commit messages.
|
|
66
|
-
// Exit so Claude Code can decide whether to respawn (with the freshness
|
|
67
|
-
// window now elapsed) or surface the failure to the user.
|
|
68
|
-
node.on('identity-collision', (info) => {
|
|
69
|
-
process.stderr.write(
|
|
70
|
-
`sym-mesh-channel: identity collision on relay — another process is holding ` +
|
|
71
|
-
`nodeId=${info.nodeId} name=${info.name}. Exiting.\n`
|
|
72
|
-
);
|
|
73
|
-
process.exit(2);
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// ── MCP Server ───────────────────────────────────────────────
|
|
77
|
-
|
|
78
|
-
const mcp = new Server(
|
|
79
|
-
{ name: 'sym-mesh', version: '0.1.0' },
|
|
80
|
-
{
|
|
81
|
-
capabilities: {
|
|
82
|
-
tools: {},
|
|
83
|
-
experimental: { 'claude/channel': {} },
|
|
84
|
-
},
|
|
85
|
-
instructions:
|
|
86
|
-
`You are a peer node on the SYM mesh (identity: ${NODE_NAME}). ` +
|
|
87
|
-
'Mesh events arrive as <channel> notifications in real-time. ' +
|
|
88
|
-
'When you see a message or CMB from another node, respond via the sym_send tool if actionable. ' +
|
|
89
|
-
'Share observations about the user\'s state via sym_observe. ' +
|
|
90
|
-
'Search mesh memory via sym_recall.',
|
|
91
|
-
},
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
// ── Tools ────────────────────────────────────────────────────
|
|
95
|
-
|
|
96
|
-
mcp.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
97
|
-
tools: [
|
|
98
|
-
{
|
|
99
|
-
name: 'sym_send',
|
|
100
|
-
description: 'Send a message to all mesh peers. Stored as a persistent CMB and broadcast via relay.',
|
|
101
|
-
inputSchema: {
|
|
102
|
-
type: 'object',
|
|
103
|
-
properties: { message: { type: 'string', description: 'Message to broadcast' } },
|
|
104
|
-
required: ['message'],
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
name: 'sym_observe',
|
|
109
|
-
description: 'Share a structured CAT7 observation with the mesh. Extract fields from what you observe.',
|
|
110
|
-
inputSchema: {
|
|
111
|
-
type: 'object',
|
|
112
|
-
properties: {
|
|
113
|
-
focus: { type: 'string' },
|
|
114
|
-
issue: { type: 'string' },
|
|
115
|
-
intent: { type: 'string' },
|
|
116
|
-
motivation: { type: 'string' },
|
|
117
|
-
commitment: { type: 'string' },
|
|
118
|
-
perspective: { type: 'string' },
|
|
119
|
-
mood: {
|
|
120
|
-
type: 'object',
|
|
121
|
-
properties: {
|
|
122
|
-
text: { type: 'string' },
|
|
123
|
-
valence: { type: 'number' },
|
|
124
|
-
arousal: { type: 'number' },
|
|
125
|
-
},
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
required: ['focus'],
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
name: 'sym_recall',
|
|
133
|
-
description: 'Search mesh memory for relevant CMBs.',
|
|
134
|
-
inputSchema: {
|
|
135
|
-
type: 'object',
|
|
136
|
-
properties: { query: { type: 'string', description: 'Search query (empty for all)' } },
|
|
137
|
-
required: ['query'],
|
|
138
|
-
},
|
|
139
|
-
},
|
|
140
|
-
{
|
|
141
|
-
name: 'sym_peers',
|
|
142
|
-
description: 'List connected mesh peers.',
|
|
143
|
-
inputSchema: { type: 'object', properties: {} },
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
name: 'sym_status',
|
|
147
|
-
description: 'Get mesh node status — relay connection, peer count, memory count.',
|
|
148
|
-
inputSchema: { type: 'object', properties: {} },
|
|
149
|
-
},
|
|
150
|
-
],
|
|
151
|
-
}));
|
|
152
|
-
|
|
153
|
-
mcp.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
154
|
-
const { name, arguments: args } = request.params;
|
|
155
|
-
|
|
156
|
-
switch (name) {
|
|
157
|
-
case 'sym_send': {
|
|
158
|
-
// Direct inter-node message — broadcast as type:'message' frame only.
|
|
159
|
-
// Do NOT also persist as a CMB via node.remember(): that caused
|
|
160
|
-
// double-delivery on receivers, who saw the same payload arrive once
|
|
161
|
-
// as event_type='message' (from this broadcast) and again as
|
|
162
|
-
// event_type='cmb' (from CMB gossip replication). One tool, one job:
|
|
163
|
-
// sym_send is for ephemeral inter-node messages; sym_observe is for
|
|
164
|
-
// structured CAT7 CMBs. Hosts that want both should call both.
|
|
165
|
-
//
|
|
166
|
-
// Report the actual delivered count (the number of peer transports
|
|
167
|
-
// that successfully accepted the broadcast), not peers().length.
|
|
168
|
-
// The two can disagree when peers are in _peers but their transports
|
|
169
|
-
// are broken — counting peers().length would lie about delivery.
|
|
170
|
-
// Requires @sym-bot/sym >= 0.3.70 where send() returns the count.
|
|
171
|
-
const msg = args.message;
|
|
172
|
-
const delivered = node.send(msg);
|
|
173
|
-
return { content: [{ type: 'text', text: `Message delivered to ${delivered} peer(s).` }] };
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
case 'sym_observe': {
|
|
177
|
-
const fields = {
|
|
178
|
-
focus: args.focus || 'observation',
|
|
179
|
-
issue: args.issue || 'none',
|
|
180
|
-
intent: args.intent || 'observation',
|
|
181
|
-
motivation: args.motivation || '',
|
|
182
|
-
commitment: args.commitment || '',
|
|
183
|
-
perspective: args.perspective || NODE_NAME,
|
|
184
|
-
mood: args.mood || { text: 'neutral', valence: 0, arousal: 0 },
|
|
185
|
-
};
|
|
186
|
-
const entry = node.remember(fields);
|
|
187
|
-
return { content: [{ type: 'text', text: entry ? `Observed: ${entry.key}` : 'Duplicate — already in memory.' }] };
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
case 'sym_recall': {
|
|
191
|
-
const results = node.recall(args.query || '');
|
|
192
|
-
if (results.length === 0) {
|
|
193
|
-
return { content: [{ type: 'text', text: 'No memories found.' }] };
|
|
194
|
-
}
|
|
195
|
-
const lines = results.slice(0, 10).map(r => {
|
|
196
|
-
const focus = r.cmb?.fields?.focus?.text || r.content || '';
|
|
197
|
-
const source = r.source || r.cmb?.createdBy || 'unknown';
|
|
198
|
-
const time = r.timestamp ? new Date(r.timestamp).toLocaleString() : '';
|
|
199
|
-
return `[${source}] ${time}\n ${focus.slice(0, 150)}`;
|
|
200
|
-
});
|
|
201
|
-
return { content: [{ type: 'text', text: lines.join('\n\n') }] };
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
case 'sym_peers': {
|
|
205
|
-
const peers = node.peers();
|
|
206
|
-
if (peers.length === 0) {
|
|
207
|
-
return { content: [{ type: 'text', text: 'No peers connected.' }] };
|
|
208
|
-
}
|
|
209
|
-
const lines = peers.map(p => `${p.name} via ${p.source || 'unknown'}`);
|
|
210
|
-
return { content: [{ type: 'text', text: `${peers.length} peer(s):\n${lines.join('\n')}` }] };
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
case 'sym_status': {
|
|
214
|
-
const s = node.status();
|
|
215
|
-
return {
|
|
216
|
-
content: [{
|
|
217
|
-
type: 'text',
|
|
218
|
-
text: `Node: ${NODE_NAME} (${node.nodeId?.slice(0, 8) || '?'})\n` +
|
|
219
|
-
`Relay: ${s.relayConnected ? 'connected' : 'disconnected'}\n` +
|
|
220
|
-
`Peers: ${s.peerCount || 0}\n` +
|
|
221
|
-
`Memories: ${s.memoryCount || 0}`,
|
|
222
|
-
}],
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
default:
|
|
227
|
-
return { content: [{ type: 'text', text: `Unknown tool: ${name}` }] };
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
// ── Peer Allowlist (optional, defense-in-depth) ─────────────
|
|
232
|
-
// SYM_ALLOWED_PEERS is a comma-separated list of peer node names.
|
|
233
|
-
// When set, only CMBs and messages from listed peers are pushed to
|
|
234
|
-
// Claude's context. When empty/unset, all authenticated peers are
|
|
235
|
-
// accepted (SVAF still gates on content relevance).
|
|
236
|
-
const ALLOWED_PEERS = (process.env.SYM_ALLOWED_PEERS || '')
|
|
237
|
-
.split(',')
|
|
238
|
-
.map(s => s.trim())
|
|
239
|
-
.filter(Boolean);
|
|
240
|
-
|
|
241
|
-
function isPeerAllowed(peerName) {
|
|
242
|
-
if (ALLOWED_PEERS.length === 0) return true; // no allowlist = accept all
|
|
243
|
-
return ALLOWED_PEERS.includes(peerName);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// ── Mesh Events → Channel Notifications ──────────────────────
|
|
247
|
-
|
|
248
|
-
function pushChannel(eventType, data) {
|
|
249
|
-
try {
|
|
250
|
-
mcp.notification({
|
|
251
|
-
method: 'notifications/claude/channel',
|
|
252
|
-
params: {
|
|
253
|
-
content: typeof data === 'string' ? data : JSON.stringify(data),
|
|
254
|
-
meta: { event_type: eventType, source: 'sym-mesh' },
|
|
255
|
-
},
|
|
256
|
-
});
|
|
257
|
-
} catch {}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
node.on('cmb-accepted', (entry) => {
|
|
261
|
-
// Don't echo back our own CMBs
|
|
262
|
-
if (entry.source === NODE_NAME || entry.cmb?.createdBy === NODE_NAME) return;
|
|
263
|
-
|
|
264
|
-
const source = entry.source || entry.cmb?.createdBy || 'unknown';
|
|
265
|
-
|
|
266
|
-
// Peer allowlist gate (defense-in-depth, see SECURITY.md)
|
|
267
|
-
if (!isPeerAllowed(source)) return;
|
|
268
|
-
|
|
269
|
-
const focus = entry.cmb?.fields?.focus?.text || entry.content || '';
|
|
270
|
-
const mood = entry.cmb?.fields?.mood?.text || '';
|
|
271
|
-
pushChannel('cmb', `[${source}] ${focus}${mood && mood !== 'neutral' ? ` (mood: ${mood})` : ''}`);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
node.on('message', (from, content) => {
|
|
275
|
-
// Peer allowlist gate
|
|
276
|
-
if (!isPeerAllowed(from)) return;
|
|
277
|
-
|
|
278
|
-
pushChannel('message', `[message from ${from}] ${content}`);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
// Peer presence events are intentionally NOT pushed to Claude's context.
|
|
282
|
-
// They're high-frequency, low-signal (peers flap on relay reconnects, daemon
|
|
283
|
-
// restarts, NAT keepalive blips), and a flood will eat the context window.
|
|
284
|
-
// Use sym_peers / sym_status on demand instead. Only CMBs and direct messages
|
|
285
|
-
// are surfaced as channel notifications — those carry actual cognitive payload.
|
|
286
|
-
|
|
287
|
-
// ── Start ────────────────────────────────────────────────────
|
|
288
|
-
|
|
289
|
-
// Clean shutdown — disconnect from the relay before exiting so other peers
|
|
290
|
-
// see us leave immediately, and so a fast restart of this MCP doesn't race
|
|
291
|
-
// our own zombie connection on the relay (which would trigger the relay's
|
|
292
|
-
// duplicate-nodeId replacement path and cause peer flap loops).
|
|
293
|
-
//
|
|
294
|
-
// Idempotent: Claude Code may send SIGTERM and then SIGKILL; we want the
|
|
295
|
-
// first signal to get us cleanly off the relay even if the second one
|
|
296
|
-
// arrives before stop() resolves.
|
|
297
|
-
let shuttingDown = false;
|
|
298
|
-
async function shutdown(signal) {
|
|
299
|
-
if (shuttingDown) return;
|
|
300
|
-
shuttingDown = true;
|
|
301
|
-
try {
|
|
302
|
-
await node.stop();
|
|
303
|
-
} catch {
|
|
304
|
-
// Best effort — we're exiting anyway. Don't block on cleanup errors.
|
|
305
|
-
}
|
|
306
|
-
process.exit(0);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
|
310
|
-
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
311
|
-
process.on('SIGHUP', () => shutdown('SIGHUP'));
|
|
312
|
-
|
|
313
|
-
async function main() {
|
|
314
|
-
// Start SymNode — connects to relay as a peer
|
|
315
|
-
await node.start();
|
|
316
|
-
|
|
317
|
-
// Start MCP server — communicates with Claude Code via stdio
|
|
318
|
-
const transport = new StdioServerTransport();
|
|
319
|
-
await mcp.connect(transport);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
main().catch((err) => {
|
|
323
|
-
process.stderr.write(`sym-mesh-channel failed: ${err.message}\n`);
|
|
324
|
-
process.exit(1);
|
|
325
|
-
});
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// Subcommand dispatch: `sym-mesh-channel init` runs the installer.
|
|
5
|
+
if (process.argv[2] === 'init') {
|
|
6
|
+
require('./bin/install.js');
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* sym-mesh-channel — MCP server that makes Claude Code a peer node on the SYM mesh.
|
|
12
|
+
*
|
|
13
|
+
* Architecture (MMP Section 13.9: Local Event Interface):
|
|
14
|
+
* SymNode (own identity, own SVAF field weights) → relay → mesh
|
|
15
|
+
* MCP channel notifications → Claude Code (real-time push)
|
|
16
|
+
* MCP tools → SymNode methods (send, observe, recall)
|
|
17
|
+
*
|
|
18
|
+
* This is a PEER NODE, not a client of the daemon. It has its own identity,
|
|
19
|
+
* its own relay connection, and its own SVAF evaluation with engineering-domain
|
|
20
|
+
* field weights. Per MMP Section 3: every participant is a peer.
|
|
21
|
+
*
|
|
22
|
+
* Copyright (c) 2026 SYM.BOT Ltd. Apache 2.0 License.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
|
|
26
|
+
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
|
|
27
|
+
const {
|
|
28
|
+
CallToolRequestSchema,
|
|
29
|
+
ListToolsRequestSchema,
|
|
30
|
+
} = require('@modelcontextprotocol/sdk/types.js');
|
|
31
|
+
const { SymNode } = require('@sym-bot/sym');
|
|
32
|
+
|
|
33
|
+
// ── Engineering-domain field weights (SVAF α_f) ──────────────
|
|
34
|
+
|
|
35
|
+
const FIELD_WEIGHTS = {
|
|
36
|
+
focus: 2.0, // code, architecture, technical decisions
|
|
37
|
+
issue: 2.0, // bugs, blockers, technical debt
|
|
38
|
+
intent: 1.5, // what needs building
|
|
39
|
+
motivation: 1.0, // why it matters
|
|
40
|
+
commitment: 1.5, // deadlines, dependencies
|
|
41
|
+
perspective: 0.5, // viewpoint — low for engineering
|
|
42
|
+
mood: 0.8, // user fatigue affects code quality
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// ── SymNode — full peer on the mesh ──────────────────────────
|
|
46
|
+
|
|
47
|
+
// Default: hostname-based identity, unique per machine. The old default
|
|
48
|
+
// ('claude-code-mac') caused ghost-peer bugs when another machine ran
|
|
49
|
+
// without SYM_NODE_NAME set — both machines claimed the same name with
|
|
50
|
+
// different nodeIds, creating phantom peers that absorbed messages.
|
|
51
|
+
const NODE_NAME = process.env.SYM_NODE_NAME || `claude-${require('os').hostname().toLowerCase().replace(/[^a-z0-9-]/g, '-')}`;
|
|
52
|
+
|
|
53
|
+
const node = new SymNode({
|
|
54
|
+
name: NODE_NAME,
|
|
55
|
+
cognitiveProfile: 'Engineering node. Code, architecture, debugging, technical decisions.',
|
|
56
|
+
svafFieldWeights: FIELD_WEIGHTS,
|
|
57
|
+
svafFreshnessSeconds: 7200, // 2hr — session-length context
|
|
58
|
+
relay: process.env.SYM_RELAY_URL || null,
|
|
59
|
+
relayToken: process.env.SYM_RELAY_TOKEN || null,
|
|
60
|
+
silent: true,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Identity collision (added in @sym-bot/sym 0.3.68): the relay told us
|
|
64
|
+
// another process is holding our nodeId. Don't try to reconnect — that
|
|
65
|
+
// caused the peer-flap loop documented in v0.1.2/v0.1.3 commit messages.
|
|
66
|
+
// Exit so Claude Code can decide whether to respawn (with the freshness
|
|
67
|
+
// window now elapsed) or surface the failure to the user.
|
|
68
|
+
node.on('identity-collision', (info) => {
|
|
69
|
+
process.stderr.write(
|
|
70
|
+
`sym-mesh-channel: identity collision on relay — another process is holding ` +
|
|
71
|
+
`nodeId=${info.nodeId} name=${info.name}. Exiting.\n`
|
|
72
|
+
);
|
|
73
|
+
process.exit(2);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// ── MCP Server ───────────────────────────────────────────────
|
|
77
|
+
|
|
78
|
+
const mcp = new Server(
|
|
79
|
+
{ name: 'sym-mesh', version: '0.1.0' },
|
|
80
|
+
{
|
|
81
|
+
capabilities: {
|
|
82
|
+
tools: {},
|
|
83
|
+
experimental: { 'claude/channel': {} },
|
|
84
|
+
},
|
|
85
|
+
instructions:
|
|
86
|
+
`You are a peer node on the SYM mesh (identity: ${NODE_NAME}). ` +
|
|
87
|
+
'Mesh events arrive as <channel> notifications in real-time. ' +
|
|
88
|
+
'When you see a message or CMB from another node, respond via the sym_send tool if actionable. ' +
|
|
89
|
+
'Share observations about the user\'s state via sym_observe. ' +
|
|
90
|
+
'Search mesh memory via sym_recall.',
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// ── Tools ────────────────────────────────────────────────────
|
|
95
|
+
|
|
96
|
+
mcp.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
97
|
+
tools: [
|
|
98
|
+
{
|
|
99
|
+
name: 'sym_send',
|
|
100
|
+
description: 'Send a message to all mesh peers. Stored as a persistent CMB and broadcast via relay.',
|
|
101
|
+
inputSchema: {
|
|
102
|
+
type: 'object',
|
|
103
|
+
properties: { message: { type: 'string', description: 'Message to broadcast' } },
|
|
104
|
+
required: ['message'],
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: 'sym_observe',
|
|
109
|
+
description: 'Share a structured CAT7 observation with the mesh. Extract fields from what you observe.',
|
|
110
|
+
inputSchema: {
|
|
111
|
+
type: 'object',
|
|
112
|
+
properties: {
|
|
113
|
+
focus: { type: 'string' },
|
|
114
|
+
issue: { type: 'string' },
|
|
115
|
+
intent: { type: 'string' },
|
|
116
|
+
motivation: { type: 'string' },
|
|
117
|
+
commitment: { type: 'string' },
|
|
118
|
+
perspective: { type: 'string' },
|
|
119
|
+
mood: {
|
|
120
|
+
type: 'object',
|
|
121
|
+
properties: {
|
|
122
|
+
text: { type: 'string' },
|
|
123
|
+
valence: { type: 'number' },
|
|
124
|
+
arousal: { type: 'number' },
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
required: ['focus'],
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: 'sym_recall',
|
|
133
|
+
description: 'Search mesh memory for relevant CMBs.',
|
|
134
|
+
inputSchema: {
|
|
135
|
+
type: 'object',
|
|
136
|
+
properties: { query: { type: 'string', description: 'Search query (empty for all)' } },
|
|
137
|
+
required: ['query'],
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
name: 'sym_peers',
|
|
142
|
+
description: 'List connected mesh peers.',
|
|
143
|
+
inputSchema: { type: 'object', properties: {} },
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'sym_status',
|
|
147
|
+
description: 'Get mesh node status — relay connection, peer count, memory count.',
|
|
148
|
+
inputSchema: { type: 'object', properties: {} },
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
}));
|
|
152
|
+
|
|
153
|
+
mcp.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
154
|
+
const { name, arguments: args } = request.params;
|
|
155
|
+
|
|
156
|
+
switch (name) {
|
|
157
|
+
case 'sym_send': {
|
|
158
|
+
// Direct inter-node message — broadcast as type:'message' frame only.
|
|
159
|
+
// Do NOT also persist as a CMB via node.remember(): that caused
|
|
160
|
+
// double-delivery on receivers, who saw the same payload arrive once
|
|
161
|
+
// as event_type='message' (from this broadcast) and again as
|
|
162
|
+
// event_type='cmb' (from CMB gossip replication). One tool, one job:
|
|
163
|
+
// sym_send is for ephemeral inter-node messages; sym_observe is for
|
|
164
|
+
// structured CAT7 CMBs. Hosts that want both should call both.
|
|
165
|
+
//
|
|
166
|
+
// Report the actual delivered count (the number of peer transports
|
|
167
|
+
// that successfully accepted the broadcast), not peers().length.
|
|
168
|
+
// The two can disagree when peers are in _peers but their transports
|
|
169
|
+
// are broken — counting peers().length would lie about delivery.
|
|
170
|
+
// Requires @sym-bot/sym >= 0.3.70 where send() returns the count.
|
|
171
|
+
const msg = args.message;
|
|
172
|
+
const delivered = node.send(msg);
|
|
173
|
+
return { content: [{ type: 'text', text: `Message delivered to ${delivered} peer(s).` }] };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
case 'sym_observe': {
|
|
177
|
+
const fields = {
|
|
178
|
+
focus: args.focus || 'observation',
|
|
179
|
+
issue: args.issue || 'none',
|
|
180
|
+
intent: args.intent || 'observation',
|
|
181
|
+
motivation: args.motivation || '',
|
|
182
|
+
commitment: args.commitment || '',
|
|
183
|
+
perspective: args.perspective || NODE_NAME,
|
|
184
|
+
mood: args.mood || { text: 'neutral', valence: 0, arousal: 0 },
|
|
185
|
+
};
|
|
186
|
+
const entry = node.remember(fields);
|
|
187
|
+
return { content: [{ type: 'text', text: entry ? `Observed: ${entry.key}` : 'Duplicate — already in memory.' }] };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
case 'sym_recall': {
|
|
191
|
+
const results = node.recall(args.query || '');
|
|
192
|
+
if (results.length === 0) {
|
|
193
|
+
return { content: [{ type: 'text', text: 'No memories found.' }] };
|
|
194
|
+
}
|
|
195
|
+
const lines = results.slice(0, 10).map(r => {
|
|
196
|
+
const focus = r.cmb?.fields?.focus?.text || r.content || '';
|
|
197
|
+
const source = r.source || r.cmb?.createdBy || 'unknown';
|
|
198
|
+
const time = r.timestamp ? new Date(r.timestamp).toLocaleString() : '';
|
|
199
|
+
return `[${source}] ${time}\n ${focus.slice(0, 150)}`;
|
|
200
|
+
});
|
|
201
|
+
return { content: [{ type: 'text', text: lines.join('\n\n') }] };
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
case 'sym_peers': {
|
|
205
|
+
const peers = node.peers();
|
|
206
|
+
if (peers.length === 0) {
|
|
207
|
+
return { content: [{ type: 'text', text: 'No peers connected.' }] };
|
|
208
|
+
}
|
|
209
|
+
const lines = peers.map(p => `${p.name} via ${p.source || 'unknown'}`);
|
|
210
|
+
return { content: [{ type: 'text', text: `${peers.length} peer(s):\n${lines.join('\n')}` }] };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
case 'sym_status': {
|
|
214
|
+
const s = node.status();
|
|
215
|
+
return {
|
|
216
|
+
content: [{
|
|
217
|
+
type: 'text',
|
|
218
|
+
text: `Node: ${NODE_NAME} (${node.nodeId?.slice(0, 8) || '?'})\n` +
|
|
219
|
+
`Relay: ${s.relayConnected ? 'connected' : 'disconnected'}\n` +
|
|
220
|
+
`Peers: ${s.peerCount || 0}\n` +
|
|
221
|
+
`Memories: ${s.memoryCount || 0}`,
|
|
222
|
+
}],
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
default:
|
|
227
|
+
return { content: [{ type: 'text', text: `Unknown tool: ${name}` }] };
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// ── Peer Allowlist (optional, defense-in-depth) ─────────────
|
|
232
|
+
// SYM_ALLOWED_PEERS is a comma-separated list of peer node names.
|
|
233
|
+
// When set, only CMBs and messages from listed peers are pushed to
|
|
234
|
+
// Claude's context. When empty/unset, all authenticated peers are
|
|
235
|
+
// accepted (SVAF still gates on content relevance).
|
|
236
|
+
const ALLOWED_PEERS = (process.env.SYM_ALLOWED_PEERS || '')
|
|
237
|
+
.split(',')
|
|
238
|
+
.map(s => s.trim())
|
|
239
|
+
.filter(Boolean);
|
|
240
|
+
|
|
241
|
+
function isPeerAllowed(peerName) {
|
|
242
|
+
if (ALLOWED_PEERS.length === 0) return true; // no allowlist = accept all
|
|
243
|
+
return ALLOWED_PEERS.includes(peerName);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// ── Mesh Events → Channel Notifications ──────────────────────
|
|
247
|
+
|
|
248
|
+
function pushChannel(eventType, data) {
|
|
249
|
+
try {
|
|
250
|
+
mcp.notification({
|
|
251
|
+
method: 'notifications/claude/channel',
|
|
252
|
+
params: {
|
|
253
|
+
content: typeof data === 'string' ? data : JSON.stringify(data),
|
|
254
|
+
meta: { event_type: eventType, source: 'sym-mesh' },
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
} catch {}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
node.on('cmb-accepted', (entry) => {
|
|
261
|
+
// Don't echo back our own CMBs
|
|
262
|
+
if (entry.source === NODE_NAME || entry.cmb?.createdBy === NODE_NAME) return;
|
|
263
|
+
|
|
264
|
+
const source = entry.source || entry.cmb?.createdBy || 'unknown';
|
|
265
|
+
|
|
266
|
+
// Peer allowlist gate (defense-in-depth, see SECURITY.md)
|
|
267
|
+
if (!isPeerAllowed(source)) return;
|
|
268
|
+
|
|
269
|
+
const focus = entry.cmb?.fields?.focus?.text || entry.content || '';
|
|
270
|
+
const mood = entry.cmb?.fields?.mood?.text || '';
|
|
271
|
+
pushChannel('cmb', `[${source}] ${focus}${mood && mood !== 'neutral' ? ` (mood: ${mood})` : ''}`);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
node.on('message', (from, content) => {
|
|
275
|
+
// Peer allowlist gate
|
|
276
|
+
if (!isPeerAllowed(from)) return;
|
|
277
|
+
|
|
278
|
+
pushChannel('message', `[message from ${from}] ${content}`);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
// Peer presence events are intentionally NOT pushed to Claude's context.
|
|
282
|
+
// They're high-frequency, low-signal (peers flap on relay reconnects, daemon
|
|
283
|
+
// restarts, NAT keepalive blips), and a flood will eat the context window.
|
|
284
|
+
// Use sym_peers / sym_status on demand instead. Only CMBs and direct messages
|
|
285
|
+
// are surfaced as channel notifications — those carry actual cognitive payload.
|
|
286
|
+
|
|
287
|
+
// ── Start ────────────────────────────────────────────────────
|
|
288
|
+
|
|
289
|
+
// Clean shutdown — disconnect from the relay before exiting so other peers
|
|
290
|
+
// see us leave immediately, and so a fast restart of this MCP doesn't race
|
|
291
|
+
// our own zombie connection on the relay (which would trigger the relay's
|
|
292
|
+
// duplicate-nodeId replacement path and cause peer flap loops).
|
|
293
|
+
//
|
|
294
|
+
// Idempotent: Claude Code may send SIGTERM and then SIGKILL; we want the
|
|
295
|
+
// first signal to get us cleanly off the relay even if the second one
|
|
296
|
+
// arrives before stop() resolves.
|
|
297
|
+
let shuttingDown = false;
|
|
298
|
+
async function shutdown(signal) {
|
|
299
|
+
if (shuttingDown) return;
|
|
300
|
+
shuttingDown = true;
|
|
301
|
+
try {
|
|
302
|
+
await node.stop();
|
|
303
|
+
} catch {
|
|
304
|
+
// Best effort — we're exiting anyway. Don't block on cleanup errors.
|
|
305
|
+
}
|
|
306
|
+
process.exit(0);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
|
310
|
+
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
311
|
+
process.on('SIGHUP', () => shutdown('SIGHUP'));
|
|
312
|
+
|
|
313
|
+
async function main() {
|
|
314
|
+
// Start SymNode — connects to relay as a peer
|
|
315
|
+
await node.start();
|
|
316
|
+
|
|
317
|
+
// Start MCP server — communicates with Claude Code via stdio
|
|
318
|
+
const transport = new StdioServerTransport();
|
|
319
|
+
await mcp.connect(transport);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
main().catch((err) => {
|
|
323
|
+
process.stderr.write(`sym-mesh-channel failed: ${err.message}\n`);
|
|
324
|
+
process.exit(1);
|
|
325
|
+
});
|