fix(mcp): allow runtime error details in tool output schemas
This commit is contained in:
@@ -15,16 +15,42 @@ class McpContractTest {
|
||||
assertEquals(17, catalog.names().size());
|
||||
for (var name : catalog.names()) {
|
||||
var definition = catalog.definition(name);
|
||||
catalog.validateArguments(name, definition.get("exampleArguments"));
|
||||
assertDoesNotThrow(() -> catalog.validateArguments(name, definition.get("exampleArguments")), name + " arguments");
|
||||
var result = McpToolResult.success(mapper, (ObjectNode) definition.get("exampleValue"));
|
||||
ContractSchema.validate(definition.get("outputSchema"), result.wire().get("structuredContent"));
|
||||
assertDoesNotThrow(() -> ContractSchema.validate(definition.get("outputSchema"), result.wire().get("structuredContent")), name + " success envelope");
|
||||
var failure = McpToolResult.failure(mapper, new AutomationFailure(AutomationFailure.Code.INVALID_TASK, "Revoked"));
|
||||
ContractSchema.validate(definition.get("outputSchema"), failure.wire().get("structuredContent"));
|
||||
assertDoesNotThrow(() -> ContractSchema.validate(definition.get("outputSchema"), failure.wire().get("structuredContent")), name + " failure envelope");
|
||||
assertTrue(Set.of("authenticated", "granted", "task").contains(definition.get("authority").asText()));
|
||||
assertEquals(definition.get("authority").asText().equals("task"), definition.get("inputSchema").get("properties").has("taskId"));
|
||||
assertFalse(catalog.descriptor(name).has("exampleArguments"));
|
||||
}
|
||||
}
|
||||
@Test void failureSchemasPreserveRuntimeDetailsWithoutAllowingUnknownEnvelopeFields() {
|
||||
var details = Map.<String, Object>of(
|
||||
"code", "InvalidProgram",
|
||||
"details", Map.of("reason", "LifecycleSignature", "offset", 123,
|
||||
"context", List.of("boot", "verification")));
|
||||
var failure = new AutomationFailure(AutomationFailure.Code.RUNTIME_INCOMPATIBLE, "Rejected", details);
|
||||
for (var name : catalog.names()) {
|
||||
var wire = McpToolResult.failure(mapper, failure).wire();
|
||||
ObjectNode envelope = (ObjectNode) wire.get("structuredContent");
|
||||
var schema = catalog.definition(name).get("outputSchema");
|
||||
assertDoesNotThrow(() -> ContractSchema.validate(schema, envelope), name + " runtime details");
|
||||
assertEquals(mapper.valueToTree(details), envelope.path("error").path("runtimeError"), name);
|
||||
assertEquals(envelope.toString(), wire.path("content").get(0).path("text").asText(), name);
|
||||
assertTrue(wire.path("isError").asBoolean(), name);
|
||||
|
||||
ObjectNode error = (ObjectNode) envelope.get("error");
|
||||
error.put("runtimeError", "invalid");
|
||||
assertThrows(IllegalArgumentException.class, () -> ContractSchema.validate(schema, envelope), name);
|
||||
error.set("runtimeError", mapper.createObjectNode());
|
||||
error.put("unexpected", true);
|
||||
assertThrows(IllegalArgumentException.class, () -> ContractSchema.validate(schema, envelope), name);
|
||||
error.remove("unexpected");
|
||||
envelope.put("unexpected", true);
|
||||
assertThrows(IllegalArgumentException.class, () -> ContractSchema.validate(schema, envelope), name);
|
||||
}
|
||||
}
|
||||
@Test void missingRootTaskUnexpectedFieldsAndWrongTypesAreRejected() {
|
||||
for (var name : catalog.names()) {
|
||||
ObjectNode sample = catalog.definition(name).get("exampleArguments").deepCopy();
|
||||
|
||||
Reference in New Issue
Block a user