Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use reqwest::{
Client as HttpClient,
};
use serde::{Deserialize, Serialize};
use serde_json::map::Entry;
use serde_json::{json, Value};
use std::cmp::Ordering;
use std::collections::HashSet;
Expand Down Expand Up @@ -4579,15 +4580,23 @@ fn rewrite_tools_list(body: &str, creds: Option<&Credentials>) -> String {
// Normalize no-arg object schemas so OpenAI-style function conversion
// always gets an explicit `properties` object.
if schema.get("type").and_then(|t| t.as_str()) == Some("object") {
schema
.entry("properties".to_string())
.or_insert_with(|| json!({}));
schema
.entry("required".to_string())
.or_insert_with(|| json!([]));
schema
.entry("additionalProperties".to_string())
.or_insert_with(|| json!(false));
let had_properties = match schema.entry("properties".to_string()) {
Entry::Occupied(_) => true,
Entry::Vacant(v) => {
v.insert(json!({}));
false
}
};
if let Entry::Vacant(v) = schema.entry("required".to_string()) {
v.insert(json!([]));
}
if !had_properties {
if let Entry::Vacant(v) =
schema.entry("additionalProperties".to_string())
{
v.insert(json!(false));
}
}
}
Comment on lines +4582 to +4600
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The normalization logic for inputSchema is correctly implemented to ensure OpenAI compatibility while preserving existing additionalProperties semantics. However, the current implementation performs multiple redundant lookups on the schema map (e.g., get, entry, entry, entry). While not a critical performance bottleneck in this context, combining these into a single entry-based match would be more efficient and idiomatic Rust.

References
  1. Prefer efficient map access patterns in Rust to avoid redundant lookups and allocations. (link)


// Add proxy-only confirmation flags / validation hints
Expand Down Expand Up @@ -7624,6 +7633,35 @@ mod tests {
);
}

#[test]
fn test_rewrite_tools_list_does_not_force_additional_properties_on_arg_schema() {
let body = serde_json::to_string(&json!({
"jsonrpc": "2.0",
"id": 1,
"result": {
"tools": [{
"name": "help",
"description": "Help",
"inputSchema": {
"type": "object",
"properties": {"query": {"type": "string"}}
}
}]
}
}))
.unwrap();

let result = rewrite_tools_list(&body, None);
let parsed: Value = serde_json::from_str(&result).unwrap();
let schema = parsed["result"]["tools"][0]["inputSchema"]
.as_object()
.expect("inputSchema must be object");
assert!(
!schema.contains_key("additionalProperties"),
"arg schema should keep JSON Schema default additionalProperties behavior"
);
}

// --- MCP annotations tests ---

#[test]
Expand Down
Loading