@kalera/munin-runtime 1.4.2 → 1.5.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.
@@ -1,4 +1,4 @@
1
1
 
2
- > @kalera/munin-runtime@1.4.2 build /home/runner/work/munin-for-agents/munin-for-agents/packages/runtime
2
+ > @kalera/munin-runtime@1.5.0 build /home/runner/work/munin-for-agents/munin-for-agents/packages/runtime
3
3
  > tsc -p tsconfig.json
4
4
 
package/dist/index.js CHANGED
@@ -128,13 +128,51 @@ export async function executeWithRetry(task, retries, backoffMs) {
128
128
  }
129
129
  export function safeError(error) {
130
130
  if (error instanceof Error) {
131
- return {
131
+ const safe = {
132
132
  name: error.name,
133
133
  message: redactText(error.message),
134
134
  };
135
+ // MuninSdkError carries structured `code` + `details`; preserve them so
136
+ // callers can branch on the code, but redact any secrets nested in details
137
+ // (both secret-named keys and inline `token=...` patterns in values).
138
+ const sdkError = error;
139
+ if (error.name === "MuninSdkError") {
140
+ if (sdkError.code !== undefined)
141
+ safe.code = sdkError.code;
142
+ if (sdkError.details !== undefined)
143
+ safe.details = redactValue(sdkError.details);
144
+ }
145
+ return safe;
135
146
  }
136
147
  return { message: redactText(String(error)) };
137
148
  }
149
+ function isSecretKey(key) {
150
+ const lower = key.toLowerCase();
151
+ return REDACT_KEYS.some((redactKey) => redactKey.toLowerCase() === lower);
152
+ }
153
+ /**
154
+ * Recursively redact secrets from an arbitrary value. A property whose KEY is a
155
+ * known secret (apiKey, token, …) has its value replaced wholesale; every other
156
+ * string is passed through inline `redactText` so embedded `token=...` secrets
157
+ * are scrubbed without losing surrounding context.
158
+ */
159
+ function redactValue(value) {
160
+ if (typeof value === "string")
161
+ return redactText(value);
162
+ if (Array.isArray(value))
163
+ return value.map((item) => redactValue(item));
164
+ if (value && typeof value === "object") {
165
+ const out = {};
166
+ for (const [key, val] of Object.entries(value)) {
167
+ out[key] =
168
+ isSecretKey(key) && (val === null || typeof val !== "object")
169
+ ? "[REDACTED]"
170
+ : redactValue(val);
171
+ }
172
+ return out;
173
+ }
174
+ return value;
175
+ }
138
176
  function redactText(text) {
139
177
  return REDACT_KEYS.reduce((acc, key) => {
140
178
  const pattern = new RegExp(`${key}\\s*[:=]\\s*[^\\s,]+`, "gi");
@@ -3,6 +3,11 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
3
3
  import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
4
4
  import { MuninClient } from "@kalera/munin-sdk";
5
5
  import { loadCliEnv, resolveProjectId, resolveEncryptionKey, safeError } from "./index.js";
6
+ function hasSingleIdentifier(args) {
7
+ const hasKey = typeof args.key === "string" && args.key.length > 0;
8
+ const hasId = typeof args.id === "string" && args.id.length > 0;
9
+ return hasKey !== hasId;
10
+ }
6
11
  export function createMcpServerInstance(env, opts) {
7
12
  if (!env.apiKey && !opts?.allowMissingApiKey) {
8
13
  throw new Error("MUNIN_API_KEY is required. Set it in your environment, or in a .env.local / .env file in your project directory (walked up automatically).");
@@ -14,7 +19,7 @@ export function createMcpServerInstance(env, opts) {
14
19
  });
15
20
  const server = new Server({
16
21
  name: "munin-mcp-server",
17
- version: "1.4.1",
22
+ version: "1.5.0",
18
23
  }, {
19
24
  capabilities: {
20
25
  tools: {},
@@ -176,10 +181,7 @@ export function createMcpServerInstance(env, opts) {
176
181
  key: { type: "string", description: "Memory key (provide this OR id, not both)" },
177
182
  id: { type: "string", description: "Memory ID (provide this OR key, not both)" },
178
183
  },
179
- oneOf: [
180
- { required: ["key"] },
181
- { required: ["id"] },
182
- ],
184
+ required: [],
183
185
  },
184
186
  },
185
187
  {
@@ -194,10 +196,6 @@ export function createMcpServerInstance(env, opts) {
194
196
  version: { type: "number", description: "The version number to rollback to (required)" },
195
197
  },
196
198
  required: ["version"],
197
- oneOf: [
198
- { required: ["key"] },
199
- { required: ["id"] },
200
- ],
201
199
  },
202
200
  },
203
201
  {
@@ -224,10 +222,7 @@ export function createMcpServerInstance(env, opts) {
224
222
  key: { type: "string", description: "Memory key (provide this OR id, not both)" },
225
223
  id: { type: "string", description: "Memory ID (provide this OR key, not both)" }
226
224
  },
227
- oneOf: [
228
- { required: ["key"] },
229
- { required: ["id"] }
230
- ]
225
+ required: [],
231
226
  }
232
227
  },
233
228
  {
@@ -291,14 +286,14 @@ export function createMcpServerInstance(env, opts) {
291
286
  break;
292
287
  }
293
288
  case "munin_versions":
294
- if (!args.key && !args.id) {
295
- throw new Error("munin_versions requires either 'key' or 'id' to identify the target memory.");
289
+ if (!hasSingleIdentifier(args)) {
290
+ throw new Error("munin_versions requires exactly one of 'key' or 'id' to identify the target memory.");
296
291
  }
297
292
  result = await client.invoke(projectId, "versions", { key: args.key, id: args.id });
298
293
  break;
299
294
  case "munin_rollback":
300
- if (!args.key && !args.id) {
301
- throw new Error("munin_rollback requires either 'key' or 'id' to identify the target memory.");
295
+ if (!hasSingleIdentifier(args)) {
296
+ throw new Error("munin_rollback requires exactly one of 'key' or 'id' to identify the target memory.");
302
297
  }
303
298
  if (typeof args.version !== "number") {
304
299
  throw new Error("munin_rollback requires a numeric 'version' to roll back to.");
@@ -306,8 +301,8 @@ export function createMcpServerInstance(env, opts) {
306
301
  result = await client.invoke(projectId, "rollback", { key: args.key, id: args.id, version: args.version });
307
302
  break;
308
303
  case "munin_delete_memory":
309
- if (!args.key && !args.id) {
310
- throw new Error("munin_delete_memory requires either 'key' or 'id' to identify the target memory.");
304
+ if (!hasSingleIdentifier(args)) {
305
+ throw new Error("munin_delete_memory requires exactly one of 'key' or 'id' to identify the target memory.");
311
306
  }
312
307
  result = await client.invoke(projectId, "delete", { key: args.key, id: args.id });
313
308
  break;
package/package.json CHANGED
@@ -8,16 +8,18 @@
8
8
  },
9
9
  "devDependencies": {
10
10
  "@types/node": "^24.3.0",
11
- "typescript": "^5.9.2"
11
+ "tsx": "^4.20.5",
12
+ "typescript": "^5.9.2",
13
+ "@kalera/munin-shared-version": "1.5.0"
12
14
  },
13
15
  "dependencies": {
14
16
  "@modelcontextprotocol/sdk": "^1.27.1",
15
- "@kalera/munin-sdk": "1.4.2"
17
+ "@kalera/munin-sdk": "1.5.0"
16
18
  },
17
- "version": "1.4.2",
19
+ "version": "1.5.0",
18
20
  "scripts": {
19
21
  "build": "tsc -p tsconfig.json",
20
22
  "lint": "tsc -p tsconfig.json --noEmit",
21
- "test": "echo 'adapter-runtime tests: pending'"
23
+ "test": "tsx test/schema.test.ts && tsx test/validation.test.ts"
22
24
  }
23
25
  }
package/src/index.ts CHANGED
@@ -157,15 +157,53 @@ export async function executeWithRetry<T>(
157
157
 
158
158
  export function safeError(error: unknown): Record<string, unknown> {
159
159
  if (error instanceof Error) {
160
- return {
160
+ const safe: Record<string, unknown> = {
161
161
  name: error.name,
162
162
  message: redactText(error.message),
163
163
  };
164
+
165
+ // MuninSdkError carries structured `code` + `details`; preserve them so
166
+ // callers can branch on the code, but redact any secrets nested in details
167
+ // (both secret-named keys and inline `token=...` patterns in values).
168
+ const sdkError = error as Error & { code?: unknown; details?: unknown };
169
+ if (error.name === "MuninSdkError") {
170
+ if (sdkError.code !== undefined) safe.code = sdkError.code;
171
+ if (sdkError.details !== undefined) safe.details = redactValue(sdkError.details);
172
+ }
173
+
174
+ return safe;
164
175
  }
165
176
 
166
177
  return { message: redactText(String(error)) };
167
178
  }
168
179
 
180
+ function isSecretKey(key: string): boolean {
181
+ const lower = key.toLowerCase();
182
+ return REDACT_KEYS.some((redactKey) => redactKey.toLowerCase() === lower);
183
+ }
184
+
185
+ /**
186
+ * Recursively redact secrets from an arbitrary value. A property whose KEY is a
187
+ * known secret (apiKey, token, …) has its value replaced wholesale; every other
188
+ * string is passed through inline `redactText` so embedded `token=...` secrets
189
+ * are scrubbed without losing surrounding context.
190
+ */
191
+ function redactValue(value: unknown): unknown {
192
+ if (typeof value === "string") return redactText(value);
193
+ if (Array.isArray(value)) return value.map((item) => redactValue(item));
194
+ if (value && typeof value === "object") {
195
+ const out: Record<string, unknown> = {};
196
+ for (const [key, val] of Object.entries(value)) {
197
+ out[key] =
198
+ isSecretKey(key) && (val === null || typeof val !== "object")
199
+ ? "[REDACTED]"
200
+ : redactValue(val);
201
+ }
202
+ return out;
203
+ }
204
+ return value;
205
+ }
206
+
169
207
  function redactText(text: string): string {
170
208
  return REDACT_KEYS.reduce((acc, key) => {
171
209
  const pattern = new RegExp(`${key}\\s*[:=]\\s*[^\\s,]+`, "gi");
package/src/mcp-server.ts CHANGED
@@ -7,6 +7,12 @@ import {
7
7
  import { MuninClient } from "@kalera/munin-sdk";
8
8
  import { loadCliEnv, resolveProjectId, resolveEncryptionKey, safeError } from "./index.js";
9
9
 
10
+ function hasSingleIdentifier(args: Record<string, unknown>): args is Record<string, unknown> & { key?: string; id?: string } {
11
+ const hasKey = typeof args.key === "string" && args.key.length > 0;
12
+ const hasId = typeof args.id === "string" && args.id.length > 0;
13
+ return hasKey !== hasId;
14
+ }
15
+
10
16
  export function createMcpServerInstance(
11
17
  env: ReturnType<typeof loadCliEnv>,
12
18
  opts?: { allowMissingApiKey?: boolean },
@@ -26,7 +32,7 @@ export function createMcpServerInstance(
26
32
  const server = new Server(
27
33
  {
28
34
  name: "munin-mcp-server",
29
- version: "1.4.1",
35
+ version: "1.5.0",
30
36
  },
31
37
  {
32
38
  capabilities: {
@@ -191,10 +197,7 @@ export function createMcpServerInstance(
191
197
  key: { type: "string", description: "Memory key (provide this OR id, not both)" },
192
198
  id: { type: "string", description: "Memory ID (provide this OR key, not both)" },
193
199
  },
194
- oneOf: [
195
- { required: ["key"] },
196
- { required: ["id"] },
197
- ],
200
+ required: [],
198
201
  },
199
202
  },
200
203
  {
@@ -209,10 +212,6 @@ export function createMcpServerInstance(
209
212
  version: { type: "number", description: "The version number to rollback to (required)" },
210
213
  },
211
214
  required: ["version"],
212
- oneOf: [
213
- { required: ["key"] },
214
- { required: ["id"] },
215
- ],
216
215
  },
217
216
  },
218
217
  {
@@ -239,10 +238,7 @@ export function createMcpServerInstance(
239
238
  key: { type: "string", description: "Memory key (provide this OR id, not both)" },
240
239
  id: { type: "string", description: "Memory ID (provide this OR key, not both)" }
241
240
  },
242
- oneOf: [
243
- { required: ["key"] },
244
- { required: ["id"] }
245
- ]
241
+ required: [],
246
242
  }
247
243
  },
248
244
  {
@@ -314,14 +310,14 @@ export function createMcpServerInstance(
314
310
  break;
315
311
  }
316
312
  case "munin_versions":
317
- if (!args.key && !args.id) {
318
- throw new Error("munin_versions requires either 'key' or 'id' to identify the target memory.");
313
+ if (!hasSingleIdentifier(args)) {
314
+ throw new Error("munin_versions requires exactly one of 'key' or 'id' to identify the target memory.");
319
315
  }
320
316
  result = await client.invoke(projectId, "versions", { key: args.key, id: args.id });
321
317
  break;
322
318
  case "munin_rollback":
323
- if (!args.key && !args.id) {
324
- throw new Error("munin_rollback requires either 'key' or 'id' to identify the target memory.");
319
+ if (!hasSingleIdentifier(args)) {
320
+ throw new Error("munin_rollback requires exactly one of 'key' or 'id' to identify the target memory.");
325
321
  }
326
322
  if (typeof args.version !== "number") {
327
323
  throw new Error("munin_rollback requires a numeric 'version' to roll back to.");
@@ -329,8 +325,8 @@ export function createMcpServerInstance(
329
325
  result = await client.invoke(projectId, "rollback", { key: args.key, id: args.id, version: args.version });
330
326
  break;
331
327
  case "munin_delete_memory":
332
- if (!args.key && !args.id) {
333
- throw new Error("munin_delete_memory requires either 'key' or 'id' to identify the target memory.");
328
+ if (!hasSingleIdentifier(args)) {
329
+ throw new Error("munin_delete_memory requires exactly one of 'key' or 'id' to identify the target memory.");
334
330
  }
335
331
  result = await client.invoke(projectId, "delete", { key: args.key, id: args.id });
336
332
  break;
@@ -373,4 +369,4 @@ export async function startMcpServer() {
373
369
  const transport = new StdioServerTransport();
374
370
  await server.connect(transport);
375
371
  console.error("Munin MCP Server running on stdio");
376
- }
372
+ }
@@ -0,0 +1,53 @@
1
+ import assert from "node:assert/strict";
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3
+ import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
4
+ import { SERVER_VERSION } from "@kalera/munin-shared-version";
5
+ import { createMcpServerInstance } from "../src/mcp-server.js";
6
+
7
+ async function run() {
8
+ const server = createMcpServerInstance(
9
+ {
10
+ baseUrl: "https://munin.kalera.dev",
11
+ apiKey: "test-key",
12
+ timeoutMs: 15_000,
13
+ retries: 0,
14
+ backoffMs: 0,
15
+ },
16
+ { allowMissingApiKey: true },
17
+ );
18
+
19
+ const client = new Client(
20
+ {
21
+ name: "runtime-schema-test-client",
22
+ version: "1.5.0",
23
+ },
24
+ {
25
+ capabilities: {},
26
+ },
27
+ );
28
+
29
+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
30
+ await Promise.all([
31
+ server.connect(serverTransport),
32
+ client.connect(clientTransport),
33
+ ]);
34
+ assert.equal(client.getServerVersion()?.version, SERVER_VERSION);
35
+
36
+ const response = await client.listTools();
37
+
38
+ const tools = response.tools ?? [];
39
+ for (const toolName of ["munin_versions", "munin_rollback", "munin_delete_memory"]) {
40
+ const tool = tools.find((candidate) => candidate.name === toolName);
41
+ assert.ok(tool, `expected ${toolName} to be registered`);
42
+ assert.equal(tool.inputSchema.type, "object");
43
+ assert.equal("oneOf" in tool.inputSchema, false, `${toolName} must not expose top-level oneOf`);
44
+ assert.equal("anyOf" in tool.inputSchema, false, `${toolName} must not expose top-level anyOf`);
45
+ assert.equal("allOf" in tool.inputSchema, false, `${toolName} must not expose top-level allOf`);
46
+ }
47
+
48
+ await Promise.all([client.close(), server.close()]);
49
+
50
+ console.log("runtime schema tests passed");
51
+ }
52
+
53
+ void run();
@@ -0,0 +1,115 @@
1
+ import assert from "node:assert/strict";
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3
+ import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
4
+ import { MuninSdkError } from "@kalera/munin-sdk";
5
+ import { createMcpServerInstance } from "../src/mcp-server.js";
6
+ import { safeError } from "../src/index.js";
7
+
8
+ interface ToolResult {
9
+ isError?: boolean;
10
+ content?: Array<{ type: string; text: string }>;
11
+ }
12
+
13
+ async function callTool(client: Client, name: string, args: Record<string, unknown>): Promise<ToolResult> {
14
+ return (await client.callTool({ name, arguments: args })) as ToolResult;
15
+ }
16
+
17
+ function expectValidationError(result: ToolResult, toolName: string, scenario: string): void {
18
+ assert.equal(result.isError, true, `${toolName} (${scenario}): expected isError=true`);
19
+ const text = result.content?.[0]?.text ?? "";
20
+ assert.match(
21
+ text,
22
+ /requires exactly one of 'key' or 'id'/,
23
+ `${toolName} (${scenario}): expected XOR validation message, got: ${text}`,
24
+ );
25
+ }
26
+
27
+ async function run() {
28
+ assert.deepEqual(
29
+ safeError(new MuninSdkError({
30
+ code: "ERR_STALE_PROTOCOL",
31
+ message: "ERR_STALE_PROTOCOL",
32
+ details: {
33
+ remediation: {
34
+ version: "2026-04-17",
35
+ setupGuideUrl: "https://example.test/setup",
36
+ apiKey: "secret-value",
37
+ instruction: "token=secret-value",
38
+ },
39
+ },
40
+ })),
41
+ {
42
+ name: "MuninSdkError",
43
+ message: "ERR_STALE_PROTOCOL",
44
+ code: "ERR_STALE_PROTOCOL",
45
+ details: {
46
+ remediation: {
47
+ version: "2026-04-17",
48
+ setupGuideUrl: "https://example.test/setup",
49
+ apiKey: "[REDACTED]",
50
+ instruction: "token=[REDACTED]",
51
+ },
52
+ },
53
+ },
54
+ );
55
+ const server = createMcpServerInstance(
56
+ {
57
+ baseUrl: "https://munin.kalera.dev",
58
+ apiKey: "test-key",
59
+ timeoutMs: 15_000,
60
+ retries: 0,
61
+ backoffMs: 0,
62
+ },
63
+ { allowMissingApiKey: true },
64
+ );
65
+
66
+ const client = new Client(
67
+ { name: "runtime-validation-test-client", version: "1.5.0" },
68
+ { capabilities: {} },
69
+ );
70
+
71
+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
72
+ await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]);
73
+
74
+ const xorTools: Array<{ name: string; extraArgs?: Record<string, unknown> }> = [
75
+ { name: "munin_versions" },
76
+ { name: "munin_rollback", extraArgs: { version: 1 } },
77
+ { name: "munin_delete_memory" },
78
+ ];
79
+
80
+ const projectId = "test-project";
81
+
82
+ for (const { name, extraArgs = {} } of xorTools) {
83
+ const base = { projectId, ...extraArgs };
84
+ expectValidationError(await callTool(client, name, { ...base }), name, "neither key nor id");
85
+ expectValidationError(
86
+ await callTool(client, name, { ...base, key: "k1", id: "i1" }),
87
+ name,
88
+ "both key and id",
89
+ );
90
+ expectValidationError(await callTool(client, name, { ...base, key: "" }), name, "empty key");
91
+ expectValidationError(await callTool(client, name, { ...base, id: "" }), name, "empty id");
92
+ expectValidationError(
93
+ await callTool(client, name, { ...base, key: "", id: "" }),
94
+ name,
95
+ "both empty",
96
+ );
97
+ }
98
+
99
+ const rollbackMissingVersion = await callTool(client, "munin_rollback", {
100
+ projectId,
101
+ key: "k1",
102
+ });
103
+ assert.equal(rollbackMissingVersion.isError, true, "munin_rollback: expected isError when version is missing");
104
+ assert.match(
105
+ rollbackMissingVersion.content?.[0]?.text ?? "",
106
+ /numeric 'version'/,
107
+ "munin_rollback: expected numeric version error message",
108
+ );
109
+
110
+ await Promise.all([client.close(), server.close()]);
111
+
112
+ console.log("runtime validation tests passed");
113
+ }
114
+
115
+ void run();