1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Validation shared by every sandbox: a nested call either becomes an MCP
//! request or a Python exception, before anything leaves the sandbox.
use monty_types::ExcType;
use serde_json::{Map, Value};
use super::catalog::CatalogEntry;
/// A Python exception to raise at the call site.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Rejection {
pub kind: ExcType,
pub message: String,
}
impl Rejection {
fn type_error(message: String) -> Self {
Self {
kind: ExcType::TypeError,
message,
}
}
}
/// Checks one call against the tool's declared parameters and returns the
/// MCP arguments, keyed by MCP name. Optional arguments passed as `None` are
/// dropped.
pub fn check_call(
entry: &CatalogEntry,
positional: usize,
kwargs: Vec<(String, Value)>,
) -> Result<Map<String, Value>, Rejection> {
let name = &entry.py_name;
if positional > 0 {
return Err(Rejection::type_error(format!(
"{name}() takes keyword arguments only, e.g. {name}(key=value)"
)));
}
let mut arguments = Map::new();
for (key, value) in kwargs {
let Some(param) = entry.param(&key) else {
let accepted = entry
.params
.iter()
.map(|param| param.py_name.as_str())
.collect::<Vec<_>>();
let accepted = if accepted.is_empty() {
"none".to_string()
} else {
accepted.join(", ")
};
return Err(Rejection::type_error(format!(
"{name}() got an unexpected keyword argument '{key}'; accepted: {accepted}"
)));
};
if value.is_null() && !param.required {
continue;
}
arguments.insert(param.key.clone(), value);
}
if let Some(missing) = entry
.params
.iter()
.find(|param| param.required && !arguments.contains_key(¶m.key))
{
return Err(Rejection::type_error(format!(
"{name}() missing required keyword argument '{}'",
missing.py_name
)));
}
Ok(arguments)
}
/// The exception raised once a snippet exceeds its nested-call budget.
pub fn budget_exceeded(limit: usize) -> Rejection {
Rejection {
kind: ExcType::RuntimeError,
message: format!("nested tool call budget of {limit} per run_code exceeded"),
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::*;
use crate::code_mode::catalog::Catalog;
use crate::tools::ToolDefinition;
fn entry() -> CatalogEntry {
let catalog = Catalog::new(&[ToolDefinition::new(
"list_objects",
None::<String>,
json!({
"type": "object",
"properties": {
"resource": {"type": "string"},
"namespace": {"type": "string"},
"all-namespaces": {"type": "boolean"},
},
"required": ["resource"],
}),
)]);
catalog.entries()[0].clone()
}
#[test]
fn maps_python_names_and_drops_none_optionals() {
let arguments = check_call(
&entry(),
0,
vec![
("resource".to_string(), json!("configmaps")),
("namespace".to_string(), Value::Null),
("all_namespaces".to_string(), json!(true)),
],
)
.unwrap();
assert_eq!(
Value::Object(arguments),
json!({"resource": "configmaps", "all-namespaces": true})
);
}
#[test]
fn rejects_unknown_missing_and_positional_arguments() {
let unknown = check_call(
&entry(),
0,
vec![
("resource".to_string(), json!("pods")),
("name".to_string(), json!("x")),
],
)
.unwrap_err();
assert_eq!(unknown.kind, ExcType::TypeError);
assert!(
unknown.message.contains(
"unexpected keyword argument 'name'; accepted: all_namespaces, namespace, resource"
) || unknown
.message
.contains("unexpected keyword argument 'name'")
);
let missing = check_call(&entry(), 0, vec![]).unwrap_err();
assert!(missing
.message
.contains("missing required keyword argument 'resource'"));
let positional = check_call(&entry(), 1, vec![]).unwrap_err();
assert!(positional.message.contains("keyword arguments only"));
}
}