@@ -83,8 +83,7 @@ def _call_mcp_tool(self, name: str, args: Dict[str, Any], read_timeout: int = No
8383
8484 if response .is_tool_successful ():
8585 # Tool execution successful
86- print ("response.json_data =" , response .json_data )
87- result = self ._parse_response_body (response .json_data or {})
86+ result = response .get_tool_result ()
8887 return OperationResult (request_id = request_id , success = True , data = result )
8988 else :
9089 # Tool execution failed
@@ -120,105 +119,4 @@ def _call_mcp_tool(self, name: str, args: Dict[str, Any], read_timeout: int = No
120119 request_id = request_id ,
121120 success = False ,
122121 error_message = f"Failed to call MCP tool { name } : { handled_error } " ,
123- )
124-
125- def _sanitize_error (self , error_str : str ) -> str :
126- """
127- Sanitizes error messages to remove sensitive information like API keys.
128-
129- Args:
130- error_str (str): The error string to sanitize.
131-
132- Returns:
133- str: The sanitized error string.
134- """
135- import re
136-
137- if not error_str :
138- return error_str
139-
140- # Remove API key from URLs
141- # Pattern: apiKey=akm-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
142- api_key_pattern = re .compile (r'apiKey=akm-[a-f0-9-]+' )
143- error_str = api_key_pattern .sub ('apiKey=***REDACTED***' , error_str )
144-
145- # Remove API key from Bearer tokens
146- # Pattern: Bearer akm-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
147- bearer_pattern = re .compile (r'Bearer akm-[a-f0-9-]+' )
148- error_str = bearer_pattern .sub ('Bearer ***REDACTED***' , error_str )
149-
150- # Remove API key from query parameters
151- # Pattern: &apiKey=akm-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
152- query_pattern = re .compile (r'&apiKey=akm-[a-f0-9-]+' )
153- error_str = query_pattern .sub ('&apiKey=***REDACTED***' , error_str )
154-
155- # Remove API key from URL paths
156- # Pattern: /callTool?apiKey=akm-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
157- url_pattern = re .compile (r'/callTool\?apiKey=akm-[a-f0-9-]+' )
158- error_str = url_pattern .sub ('/callTool?apiKey=***REDACTED***' , error_str )
159-
160- return error_str
161-
162- def _parse_response_body (
163- self , body : Dict [str , Any ], parse_json : bool = False
164- ) -> Any :
165- """
166- Parses the response body from the MCP tool.
167-
168- Args:
169- body (Dict[str, Any]): The response body.
170- parse_json (bool, optional): Whether to parse the text as JSON.
171- Defaults to False.
172-
173- Returns:
174- Any: The parsed content. If parse_json is True, returns the parsed
175- JSON object; otherwise returns the raw text.
176-
177-
178- Raises:
179- AGBError: If the response contains errors or is invalid.
180- """
181- try :
182- response_data = body .get ("data" , {})
183- if response_data .get ("isError" , False ):
184- error_content = response_data .get ("content" , [])
185- try :
186- print ("error_content =" )
187- print (json .dumps (error_content , ensure_ascii = False , indent = 2 ))
188- except Exception :
189- print (f"error_content: { error_content } " )
190- error_message = "; " .join (
191- item .get ("text" , "Unknown error" )
192- for item in error_content
193- if isinstance (item , dict )
194- )
195- raise AGBError (f"Error in response: { error_message } " )
196-
197-
198- if not response_data :
199- raise AGBError ("No data field in response" )
200-
201- # Handle 'content' field for other methods
202- content = response_data .get ("content" , [])
203- if not content or not isinstance (content , list ):
204- raise AGBError ("No content found in response" )
205-
206- content_item = content [0 ]
207- text_string = content_item .get ("text" )
208-
209- # Allow text field to be empty string
210- if text_string is None :
211- raise AGBError ("Text field not found in response" )
212-
213- return text_string
214-
215- except AGBError as e :
216- # Transform AGBError to the expected type
217- handled_error = self ._handle_error (e )
218- raise handled_error
219- except Exception as e :
220- # Transform AGBError to the expected type
221- handled_error = self ._handle_error (
222- AGBError (f"Error parsing response body: { e } " )
223- )
224- raise handled_error
122+ )
0 commit comments