File tree Expand file tree Collapse file tree 2 files changed +48
-7
lines changed
Expand file tree Collapse file tree 2 files changed +48
-7
lines changed Original file line number Diff line number Diff line change @@ -516,13 +516,17 @@ async function handleInternalRequest(
516516 // Many APIs (e.g., Microsoft Graph) return 202 with empty body
517517 responseData = { status }
518518 } else {
519- try {
520- responseData = await response . json ( )
521- } catch ( jsonError ) {
522- logger . error ( `[${ requestId } ] JSON parse error for ${ toolId } :` , {
523- error : jsonError instanceof Error ? jsonError . message : String ( jsonError ) ,
524- } )
525- throw new Error ( `Failed to parse response from ${ toolId } : ${ jsonError } ` )
519+ if ( tool . transformResponse ) {
520+ responseData = null
521+ } else {
522+ try {
523+ responseData = await response . json ( )
524+ } catch ( jsonError ) {
525+ logger . error ( `[${ requestId } ] JSON parse error for ${ toolId } :` , {
526+ error : jsonError instanceof Error ? jsonError . message : String ( jsonError ) ,
527+ } )
528+ throw new Error ( `Failed to parse response from ${ toolId } : ${ jsonError } ` )
529+ }
526530 }
527531 }
528532
Original file line number Diff line number Diff line change @@ -527,6 +527,43 @@ describe('executeRequest', () => {
527527 error : 'Server Error' , // Should use statusText in the error message
528528 } )
529529 } )
530+
531+ it ( 'should handle transformResponse with non-JSON response' , async ( ) => {
532+ const toolWithTransform = {
533+ ...mockTool ,
534+ transformResponse : async ( response : Response ) => {
535+ const xmlText = await response . text ( )
536+ return {
537+ success : true ,
538+ output : {
539+ parsedData : 'mocked xml parsing result' ,
540+ originalXml : xmlText ,
541+ } ,
542+ }
543+ } ,
544+ }
545+
546+ mockFetch . mockResolvedValueOnce ( {
547+ ok : true ,
548+ status : 200 ,
549+ statusText : 'OK' ,
550+ text : async ( ) => '<xml><test>Mock XML response</test></xml>' ,
551+ } )
552+
553+ const result = await executeRequest ( 'test-tool' , toolWithTransform , {
554+ url : 'https://api.example.com' ,
555+ method : 'GET' ,
556+ headers : { } ,
557+ } )
558+
559+ expect ( result ) . toEqual ( {
560+ success : true ,
561+ output : {
562+ parsedData : 'mocked xml parsing result' ,
563+ originalXml : '<xml><test>Mock XML response</test></xml>' ,
564+ } ,
565+ } )
566+ } )
530567} )
531568
532569describe ( 'createParamSchema' , ( ) => {
You can’t perform that action at this time.
0 commit comments