Register a tool function with metadata
(
&self,
name: String,
description: String,
function: PyObject,
parameters_schema: &Bound<'_, PyDict>,
return_type: Option<String>,
)
| 97 | |
| 98 | /// Register a tool function with metadata |
| 99 | pub fn register_tool( |
| 100 | &self, |
| 101 | name: String, |
| 102 | description: String, |
| 103 | function: PyObject, |
| 104 | parameters_schema: &Bound<'_, PyDict>, |
| 105 | return_type: Option<String>, |
| 106 | ) -> PyResult<()> { |
| 107 | // Validate inputs |
| 108 | if name.trim().is_empty() { |
| 109 | return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>( |
| 110 | "Tool name cannot be empty", |
| 111 | )); |
| 112 | } |
| 113 | |
| 114 | if description.trim().is_empty() { |
| 115 | return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>( |
| 116 | "Tool description cannot be empty", |
| 117 | )); |
| 118 | } |
| 119 | |
| 120 | // Convert parameters schema to JSON |
| 121 | let schema_json = python_dict_to_json(parameters_schema)?; |
| 122 | |
| 123 | // Create metadata |
| 124 | let metadata = ToolMetadata::new( |
| 125 | name.clone(), |
| 126 | description, |
| 127 | schema_json, |
| 128 | return_type.unwrap_or_else(|| "Any".to_string()), |
| 129 | ); |
| 130 | |
| 131 | // Store tool and metadata |
| 132 | { |
| 133 | let mut tools = self.tools.write().map_err(|e| { |
| 134 | PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!( |
| 135 | "Failed to acquire tools lock: {}", |
| 136 | e |
| 137 | )) |
| 138 | })?; |
| 139 | tools.insert(name.clone(), function); |
| 140 | } |
| 141 | |
| 142 | { |
| 143 | let mut meta = self.metadata.write().map_err(|e| { |
| 144 | PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!( |
| 145 | "Failed to acquire metadata lock: {}", |
| 146 | e |
| 147 | )) |
| 148 | })?; |
| 149 | meta.insert(name, metadata); |
| 150 | } |
| 151 | |
| 152 | Ok(()) |
| 153 | } |
| 154 | |
| 155 | /// Unregister a tool |
| 156 | pub fn unregister_tool(&self, name: &str) -> PyResult<bool> { |