13
13
# limitations under the License.
14
14
15
15
16
- import types
17
- from typing import Any , Callable , Mapping , Optional , Union
16
+ from typing import Any , Callable , Mapping , Optional , Sequence , Union
18
17
19
18
from aiohttp import ClientSession
20
19
21
- from .protocol import ManifestSchema , ToolSchema
22
- from .tool import ToolboxTool , identify_required_authn_params
20
+ from .protocol import ManifestSchema , ParameterSchema , ToolSchema
21
+ from .tool import ToolboxTool
23
22
24
23
25
24
class ToolboxClient :
@@ -59,24 +58,26 @@ def __parse_tool(
59
58
self ,
60
59
name : str ,
61
60
schema : ToolSchema ,
62
- auth_token_getters : dict [str , Callable [[], str ]],
61
+ auth_token_getters : Mapping [str , Callable [[], str ]],
63
62
all_bound_params : Mapping [str , Union [Callable [[], Any ], Any ]],
63
+ strict : bool ,
64
64
) -> ToolboxTool :
65
- """Internal helper to create a callable tool from its schema."""
66
- # sort into reg, authn, and bound params
67
- params = []
68
- authn_params : dict [str , list [str ]] = {}
69
- bound_params : dict [str , Callable [[], str ]] = {}
70
- for p in schema .parameters :
71
- if p .authSources : # authn parameter
72
- authn_params [p .name ] = p .authSources
73
- elif p .name in all_bound_params : # bound parameter
74
- bound_params [p .name ] = all_bound_params [p .name ]
75
- else : # regular parameter
76
- params .append (p )
77
-
78
- authn_params = identify_required_authn_params (
79
- authn_params , auth_token_getters .keys ()
65
+ """
66
+ Internal helper to create a callable ToolboxTool from its schema.
67
+
68
+ Args:
69
+ name: The name of the tool.
70
+ schema: The ToolSchema defining the tool.
71
+ auth_token_getters: Mapping of auth service names to token getters.
72
+ all_bound_params: Mapping of all initially bound parameter names to values/callables.
73
+ strict: The strictness setting for the created ToolboxTool instance.
74
+
75
+ Returns:
76
+ An initialized ToolboxTool instance.
77
+ """
78
+
79
+ params : Sequence [ParameterSchema ] = (
80
+ schema .parameters if schema .parameters is not None else []
80
81
)
81
82
82
83
tool = ToolboxTool (
@@ -85,10 +86,9 @@ def __parse_tool(
85
86
name = name ,
86
87
description = schema .description ,
87
88
params = params ,
88
- # create a read-only values for the maps to prevent mutation
89
- required_authn_params = types .MappingProxyType (authn_params ),
90
- auth_service_token_getters = types .MappingProxyType (auth_token_getters ),
91
- bound_params = types .MappingProxyType (bound_params ),
89
+ auth_service_token_getters = auth_token_getters ,
90
+ bound_params = all_bound_params ,
91
+ strict = strict ,
92
92
)
93
93
return tool
94
94
@@ -127,8 +127,9 @@ async def close(self):
127
127
async def load_tool (
128
128
self ,
129
129
name : str ,
130
- auth_token_getters : dict [str , Callable [[], str ]] = {},
130
+ auth_token_getters : Mapping [str , Callable [[], str ]] = {},
131
131
bound_params : Mapping [str , Union [Callable [[], Any ], Any ]] = {},
132
+ strict : bool = True ,
132
133
) -> ToolboxTool :
133
134
"""
134
135
Asynchronously loads a tool from the server.
@@ -143,8 +144,8 @@ async def load_tool(
143
144
callables that return the corresponding authentication token.
144
145
bound_params: A mapping of parameter names to bind to specific values or
145
146
callables that are called to produce values as needed.
146
-
147
-
147
+ strict: If True (default), the loaded tool instance will operate in
148
+ strict validation mode. If False, it will be non-strict.
148
149
149
150
Returns:
150
151
ToolboxTool: A callable object representing the loaded tool, ready
@@ -161,35 +162,38 @@ async def load_tool(
161
162
162
163
# parse the provided definition to a tool
163
164
if name not in manifest .tools :
164
- # TODO: Better exception
165
- raise Exception (f"Tool '{ name } ' not found!" )
165
+ raise Exception (
166
+ f"Tool '{ name } ' not found in the manifest received from { url } "
167
+ )
166
168
tool = self .__parse_tool (
167
- name , manifest .tools [name ], auth_token_getters , bound_params
169
+ name , manifest .tools [name ], auth_token_getters , bound_params , strict
168
170
)
169
171
170
172
return tool
171
173
172
174
async def load_toolset (
173
175
self ,
174
176
name : Optional [str ] = None ,
175
- auth_token_getters : dict [str , Callable [[], str ]] = {},
177
+ auth_token_getters : Mapping [str , Callable [[], str ]] = {},
176
178
bound_params : Mapping [str , Union [Callable [[], Any ], Any ]] = {},
179
+ strict : bool = True ,
177
180
) -> list [ToolboxTool ]:
178
181
"""
179
182
Asynchronously fetches a toolset and loads all tools defined within it.
180
183
181
184
Args:
182
- name: Name of the toolset to load tools.
185
+ name: Optional name of the toolset to load. If None, attempts to load
186
+ the default toolset.
183
187
auth_token_getters: A mapping of authentication service names to
184
188
callables that return the corresponding authentication token.
185
189
bound_params: A mapping of parameter names to bind to specific values or
186
190
callables that are called to produce values as needed.
187
-
188
-
191
+ strict: If True (default), all loaded tool instances will operate in
192
+ strict validation mode. If False, they will be non-strict.
189
193
190
194
Returns:
191
- list[ToolboxTool]: A list of callables, one for each tool defined
192
- in the toolset.
195
+ list[ToolboxTool]: A list of callables, one for each tool defined in
196
+ the toolset.
193
197
"""
194
198
# Request the definition of the tool from the server
195
199
url = f"{ self .__base_url } /api/toolset/{ name or '' } "
@@ -199,7 +203,7 @@ async def load_toolset(
199
203
200
204
# parse each tools name and schema into a list of ToolboxTools
201
205
tools = [
202
- self .__parse_tool (n , s , auth_token_getters , bound_params )
206
+ self .__parse_tool (n , s , auth_token_getters , bound_params , strict )
203
207
for n , s in manifest .tools .items ()
204
208
]
205
209
return tools
0 commit comments