1414from google .adk .agents .llm_agent import AfterToolCallback , BeforeToolCallback
1515from google .adk .models import LlmResponse
1616from google .adk .tools import BaseTool , ToolContext
17- from mcp .types import CallToolResult
17+ from mcp .types import CallToolResult , TextContent
1818
1919from agents .matmaster_agent .constant import (
2020 CURRENT_ENV ,
3232 function_calls_to_str ,
3333 get_session_state ,
3434 get_unique_function_call ,
35- is_json ,
3635 update_llm_response ,
3736)
3837from agents .matmaster_agent .utils .io_oss import update_tgz_dict
38+ from agents .matmaster_agent .utils .tool_response_utils import check_valid_tool_response
3939
4040logger = logging .getLogger (__name__ )
4141
@@ -457,12 +457,7 @@ async def wrapper(
457457 raise TypeError ('Not CalculationMCPTool type' )
458458
459459 # 检查是否为有效的 json 字典
460- if not (
461- tool_response
462- and tool_response .content
463- and tool_response .content [0 ].text
464- and is_json (tool_response .content [0 ].text )
465- ):
460+ if not check_valid_tool_response (tool_response ):
466461 return None
467462
468463 tool_result = json .loads (tool_response .content [0 ].text )
@@ -473,6 +468,44 @@ async def wrapper(
473468 return wrapper
474469
475470
471+ def remove_job_link (func : AfterToolCallback ) -> AfterToolCallback :
472+ @wraps (func )
473+ async def wrapper (
474+ tool : BaseTool ,
475+ args : dict ,
476+ tool_context : ToolContext ,
477+ tool_response : Union [dict , CallToolResult ],
478+ ) -> Optional [dict ]:
479+ # 两步操作:
480+ # 1. 调用被装饰的 after_tool_callback;
481+ # 2. 如果调用的 after_tool_callback 有返回值,以这个为准
482+ # 如果 tool 为 Transfer2Agent,直接 return
483+ if tool .name == Transfer2Agent :
484+ return None
485+
486+ if (
487+ after_tool_result := await func (tool , args , tool_context , tool_response )
488+ ) is not None :
489+ return after_tool_result
490+
491+ # 检查是否为有效的 json 字典
492+ if not check_valid_tool_response (tool_response ):
493+ return None
494+
495+ # 移除 job_link
496+ tool_result : dict = json .loads (tool_response .content [0 ].text )
497+ if tool_result .get ('extra_info' , None ) is not None :
498+ del tool_result ['extra_info' ]['job_link' ]
499+ tool_response .content [0 ] = TextContent (
500+ type = 'text' , text = json .dumps (tool_result )
501+ )
502+
503+ logger .info (f"[remove_job_link] final_tool_result = { tool_response } " )
504+ return tool_response
505+
506+ return wrapper
507+
508+
476509def catch_after_tool_callback_error (func : AfterToolCallback ) -> AfterToolCallback :
477510 @wraps (func )
478511 async def wrapper (
0 commit comments