@@ -185,36 +185,52 @@ export async function executeToolStep(
185185 } ,
186186 ] ,
187187 } ) ;
188- await client . connect ( transport ) ;
189-
190- // Fetch tool schema for type coercion
191- let inputSchema : JSONSchema | undefined ;
188+ let result : unknown ;
192189 try {
193- const { tools } = await client . listTools ( ) ;
194- const tool = tools . find ( ( t ) => t . name === toolName ) ;
195- inputSchema = tool ?. inputSchema as JSONSchema | undefined ;
196- } catch {
197- // If we can't get the schema, proceed without type coercion
198- }
190+ await client . connect ( transport ) ;
191+
192+ // Fetch tool schema for type coercion
193+ let inputSchema : JSONSchema | undefined ;
194+ try {
195+ const { tools } = await client . listTools ( ) ;
196+ const tool = tools . find ( ( t ) => t . name === toolName ) ;
197+ inputSchema = tool ?. inputSchema as JSONSchema | undefined ;
198+ } catch {
199+ // If we can't get the schema, proceed without type coercion
200+ }
199201
200- // Sanitize input and coerce types based on tool schema
201- const sanitizedInput = sanitizeInput ( input , inputSchema ) ;
202+ // Sanitize input and coerce types based on tool schema
203+ const sanitizedInput = sanitizeInput ( input , inputSchema ) ;
202204
203- const timeoutMs = step . config ?. timeoutMs ?? 30000 ;
205+ const timeoutMs = step . config ?. timeoutMs ?? 30000 ;
204206
205- const { content, structuredContent, isError } = await client . callTool (
206- {
207- name : toolName ,
208- arguments : sanitizedInput ,
209- } ,
210- undefined ,
211- {
212- timeout : timeoutMs ,
213- } ,
214- ) ;
215- await client . close ( ) ;
207+ const { content, structuredContent, isError } = await client . callTool (
208+ {
209+ name : toolName ,
210+ arguments : sanitizedInput ,
211+ } ,
212+ undefined ,
213+ {
214+ timeout : timeoutMs ,
215+ } ,
216+ ) ;
216217
217- const result = structuredContent ?? content ;
218+ result = structuredContent ?? content ;
219+
220+ if ( isError ) {
221+ throw new Error ( JSON . stringify ( result ) ) ;
222+ }
223+ } catch ( error ) {
224+ await client . close ( ) ;
225+ return {
226+ error : error instanceof Error ? error . message : String ( error ) ,
227+ startedAt,
228+ completedAt : Date . now ( ) ,
229+ stepId : step . name ,
230+ } ;
231+ } finally {
232+ await client . close ( ) ;
233+ }
218234
219235 // If there's transform code, run it on the raw tool result
220236 if ( transformCode ) {
@@ -243,7 +259,6 @@ export async function executeToolStep(
243259 return {
244260 output,
245261 startedAt,
246- error : isError ? JSON . stringify ( result ) : undefined ,
247262 completedAt : Date . now ( ) ,
248263 stepId : step . name ,
249264 } ;
@@ -252,7 +267,6 @@ export async function executeToolStep(
252267 return {
253268 output : result ,
254269 startedAt,
255- error : isError ? JSON . stringify ( result ) : undefined ,
256270 completedAt : Date . now ( ) ,
257271 stepId : step . name ,
258272 } ;
0 commit comments