@@ -56,29 +56,12 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
5656 // Process the URL first to handle path/query params
5757 const processedUrl = processUrl ( params . url , params . pathParams , params . params )
5858
59- // For external URLs that need proxying
59+ // For external URLs that need proxying in the browser, we still return the
60+ // external URL here and let executeTool route through the POST /api/proxy
61+ // endpoint uniformly. This avoids querystring body encoding and prevents
62+ // the proxy GET route from being hit from the client.
6063 if ( shouldUseProxy ( processedUrl ) ) {
61- let proxyUrl = `/api/proxy?url=${ encodeURIComponent ( processedUrl ) } `
62-
63- if ( params . method ) {
64- proxyUrl += `&method=${ encodeURIComponent ( params . method ) } `
65- }
66-
67- if ( params . body && [ 'POST' , 'PUT' , 'PATCH' ] . includes ( params . method ?. toUpperCase ( ) || '' ) ) {
68- const bodyStr =
69- typeof params . body === 'string' ? params . body : JSON . stringify ( params . body )
70- proxyUrl += `&body=${ encodeURIComponent ( bodyStr ) } `
71- }
72-
73- // Forward all headers as URL parameters
74- const userHeaders = transformTable ( params . headers || null )
75- for ( const [ key , value ] of Object . entries ( userHeaders ) ) {
76- if ( value !== undefined && value !== null ) {
77- proxyUrl += `&header.${ encodeURIComponent ( key ) } =${ encodeURIComponent ( String ( value ) ) } `
78- }
79- }
80-
81- return proxyUrl
64+ return processedUrl
8265 }
8366
8467 return processedUrl
@@ -137,13 +120,26 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
137120 } ,
138121
139122 transformResponse : async ( response : Response ) => {
140- // For proxy responses, we need to parse the JSON and extract the data
123+ // Build headers once for consistent return structures
124+ const headers : Record < string , string > = { }
125+ response . headers . forEach ( ( value , key ) => {
126+ headers [ key ] = value
127+ } )
128+
141129 const contentType = response . headers . get ( 'content-type' ) || ''
142- if ( contentType . includes ( 'application/json' ) ) {
143- const jsonResponse = await response . json ( )
130+ const isJson = contentType . includes ( 'application/json' )
131+
132+ if ( isJson ) {
133+ // Use a clone to safely inspect JSON without consuming the original body
134+ let jsonResponse : any
135+ try {
136+ jsonResponse = await response . clone ( ) . json ( )
137+ } catch ( _e ) {
138+ jsonResponse = undefined
139+ }
144140
145- // Check if this is a proxy response
146- if ( jsonResponse . data !== undefined && jsonResponse . status !== undefined ) {
141+ // Proxy responses wrap the real payload
142+ if ( jsonResponse && jsonResponse . data !== undefined && jsonResponse . status !== undefined ) {
147143 return {
148144 success : jsonResponse . success ,
149145 output : {
@@ -153,32 +149,30 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
153149 } ,
154150 error : jsonResponse . success
155151 ? undefined
156- : // Extract and display the actual API error message from the response if available
157- jsonResponse . data && typeof jsonResponse . data === 'object' && jsonResponse . data . error
152+ : jsonResponse . data && typeof jsonResponse . data === 'object' && jsonResponse . data . error
158153 ? `HTTP error ${ jsonResponse . status } : ${ jsonResponse . data . error . message || JSON . stringify ( jsonResponse . data . error ) } `
159154 : jsonResponse . error || `HTTP error ${ jsonResponse . status } ` ,
160155 }
161156 }
162- }
163-
164- // Standard response handling
165- const headers : Record < string , string > = { }
166- response . headers . forEach ( ( value , key ) => {
167- headers [ key ] = value
168- } )
169157
170- let data
171- try {
172- data = await ( contentType . includes ( 'application/json' ) ? response . json ( ) : response . text ( ) )
173- } catch ( error ) {
174- // If response body reading fails, we can't retry reading - just use error message
175- data = `Failed to parse response: ${ error instanceof Error ? error . message : String ( error ) } `
158+ // Non-proxy JSON response: return parsed JSON directly
159+ return {
160+ success : response . ok ,
161+ output : {
162+ data : jsonResponse ?? ( await response . text ( ) ) ,
163+ status : response . status ,
164+ headers,
165+ } ,
166+ error : response . ok ? undefined : `HTTP error ${ response . status } : ${ response . statusText } ` ,
167+ }
176168 }
177169
170+ // Non-JSON response: return text
171+ const textData = await response . text ( )
178172 return {
179173 success : response . ok ,
180174 output : {
181- data,
175+ data : textData ,
182176 status : response . status ,
183177 headers,
184178 } ,
0 commit comments