MCPcopy Create free account
hub / github.com/AI45Lab/Code / _handle_pre_tool_use

Method _handle_pre_tool_use

examples/ahp_agent_monitors_agent.py:136–158  ·  view source on GitHub ↗
(self, payload: Dict[str, Any], depth: int)

Source from the content-addressed store, hash-verified

134 return {"action": "continue", "reason": "分析失败,默认允许"}
135
136 def _handle_pre_tool_use(self, payload: Dict[str, Any], depth: int) -> Dict[str, Any]:
137 tool = payload.get("tool", "unknown")
138 args = payload.get("arguments", {})
139 self.stats["requests"] += 1
140 log(f"pre_tool_use: tool={tool} args={json.dumps(args)[:80]} depth={depth}")
141
142 prompt = PRE_TOOL_PROMPT.format(
143 tool=tool,
144 args=json.dumps(args, ensure_ascii=False),
145 depth=depth,
146 )
147 decision = self._llm_decide(prompt)
148 action = decision.get("action", "continue")
149 reason = decision.get("reason", "")
150
151 if action == "block":
152 self.stats["blocked"] += 1
153 log(f" 已阻止: {reason}")
154 else:
155 self.stats["allowed"] += 1
156 log(f" 已允许: {reason}")
157
158 return {"action": action, "reason": reason}
159
160 def _handle_notification(self, event_type: str, payload: Dict[str, Any], depth: int):
161 """通知是即发即弃的 — 不发送响应"""

Callers 1

_handle_requestMethod · 0.95

Calls 3

_llm_decideMethod · 0.95
logFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected