fix(mcp): deliver typed errors without invalid structured output (#1277)
* fix(mcp): deliver tool errors within advertised schema contracts Retain complete typed JSON error content while omitting non-conforming structured errors at the public transport boundary. Recover oversized diagnostics into Evidence before removing their private structured carrier. Update schema-validated client readers and cover actual stdio delivery. Closes #1261 Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing) * test(mcp): read typed errors in canonical Evidence regressions * test(mcp): validate public error diagnostics in acceptance workflows
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { requireMcpToolError } from "./mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
@@ -158,11 +159,11 @@ const verifyDefaultScreenshotDelivery = async ({
|
||||
);
|
||||
assert.equal(constrained.isError, true);
|
||||
assert.equal(
|
||||
constrained.structuredContent?.error?.details?.resource,
|
||||
requireMcpToolError(constrained)?.details?.resource,
|
||||
"transport",
|
||||
);
|
||||
const reference =
|
||||
constrained.structuredContent?.error?.details?.reported_limits
|
||||
requireMcpToolError(constrained)?.details?.reported_limits
|
||||
?.evidence_reference;
|
||||
assert.equal(reference?.kind, "retained-evidence");
|
||||
await client.ping();
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
import {
|
||||
requireMcpToolError,
|
||||
requireMcpLifecycleResult,
|
||||
requireMcpEvidenceResult,
|
||||
} from "./mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { randomUUID } from "node:crypto";
|
||||
@@ -15,10 +20,6 @@ import {
|
||||
} from "../../dist/domain/analysisSnapshot.js";
|
||||
import { createEvidence } from "../../dist/domain/evidence.js";
|
||||
import { createEvidenceBundle } from "../../dist/domain/evidenceBundle.js";
|
||||
import {
|
||||
requireMcpLifecycleResult,
|
||||
requireMcpEvidenceResult,
|
||||
} from "./mcp-verifier-results.mjs";
|
||||
|
||||
/** Reject a valid older snapshot whose instruction-only collector omitted a real edge. */
|
||||
export async function verifyLegacyGhidraReferenceSnapshot(
|
||||
@@ -124,7 +125,7 @@ export async function verifyLegacyGhidraReferenceSnapshot(
|
||||
},
|
||||
});
|
||||
assert.equal(reply.isError, true);
|
||||
const mcpRejection = reply.structuredContent.error;
|
||||
const mcpRejection = requireMcpToolError(reply);
|
||||
assert.equal(mcpRejection.code, "evidence_integrity_mismatch");
|
||||
assert.match(mcpRejection.details.reason, /profile_mismatch/u);
|
||||
assert.match(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { analysisErrorProjectionSchema } from "../../dist/contracts/errorSchemas.js";
|
||||
import { toolContract } from "../../dist/contracts/toolContracts.js";
|
||||
|
||||
/** Read the textual JSON projection from one MCP tool call. */
|
||||
@@ -9,6 +10,12 @@ export const mcpTextValue = (result) => {
|
||||
|
||||
const jsonValue = (result) => JSON.parse(mcpTextValue(result));
|
||||
|
||||
/** Read the complete canonical diagnostic from an MCP tool failure. */
|
||||
export const requireMcpToolError = (result) => {
|
||||
if (result.isError !== true) throw new Error("Expected an MCP tool failure");
|
||||
return analysisErrorProjectionSchema.parse(jsonValue(result).error);
|
||||
};
|
||||
|
||||
const requireOutput = (result, operation, contract) => {
|
||||
if (result.isError === true)
|
||||
throw new Error(`${operation} failed: ${mcpTextValue(result)}`);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { requireMcpToolError } from "./mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
/** Exercise segmented-address truncation through real CLI/MCP reads and edits. */
|
||||
@@ -40,7 +41,7 @@ export async function verifyGhidraAddressBoundaries(client, entry, cli) {
|
||||
{ timeout: 240000 },
|
||||
);
|
||||
assert.equal(reply.isError, true, `${name} accepted ${address}`);
|
||||
const error = reply.structuredContent?.error;
|
||||
const error = requireMcpToolError(reply);
|
||||
assert.equal(error?.code, "invalid_request", JSON.stringify(reply));
|
||||
assert.equal(error.details.operation, name);
|
||||
const issues = JSON.stringify(error.details.issues);
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
import {
|
||||
requireMcpToolError,
|
||||
mcpTextValue,
|
||||
requireMcpOperationResult,
|
||||
} from "./mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
@@ -7,11 +12,9 @@ import { setTimeout as delay } from "node:timers/promises";
|
||||
import { promisify } from "node:util";
|
||||
import Ajv from "ajv/dist/2020.js";
|
||||
import addFormats from "ajv-formats";
|
||||
|
||||
import { analysisErrorProjectionSchema } from "../../dist/contracts/errorSchemas.js";
|
||||
import {
|
||||
mcpTextValue,
|
||||
requireMcpOperationResult,
|
||||
} from "./mcp-verifier-results.mjs";
|
||||
|
||||
import { verifyLegacyGhidraReferenceSnapshot } from "./ghidra-reference-snapshot-e2e.mjs";
|
||||
import { verifyGhidraSnapshotLifecycle } from "./real-ghidra-snapshot-lifecycle.mjs";
|
||||
import { verifyGhidraTargetAdmission } from "./real-ghidra-target-admission.mjs";
|
||||
@@ -61,23 +64,19 @@ export async function verifyGhidraBoundaries(
|
||||
true,
|
||||
`${name} accepted ${JSON.stringify(args)}`,
|
||||
);
|
||||
// SDK input-schema rejections are text-only; application errors use the
|
||||
// canonical structured error projection, separately from success schemas.
|
||||
if (reply.structuredContent !== undefined) {
|
||||
analysisErrorProjectionSchema.parse(reply.structuredContent.error);
|
||||
assert.deepEqual(
|
||||
reply.structuredContent,
|
||||
JSON.parse(mcpTextValue(reply)),
|
||||
);
|
||||
}
|
||||
// SDK input rejections are prose; application errors carry canonical JSON
|
||||
// text without publishing data outside the advertised success schema.
|
||||
const error = mcpTextValue(reply).startsWith("{")
|
||||
? requireMcpToolError(reply)
|
||||
: undefined;
|
||||
assert.equal(reply.structuredContent, undefined);
|
||||
if (diagnostic !== undefined) {
|
||||
const error = reply.structuredContent?.error;
|
||||
assert.equal(error?.code, "invalid_request");
|
||||
assert.equal(error?.details?.operation, name);
|
||||
assert.match(JSON.stringify(error.details.issues), diagnostic);
|
||||
}
|
||||
rejectedCalls++;
|
||||
return reply.structuredContent?.error;
|
||||
return error;
|
||||
};
|
||||
const cli = async (command, value, flags = []) => {
|
||||
const { stdout } = await promisify(execFile)(
|
||||
@@ -481,7 +480,7 @@ export async function verifyGhidraBoundaries(
|
||||
options,
|
||||
);
|
||||
assert.equal(regexFailure.isError, true);
|
||||
const regexError = regexFailure.structuredContent.error;
|
||||
const regexError = requireMcpToolError(regexFailure);
|
||||
assert.equal(regexError.code, "resource_constraint");
|
||||
assert.equal(regexError.details.operation, "search_strings");
|
||||
assert.equal(regexError.details.resource, "memory");
|
||||
@@ -503,7 +502,7 @@ export async function verifyGhidraBoundaries(
|
||||
options,
|
||||
);
|
||||
assert.equal(compileFailure.isError, true);
|
||||
const compileError = compileFailure.structuredContent.error;
|
||||
const compileError = requireMcpToolError(compileFailure);
|
||||
assert.equal(compileError.code, "resource_constraint");
|
||||
assert.equal(compileError.details.operation, "search_procedures");
|
||||
assert.match(compileError.details.reason, /stack while compiling pattern/u);
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import {
|
||||
requireMcpToolError,
|
||||
requireMcpOperationResult,
|
||||
} from "./mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { access, mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { parseAnalysisSnapshot } from "../../dist/domain/analysisSnapshot.js";
|
||||
import { parseEvidenceBundle } from "../../dist/domain/evidenceBundle.js";
|
||||
import { requireMcpOperationResult } from "./mcp-verifier-results.mjs";
|
||||
|
||||
/** Keep mutable Ghidra observations out of immutable snapshots across target opens. */
|
||||
export async function verifyGhidraSnapshotLifecycle(
|
||||
@@ -31,7 +34,7 @@ export async function verifyGhidraSnapshotLifecycle(
|
||||
true,
|
||||
`${name} accepted a mutated snapshot binding`,
|
||||
);
|
||||
const error = reply.structuredContent?.error;
|
||||
const error = requireMcpToolError(reply);
|
||||
assert.equal(
|
||||
error?.code,
|
||||
"evidence_integrity_mismatch",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { requireMcpToolError } from "./lib/mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
@@ -232,12 +233,12 @@ const checkHardenedRuntime = async (directory, binary) => {
|
||||
{ timeout: CLI_TIMEOUT_MS },
|
||||
);
|
||||
assert.equal(deniedMcp.isError, true, JSON.stringify(deniedMcp));
|
||||
assert.equal(deniedMcp.structuredContent.error.code, denied.code);
|
||||
assert.equal(requireMcpToolError(deniedMcp).code, denied.code);
|
||||
assert.match(
|
||||
deniedMcp.structuredContent.error.details.reason,
|
||||
requireMcpToolError(deniedMcp).details.reason,
|
||||
/^debugger-attach-denied: /u,
|
||||
);
|
||||
assert.ok(deniedMcp.structuredContent.error.details.partial_observation);
|
||||
assert.ok(requireMcpToolError(deniedMcp).details.partial_observation);
|
||||
});
|
||||
const allowed = join(directory, "debuggable");
|
||||
const entitlements = join(directory, "get-task-allow.plist");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { requireMcpToolError } from "./lib/mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { join } from "node:path";
|
||||
import { execFile } from "node:child_process";
|
||||
@@ -50,7 +51,7 @@ const verifyMcpUnknownProvider = async (client, mcpOptions) => {
|
||||
);
|
||||
if (
|
||||
unknownProvider.isError !== true ||
|
||||
unknownProvider.structuredContent?.error?.details?.selection_reason !==
|
||||
requireMcpToolError(unknownProvider)?.details?.selection_reason !==
|
||||
"unknown_provider"
|
||||
)
|
||||
throw new Error("packaged MCP accepted an unknown analysis provider");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
import { requireMcpToolError } from "./lib/mcp-verifier-results.mjs";
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
@@ -172,7 +173,7 @@ const mcp = async (cliDossier) => {
|
||||
arguments: { address: "not-an-address" },
|
||||
});
|
||||
assert.equal(invalid.isError, true);
|
||||
assert.equal(invalid.structuredContent.error.code, "invalid_request");
|
||||
assert.equal(requireMcpToolError(invalid).code, "invalid_request");
|
||||
await call("close_binary", {});
|
||||
opened = false;
|
||||
report.checks.push(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { requireMcpToolError } from "../../lib/mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
access,
|
||||
@@ -208,13 +209,12 @@ export async function verifyAndroidFailureRecovery({
|
||||
{ timeout: 150_000 },
|
||||
);
|
||||
assert.equal(response.isError, true);
|
||||
assert.equal(response.structuredContent.error.code, "invalid_request");
|
||||
assert.deepEqual(
|
||||
response.structuredContent.error.details.issues[0].path,
|
||||
["class_name"],
|
||||
);
|
||||
assert.equal(requireMcpToolError(response).code, "invalid_request");
|
||||
assert.deepEqual(requireMcpToolError(response).details.issues[0].path, [
|
||||
"class_name",
|
||||
]);
|
||||
assert.match(
|
||||
response.structuredContent.error.details.issues[0].message,
|
||||
requireMcpToolError(response).details.issues[0].message,
|
||||
/search_android_classes/u,
|
||||
);
|
||||
assert.doesNotMatch(JSON.stringify(response), /rea doctor/u);
|
||||
@@ -231,8 +231,8 @@ export async function verifyAndroidFailureRecovery({
|
||||
},
|
||||
{ timeout: 150_000 },
|
||||
);
|
||||
assert.equal(method.structuredContent.error.code, "invalid_request");
|
||||
assert.deepEqual(method.structuredContent.error.details.issues[0].path, [
|
||||
assert.equal(requireMcpToolError(method).code, "invalid_request");
|
||||
assert.deepEqual(requireMcpToolError(method).details.issues[0].path, [
|
||||
"method_name",
|
||||
]);
|
||||
await client.ping();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { requireMcpToolError } from "../../lib/mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile, spawn } from "node:child_process";
|
||||
import { randomUUID } from "node:crypto";
|
||||
@@ -172,11 +173,11 @@ window.loadFile("renderer.html");require("node:fs").writeFileSync(${JSON.stringi
|
||||
{ timeout: 150_000 },
|
||||
);
|
||||
const capture = active.isError
|
||||
? active.structuredContent.error.details.partial_observation.capture
|
||||
? requireMcpToolError(active).details.partial_observation.capture
|
||||
: active.structuredContent.normalized_result;
|
||||
if (active.isError) {
|
||||
assert.equal(
|
||||
active.structuredContent.error.code,
|
||||
requireMcpToolError(active).code,
|
||||
"cleanup_incomplete",
|
||||
JSON.stringify(active),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user