MCPcopy Create free account
hub / github.com/SqueezeAILab/LLMCompiler / StructuredTool

Class StructuredTool

src/tools/base.py:225–375  ·  view source on GitHub ↗

Tool that can operate on any number of inputs.

Source from the content-addressed store, hash-verified

223
224
225class StructuredTool(BaseTool):
226 """Tool that can operate on any number of inputs."""
227
228 description: str = ""
229 args_schema: Type[BaseModel] = Field(..., description="The tool schema.")
230 """The input arguments' schema."""
231 func: Optional[Callable[..., Any]]
232 """The function to run when the tool is called."""
233 coroutine: Optional[Callable[..., Awaitable[Any]]] = None
234 """The asynchronous version of the function."""
235 stringify_rule: Optional[Callable[..., str]] = None
236
237 # --- Runnable ---
238
239 async def ainvoke(
240 self,
241 input: Union[str, Dict],
242 config: Optional[RunnableConfig] = None,
243 **kwargs: Any,
244 ) -> Any:
245 if not self.coroutine:
246 # If the tool does not implement async, fall back to default implementation
247 return await asyncio.get_running_loop().run_in_executor(
248 None, partial(self.invoke, input, config, **kwargs)
249 )
250
251 return super().ainvoke(input, config, **kwargs)
252
253 # --- Tool ---
254
255 @property
256 def args(self) -> dict:
257 """The tool's input arguments."""
258 return self.args_schema.schema()["properties"]
259
260 def _run(
261 self,
262 *args: Any,
263 run_manager: Optional[CallbackManagerForToolRun] = None,
264 **kwargs: Any,
265 ) -> Any:
266 """Use the tool."""
267 if self.func:
268 new_argument_supported = signature(self.func).parameters.get("callbacks")
269 return (
270 self.func(
271 *args,
272 callbacks=run_manager.get_child() if run_manager else None,
273 **kwargs,
274 )
275 if new_argument_supported
276 else self.func(*args, **kwargs)
277 )
278 raise NotImplementedError("Tool does not support sync")
279
280 async def _arun(
281 self,
282 *args: Any,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected