This repository was archived by the owner on Nov 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 518
Expand file tree
/
Copy pathgenerate_tool_specs.py
More file actions
158 lines (127 loc) · 5.24 KB
/
generate_tool_specs.py
File metadata and controls
158 lines (127 loc) · 5.24 KB
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
157
158
#!/usr/bin/env python3
import inspect
import json
from pathlib import Path
from typing import Any, Dict, List, Optional, Type
from pydantic import BaseModel
from crewai_tools import tools
from crewai.tools.base_tool import BaseTool, EnvVar
from pydantic.json_schema import GenerateJsonSchema
from pydantic_core import PydanticOmit
class SchemaGenerator(GenerateJsonSchema):
def handle_invalid_for_json_schema(self, schema, error_info):
raise PydanticOmit
class ToolSpecExtractor:
def __init__(self) -> None:
self.tools_spec: List[Dict[str, Any]] = []
self.processed_tools: set[str] = set()
def extract_all_tools(self) -> List[Dict[str, Any]]:
for name in dir(tools):
if name.endswith("Tool") and name not in self.processed_tools:
obj = getattr(tools, name, None)
if inspect.isclass(obj):
self.extract_tool_info(obj)
self.processed_tools.add(name)
return self.tools_spec
def extract_tool_info(self, tool_class: BaseTool) -> None:
try:
core_schema = tool_class.__pydantic_core_schema__
if not core_schema:
return
schema = self._unwrap_schema(core_schema)
fields = schema.get("schema", {}).get("fields", {})
tool_info = {
"name": tool_class.__name__,
"humanized_name": self._extract_field_default(
fields.get("name"), fallback=tool_class.__name__
),
"description": self._extract_field_default(
fields.get("description")
).strip(),
"run_params_schema": self._extract_params(fields.get("args_schema")),
"init_params_schema": self._extract_init_params(tool_class),
"env_vars": self._extract_env_vars(fields.get("env_vars")),
"package_dependencies": self._extract_field_default(
fields.get("package_dependencies"), fallback=[]
),
}
self.tools_spec.append(tool_info)
except Exception as e:
print(f"Error extracting {tool_class.__name__}: {e}")
def _unwrap_schema(self, schema: Dict) -> Dict:
while (
schema.get("type") in {"function-after", "default"} and "schema" in schema
):
schema = schema["schema"]
return schema
def _extract_field_default(self, field: Optional[Dict], fallback: str = "") -> str:
if not field:
return fallback
schema = field.get("schema", {})
default = schema.get("default")
return default if isinstance(default, (list, str, int)) else fallback
def _extract_params(
self, args_schema_field: Optional[Dict]
) -> List[Dict[str, str]]:
if not args_schema_field:
return {}
args_schema_class = args_schema_field.get("schema", {}).get("default")
if not (
inspect.isclass(args_schema_class)
and hasattr(args_schema_class, "__pydantic_core_schema__")
):
return {}
try:
return args_schema_class.model_json_schema(
schema_generator=SchemaGenerator, mode="validation"
)
except Exception as e:
print(f"Error extracting params from {args_schema_class}: {e}")
return {}
def _extract_env_vars(self, env_vars_field: Optional[Dict]) -> List[Dict[str, str]]:
if not env_vars_field:
return []
env_vars = []
for env_var in env_vars_field.get("schema", {}).get("default", []):
if isinstance(env_var, EnvVar):
env_vars.append(
{
"name": env_var.name,
"description": env_var.description,
"required": env_var.required,
"default": env_var.default,
}
)
return env_vars
def _extract_init_params(self, tool_class: BaseTool) -> dict:
ignored_init_params = [
"name",
"description",
"env_vars",
"args_schema",
"description_updated",
"cache_function",
"result_as_answer",
"max_usage_count",
"current_usage_count",
"package_dependencies",
]
json_schema = tool_class.model_json_schema(
schema_generator=SchemaGenerator, mode="serialization"
)
properties = {}
for key, value in json_schema["properties"].items():
if key not in ignored_init_params:
properties[key] = value
json_schema["properties"] = properties
return json_schema
def save_to_json(self, output_path: str) -> None:
with open(output_path, "w", encoding="utf-8") as f:
json.dump({"tools": self.tools_spec}, f, indent=2, sort_keys=True)
print(f"Saved tool specs to {output_path}")
if __name__ == "__main__":
output_file = Path(__file__).parent / "tool.specs.json"
extractor = ToolSpecExtractor()
specs = extractor.extract_all_tools()
extractor.save_to_json(str(output_file))
print(f"Extracted {len(specs)} tool classes.")