| import importlib.util | |
| import inspect | |
| import json | |
| import sys | |
| from pathlib import Path | |
| tool_path = Path(sys.argv[1]) | |
| def make_specs_from_tools_class(module) -> list: | |
| specs = [] | |
| if not hasattr(module, 'Tools'): | |
| return specs | |
| cls = getattr(module, 'Tools') | |
| for name, func in inspect.getmembers(cls, predicate=inspect.isfunction): | |
| if name.startswith('_'): | |
| continue | |
| sig = inspect.signature(func) | |
| props = {} | |
| req = [] | |
| for pname, p in sig.parameters.items(): | |
| if pname == 'self': | |
| continue | |
| props[pname] = {"type":"string"} | |
| if p.default is inspect._empty: | |
| req.append(pname) | |
| specs.append({ | |
| "name": name, | |
| "description": func.__doc__.strip() if func.__doc__ else name, | |
| "parameters": {"type":"object","properties": props, **({"required": req} if req else {})} | |
| }) | |
| return specs | |
| spec = importlib.util.spec_from_file_location(tool_path.stem, str(tool_path)) | |
| mod = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(mod) | |
| specs = make_specs_from_tools_class(mod) | |
| print(json.dumps(specs)) | |