feat(import): preserve supported HTTP contracts

This commit is contained in:
bsodfather committed 2026-09-13 03:30:31 +03:00
1 parent 083974c27f
commit f8ee7c9bfe
87 files changed
+7496 -534

No files matched your search

@@ -2,12 +2,13 @@ use std::collections::BTreeMap;
use std::time::Instant;
use crank_community_mcp::manifest::{
analyze_published_tool_catalog, catalog_tool_definitions, tool_definitions,
analyze_published_tool_catalog, catalog_tool_definitions, schema_to_json_schema,
tool_definitions,
};
use crank_core::{
ExecutionConfig, HttpMethod, Operation, OperationId, OperationSecurityLevel, OperationStatus,
Protocol, RestTarget, Target, ToolAccessMode, ToolDescription, ToolSelectionPolicy,
WorkspaceId,
Protocol, RestRequestContract, RestRequestContractVersion, RestRequestDescriptor, RestTarget,
Target, ToolAccessMode, ToolDescription, ToolSelectionPolicy, WorkspaceId,
};
use crank_mapping::MappingSet;
use crank_registry::{PublishedAgentCatalog, PublishedAgentTool, RegistryOperation};
@@ -61,6 +62,107 @@ fn marks_destructive_tools_as_two_step_confirmation_calls() {
assert_eq!(definition["inputSchema"]["required"], json!(["base"]));
}
#[test]
fn projects_nullable_and_binary_as_valid_closed_json_schema() {
let nullable = Schema {
kind: SchemaKind::String,
nullable: true,
..empty_schema(SchemaKind::String)
};
let projected = schema_to_json_schema(&nullable);
assert_eq!(projected["type"], json!(["string", "null"]));
assert!(projected.get("nullable").is_none());
let nullable_enum = Schema {
kind: SchemaKind::Enum,
nullable: true,
enum_values: vec!["draft".to_owned()],
..empty_schema(SchemaKind::Enum)
};
let projected = schema_to_json_schema(&nullable_enum);
assert_eq!(projected["type"], json!(["string", "null"]));
assert_eq!(projected["enum"], json!(["draft", null]));
let binary = Schema {
kind: SchemaKind::Object,
format: Some("binary".to_owned()),
fields: BTreeMap::from([(
"data_base64".to_owned(),
Schema {
required: true,
..empty_schema(SchemaKind::String)
},
)]),
..empty_schema(SchemaKind::Object)
};
let projected = schema_to_json_schema(&binary);
assert_eq!(projected["additionalProperties"], false);
assert_eq!(projected["format"], "binary");
}
#[test]
fn optional_request_pair_uses_closed_presence_and_absence_branches() {
let mut tool = published_tool();
let selected = Schema {
kind: SchemaKind::Object,
format: Some(crank_schema::CLOSED_OBJECT_SCHEMA_FORMAT.to_owned()),
fields: BTreeMap::from([
(
"body".to_owned(),
Schema {
required: true,
..empty_schema(SchemaKind::Object)
},
),
(
"content_type".to_owned(),
Schema {
kind: SchemaKind::Enum,
required: true,
enum_values: vec!["application/json".to_owned()],
..empty_schema(SchemaKind::Enum)
},
),
]),
..empty_schema(SchemaKind::Object)
};
let absent = Schema {
kind: SchemaKind::Object,
format: Some(crank_schema::CLOSED_OBJECT_SCHEMA_FORMAT.to_owned()),
..empty_schema(SchemaKind::Object)
};
tool.operation.input_schema = Schema {
kind: SchemaKind::Oneof,
variants: vec![selected, absent],
..empty_schema(SchemaKind::Oneof)
};
let Target::Rest(target) = &mut tool.operation.target;
target.request_contract = Some(RestRequestContract {
version: RestRequestContractVersion::V1,
body_field: "body".to_owned(),
selector_field: Some("content_type".to_owned()),
body_required: false,
variants: vec![RestRequestDescriptor {
media_type: "application/json".to_owned(),
schema_variant: 0,
}],
});
let schema = &tool_definitions(&tool)[0]["inputSchema"];
assert_eq!(schema["anyOf"][0]["additionalProperties"], false);
assert_eq!(
schema["anyOf"][0]["required"],
json!(["body", "content_type"])
);
assert_eq!(schema["anyOf"][1]["additionalProperties"], false);
assert!(
schema["anyOf"][1]["properties"]
.as_object()
.unwrap()
.is_empty()
);
}
#[test]
fn catalog_budget_uses_the_same_definitions_as_tools_list() {
let tool = published_tool();
@@ -154,10 +256,14 @@ fn operation() -> RegistryOperation {
method: HttpMethod::Get,
path_template: "/v2/rates".to_owned(),
static_headers: BTreeMap::new(),
request_contract: None,
response_contract: Vec::new(),
multipart_field_mime_types: BTreeMap::new(),
}),
input_schema: input_schema(),
output_schema: Schema {
kind: SchemaKind::Object,
format: None,
description: None,
required: false,
nullable: false,
@@ -201,6 +307,7 @@ fn input_schema() -> Schema {
"base".to_owned(),
Schema {
kind: SchemaKind::String,
format: None,
description: None,
required: true,
nullable: false,
@@ -215,6 +322,7 @@ fn input_schema() -> Schema {
"mode".to_owned(),
Schema {
kind: SchemaKind::Enum,
format: None,
description: None,
required: false,
nullable: false,
@@ -228,6 +336,7 @@ fn input_schema() -> Schema {
Schema {
kind: SchemaKind::Object,
format: None,
description: None,
required: false,
nullable: false,
@@ -239,6 +348,21 @@ fn input_schema() -> Schema {
}
}
fn empty_schema(kind: SchemaKind) -> Schema {
Schema {
kind,
format: None,
description: None,
required: false,
nullable: false,
default_value: None,
fields: BTreeMap::new(),
items: None,
enum_values: Vec::new(),
variants: Vec::new(),
}
}
fn timestamp(value: &str) -> OffsetDateTime {
OffsetDateTime::parse(value, &Rfc3339).unwrap()
}