123 lines
8.1 KiB
TypeScript
123 lines
8.1 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { CallToolResultSchema } from "@modelcontextprotocol/core"
|
|
import type { CallToolResult } from "@modelcontextprotocol/server"
|
|
import { boundedToolError, boundedToolResult, MCP_CALL_RESULT_BYTES, RESULT_BUDGET_META } from "./result-budget.js"
|
|
|
|
const id = "00000000-0000-4000-8000-000000000001", plan = "00000000-0000-4000-8000-000000000002"
|
|
const projected = (result: CallToolResult) => (result.structuredContent as { result: Record<string, unknown> }).result
|
|
function accepted(output: unknown, projection: "read" | "operation" | "plan" = "read", meta?: Record<string, unknown>) {
|
|
const decision = boundedToolResult(output, projection, meta)
|
|
assert.equal(decision.kind, "result")
|
|
if (decision.kind !== "result") throw Error("Missing result")
|
|
assert.equal(decision.bytes, Buffer.byteLength(JSON.stringify(decision.result)))
|
|
assert(decision.bytes <= MCP_CALL_RESULT_BYTES)
|
|
assert(CallToolResultSchema.safeParse(decision.result).success)
|
|
return decision.result
|
|
}
|
|
test("normal result keeps SDK content/result shape and metadata", () => {
|
|
const output = { items: [{ name: "узел 🛰️", quality: "unknown" }], nextOffset: 1 }
|
|
const result = accepted(output, "read", { trace: "ok" })
|
|
assert.deepEqual(result.structuredContent, { result: output })
|
|
assert.deepEqual(result._meta, { trace: "ok" })
|
|
assert.equal(result.content[0].type === "text" && result.content[0].text, JSON.stringify(output))
|
|
})
|
|
test("UTF8, double encoding and metadata each contribute to the full limit", () => {
|
|
for (const value of ["a".repeat(530000), "🛰️".repeat(80000), '\\"'.repeat(140000)]) {
|
|
assert(Buffer.byteLength(JSON.stringify({ value })) < MCP_CALL_RESULT_BYTES)
|
|
assert.equal(accepted({ value }).isError, true)
|
|
}
|
|
assert.equal(accepted({ items: [] }, "read", { trace: "m".repeat(MCP_CALL_RESULT_BYTES) }).isError, true)
|
|
})
|
|
test("exact inclusive full result boundary, no arbitrary JSON truncation", () => {
|
|
const overhead = Buffer.byteLength(JSON.stringify({ content: [{ type: "text", text: JSON.stringify({ x: "" }) }], structuredContent: { result: { x: "" } } }))
|
|
const length = Math.floor((MCP_CALL_RESULT_BYTES - overhead) / 2)
|
|
const fit = accepted({ x: "a".repeat(length) })
|
|
assert.equal(fit.isError, undefined)
|
|
assert.equal(accepted({ x: "a".repeat(length + 1) }).isError, true)
|
|
})
|
|
test("oversized applied/running/unknown outcomes preserve actual identity and verification without counts", () => {
|
|
for (const state of ["applied", "running", "unknown", "partial", "failed", "cancelled"]) {
|
|
const output = { id, plan_id: plan, state, verification: "inconclusive", error_code: null,
|
|
result: { details: "z".repeat(MCP_CALL_RESULT_BYTES), confirmedWrites: 123, secret: "do not project" } }
|
|
const result = accepted(output, "operation")
|
|
assert.equal(result.isError, undefined)
|
|
assert.deepEqual(projected(result), { id, plan_id: plan, state, verification: "inconclusive", error_code: null })
|
|
assert.deepEqual(result._meta?.[RESULT_BUDGET_META], { version: 1, detailsOmitted: true, reason: "byte-budget", automaticReplay: false })
|
|
assert(!JSON.stringify(result).includes("confirmedWrites"))
|
|
assert.equal(output.state, state)
|
|
}
|
|
})
|
|
test("non-JSON detail cannot overwrite the already applied receipt", () => {
|
|
for (const detail of [1n, undefined, { toJSON: () => { throw Error("private failure") } }]) {
|
|
const output = { id, plan_id: plan, state: "applied", verification: "passed", result: detail }
|
|
// Undefined property is JSON-compatible; BigInt/throwing toJSON require receipt.
|
|
const result = accepted(output, "operation")
|
|
assert.equal(result.isError, undefined)
|
|
assert.equal(projected(result).state, "applied")
|
|
}
|
|
const output: Record<string, unknown> = { id, plan_id: plan, state: "applied", verification: "passed" }
|
|
output.result = output
|
|
assert.equal(projected(accepted(output, "operation")).state, "applied")
|
|
})
|
|
test("compact receipt keeps a known pre-dispatch stop and its verified prefix", () => {
|
|
for (const [state, confirmedWrites] of [["rejected", 0], ["partial", 2]] as const) {
|
|
const result = { failedStepDispatched: false, confirmedWrites }
|
|
const output = { id, plan_id: plan, state, verification: "inconclusive", error_code: "STATE_CONFLICT" }
|
|
assert.deepEqual(projected(accepted({ ...output, result: { ...result, detail: 1n } }, "operation")), { ...output, result })
|
|
for (const invalid of [{ ...result, confirmedWrites: -1 }, { ...result, failedStepDispatched: true }]) {
|
|
assert.deepEqual(projected(accepted({ ...output, result: { ...invalid, detail: 1n } }, "operation")), output)
|
|
}
|
|
}
|
|
})
|
|
test("prepare omission keeps real plan/state, requests review, never invents executionAvailable", () => {
|
|
const result = accepted({ planId: plan, state: "approved", expiresAt: "2026-10-31T12:00:00Z", executionAvailable: true, changes: "x".repeat(MCP_CALL_RESULT_BYTES) }, "plan")
|
|
assert.equal(result.isError, undefined)
|
|
assert.deepEqual(projected(result), { planId: plan, state: "approved", expiresAt: "2026-10-31T12:00:00Z" })
|
|
assert.equal((result._meta?.[RESULT_BUDGET_META] as Record<string, unknown>).reviewRequired, true)
|
|
assert(!JSON.stringify(result).includes("executionAvailable"))
|
|
})
|
|
test("malformed effect receipt is explicit integration stop, never fabricated failed/unknown", () => {
|
|
assert.deepEqual(boundedToolResult({ result: "x".repeat(MCP_CALL_RESULT_BYTES) }, "operation"), { kind: "receipt-required", reason: "byte-budget" })
|
|
const output = { get id() { throw Error("must not read getter") }, plan_id: plan, state: "applied", verification: "passed" }
|
|
assert.deepEqual(boundedToolResult(output, "operation"), { kind: "receipt-required", reason: "not-json" })
|
|
})
|
|
test("read unserializable output is bounded error with no empty healthy success", () => {
|
|
for (const output of [undefined, 1n, { toJSON: () => { throw Error("secret") } }]) {
|
|
const result = accepted(output)
|
|
assert.equal(result.isError, true)
|
|
assert.equal(result.structuredContent, undefined)
|
|
assert(!JSON.stringify(result).includes("secret"))
|
|
}
|
|
})
|
|
test("snapshot prevents post-check mutation/toJSON from enlarging wire result", () => {
|
|
let calls = 0
|
|
const result = accepted({ toJSON: () => ({ value: ++calls === 1 ? "short" : "x".repeat(MCP_CALL_RESULT_BYTES) }) })
|
|
JSON.stringify(result)
|
|
assert.equal(calls, 1)
|
|
})
|
|
test("bounded existing error preserves NetFlow error code, no metadata/raw error leakage", () => {
|
|
const error = boundedToolError("RATE_LIMITED", "Ресурс чтения NetFlow занят".repeat(10000))
|
|
assert(CallToolResultSchema.safeParse(error).success)
|
|
assert(Buffer.byteLength(JSON.stringify(error)) < MCP_CALL_RESULT_BYTES)
|
|
assert.equal(error.isError, true)
|
|
assert.equal(error.content[0].type === "text" && JSON.parse(error.content[0].text).code, "RATE_LIMITED")
|
|
const quality = { quality: "unknown", complete: false, losses: { received: 7, dropped: 2 }, nextOffset: null }
|
|
assert.deepEqual(accepted(quality).structuredContent, { result: quality })
|
|
})
|
|
test("aggregate small fields overflow even when no individual string exceeds16k", () => {
|
|
const result = accepted({ items: Array.from({ length: 500 }, () => ({ name: "a".repeat(1200) })) })
|
|
assert.equal(result.isError, true)
|
|
})
|
|
test("unserializable metadata preserves an actual passed operation with actual error code", () => {
|
|
const metadata: Record<string, unknown> = {}; metadata.self = metadata
|
|
const result = accepted({ id, plan_id: plan, state: "applied", verification: "passed", error_code: "REAL_CODE" }, "operation", metadata)
|
|
assert.deepEqual(projected(result), { id, plan_id: plan, state: "applied", verification: "passed", error_code: "REAL_CODE" })
|
|
assert.equal(result.isError, undefined)
|
|
assert.equal((result._meta?.[RESULT_BUDGET_META] as Record<string, unknown>).reason, "not-json")
|
|
})
|
|
test("hostile proxy cannot escape into the generic tool-error catch", () => {
|
|
const proxy = new Proxy({}, { getOwnPropertyDescriptor: () => { throw Error("private") }, get: () => { throw Error("private") } })
|
|
assert.deepEqual(boundedToolResult(proxy, "operation"), { kind: "receipt-required", reason: "not-json" })
|
|
})
|