feat(mcp): return structured typed tool results

This commit is contained in:
morluto
2026-07-13 11:51:19 +00:00
parent 3be439cbf3
commit eb38e4c402
12 changed files with 203 additions and 32 deletions
+60 -1
View File
@@ -1,8 +1,10 @@
import { mkdtemp, writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { createPackageWithOptions } from "@electron/asar";
import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
import { describe, expect, it } from "vitest";
import { z } from "zod";
@@ -16,6 +18,63 @@ import { createServer } from "../src/server/createServer.js";
import { observed } from "./fixtures/analysisExecution.js";
describe("artifact graph MCP integration", () => {
it("returns ASAR integrity path and hashes as structured error data", async () => {
const root = await mkdtemp(join(tmpdir(), "rea-asar-integrity-mcp-"));
const source = join(root, "source");
const original = "console.log('ok');\n";
const changed = "changed();\n";
await mkdir(source);
await writeFile(join(source, "main.js"), original);
const archive = join(root, "fixture.asar");
await createPackageWithOptions(source, archive, { unpack: "*.js" });
await writeFile(join(`${archive}.unpacked`, "main.js"), changed);
const session = new BinarySession(new ArtifactProvider());
const server = createServer(session, session);
const client = new Client({ name: "asar-integrity-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const opened = await client.callTool({
name: "open_binary",
arguments: { path: archive },
});
expect(opened.isError).not.toBe(true);
const result = await client.callTool({
name: "inventory_artifact",
arguments: {},
});
expect(result.isError).toBe(true);
expect(result.structuredContent).toEqual({
error: {
tag: "ArtifactOperationError",
category: "integrity_mismatch",
message:
"Artifact is invalid or has changed. Get a fresh copy and try again.",
details: {
operation: "inventory_artifact",
reason: "integrity",
logicalPath: "main.js",
declaredSha256: createHash("sha256").update(original).digest("hex"),
calculatedSha256: createHash("sha256")
.update(changed)
.digest("hex"),
unpacked: true,
},
},
});
} finally {
await Promise.allSettled([
client.close(),
server.close(),
session.close(),
]);
}
});
it("rejects altered payloads that reuse session Evidence IDs", async () => {
const session = new BinarySession(() => ({
health: () => Promise.resolve(),
+31
View File
@@ -14,6 +14,19 @@ import {
sessionOutputSchemas,
} from "../src/contracts/toolOutputSchemas.js";
const emptySchemaPaths = (value: unknown, path = "$"): string[] => {
if (Array.isArray(value))
return value.flatMap((item, index) =>
emptySchemaPaths(item, `${path}[${String(index)}]`),
);
if (typeof value !== "object" || value === null) return [];
const entries = Object.entries(value);
if (entries.length === 0) return [path];
return entries.flatMap(([key, item]) =>
emptySchemaPaths(item, `${path}.${key}`),
);
};
describe("tool contract surface", () => {
it("advertises complete typed schemas and annotations for all analysis tools", () => {
const contracts = [
@@ -122,4 +135,22 @@ describe("tool contract surface", () => {
expect(contract.description).toMatch(/[.;]/u);
}
});
it("publishes no unconstrained output-schema holes across all 68 tools", () => {
const contracts = [
...OFFICIAL_TOOL_CONTRACTS,
...ENHANCED_TOOL_CONTRACTS,
...NATIVE_TOOL_CONTRACTS,
...ARTIFACT_TOOL_CONTRACTS,
...SESSION_TOOL_CONTRACTS,
];
expect(contracts).toHaveLength(68);
for (const contract of contracts) {
const schema = z.toJSONSchema(contract.outputSchema, {
target: "draft-07",
unrepresentable: "any",
});
expect(emptySchemaPaths(schema), contract.name).toEqual([]);
}
});
});
+16 -10
View File
@@ -312,7 +312,9 @@ describe("enhanced MCP tools", () => {
status: "error",
error: {
tag: "AnalysisOutputError",
message: "Invalid analysis output for decompile: failed",
category: "execution_failure",
message:
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
details: { operation: "decompile", reason: "failed" },
},
},
@@ -352,7 +354,11 @@ describe("enhanced MCP tools", () => {
{
address: "0x1",
status: "error",
error: { tag: "AnalysisOutputError" },
error: {
category: "execution_failure",
message:
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
},
},
],
},
@@ -551,8 +557,8 @@ describe("enhanced MCP tools", () => {
});
expect(result.isError).toBe(true);
const text = result.content.find((item) => item.type === "text");
expect(text?.type === "text" ? text.text : "").toContain(
"non-advancing pagination offset",
expect(text?.type === "text" ? text.text : "").toBe(
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
);
});
@@ -598,8 +604,8 @@ describe("enhanced MCP tools", () => {
});
expect(result.isError).toBe(true);
const text = result.content.find((item) => item.type === "text");
expect(text?.type === "text" ? text.text : "").toContain(
"HopperProtocolError",
expect(text?.type === "text" ? text.text : "").toBe(
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
);
});
@@ -619,8 +625,8 @@ describe("enhanced MCP tools", () => {
});
expect(result.isError).toBe(true);
const text = result.content.find((item) => item.type === "text");
expect(text?.type === "text" ? text.text : "").toContain(
"AnalysisOutputError",
expect(text?.type === "text" ? text.text : "").toBe(
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
);
});
@@ -655,8 +661,8 @@ describe("enhanced MCP tools", () => {
});
expect(result.isError).toBe(true);
const text = result.content.find((item) => item.type === "text");
expect(text?.type === "text" ? text.text : "").toContain(
"AnalysisOutputError",
expect(text?.type === "text" ? text.text : "").toBe(
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
);
});
+26 -3
View File
@@ -252,6 +252,17 @@ describe("full MCP integration with multi-tool sequences", () => {
},
});
expect(unavailable.isError).toBe(true);
expect(structured(unavailable)).toMatchObject({
error: {
tag: "AnalysisCapabilityUnavailableError",
category: "unsupported_provider",
details: {
providerId: "partial",
operation: "procedure_pseudo_code",
reason: "Decompiler is not installed.",
},
},
});
expect(received[0]).not.toHaveProperty("unknown_registry_approved");
expect(
structured(
@@ -354,7 +365,7 @@ describe("full MCP integration with multi-tool sequences", () => {
expect(names).not.toContain("open_binary");
});
it("propagates remote Hopper errors with structured content", async () => {
it("projects remote failures without provider or bridge details", async () => {
const client = await connect({
execute: () =>
Promise.resolve(err(new HopperRemoteError(-32000, "bridge timeout"))),
@@ -365,8 +376,20 @@ describe("full MCP integration with multi-tool sequences", () => {
arguments: {},
});
expect(result.isError).toBe(true);
expect(text(result)).toContain("HopperRemoteError");
expect(text(result)).toContain("bridge timeout");
expect(structured(result)).toMatchObject({
error: {
tag: "HopperRemoteError",
category: "execution_failure",
details: {
code: -32000,
safeMessage: "bridge timeout",
diagnosticType: "remote",
},
},
});
expect(text(result)).toBe(
"Analysis could not complete. Retry once; if it continues, run `rea doctor`.",
);
});
it("handles concurrent tool calls without corruption", async () => {
+1 -1
View File
@@ -199,7 +199,7 @@ describe("official Hopper proxy tools", () => {
expect(result.content).toEqual([
{
type: "text",
text: "HopperRemoteError: Hopper request failed (-1): denied",
text: "Analysis could not complete. Retry once; if it continues, run `rea doctor`.",
},
]);
});
+12 -2
View File
@@ -58,7 +58,9 @@ describe("target-free MCP lifecycle", () => {
arguments: {},
});
expect(before.isError).toBe(true);
expect(text(before)).toContain("NoBinaryOpenError");
expect(text(before)).toBe(
"No app is open. Ask the user which app to investigate, then call open_binary with its local path.",
);
expect((await mcp.listTools()).tools).toHaveLength(68);
const deniedCapture = await mcp.callTool({
name: "capture_process_scenario",
@@ -69,7 +71,15 @@ describe("target-free MCP lifecycle", () => {
},
});
expect(deniedCapture.isError).toBe(true);
expect(text(deniedCapture)).toContain("process capture is disabled");
expect(structured(deniedCapture)).toMatchObject({
error: {
tag: "ProcessCaptureError",
category: "permission_required",
},
});
expect(text(deniedCapture)).toBe(
"Process capture is disabled. Set `REA_PROCESS_CAPTURE_ENABLED=true`, configure approved roots, then restart REA.",
);
expect(
(await mcp.callTool({ name: "open_binary", arguments: { path: first } }))
.isError,
+13 -1
View File
@@ -26,9 +26,21 @@ describe("tool result projection", () => {
content: [
{
type: "text",
text: "AnalysisOutputError: Analysis output does not match the tool contract",
text: "Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
},
],
structuredContent: {
error: {
tag: "AnalysisOutputError",
category: "execution_failure",
message:
"Analysis returned an unreadable result. Retry once; if it continues, run `rea doctor`.",
details: {
operation: "provider_neutral_fixture",
reason: "output does not match the tool contract",
},
},
},
isError: true,
});
});