Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(mcp): never send a JSON array as structuredContent (#1262)

antigravitysoham-eng14e989eafb62
diff --git a/services/mcp/src/server.ts b/services/mcp/src/server.ts
index 235040b6c..792fbeb32 100644
--- a/services/mcp/src/server.ts
+++ b/services/mcp/src/server.ts
@@ -204,6 +204,19 @@ export async function runServer(cfg: ServerConfig, log: Logger): Promise<void> {
// Result envelope helpers
// ---------------------------------------------------------------------------
+/**
+ * structuredContent must be a JSON object (MCP spec), and the SDK client
+ * rejects the whole result otherwise. `typeof [] === "object"`, so the old
+ * guard let arrays through and every tool returning a list
+ * (aisoc_list_investigations) failed with "-32602 Invalid tools/call result".
+ * Arrays still reach the host in full as the fenced JSON text content.
+ */
+function asStructuredContent(data: unknown): Record<string, unknown> | undefined {
+ return typeof data === "object" && data !== null && !Array.isArray(data)
+ ? (data as Record<string, unknown>)
+ : undefined;
+}
+
/**
* Convert a `ToolResult` into the MCP `CallToolResult` shape. JSON results
* become a fenced JSON code block so MCP hosts that render Markdown still
@@ -220,19 +233,12 @@ function toSuccessResult(result: ToolResult): {
const text = renderJson(result.data);
return {
content: [{ type: "text", text }],
- structuredContent:
- // Only surface as structuredContent if it's an object, per spec.
- typeof result.data === "object" && result.data !== null
- ? (result.data as Record<string, unknown>)
- : undefined,
+ structuredContent: asStructuredContent(result.data),
};
}
return {
content: [{ type: "text", text: result.text }],
- structuredContent:
- typeof result.data === "object" && result.data !== null
- ? (result.data as Record<string, unknown>)
- : undefined,
+ structuredContent: asStructuredContent(result.data),
};
}
diff --git a/services/mcp/tests/structured-content.test.ts b/services/mcp/tests/structured-content.test.ts
new file mode 100644
index 000000000..206672381
--- /dev/null
+++ b/services/mcp/tests/structured-content.test.ts
@@ -0,0 +1,55 @@
+/**
+ * structuredContent must be a JSON object (MCP spec), and the SDK client
+ * enforces it. toSuccessResult guarded with `typeof data === "object"`, which
+ * is also true for arrays, so a tool returning a list (aisoc_list_investigations,
+ * a bare array from GET /api/v1/investigations) produced a result every host
+ * rejected: "MCP error -32602: Invalid tools/call result ... expected record".
+ *
+ * Driven end to end: the real server, an SDK client over an in-memory
+ * transport, and a stubbed fetch standing in for the AiSOC API.
+ */
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { Client } from "@modelcontextprotocol/sdk/client/index.js";
+import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
+
+import type { Logger } from "../src/config.js";
+import { buildServer } from "../src/server.js";
+
+const SILENT: Logger = { info: () => undefined, warn: () => undefined, error: () => undefined };
+
+const RUNS = [
+ { id: "27a7a8c0-843e-5170-8310-96114b716ff3", status: "completed", model_used: "kafka:auto_triage:llm", iterations: 2, total_tokens: 0 },
+];
+
+async function connect(body: unknown) {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } })),
+ );
+ const server = buildServer(
+ { aisocUrl: "http://aisoc.test", apiKey: "aisoc_test", timeoutMs: 5000, verbose: false, userAgent: "test" },
+ SILENT,
+ );
+ const [a, b] = InMemoryTransport.createLinkedPair();
+ const client = new Client({ name: "test", version: "1" });
+ await Promise.all([server.connect(a), client.connect(b)]);
+ return client;
+}
+
+afterEach(() => vi.unstubAllGlobals());
+
+describe("tool results returning a JSON array", () => {
+ it("aisoc_list_investigations returns a result the SDK client accepts", async () => {
+ const client = await connect(RUNS);
+ const res = await client.callTool({ name: "aisoc_list_investigations", arguments: {} });
+ expect(res.isError).toBeFalsy();
+ const text = (res.content as { type: string; text: string }[])[0].text;
+ expect(text).toContain(RUNS[0].id);
+ });
+
+ it("never sets structuredContent to an array", async () => {
+ const client = await connect(RUNS);
+ const res = await client.callTool({ name: "aisoc_list_investigations", arguments: {} });
+ expect(Array.isArray(res.structuredContent)).toBe(false);
+ });
+});