Skip to content

Commit d6f88be

Browse files
authored
fix(mcp): summarize copied browser observations in input declarations (morluto#7469)
Declare copied complete browser observations as abbreviated object operands, keeping canonical validation and normalization controls unchanged. Preserve all other tool declarations and Evidence identities. Refs morluto#4973 and morluto#4081.
1 parent b8cdbdf commit d6f88be

8 files changed

Lines changed: 158 additions & 66 deletions

‎docs/mcp-contracts.md‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,11 +1248,22 @@ failures for such entries name this cause and the operation to rerun with
12481248
Advertised input and output schemas contain no reachable recursive references.
12491249
Input schemas inline repeated definitions before applying the selected
12501250
presentation profile. Both `full` and `compact` start from this self-contained
1251-
projection; output schemas can share schema-local definitions. Full inputs
1252-
retain nested fields, descriptions and valid literal examples. The compatibility
1251+
projection; output schemas can share schema-local definitions. Analyst-authored
1252+
full inputs retain nested fields, descriptions and valid literal examples. The compatibility
12531253
projections below can relax advertised constraints; canonical runtime validation
12541254
still enforces the complete input contract.
12551255

1256+
`compare_web_captures` declares its complete producer observations as object
1257+
operands instead of repeating their full output schemas in the input catalog.
1258+
Pass the original `inspect_web_page` normalized result in `before.inspection`
1259+
and `after.inspection`, optional complete `discover_webmcp_tools` results in
1260+
`webmcp`, or complete `capture_browser_scenario` results in the scenario fields.
1261+
Do not construct partial observations from the abbreviated declaration: the
1262+
server still validates every canonical field before comparison. Input groups
1263+
and analyst-authored normalization rules remain explicit. This presentation
1264+
changes no capture bytes, identity, completeness, or comparison semantics and
1265+
does not reduce `inspect_analysis_view` selectors or other tools' declarations.
1266+
12561267
When a host forwards input schemas to the model, their examples consume context.
12571268
REA's advertised examples therefore never copy complete producer Evidence.
12581269
An embedded Evidence record is shown as its exact `{"kind":"retained-evidence","evidence_id":"ev_..."}`
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { Ajv } from "ajv";
2+
import { Ajv2020 } from "ajv/dist/2020.js";
3+
import { expect, it } from "vitest";
4+
5+
import { presentInputJsonSchema } from "./inputSchemaPresentation.js";
6+
7+
it.each(["draft-2020-12", "draft-07"] as const)(
8+
"summarizes only declared observation schemas, preserving literal data in %s",
9+
(target) => {
10+
const literal = {
11+
type: "object",
12+
"x-rea-complete-observation": true,
13+
properties: { producer_data: { type: "string" } },
14+
};
15+
const controls = {
16+
type: "object",
17+
properties: { match: { type: "string", minLength: 1 } },
18+
required: ["match"],
19+
additionalProperties: false,
20+
};
21+
const canonical = {
22+
$schema:
23+
target === "draft-07"
24+
? "http://json-schema.org/draft-07/schema#"
25+
: "https://json-schema.org/draft/2020-12/schema",
26+
type: "object",
27+
properties: {
28+
observation: {
29+
type: "object",
30+
description: "Complete result from the producer.",
31+
"x-rea-complete-observation": true,
32+
properties: { id: { type: "string" } },
33+
required: ["id"],
34+
},
35+
controls,
36+
literal: { const: literal },
37+
},
38+
required: ["observation", "controls", "literal"],
39+
additionalProperties: false,
40+
};
41+
const presented = presentInputJsonSchema(canonical, () => undefined);
42+
expect(presented).toHaveProperty("properties.observation", {
43+
type: "object",
44+
description: expect.stringContaining("complete canonical observation"),
45+
});
46+
expect(presented).toHaveProperty("properties.controls", controls);
47+
expect(presented).toHaveProperty("properties.literal.const", literal);
48+
const ajv =
49+
target === "draft-07"
50+
? new Ajv({ strict: false })
51+
: new Ajv2020({ strict: false });
52+
const validate = ajv.compile(presented);
53+
expect(
54+
validate({ observation: {}, controls: { match: "x" }, literal }),
55+
).toBe(true);
56+
expect(
57+
validate({ observation: "{}", controls: { match: "x" }, literal }),
58+
).toBe(false);
59+
expect(
60+
validate({ observation: {}, controls: { match: "" }, literal }),
61+
).toBe(false);
62+
expect(
63+
ajv.compile(canonical)({
64+
observation: {},
65+
controls: { match: "x" },
66+
literal,
67+
}),
68+
).toBe(false);
69+
},
70+
);

‎src/contracts/inputSchemaPresentation.ts‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,47 @@ const describeSchema = (
4242
references: JsonSchemaReferences,
4343
inherited: JsonSchemaScope | undefined,
4444
describeProperty: (property: string) => string | undefined,
45+
propertyDescription?: string,
4546
): unknown => {
4647
if (!isObject(schema)) return schema;
48+
// These operands are complete producer results, not fields an agent builds.
49+
// Their owner keeps full canonical validation; only their wire declaration
50+
// omits the repeated result schema. Analyst-authored controls stay explicit.
51+
if (schema.type === "object" && schema["x-rea-complete-observation"] === true)
52+
return {
53+
type: "object",
54+
description: [
55+
propertyDescription ??
56+
(typeof schema.description === "string"
57+
? schema.description
58+
: undefined),
59+
"Pass the complete returned normalized_result unchanged. Its fields are omitted from this declaration; the server still validates the complete canonical observation.",
60+
]
61+
.filter((text) => text !== undefined)
62+
.join(" "),
63+
};
4764
const scope = references.enter(schema, inherited);
48-
return Object.fromEntries(
65+
const described = Object.fromEntries(
4966
Object.entries(schema).map(([key, value]) => {
5067
if (schemaMaps.has(key) && isObject(value))
5168
return [
5269
key,
5370
Object.fromEntries(
5471
Object.entries(value).map(([name, child]) => {
55-
const described = describeSchema(
56-
child,
57-
references,
58-
scope,
59-
describeProperty,
60-
);
61-
if (
62-
key !== "properties" ||
63-
!isObject(described) ||
64-
!isObject(child)
65-
)
66-
return [name, described];
6772
const description =
68-
referencedDescription(references, child, scope) ??
69-
describeProperty(name);
73+
key === "properties" && isObject(child)
74+
? (referencedDescription(references, child, scope) ??
75+
describeProperty(name))
76+
: undefined;
7077
return [
7178
name,
72-
description === undefined
73-
? described
74-
: { ...described, description },
79+
describeSchema(
80+
child,
81+
references,
82+
scope,
83+
describeProperty,
84+
description,
85+
),
7586
];
7687
}),
7788
),
@@ -96,6 +107,9 @@ const describeSchema = (
96107
return [key, value];
97108
}),
98109
);
110+
return propertyDescription === undefined
111+
? described
112+
: { ...described, description: propertyDescription };
99113
};
100114

101115
const objectBranch = (

‎src/contracts/toolSchemaMetadata.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const toolInputSchemaWithMetadata = <Contract extends ToolContract>(
8585
},
8686
);
8787

88-
/** Return the same complete input JSON Schema projection registered on MCP. */
88+
/** Return the full-profile advertised input JSON Schema registered on MCP. */
8989
export const advertisedInputJsonSchema = <Contract extends ToolContract>(
9090
contract: Contract,
9191
): Record<string, unknown> =>

‎src/domain/browserScenarioDiffValues.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ const normalizationInputSchema = z
7474

7575
/** Input for step-aligned comparison of two browser scenario captures. */
7676
export const compareBrowserScenariosInputSchema = z.strictObject({
77-
before_scenario: retainedBrowserScenarioCaptureSchema.describe(
78-
"Earlier complete normalized_result from capture_browser_scenario.",
79-
),
80-
after_scenario: retainedBrowserScenarioCaptureSchema.describe(
81-
"Later complete normalized_result from capture_browser_scenario.",
82-
),
77+
before_scenario: retainedBrowserScenarioCaptureSchema
78+
.describe(
79+
"Earlier complete normalized_result from capture_browser_scenario.",
80+
)
81+
.meta({ "x-rea-complete-observation": true }),
82+
after_scenario: retainedBrowserScenarioCaptureSchema
83+
.describe("Later complete normalized_result from capture_browser_scenario.")
84+
.meta({ "x-rea-complete-observation": true }),
8385
normalization: normalizationInputSchema
8486
.default({ rules: [] })
8587
.describe("Exact-literal rules committed in the comparison result."),

‎src/domain/webCaptureDiffSchemas.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { webMcpDiscoverySchema } from "./webMcpDiscovery.js";
66

77
/** One normalized passive page snapshot accepted by capture comparison. */
88
export const captureSnapshotSchema = z.object({
9-
inspection: webPageInspectionSchema.describe(
10-
"Complete normalized_result from inspect_web_page, including capture identity, observations, completeness, and limitations.",
11-
),
9+
inspection: webPageInspectionSchema
10+
.describe(
11+
"Complete normalized_result from inspect_web_page, including capture identity, observations, completeness, and limitations.",
12+
)
13+
.meta({ "x-rea-complete-observation": true }),
1214
webmcp: webMcpDiscoverySchema
15+
.meta({ "x-rea-complete-observation": true })
1316
.nullable()
1417
.default(null)
1518
.describe(

‎tests/boundary/mcp/browserComparisonInput.test.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,23 @@ function invalidComparisonInputs(
6262
const scenarioCapture = browserScenarioCaptureSchema.parse(
6363
scenario.before_scenario,
6464
);
65-
return [
65+
const runtimeOnlyInvalid = [
66+
{ before: { inspection: {} }, after: passive.after },
67+
{ before: passive.before, after: { ...passive.after, webmcp: {} } },
68+
{ before_scenario: {}, after_scenario: {} },
69+
{ ...scenario, before_scenario: { ...scenarioCapture, steps: [{}] } },
70+
];
71+
const advertisedInvalid = [
6672
{},
6773
{ before: passive.before },
68-
{ before: { inspection: {} }, after: passive.after },
74+
{ before: { inspection: "{}" }, after: passive.after },
6975
{
7076
before: passive.before,
71-
after: { ...passive.after, webmcp: {} },
77+
after: { ...passive.after, webmcp: "{}" },
7278
},
73-
{ before_scenario: {}, after_scenario: {} },
79+
{ before_scenario: "{}", after_scenario: scenarioCapture },
7480
{ before_scenario: scenario.before_scenario },
7581
{ ...passive, unexpected: true },
76-
{
77-
...scenario,
78-
before_scenario: { ...scenarioCapture, steps: [{}] },
79-
},
8082
{
8183
...scenario,
8284
normalization: {
@@ -86,6 +88,7 @@ function invalidComparisonInputs(
8688
},
8789
},
8890
];
91+
return { advertisedInvalid, runtimeOnlyInvalid };
8992
}
9093

9194
describe("browser comparison input boundary", () => {
@@ -118,6 +121,10 @@ describe("browser comparison input boundary", () => {
118121
({ name }) => name === contract.name,
119122
);
120123
if (advertised === undefined) throw new Error("Missing comparison tool");
124+
// Claude Desktop rejects tools above its 64 KiB per-input-schema limit.
125+
expect(
126+
Buffer.byteLength(JSON.stringify(advertised.inputSchema)),
127+
).toBeLessThanOrEqual(65_536);
121128
expect(advertised.inputSchema.type).toBe("object");
122129
// Anthropic tool input schemas reject root combinators.
123130
expect(advertised.inputSchema).not.toHaveProperty("anyOf");
@@ -146,6 +153,9 @@ describe("browser comparison input boundary", () => {
146153
"before + after; before_scenario + after_scenario + [normalization]",
147154
);
148155
const runtimeOnlyInvalid = [
156+
// Complete observations are copied from producer results, not authored
157+
// from their declaration. Malformed payloads still fail before effects.
158+
...invalid.runtimeOnlyInvalid,
149159
{ ...passive, ...scenario },
150160
{ ...passive, normalization: { rules: [] } },
151161
{
@@ -165,7 +175,7 @@ describe("browser comparison input boundary", () => {
165175
runtimeValid: true,
166176
}),
167177
),
168-
...invalid.map((input) => ({
178+
...invalid.advertisedInvalid.map((input) => ({
169179
input,
170180
advertisedValid: false,
171181
runtimeValid: false,

‎tests/boundary/mcp/toolSchemaValidity.test.ts‎

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -582,34 +582,16 @@ describe("MCP JSON Schema validity", () => {
582582
expectKnownAuthorityHints(tools);
583583
const byName = new Map(tools.map((tool) => [tool.name, tool]));
584584
expectManagedLocalSignatureSchema(byName.get("inspect_managed_members"));
585+
const comparison = byName.get("compare_web_captures")?.inputSchema;
585586
for (const side of ["before", "after"])
586-
expect(byName.get("compare_web_captures")?.inputSchema).toHaveProperty(
587-
[
588-
"properties",
589-
side,
590-
"properties",
591-
"inspection",
592-
"properties",
593-
"scripts",
594-
"properties",
595-
"items",
596-
"items",
597-
"properties",
598-
"source",
599-
"anyOf",
600-
],
601-
expect.arrayContaining([
602-
expect.objectContaining({
603-
properties: expect.objectContaining({
604-
reason: { type: "string" },
605-
}),
606-
}),
607-
expect.objectContaining({
608-
properties: expect.objectContaining({
609-
artifact: expect.objectContaining({ type: "object" }),
610-
}),
611-
}),
612-
]),
587+
expect(comparison).toHaveProperty(
588+
["properties", side, "properties", "inspection"],
589+
{
590+
type: "object",
591+
description: expect.stringContaining(
592+
"complete canonical observation",
593+
),
594+
},
613595
);
614596
const catalogProjection = tools.map((tool) => ({
615597
name: tool.name,

0 commit comments

Comments
 (0)