@murmurv2/mcp-server 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 alexfrmn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,364 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { readFileSync } from "node:fs";
3
+ import path from "node:path";
4
+ import { createInterface } from "node:readline";
5
+ import { SQLiteDedupeOutboxStore, SQLiteMessageStore, } from "@murmurv2/core";
6
+ import { encryptPayload, signEnvelope } from "@murmurv2/security";
7
+ // --- Load agent config (optional — gracefully degrade if missing) ---
8
+ const dataDir = process.env.DATA_DIR || ".data";
9
+ const configPath = path.join(dataDir, "agent-config.json");
10
+ const dbPath = process.env.MURMUR_STORE_PATH ?? path.join(dataDir, "murmur.db");
11
+ let agentConfig = null;
12
+ try {
13
+ agentConfig = JSON.parse(readFileSync(configPath, "utf8"));
14
+ }
15
+ catch {
16
+ // Agent config not found — send/inbox/peers tools will be unavailable
17
+ }
18
+ const store = new SQLiteMessageStore(dbPath);
19
+ // Outbox store — shared with daemon, only created if agent config exists
20
+ let outbox = null;
21
+ if (agentConfig) {
22
+ outbox = new SQLiteDedupeOutboxStore(dbPath);
23
+ }
24
+ // --- Stable envelope payload for signing ---
25
+ const stableEnvelopePayload = (envelope) => {
26
+ return JSON.stringify({
27
+ schemaVersion: envelope.schemaVersion,
28
+ msgId: envelope.msgId,
29
+ conversationId: envelope.conversationId,
30
+ senderAgentId: envelope.senderAgentId,
31
+ recipients: [...envelope.recipients],
32
+ createdAt: envelope.createdAt,
33
+ payloadCiphertext: envelope.payloadCiphertext,
34
+ payloadNonce: envelope.payloadNonce,
35
+ });
36
+ };
37
+ // --- JSON-RPC helpers ---
38
+ const send = (payload) => {
39
+ process.stdout.write(`${JSON.stringify(payload)}\n`);
40
+ };
41
+ const ok = (id, result) => {
42
+ if (id === undefined)
43
+ return;
44
+ send({ jsonrpc: "2.0", id, result });
45
+ };
46
+ const fail = (id, message) => {
47
+ if (id === undefined)
48
+ return;
49
+ send({ jsonrpc: "2.0", id, error: { code: -32000, message } });
50
+ };
51
+ const asMessage = (r) => ({
52
+ id: r.id,
53
+ conversationId: r.conversationId,
54
+ msgId: r.msgId,
55
+ direction: r.direction,
56
+ sender: r.sender,
57
+ text: r.text,
58
+ createdAt: r.createdAt,
59
+ transport: r.transport,
60
+ });
61
+ // --- Tool handlers ---
62
+ const handleTool = async (name, args) => {
63
+ // === Original tools ===
64
+ if (name === "send_message") {
65
+ const text = String(args.text ?? "").trim();
66
+ if (!text)
67
+ throw new Error("text is required");
68
+ const conversationId = String(args.conversationId ?? "local:default");
69
+ const sender = String(args.sender ?? "mcp-client");
70
+ const msgId = String(args.msgId ?? randomUUID());
71
+ const row = await store.append({
72
+ conversationId,
73
+ msgId,
74
+ direction: "outbound",
75
+ sender,
76
+ text,
77
+ createdAt: new Date().toISOString(),
78
+ transport: "mcp",
79
+ });
80
+ return { message: asMessage(row) };
81
+ }
82
+ if (name === "list_conversations") {
83
+ const limit = Number(args.limit ?? 50);
84
+ const conversations = await store.listConversations(Number.isFinite(limit) ? limit : 50);
85
+ return { conversations };
86
+ }
87
+ if (name === "search_messages") {
88
+ const query = String(args.query ?? "").trim();
89
+ if (!query)
90
+ throw new Error("query is required");
91
+ const limit = Number(args.limit ?? 50);
92
+ const messages = await store.searchMessages(query, Number.isFinite(limit) ? limit : 50);
93
+ return { messages: messages.map(asMessage) };
94
+ }
95
+ // === New agent-to-agent tools (require agent config) ===
96
+ if (name === "murmur_send") {
97
+ if (!agentConfig || !outbox)
98
+ throw new Error("agent config not loaded — run agent-config-init.mjs first");
99
+ const to = String(args.to ?? "").trim();
100
+ if (!to)
101
+ throw new Error("'to' (recipient agent ID) is required");
102
+ const text = String(args.text ?? "").trim();
103
+ if (!text)
104
+ throw new Error("'text' is required");
105
+ const peer = agentConfig.peers[to];
106
+ if (!peer)
107
+ throw new Error(`unknown peer: ${to} — add to peers in agent-config.json`);
108
+ const conversationId = String(args.conversationId ?? `dm:${agentConfig.agentId}:${to}`);
109
+ const msgId = randomUUID();
110
+ // Encrypt
111
+ const encrypted = await encryptPayload(text, peer.encryption.publicKey, agentConfig.keys.encryption.privateKey);
112
+ // Build envelope
113
+ const envelope = {
114
+ schemaVersion: "1.0",
115
+ msgId,
116
+ conversationId,
117
+ senderAgentId: agentConfig.agentId,
118
+ recipients: [to],
119
+ createdAt: new Date().toISOString(),
120
+ payloadCiphertext: encrypted.ciphertext,
121
+ payloadNonce: encrypted.nonce,
122
+ signature: "",
123
+ };
124
+ // Sign
125
+ envelope.signature = await signEnvelope(stableEnvelopePayload(envelope), agentConfig.keys.signing.privateKey);
126
+ // Enqueue to outbox — daemon will flush to NATS
127
+ await outbox.enqueue(peer.subject, envelope);
128
+ // Store outbound copy in message store
129
+ await store.append({
130
+ conversationId,
131
+ msgId,
132
+ direction: "outbound",
133
+ sender: agentConfig.agentId,
134
+ text,
135
+ createdAt: envelope.createdAt,
136
+ transport: "nats",
137
+ });
138
+ return { msgId, to, conversationId, status: "queued" };
139
+ }
140
+ if (name === "murmur_inbox") {
141
+ if (!agentConfig)
142
+ throw new Error("agent config not loaded — run agent-config-init.mjs first");
143
+ const limit = Number(args.limit ?? 20);
144
+ const effectiveLimit = Number.isFinite(limit) ? limit : 20;
145
+ // Search inbound messages for this agent
146
+ const messages = await store.searchMessages(agentConfig.agentId, effectiveLimit * 5);
147
+ // Filter to only inbound messages
148
+ const inbound = messages
149
+ .filter((m) => m.direction === "inbound")
150
+ .slice(0, effectiveLimit);
151
+ return { messages: inbound.map(asMessage), count: inbound.length };
152
+ }
153
+ if (name === "murmur_request") {
154
+ if (!agentConfig || !outbox)
155
+ throw new Error("agent config not loaded — run agent-config-init.mjs first");
156
+ const to = String(args.to ?? "").trim();
157
+ if (!to)
158
+ throw new Error("'to' (recipient agent ID) is required");
159
+ const text = String(args.text ?? "").trim();
160
+ if (!text)
161
+ throw new Error("'text' is required");
162
+ const peer = agentConfig.peers[to];
163
+ if (!peer)
164
+ throw new Error(`unknown peer: ${to} — add to peers in agent-config.json`);
165
+ const timeoutMs = Number(args.timeout_ms ?? 300_000);
166
+ const pollMs = Number(args.poll_interval_ms ?? 10_000);
167
+ const conversationId = String(args.conversationId ?? `dm:${agentConfig.agentId}:${to}`);
168
+ const msgId = randomUUID();
169
+ const sentAt = new Date().toISOString();
170
+ // Encrypt
171
+ const encrypted = await encryptPayload(text, peer.encryption.publicKey, agentConfig.keys.encryption.privateKey);
172
+ // Build envelope
173
+ const envelope = {
174
+ schemaVersion: "1.0",
175
+ msgId,
176
+ conversationId,
177
+ senderAgentId: agentConfig.agentId,
178
+ recipients: [to],
179
+ createdAt: sentAt,
180
+ payloadCiphertext: encrypted.ciphertext,
181
+ payloadNonce: encrypted.nonce,
182
+ signature: "",
183
+ };
184
+ // Sign
185
+ envelope.signature = await signEnvelope(stableEnvelopePayload(envelope), agentConfig.keys.signing.privateKey);
186
+ // Enqueue to outbox
187
+ await outbox.enqueue(peer.subject, envelope);
188
+ // Store outbound copy
189
+ await store.append({
190
+ conversationId,
191
+ msgId,
192
+ direction: "outbound",
193
+ sender: agentConfig.agentId,
194
+ text,
195
+ createdAt: sentAt,
196
+ transport: "nats",
197
+ });
198
+ // Poll for response
199
+ const deadline = Date.now() + timeoutMs;
200
+ let reply = null;
201
+ while (Date.now() < deadline) {
202
+ const inbound = await store.getInboundAfter(conversationId, sentAt, 1);
203
+ if (inbound.length > 0) {
204
+ reply = inbound[0];
205
+ break;
206
+ }
207
+ await new Promise((r) => setTimeout(r, pollMs));
208
+ }
209
+ if (reply) {
210
+ return {
211
+ status: "received",
212
+ msgId,
213
+ conversationId,
214
+ sentAt,
215
+ reply: asMessage(reply),
216
+ };
217
+ }
218
+ return {
219
+ status: "timeout",
220
+ msgId,
221
+ conversationId,
222
+ sentAt,
223
+ timeout_ms: timeoutMs,
224
+ hint: "Use murmur_inbox to check for late responses",
225
+ };
226
+ }
227
+ if (name === "murmur_peers") {
228
+ if (!agentConfig)
229
+ throw new Error("agent config not loaded — run agent-config-init.mjs first");
230
+ const peerList = Object.entries(agentConfig.peers).map(([id, p]) => ({
231
+ agentId: id,
232
+ subject: p.subject,
233
+ hasEncryptionKey: !!p.encryption?.publicKey,
234
+ hasSigningKey: !!p.signing?.publicKey,
235
+ }));
236
+ return { agentId: agentConfig.agentId, peers: peerList };
237
+ }
238
+ throw new Error(`unknown tool: ${name}`);
239
+ };
240
+ // --- Tool definitions ---
241
+ const tools = [
242
+ {
243
+ name: "send_message",
244
+ description: "Store a local outbound message in the Murmur message store.",
245
+ inputSchema: {
246
+ type: "object",
247
+ properties: {
248
+ conversationId: { type: "string" },
249
+ text: { type: "string" },
250
+ sender: { type: "string" },
251
+ msgId: { type: "string" },
252
+ },
253
+ required: ["text"],
254
+ },
255
+ },
256
+ {
257
+ name: "list_conversations",
258
+ description: "List known conversations from local persisted message store.",
259
+ inputSchema: {
260
+ type: "object",
261
+ properties: {
262
+ limit: { type: "number" },
263
+ },
264
+ },
265
+ },
266
+ {
267
+ name: "search_messages",
268
+ description: "Search local stored messages by text/sender/conversation.",
269
+ inputSchema: {
270
+ type: "object",
271
+ properties: {
272
+ query: { type: "string" },
273
+ limit: { type: "number" },
274
+ },
275
+ required: ["query"],
276
+ },
277
+ },
278
+ {
279
+ name: "murmur_send",
280
+ description: "Send an encrypted, signed message to another agent via NATS. Message is queued in outbox and delivered by murmur-daemon.",
281
+ inputSchema: {
282
+ type: "object",
283
+ properties: {
284
+ to: { type: "string", description: "Recipient agent ID (must be in peers config)" },
285
+ text: { type: "string", description: "Message text (will be encrypted)" },
286
+ conversationId: { type: "string", description: "Optional conversation ID" },
287
+ },
288
+ required: ["to", "text"],
289
+ },
290
+ },
291
+ {
292
+ name: "murmur_request",
293
+ description: "Send a message and wait for the reply. Combines murmur_send + automatic polling — the tool blocks until the peer responds or timeout is reached. Ideal for autonomous agent-to-agent conversations.",
294
+ inputSchema: {
295
+ type: "object",
296
+ properties: {
297
+ to: { type: "string", description: "Recipient agent ID (must be in peers config)" },
298
+ text: { type: "string", description: "Message text (will be encrypted)" },
299
+ conversationId: { type: "string", description: "Optional conversation ID" },
300
+ timeout_ms: { type: "number", description: "Max wait time in ms (default: 300000 = 5 min)" },
301
+ poll_interval_ms: { type: "number", description: "Poll interval in ms (default: 10000 = 10s)" },
302
+ },
303
+ required: ["to", "text"],
304
+ },
305
+ },
306
+ {
307
+ name: "murmur_inbox",
308
+ description: "Read inbound messages received from other agents. Messages are decrypted and stored by murmur-daemon.",
309
+ inputSchema: {
310
+ type: "object",
311
+ properties: {
312
+ limit: { type: "number", description: "Max messages to return (default 20)" },
313
+ },
314
+ },
315
+ },
316
+ {
317
+ name: "murmur_peers",
318
+ description: "List known peer agents and their key status.",
319
+ inputSchema: {
320
+ type: "object",
321
+ properties: {},
322
+ },
323
+ },
324
+ ];
325
+ // --- JSON-RPC stdio loop ---
326
+ const rl = createInterface({ input: process.stdin, crlfDelay: Infinity });
327
+ rl.on("line", async (line) => {
328
+ if (!line.trim())
329
+ return;
330
+ let req;
331
+ try {
332
+ req = JSON.parse(line);
333
+ }
334
+ catch {
335
+ return;
336
+ }
337
+ try {
338
+ if (req.method === "initialize") {
339
+ ok(req.id, {
340
+ protocolVersion: "2024-11-05",
341
+ serverInfo: { name: "murmur-v2-mcp", version: "0.2.0" },
342
+ capabilities: { tools: {} },
343
+ });
344
+ return;
345
+ }
346
+ if (req.method === "tools/list") {
347
+ ok(req.id, { tools });
348
+ return;
349
+ }
350
+ if (req.method === "tools/call") {
351
+ const name = String(req.params?.name ?? "");
352
+ const args = req.params?.arguments ?? {};
353
+ const result = await handleTool(name, args);
354
+ ok(req.id, { content: [{ type: "text", text: JSON.stringify(result) }] });
355
+ return;
356
+ }
357
+ if (req.method === "notifications/initialized")
358
+ return;
359
+ fail(req.id, `unsupported method: ${req.method}`);
360
+ }
361
+ catch (err) {
362
+ fail(req.id, err instanceof Error ? err.message : "request failed");
363
+ }
364
+ });
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@murmurv2/mcp-server",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "dist/src/index.js",
6
+ "types": "dist/src/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc -p tsconfig.json",
9
+ "prepack": "npm run build"
10
+ },
11
+ "dependencies": {
12
+ "@murmurv2/core": "^0.1.0",
13
+ "@murmurv2/security": "^0.1.0"
14
+ },
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/alexfrmn/mur-mur-v2.git",
19
+ "directory": "packages/mcp-server"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "files": [
25
+ "dist/src",
26
+ "LICENSE"
27
+ ],
28
+ "description": "Murmur V2 MCP server — 7 tools for agent-to-agent messaging over the Model Context Protocol."
29
+ }