MCPcopy Create free account
hub / github.com/NVIDIA/NeMo-Relay / wrap_py_tool_request_intercept_fn

Function wrap_py_tool_request_intercept_fn

crates/python/src/py_callable.rs:240–254  ·  view source on GitHub ↗

Wrap a Python callable `(str, Json) -> Json` for tool request intercepts.

(py_fn: Py<PyAny>)

Source from the content-addressed store, hash-verified

238
239/// Wrap a Python callable `(str, Json) -> Json` for tool request intercepts.
240pub fn wrap_py_tool_request_intercept_fn(py_fn: Py<PyAny>) -> ToolInterceptFn {
241 Arc::new(move |name: &str, args: Json| {
242 Python::attach(|py| {
243 let py_args = json_to_py(py, &args).map_err(|e| {
244 FlowError::Internal(format!("tool callback json_to_py failed for '{name}': {e}"))
245 })?;
246 let result = py_fn.call1(py, (name, py_args)).map_err(|e| {
247 FlowError::Internal(format!("Python tool callable failed for '{name}': {e}"))
248 })?;
249 py_to_json(result.bind(py)).map_err(|e| {
250 FlowError::Internal(format!("tool callback py_to_json failed for '{name}': {e}"))
251 })
252 })
253 })
254}
255
256/// Wrap a Python callable `(Json) -> Json` for tool execution intercepts.
257/// Supports both sync and async Python callables. If the callable returns a

Calls 2

json_to_pyFunction · 0.85
py_to_jsonFunction · 0.85

Tested by 1