feat(agent): human-in-the-loop approval for MCP tool calls (#1173)

Add an opt-in human approval gate so Agent runs pause before executing
MCP tools that operators flag as dangerous, surface an approval card in
the chat UI, and only resume after the user approves (optionally with
edited args) or rejects.

Backend
- New mcp_tool_approvals table + repo/service to mark per-tool approval
  required (PG migration 000042 + sqlite init).
- approval.Gate coordinates RequestAndWait / Resolve with sync.Once
  delivery, configurable timeout, and Redis Pub/Sub fan-out so multi-
  replica deployments work without sticky sessions.
- MCPTool.Execute integrates the gate; uses a round-level ApprovalCtx
  (without the per-tool 60s timeout) for the wait, and re-derives a
  fresh 60s exec ctx after approval so CallTool keeps a full window.
- New SSE response types (tool_approval_required / _resolved) and
  EventBus events plumb approval state to AgentStreamDisplay.
- REST: list/set per-tool approval flag, resolve pending approval.
- Configurable via agent.tool_approval_timeout_seconds (yaml) or
  WEKNORA_AGENT_TOOL_APPROVAL_TIMEOUT env (accepts seconds or Go
  duration).

Frontend
- MCP settings: per-tool "require approval" switch on the test panel.
- Chat: ToolApprovalCard renders the pause point with editable JSON
  args, validation feedback, mm:ss countdown that turns warning/danger
  near deadline, and a resolved state that retains context.
- i18n strings added for zh-CN / en-US / ko-KR / ru-RU.

Docs
- docs/zh/mcp-approval.md covering behavior, config, API, deployment
  considerations (Redis cross-instance, restart limitations).
This commit is contained in:
wizardchen
2026-05-10 21:23:49 +08:00
committed by lyingbug
parent c1826030ab
commit 5510ea8f5a
36 changed files with 1814 additions and 36 deletions
+15 -1
View File
@@ -37,6 +37,9 @@ type AgentConfig struct {
// LLMCallTimeout is the default timeout for a single LLM call in seconds.
// Default: 120 (standard agents) or 300 (can be overridden by Env).
LLMCallTimeout int `yaml:"llm_call_timeout" json:"llm_call_timeout"`
// ToolApprovalTimeoutSeconds is how long the agent waits for human approval on a flagged MCP tool.
// 0 means default 600 (10 minutes).
ToolApprovalTimeoutSeconds int `yaml:"tool_approval_timeout_seconds" json:"tool_approval_timeout_seconds"`
}
// IMConfig configures the IM integration service.
@@ -269,7 +272,9 @@ func DefaultTemplateByMode(templates []PromptTemplate, mode string) *PromptTempl
// LocalizeTemplates returns a deep copy of the template list with Name and
// Description replaced according to the given locale. Fallback chain:
// locale → primary language (e.g. "zh" from "zh-CN") → original Name/Description.
//
// locale → primary language (e.g. "zh" from "zh-CN") → original Name/Description.
//
// The returned slice is safe to serialise directly; it never mutates the original.
func LocalizeTemplates(templates []PromptTemplate, locale string) []PromptTemplate {
if len(templates) == 0 {
@@ -568,6 +573,15 @@ func applyAgentEnvOverrides(cfg *Config) {
cfg.Agent.LLMCallTimeout = int(sec.Seconds())
}
}
// MCP tool human-approval wait timeout (issue #1173). Accepts Go duration
// (e.g. "10m", "30s") or a bare number interpreted as seconds.
if value := strings.TrimSpace(os.Getenv("WEKNORA_AGENT_TOOL_APPROVAL_TIMEOUT")); value != "" {
if d, err := time.ParseDuration(value); err == nil {
cfg.Agent.ToolApprovalTimeoutSeconds = int(d.Seconds())
} else if d, err := time.ParseDuration(value + "s"); err == nil {
cfg.Agent.ToolApprovalTimeoutSeconds = int(d.Seconds())
}
}
}
// backfillConversationDefaults resolves prompt template ID references