5510ea8f5a
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).
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
)
|
|
|
|
type mcpToolApprovalService struct {
|
|
repo interfaces.MCPToolApprovalRepository
|
|
mcpRepo interfaces.MCPServiceRepository
|
|
}
|
|
|
|
// NewMCPToolApprovalService constructs the MCP tool approval service.
|
|
func NewMCPToolApprovalService(
|
|
repo interfaces.MCPToolApprovalRepository,
|
|
mcpRepo interfaces.MCPServiceRepository,
|
|
) interfaces.MCPToolApprovalService {
|
|
return &mcpToolApprovalService{repo: repo, mcpRepo: mcpRepo}
|
|
}
|
|
|
|
func (s *mcpToolApprovalService) ListByService(ctx context.Context, tenantID uint64, serviceID string) ([]*types.MCPToolApproval, error) {
|
|
svc, err := s.mcpRepo.GetByID(ctx, tenantID, serviceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if svc == nil {
|
|
return nil, fmt.Errorf("mcp service not found")
|
|
}
|
|
return s.repo.ListByService(ctx, tenantID, serviceID)
|
|
}
|
|
|
|
func (s *mcpToolApprovalService) SetRequireApproval(
|
|
ctx context.Context, tenantID uint64, serviceID, toolName string, require bool,
|
|
) error {
|
|
if toolName == "" {
|
|
return fmt.Errorf("tool_name is required")
|
|
}
|
|
svc, err := s.mcpRepo.GetByID(ctx, tenantID, serviceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if svc == nil {
|
|
return fmt.Errorf("mcp service not found")
|
|
}
|
|
row := &types.MCPToolApproval{
|
|
TenantID: tenantID,
|
|
ServiceID: serviceID,
|
|
ToolName: toolName,
|
|
RequireApproval: require,
|
|
}
|
|
return s.repo.Upsert(ctx, row)
|
|
}
|
|
|
|
func (s *mcpToolApprovalService) IsRequired(ctx context.Context, tenantID uint64, serviceID, toolName string) (bool, error) {
|
|
return s.repo.IsRequired(ctx, tenantID, serviceID, toolName)
|
|
}
|