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:
+19
-9
@@ -165,7 +165,7 @@ func formatToolHint(name string, args map[string]any) string {
|
||||
// When ParallelToolCalls is enabled and there are 2+ tool calls, they execute concurrently.
|
||||
func (e *AgentEngine) executeToolCalls(
|
||||
ctx context.Context, response *types.ChatResponse,
|
||||
step *types.AgentStep, iteration int, sessionID string,
|
||||
step *types.AgentStep, iteration int, sessionID, assistantMessageID string,
|
||||
) {
|
||||
if len(response.ToolCalls) == 0 {
|
||||
return
|
||||
@@ -177,12 +177,12 @@ func (e *AgentEngine) executeToolCalls(
|
||||
|
||||
// Use parallel execution when enabled and there are multiple tool calls
|
||||
if e.config.ParallelToolCalls && n >= 2 {
|
||||
e.executeToolCallsParallel(ctx, response, step, iteration, sessionID)
|
||||
e.executeToolCallsParallel(ctx, response, step, iteration, sessionID, assistantMessageID)
|
||||
return
|
||||
}
|
||||
|
||||
for i, tc := range response.ToolCalls {
|
||||
e.executeSingleToolCall(ctx, tc, i, step, iteration, round, sessionID)
|
||||
e.executeSingleToolCall(ctx, tc, i, step, iteration, round, sessionID, assistantMessageID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ func (e *AgentEngine) executeToolCalls(
|
||||
// collecting results in original order.
|
||||
func (e *AgentEngine) executeToolCallsParallel(
|
||||
ctx context.Context, response *types.ChatResponse,
|
||||
step *types.AgentStep, iteration int, sessionID string,
|
||||
step *types.AgentStep, iteration int, sessionID, assistantMessageID string,
|
||||
) {
|
||||
round := iteration + 1
|
||||
n := len(response.ToolCalls)
|
||||
@@ -203,7 +203,7 @@ func (e *AgentEngine) executeToolCallsParallel(
|
||||
for i, tc := range response.ToolCalls {
|
||||
i, tc := i, tc // capture loop vars
|
||||
g.Go(func() error {
|
||||
toolCall := e.runToolCall(gCtx, tc, i, iteration, round, sessionID)
|
||||
toolCall := e.runToolCall(gCtx, tc, i, iteration, round, sessionID, assistantMessageID)
|
||||
mu.Lock()
|
||||
results[i] = toolCall
|
||||
mu.Unlock()
|
||||
@@ -258,9 +258,9 @@ func (e *AgentEngine) executeToolCallsParallel(
|
||||
// executeSingleToolCall runs one tool call sequentially (original behavior).
|
||||
func (e *AgentEngine) executeSingleToolCall(
|
||||
ctx context.Context, tc types.LLMToolCall, i int,
|
||||
step *types.AgentStep, iteration, round int, sessionID string,
|
||||
step *types.AgentStep, iteration, round int, sessionID, assistantMessageID string,
|
||||
) {
|
||||
toolCall := e.runToolCall(ctx, tc, i, iteration, round, sessionID)
|
||||
toolCall := e.runToolCall(ctx, tc, i, iteration, round, sessionID, assistantMessageID)
|
||||
step.ToolCalls = append(step.ToolCalls, toolCall)
|
||||
|
||||
result := toolCall.Result
|
||||
@@ -304,7 +304,7 @@ func (e *AgentEngine) executeSingleToolCall(
|
||||
// It returns the completed ToolCall struct. Safe to call from multiple goroutines.
|
||||
func (e *AgentEngine) runToolCall(
|
||||
ctx context.Context, tc types.LLMToolCall, i int,
|
||||
iteration, round int, sessionID string,
|
||||
iteration, round int, sessionID, assistantMessageID string,
|
||||
) types.ToolCall {
|
||||
tc.ID = agenttools.NormalizeToolCallID(tc.ID, tc.Function.Name, i)
|
||||
total := "?" // unknown in isolation; callers log the batch size
|
||||
@@ -392,7 +392,17 @@ func (e *AgentEngine) runToolCall(
|
||||
},
|
||||
})
|
||||
|
||||
execCtx, toolCancel := context.WithTimeout(toolCtx, defaultToolExecTimeout)
|
||||
toolExecCtx := agenttools.WithToolExecContext(toolCtx, &agenttools.ToolExecContext{
|
||||
SessionID: sessionID,
|
||||
AssistantMessageID: assistantMessageID,
|
||||
EventBus: e.eventBus,
|
||||
ToolCallID: tc.ID,
|
||||
// ApprovalCtx keeps the round-level ctx without the per-tool 60s timeout,
|
||||
// so MCP tool human-approval (issue #1173) can legitimately block longer.
|
||||
ApprovalCtx: toolCtx,
|
||||
})
|
||||
|
||||
execCtx, toolCancel := context.WithTimeout(toolExecCtx, defaultToolExecTimeout)
|
||||
result, err := e.toolRegistry.ExecuteTool(
|
||||
execCtx, tc.Function.Name,
|
||||
json.RawMessage(tc.Function.Arguments),
|
||||
|
||||
@@ -0,0 +1,361 @@
|
||||
// Package approval implements human-in-the-loop gating for dangerous MCP tool calls (issue #1173).
|
||||
package approval
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// pubsubChannel is the Redis channel used to fan-out Resolve calls across
|
||||
// backend replicas (issue #1173 cross-instance support).
|
||||
const pubsubChannel = "weknora:mcp_approval:resolve"
|
||||
|
||||
// resolveMessage is the JSON payload published when one instance receives a
|
||||
// Resolve API call but the pending wait may live on another instance.
|
||||
type resolveMessage struct {
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
PendingID string `json:"pending_id"`
|
||||
Approved bool `json:"approved"`
|
||||
ModifiedArgs json.RawMessage `json:"modified_args,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
TimedOut bool `json:"timed_out,omitempty"`
|
||||
Canceled bool `json:"canceled,omitempty"`
|
||||
}
|
||||
|
||||
// Checker answers whether a concrete MCP tool requires human approval before execution.
|
||||
type Checker interface {
|
||||
IsRequired(ctx context.Context, tenantID uint64, serviceID, toolName string) (bool, error)
|
||||
}
|
||||
|
||||
// Decision is the outcome of a pending tool approval.
|
||||
type Decision struct {
|
||||
Approved bool
|
||||
ModifiedArgs json.RawMessage // optional JSON object; when set and Approved, replaces original args
|
||||
Reason string
|
||||
TimedOut bool
|
||||
ContextCanceled bool
|
||||
}
|
||||
|
||||
// PendingRequest carries everything needed to block and notify the UI.
|
||||
type PendingRequest struct {
|
||||
TenantID uint64
|
||||
SessionID string
|
||||
AssistantMessageID string
|
||||
RequestID string
|
||||
EventBus *event.EventBus
|
||||
ServiceID string
|
||||
ServiceName string
|
||||
MCPToolName string // name on MCP server
|
||||
RegisteredToolName string // registry name e.g. mcp_svc_tool
|
||||
Description string
|
||||
Args json.RawMessage
|
||||
ToolCallID string
|
||||
}
|
||||
|
||||
// MCPApproval is the surface used by MCPTool (mockable in tests).
|
||||
type MCPApproval interface {
|
||||
NeedsApproval(ctx context.Context, tenantID uint64, serviceID, toolName string) bool
|
||||
RequestAndWait(ctx context.Context, req PendingRequest) (Decision, error)
|
||||
}
|
||||
|
||||
var _ MCPApproval = (*Gate)(nil)
|
||||
|
||||
// Gate coordinates wait/resolve for MCP tool approvals.
|
||||
//
|
||||
// Pending waiters live in-memory on the instance that started RequestAndWait.
|
||||
// When a redis client is supplied, Resolve calls hitting any replica are
|
||||
// published over Redis Pub/Sub so the owning instance can deliver the decision
|
||||
// (issue #1173 cross-instance support). Without redis, the gate degrades to
|
||||
// single-process behavior (deployments must use sticky sessions).
|
||||
type Gate struct {
|
||||
mu sync.Mutex
|
||||
pending map[string]*waiter
|
||||
checker Checker
|
||||
timeout time.Duration
|
||||
rdb *redis.Client // optional; nil disables cross-instance fan-out
|
||||
}
|
||||
|
||||
type waiter struct {
|
||||
ch chan Decision
|
||||
tenantID uint64
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (w *waiter) deliver(d Decision) {
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
w.once.Do(func() {
|
||||
select {
|
||||
case w.ch <- d:
|
||||
default:
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var (
|
||||
// ErrPendingNotFound is returned when Resolve is called with an unknown id.
|
||||
ErrPendingNotFound = errors.New("tool approval pending not found")
|
||||
// ErrTenantMismatch is returned when Resolve tenant does not match the pending request.
|
||||
ErrTenantMismatch = errors.New("tenant mismatch for tool approval")
|
||||
)
|
||||
|
||||
// NewGate builds a gate. checker may be nil (disables gating). cfg may be nil
|
||||
// (defaults apply). rdb may be nil (single-instance mode).
|
||||
func NewGate(cfg *config.Config, checker Checker, rdb *redis.Client) *Gate {
|
||||
timeout := 10 * time.Minute
|
||||
if cfg != nil && cfg.Agent != nil && cfg.Agent.ToolApprovalTimeoutSeconds > 0 {
|
||||
timeout = time.Duration(cfg.Agent.ToolApprovalTimeoutSeconds) * time.Second
|
||||
}
|
||||
g := &Gate{
|
||||
pending: make(map[string]*waiter),
|
||||
checker: checker,
|
||||
timeout: timeout,
|
||||
rdb: rdb,
|
||||
}
|
||||
if rdb != nil {
|
||||
go g.runSubscriber()
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
// runSubscriber listens for cross-instance Resolve fan-outs and delivers
|
||||
// decisions to local waiters. Runs for the lifetime of the process.
|
||||
func (g *Gate) runSubscriber() {
|
||||
ctx := context.Background()
|
||||
for {
|
||||
sub := g.rdb.Subscribe(ctx, pubsubChannel)
|
||||
ch := sub.Channel()
|
||||
for msg := range ch {
|
||||
var m resolveMessage
|
||||
if err := json.Unmarshal([]byte(msg.Payload), &m); err != nil {
|
||||
logger.GetLogger(ctx).Warnf("mcp approval pubsub: bad payload: %v", err)
|
||||
continue
|
||||
}
|
||||
if err := g.deliverLocal(m.TenantID, m.PendingID, Decision{
|
||||
Approved: m.Approved,
|
||||
ModifiedArgs: m.ModifiedArgs,
|
||||
Reason: m.Reason,
|
||||
TimedOut: m.TimedOut,
|
||||
ContextCanceled: m.Canceled,
|
||||
}); err != nil && !errors.Is(err, ErrPendingNotFound) {
|
||||
logger.GetLogger(ctx).Warnf("mcp approval pubsub deliver: %v", err)
|
||||
}
|
||||
}
|
||||
_ = sub.Close()
|
||||
// Reconnect after brief backoff if Redis hiccups.
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// NeedsApproval returns whether execution should pause for human confirmation.
|
||||
func (g *Gate) NeedsApproval(ctx context.Context, tenantID uint64, serviceID, toolName string) bool {
|
||||
if g == nil || g.checker == nil || tenantID == 0 || serviceID == "" || toolName == "" {
|
||||
return false
|
||||
}
|
||||
ok, err := g.checker.IsRequired(ctx, tenantID, serviceID, toolName)
|
||||
if err != nil {
|
||||
logger.GetLogger(ctx).Warnf("mcp tool approval check failed (skip gate): %v", err)
|
||||
return false
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// RequestAndWait emits a UI event, then blocks until Resolve, timeout, or ctx cancellation.
|
||||
func (g *Gate) RequestAndWait(ctx context.Context, req PendingRequest) (Decision, error) {
|
||||
if g == nil {
|
||||
return Decision{Approved: true}, nil
|
||||
}
|
||||
if g.checker == nil {
|
||||
return Decision{Approved: true}, nil
|
||||
}
|
||||
if req.EventBus == nil {
|
||||
return Decision{}, fmt.Errorf("tool approval: EventBus is nil")
|
||||
}
|
||||
|
||||
pendingID := uuid.New().String()
|
||||
w := &waiter{
|
||||
ch: make(chan Decision, 1),
|
||||
tenantID: req.TenantID,
|
||||
}
|
||||
|
||||
g.mu.Lock()
|
||||
g.pending[pendingID] = w
|
||||
g.mu.Unlock()
|
||||
|
||||
defer func() {
|
||||
g.mu.Lock()
|
||||
delete(g.pending, pendingID)
|
||||
g.mu.Unlock()
|
||||
}()
|
||||
|
||||
var argsObj interface{}
|
||||
if len(req.Args) > 0 {
|
||||
_ = json.Unmarshal(req.Args, &argsObj)
|
||||
}
|
||||
|
||||
timeoutSec := int(g.timeout / time.Second)
|
||||
if timeoutSec < 1 {
|
||||
timeoutSec = 1
|
||||
}
|
||||
|
||||
evtData := event.ToolApprovalRequiredData{
|
||||
PendingID: pendingID,
|
||||
TenantID: req.TenantID,
|
||||
SessionID: req.SessionID,
|
||||
AssistantMessageID: req.AssistantMessageID,
|
||||
ServiceID: req.ServiceID,
|
||||
ServiceName: req.ServiceName,
|
||||
MCPToolName: req.MCPToolName,
|
||||
RegisteredToolName: req.RegisteredToolName,
|
||||
Description: req.Description,
|
||||
Args: argsObj,
|
||||
ArgsJSON: string(req.Args),
|
||||
TimeoutSeconds: timeoutSec,
|
||||
RequestedAtUnix: time.Now().Unix(),
|
||||
ToolCallID: req.ToolCallID,
|
||||
RequestID: req.RequestID,
|
||||
}
|
||||
|
||||
if err := req.EventBus.Emit(ctx, event.Event{
|
||||
ID: pendingID + "-approval-required",
|
||||
Type: event.EventToolApprovalRequired,
|
||||
SessionID: req.SessionID,
|
||||
Data: evtData,
|
||||
Metadata: map[string]interface{}{
|
||||
"assistant_message_id": req.AssistantMessageID,
|
||||
"pending_id": pendingID,
|
||||
},
|
||||
RequestID: req.RequestID,
|
||||
}); err != nil {
|
||||
return Decision{}, fmt.Errorf("emit tool approval required: %w", err)
|
||||
}
|
||||
|
||||
timer := time.NewTimer(g.timeout)
|
||||
defer timer.Stop()
|
||||
|
||||
emitResolved := func(d Decision) {
|
||||
if req.EventBus == nil {
|
||||
return
|
||||
}
|
||||
_ = req.EventBus.Emit(context.WithoutCancel(ctx), event.Event{
|
||||
ID: pendingID + "-approval-resolved",
|
||||
Type: event.EventToolApprovalResolved,
|
||||
SessionID: req.SessionID,
|
||||
Data: event.ToolApprovalResolvedData{
|
||||
PendingID: pendingID,
|
||||
Approved: d.Approved,
|
||||
Reason: d.Reason,
|
||||
TimedOut: d.TimedOut,
|
||||
Canceled: d.ContextCanceled,
|
||||
},
|
||||
Metadata: map[string]interface{}{
|
||||
"assistant_message_id": req.AssistantMessageID,
|
||||
},
|
||||
RequestID: req.RequestID,
|
||||
})
|
||||
}
|
||||
|
||||
var d Decision
|
||||
select {
|
||||
case d = <-w.ch:
|
||||
emitResolved(d)
|
||||
return d, nil
|
||||
case <-timer.C:
|
||||
d = Decision{Approved: false, Reason: "approval timeout", TimedOut: true}
|
||||
w.deliver(d)
|
||||
d = <-w.ch
|
||||
emitResolved(d)
|
||||
return d, nil
|
||||
case <-ctx.Done():
|
||||
d = Decision{Approved: false, Reason: "request canceled", ContextCanceled: true}
|
||||
w.deliver(d)
|
||||
d = <-w.ch
|
||||
emitResolved(d)
|
||||
return d, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve completes a pending approval. tenantID must match the tenant that
|
||||
// started the wait. If the waiter is not on this instance and Redis Pub/Sub
|
||||
// is configured, the decision is fanned out to all replicas (best-effort).
|
||||
func (g *Gate) Resolve(tenantID uint64, pendingID string, d Decision) error {
|
||||
if g == nil {
|
||||
return fmt.Errorf("gate is nil")
|
||||
}
|
||||
switch err := g.deliverLocal(tenantID, pendingID, d); {
|
||||
case err == nil:
|
||||
return nil
|
||||
case errors.Is(err, ErrTenantMismatch):
|
||||
return err
|
||||
case errors.Is(err, ErrPendingNotFound):
|
||||
if g.rdb == nil {
|
||||
return err
|
||||
}
|
||||
// Fan out to other replicas.
|
||||
payload, mErr := json.Marshal(resolveMessage{
|
||||
TenantID: tenantID,
|
||||
PendingID: pendingID,
|
||||
Approved: d.Approved,
|
||||
ModifiedArgs: d.ModifiedArgs,
|
||||
Reason: d.Reason,
|
||||
TimedOut: d.TimedOut,
|
||||
Canceled: d.ContextCanceled,
|
||||
})
|
||||
if mErr != nil {
|
||||
return fmt.Errorf("encode pubsub payload: %w", mErr)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if pErr := g.rdb.Publish(ctx, pubsubChannel, payload).Err(); pErr != nil {
|
||||
return fmt.Errorf("publish approval resolve: %w", pErr)
|
||||
}
|
||||
// Best-effort: the owning instance will deliver and emit Resolved.
|
||||
return nil
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// deliverLocal attempts to satisfy a waiter on this instance only.
|
||||
func (g *Gate) deliverLocal(tenantID uint64, pendingID string, d Decision) error {
|
||||
g.mu.Lock()
|
||||
w, ok := g.pending[pendingID]
|
||||
if !ok {
|
||||
g.mu.Unlock()
|
||||
return ErrPendingNotFound
|
||||
}
|
||||
if w.tenantID != tenantID {
|
||||
g.mu.Unlock()
|
||||
return ErrTenantMismatch
|
||||
}
|
||||
g.mu.Unlock()
|
||||
|
||||
w.deliver(d)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Adapter makes MCPToolApprovalService satisfy Checker without importing the service package here.
|
||||
type Adapter struct {
|
||||
Svc interface {
|
||||
IsRequired(ctx context.Context, tenantID uint64, serviceID, toolName string) (bool, error)
|
||||
}
|
||||
}
|
||||
|
||||
// IsRequired implements Checker.
|
||||
func (a *Adapter) IsRequired(ctx context.Context, tenantID uint64, serviceID, toolName string) (bool, error) {
|
||||
if a == nil || a.Svc == nil {
|
||||
return false, nil
|
||||
}
|
||||
return a.Svc.IsRequired(ctx, tenantID, serviceID, toolName)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package approval
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type stubChecker struct {
|
||||
required bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (s *stubChecker) IsRequired(ctx context.Context, tenantID uint64, serviceID, toolName string) (bool, error) {
|
||||
return s.required, s.err
|
||||
}
|
||||
|
||||
func TestGate_RequestAndWait_Approve(t *testing.T) {
|
||||
bus := event.NewEventBus()
|
||||
g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 2}}, &stubChecker{required: true}, nil)
|
||||
|
||||
ctx := context.Background()
|
||||
req := PendingRequest{
|
||||
TenantID: 1,
|
||||
SessionID: "s1",
|
||||
AssistantMessageID: "m1",
|
||||
EventBus: bus,
|
||||
ServiceID: "svc",
|
||||
ServiceName: "svcname",
|
||||
MCPToolName: "danger_tool",
|
||||
RegisteredToolName: "mcp_svcname_danger_tool",
|
||||
Description: "desc",
|
||||
Args: json.RawMessage(`{"a":1}`),
|
||||
ToolCallID: "tc1",
|
||||
}
|
||||
|
||||
bus.On(event.EventToolApprovalRequired, func(_ context.Context, evt event.Event) error {
|
||||
data, ok := evt.Data.(event.ToolApprovalRequiredData)
|
||||
require.True(t, ok)
|
||||
require.NotEmpty(t, data.PendingID)
|
||||
go func() {
|
||||
_ = g.Resolve(1, data.PendingID, Decision{Approved: true, ModifiedArgs: json.RawMessage(`{"a":2}`)})
|
||||
}()
|
||||
return nil
|
||||
})
|
||||
|
||||
d, err := g.RequestAndWait(ctx, req)
|
||||
require.NoError(t, err)
|
||||
require.True(t, d.Approved)
|
||||
require.JSONEq(t, `{"a":2}`, string(d.ModifiedArgs))
|
||||
}
|
||||
|
||||
func TestGate_RequestAndWait_Timeout(t *testing.T) {
|
||||
g := NewGate(&config.Config{Agent: &config.AgentConfig{ToolApprovalTimeoutSeconds: 1}}, &stubChecker{required: true}, nil)
|
||||
ctx := context.Background()
|
||||
req := PendingRequest{
|
||||
TenantID: 1,
|
||||
SessionID: "s1",
|
||||
AssistantMessageID: "m1",
|
||||
EventBus: event.NewEventBus(),
|
||||
ServiceID: "svc",
|
||||
ServiceName: "svcname",
|
||||
MCPToolName: "t",
|
||||
RegisteredToolName: "mcp_svcname_t",
|
||||
Args: json.RawMessage(`{}`),
|
||||
}
|
||||
d, err := g.RequestAndWait(ctx, req)
|
||||
require.NoError(t, err)
|
||||
require.False(t, d.Approved)
|
||||
require.True(t, d.TimedOut)
|
||||
}
|
||||
|
||||
func TestGate_NeedsApproval_NoChecker(t *testing.T) {
|
||||
g := NewGate(nil, nil, nil)
|
||||
require.False(t, g.NeedsApproval(context.Background(), 1, "x", "y"))
|
||||
}
|
||||
@@ -397,7 +397,7 @@ loop:
|
||||
// every exit path (break/continue/next) without having to sprinkle
|
||||
// manual finish calls throughout the many branches below.
|
||||
outcome, iterErr := e.runReActIteration(ctx, state, &messages, tools,
|
||||
sessionID, query, &emptyRetries, &consecutiveSameContent, &lastResponseContent)
|
||||
sessionID, messageID, query, &emptyRetries, &consecutiveSameContent, &lastResponseContent)
|
||||
if iterErr != nil {
|
||||
return state, iterErr
|
||||
}
|
||||
@@ -451,7 +451,7 @@ func (e *AgentEngine) runReActIteration(
|
||||
state *types.AgentState,
|
||||
messagesPtr *[]chat.Message,
|
||||
tools []chat.Tool,
|
||||
sessionID, query string,
|
||||
sessionID, assistantMessageID, query string,
|
||||
emptyRetries, consecutiveSameContent *int,
|
||||
lastResponseContent *string,
|
||||
) (outcome iterOutcome, retErr error) {
|
||||
@@ -625,7 +625,7 @@ func (e *AgentEngine) runReActIteration(
|
||||
}
|
||||
|
||||
// 3. Act: Execute tool calls
|
||||
e.executeToolCalls(ctx, response, &step, state.CurrentRound, sessionID)
|
||||
e.executeToolCalls(ctx, response, &step, state.CurrentRound, sessionID, assistantMessageID)
|
||||
toolCallCount = len(step.ToolCalls)
|
||||
|
||||
// 4. Observe: Add tool results to messages and write to context
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
)
|
||||
|
||||
type execCtxKey struct{}
|
||||
|
||||
// ToolExecContext is attached to context during agent tool execution (per tool call).
|
||||
type ToolExecContext struct {
|
||||
SessionID string
|
||||
AssistantMessageID string
|
||||
RequestID string
|
||||
ToolCallID string
|
||||
EventBus *event.EventBus
|
||||
// ApprovalCtx is the parent ctx WITHOUT defaultToolExecTimeout; used when the tool
|
||||
// must wait for human approval that may exceed normal tool exec timeout (issue #1173).
|
||||
// Falls back to the per-tool execCtx when nil.
|
||||
ApprovalCtx context.Context
|
||||
}
|
||||
|
||||
// WithToolExecContext returns ctx that carries ToolExecContext for MCP approval and similar features.
|
||||
func WithToolExecContext(ctx context.Context, meta *ToolExecContext) context.Context {
|
||||
if meta == nil {
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, execCtxKey{}, meta)
|
||||
}
|
||||
|
||||
// ToolExecFromContext returns metadata attached by the agent engine, if any.
|
||||
func ToolExecFromContext(ctx context.Context) (*ToolExecContext, bool) {
|
||||
v := ctx.Value(execCtxKey{})
|
||||
if v == nil {
|
||||
return nil, false
|
||||
}
|
||||
meta, ok := v.(*ToolExecContext)
|
||||
return meta, ok && meta != nil
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/agent/approval"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/mcp"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
@@ -19,14 +20,16 @@ type MCPTool struct {
|
||||
service *types.MCPService
|
||||
mcpTool *types.MCPTool
|
||||
mcpManager *mcp.MCPManager
|
||||
gate approval.MCPApproval // optional human approval before CallTool (issue #1173)
|
||||
}
|
||||
|
||||
// NewMCPTool creates a new MCP tool wrapper
|
||||
func NewMCPTool(service *types.MCPService, mcpTool *types.MCPTool, mcpManager *mcp.MCPManager) *MCPTool {
|
||||
func NewMCPTool(service *types.MCPService, mcpTool *types.MCPTool, mcpManager *mcp.MCPManager, gate approval.MCPApproval) *MCPTool {
|
||||
return &MCPTool{
|
||||
service: service,
|
||||
mcpTool: mcpTool,
|
||||
mcpManager: mcpManager,
|
||||
gate: gate,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,13 +96,77 @@ func (t *MCPTool) Execute(ctx context.Context, args json.RawMessage) (*types.Too
|
||||
// Parse args from json.RawMessage
|
||||
var input MCPInput
|
||||
if err := json.Unmarshal(args, &input); err != nil {
|
||||
logger.Errorf(ctx, "[Tool][DatabaseQuery] Failed to parse args: %v", err)
|
||||
logger.Errorf(ctx, "[Tool][MCPTool] Failed to parse args: %v", err)
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: fmt.Sprintf("Failed to parse args: %v", err),
|
||||
}, err
|
||||
}
|
||||
|
||||
// Human approval gate for dangerous tools (issue #1173)
|
||||
if t.gate != nil {
|
||||
if meta, ok := ToolExecFromContext(ctx); ok && meta != nil && meta.EventBus != nil {
|
||||
tenantID, _ := types.TenantIDFromContext(ctx)
|
||||
if t.gate.NeedsApproval(ctx, tenantID, t.service.ID, t.mcpTool.Name) {
|
||||
// Use ApprovalCtx (round-level ctx WITHOUT defaultToolExecTimeout) so
|
||||
// human approval can legitimately wait longer than the per-tool 60s.
|
||||
// User-stop / request cancel still propagates because ApprovalCtx is a
|
||||
// child of the request ctx.
|
||||
waitCtx := ctx
|
||||
if meta.ApprovalCtx != nil {
|
||||
waitCtx = meta.ApprovalCtx
|
||||
}
|
||||
decision, waitErr := t.gate.RequestAndWait(waitCtx, approval.PendingRequest{
|
||||
TenantID: tenantID,
|
||||
SessionID: meta.SessionID,
|
||||
AssistantMessageID: meta.AssistantMessageID,
|
||||
RequestID: meta.RequestID,
|
||||
EventBus: meta.EventBus,
|
||||
ServiceID: t.service.ID,
|
||||
ServiceName: t.service.Name,
|
||||
MCPToolName: t.mcpTool.Name,
|
||||
RegisteredToolName: t.Name(),
|
||||
Description: t.mcpTool.Description,
|
||||
Args: args,
|
||||
ToolCallID: meta.ToolCallID,
|
||||
})
|
||||
if waitErr != nil {
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: fmt.Sprintf("Tool approval failed: %v", waitErr),
|
||||
}, nil
|
||||
}
|
||||
if !decision.Approved {
|
||||
msg := decision.Reason
|
||||
if msg == "" {
|
||||
msg = "tool execution rejected by user"
|
||||
}
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: msg,
|
||||
}, nil
|
||||
}
|
||||
if len(decision.ModifiedArgs) > 0 {
|
||||
args = decision.ModifiedArgs
|
||||
if err := json.Unmarshal(args, &input); err != nil {
|
||||
return &types.ToolResult{
|
||||
Success: false,
|
||||
Error: fmt.Sprintf("Invalid modified_args after approval: %v", err),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
// Approval may have consumed most/all of the per-tool 60s budget set by the
|
||||
// agent engine (act.go). Re-derive a fresh tool-exec ctx from ApprovalCtx so
|
||||
// the actual MCP CallTool gets a full timeout window. (issue #1173 follow-up)
|
||||
if meta.ApprovalCtx != nil {
|
||||
freshCtx, freshCancel := context.WithTimeout(meta.ApprovalCtx, 60*time.Second)
|
||||
defer freshCancel()
|
||||
ctx = freshCtx
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get or create MCP client
|
||||
client, err := t.mcpManager.GetOrCreateClient(t.service)
|
||||
if err != nil {
|
||||
@@ -326,6 +393,7 @@ func RegisterMCPTools(
|
||||
registry *ToolRegistry,
|
||||
services []*types.MCPService,
|
||||
mcpManager *mcp.MCPManager,
|
||||
gate approval.MCPApproval,
|
||||
) error {
|
||||
if len(services) == 0 {
|
||||
return nil
|
||||
@@ -393,7 +461,7 @@ func RegisterMCPTools(
|
||||
|
||||
// Register each tool
|
||||
for _, mcpTool := range mcpTools {
|
||||
tool := NewMCPTool(service, mcpTool, mcpManager)
|
||||
tool := NewMCPTool(service, mcpTool, mcpManager, gate)
|
||||
toolName := tool.Name()
|
||||
|
||||
// Check for name collision before registering (first-wins policy).
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// MCPToolApprovalRepository implements interfaces.MCPToolApprovalRepository.
|
||||
type MCPToolApprovalRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewMCPToolApprovalRepository creates a repository backed by GORM.
|
||||
func NewMCPToolApprovalRepository(db *gorm.DB) interfaces.MCPToolApprovalRepository {
|
||||
return &MCPToolApprovalRepository{db: db}
|
||||
}
|
||||
|
||||
// ListByService returns all stored approval rows for an MCP service (may be empty).
|
||||
func (r *MCPToolApprovalRepository) ListByService(ctx context.Context, tenantID uint64, serviceID string) ([]*types.MCPToolApproval, error) {
|
||||
var rows []*types.MCPToolApproval
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND service_id = ?", tenantID, serviceID).
|
||||
Order("tool_name ASC").
|
||||
Find(&rows).Error
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list mcp tool approvals: %w", err)
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// IsRequired returns true when a row exists with require_approval = true.
|
||||
func (r *MCPToolApprovalRepository) IsRequired(ctx context.Context, tenantID uint64, serviceID, toolName string) (bool, error) {
|
||||
var row types.MCPToolApproval
|
||||
err := r.db.WithContext(ctx).
|
||||
Select("require_approval").
|
||||
Where("tenant_id = ? AND service_id = ? AND tool_name = ?", tenantID, serviceID, toolName).
|
||||
First(&row).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("get mcp tool approval: %w", err)
|
||||
}
|
||||
return row.RequireApproval, nil
|
||||
}
|
||||
|
||||
// Upsert creates or updates the approval flag for a tool.
|
||||
func (r *MCPToolApprovalRepository) Upsert(ctx context.Context, row *types.MCPToolApproval) error {
|
||||
if row == nil {
|
||||
return errors.New("row is nil")
|
||||
}
|
||||
var existing types.MCPToolApproval
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND service_id = ? AND tool_name = ?", row.TenantID, row.ServiceID, row.ToolName).
|
||||
First(&existing).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
if row.ID == "" {
|
||||
row.ID = uuid.New().String()
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Create(row).Error; err != nil {
|
||||
return fmt.Errorf("create mcp tool approval: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("get mcp tool approval for upsert: %w", err)
|
||||
}
|
||||
return r.db.WithContext(ctx).Model(&existing).Updates(map[string]interface{}{
|
||||
"require_approval": row.RequireApproval,
|
||||
"updated_at": time.Now(),
|
||||
}).Error
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/agent"
|
||||
"github.com/Tencent/WeKnora/internal/agent/approval"
|
||||
"github.com/Tencent/WeKnora/internal/agent/skills"
|
||||
"github.com/Tencent/WeKnora/internal/agent/tools"
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
@@ -59,6 +60,7 @@ type agentService struct {
|
||||
webSearchStateService interfaces.WebSearchStateService
|
||||
wikiPageService interfaces.WikiPageService
|
||||
tenantService interfaces.TenantService
|
||||
toolApprovalGate approval.MCPApproval
|
||||
}
|
||||
|
||||
// NewAgentService creates a new agent service
|
||||
@@ -78,6 +80,7 @@ func NewAgentService(
|
||||
webSearchStateService interfaces.WebSearchStateService,
|
||||
wikiPageService interfaces.WikiPageService,
|
||||
tenantService interfaces.TenantService,
|
||||
toolApprovalGate approval.MCPApproval,
|
||||
) interfaces.AgentService {
|
||||
return &agentService{
|
||||
cfg: cfg,
|
||||
@@ -95,6 +98,7 @@ func NewAgentService(
|
||||
webSearchStateService: webSearchStateService,
|
||||
wikiPageService: wikiPageService,
|
||||
tenantService: tenantService,
|
||||
toolApprovalGate: toolApprovalGate,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +227,7 @@ func (s *agentService) registerMCPTools(
|
||||
}
|
||||
}
|
||||
if len(enabledServices) > 0 {
|
||||
if err := tools.RegisterMCPTools(ctx, toolRegistry, enabledServices, s.mcpManager); err != nil {
|
||||
if err := tools.RegisterMCPTools(ctx, toolRegistry, enabledServices, s.mcpManager, s.toolApprovalGate); err != nil {
|
||||
logger.Warnf(ctx, "Failed to register MCP tools: %v", err)
|
||||
} else {
|
||||
logger.Infof(ctx, "Registered MCP tools from %d enabled services", len(enabledServices))
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -32,6 +32,7 @@ import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/agent/approval"
|
||||
"github.com/Tencent/WeKnora/internal/application/repository"
|
||||
memoryRepo "github.com/Tencent/WeKnora/internal/application/repository/memory/neo4j"
|
||||
dorisRepo "github.com/Tencent/WeKnora/internal/application/repository/retriever/doris"
|
||||
@@ -148,6 +149,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(neo4jRepo.NewNeo4jRepository))
|
||||
must(container.Provide(memoryRepo.NewMemoryRepository))
|
||||
must(container.Provide(repository.NewMCPServiceRepository))
|
||||
must(container.Provide(repository.NewMCPToolApprovalRepository))
|
||||
must(container.Provide(repository.NewCustomAgentRepository))
|
||||
must(container.Provide(repository.NewOrganizationRepository))
|
||||
must(container.Provide(repository.NewKBShareRepository))
|
||||
@@ -190,6 +192,7 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
|
||||
must(container.Provide(service.NewMessageService))
|
||||
must(container.Provide(service.NewMCPServiceService))
|
||||
must(container.Provide(service.NewMCPToolApprovalService))
|
||||
must(container.Provide(service.NewCustomAgentService))
|
||||
must(container.Provide(memoryService.NewMemoryService))
|
||||
must(container.Provide(service.NewWikiPageService))
|
||||
@@ -221,6 +224,11 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
// SessionService is passed as parameter to CreateAgentEngine method when creating AgentService
|
||||
logger.Debugf(ctx, "[Container] Registering event bus and agent service...")
|
||||
must(container.Provide(event.NewEventBus))
|
||||
must(container.Provide(func(cfg *config.Config, s interfaces.MCPToolApprovalService, rdb *redis.Client) *approval.Gate {
|
||||
return approval.NewGate(cfg, &approval.Adapter{Svc: s}, rdb)
|
||||
}))
|
||||
// Expose Gate as MCPApproval interface so AgentService and others can depend on the abstraction.
|
||||
must(container.Provide(func(g *approval.Gate) approval.MCPApproval { return g }))
|
||||
must(container.Provide(service.NewAgentService))
|
||||
|
||||
// Session service (depends on agent service)
|
||||
|
||||
@@ -54,6 +54,10 @@ const (
|
||||
EventAgentReferences EventType = "references" // 知识引用
|
||||
EventAgentFinalAnswer EventType = "final_answer" // 最终答案
|
||||
|
||||
// MCP tool human approval (issue #1173)
|
||||
EventToolApprovalRequired EventType = "tool_approval_required"
|
||||
EventToolApprovalResolved EventType = "tool_approval_resolved"
|
||||
|
||||
// Error events
|
||||
EventError EventType = "error" // 错误事件
|
||||
|
||||
|
||||
@@ -209,3 +209,31 @@ type StopData struct {
|
||||
MessageID string `json:"message_id"`
|
||||
Reason string `json:"reason,omitempty"` // Optional reason for stopping
|
||||
}
|
||||
|
||||
// ToolApprovalRequiredData is emitted when an MCP tool marked dangerous is about to run.
|
||||
type ToolApprovalRequiredData struct {
|
||||
PendingID string `json:"pending_id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
SessionID string `json:"session_id"`
|
||||
AssistantMessageID string `json:"assistant_message_id"`
|
||||
ServiceID string `json:"service_id"`
|
||||
ServiceName string `json:"service_name"`
|
||||
MCPToolName string `json:"mcp_tool_name"`
|
||||
RegisteredToolName string `json:"registered_tool_name"`
|
||||
Description string `json:"description"`
|
||||
Args interface{} `json:"args,omitempty"`
|
||||
ArgsJSON string `json:"args_json,omitempty"`
|
||||
TimeoutSeconds int `json:"timeout_seconds"`
|
||||
RequestedAtUnix int64 `json:"requested_at"`
|
||||
ToolCallID string `json:"tool_call_id"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
}
|
||||
|
||||
// ToolApprovalResolvedData confirms the user decision (or timeout/cancel).
|
||||
type ToolApprovalResolvedData struct {
|
||||
PendingID string `json:"pending_id"`
|
||||
Approved bool `json:"approved"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
TimedOut bool `json:"timed_out,omitempty"`
|
||||
Canceled bool `json:"canceled,omitempty"`
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/agent/approval"
|
||||
"github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
@@ -14,13 +17,21 @@ import (
|
||||
|
||||
// MCPServiceHandler handles MCP service related HTTP requests
|
||||
type MCPServiceHandler struct {
|
||||
mcpServiceService interfaces.MCPServiceService
|
||||
mcpServiceService interfaces.MCPServiceService
|
||||
mcpToolApprovalService interfaces.MCPToolApprovalService
|
||||
toolApprovalGate *approval.Gate
|
||||
}
|
||||
|
||||
// NewMCPServiceHandler creates a new MCP service handler
|
||||
func NewMCPServiceHandler(mcpServiceService interfaces.MCPServiceService) *MCPServiceHandler {
|
||||
func NewMCPServiceHandler(
|
||||
mcpServiceService interfaces.MCPServiceService,
|
||||
mcpToolApprovalService interfaces.MCPToolApprovalService,
|
||||
toolApprovalGate *approval.Gate,
|
||||
) *MCPServiceHandler {
|
||||
return &MCPServiceHandler{
|
||||
mcpServiceService: mcpServiceService,
|
||||
mcpServiceService: mcpServiceService,
|
||||
mcpToolApprovalService: mcpToolApprovalService,
|
||||
toolApprovalGate: toolApprovalGate,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -445,3 +456,116 @@ func (h *MCPServiceHandler) GetMCPServiceResources(c *gin.Context) {
|
||||
"data": resources,
|
||||
})
|
||||
}
|
||||
|
||||
// ListMCPToolApprovals returns persisted require_approval flags for tools on an MCP service.
|
||||
func (h *MCPServiceHandler) ListMCPToolApprovals(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
serviceID := secutils.SanitizeForLog(c.Param("id"))
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
if h.mcpToolApprovalService == nil {
|
||||
c.Error(errors.NewInternalServerError("MCP tool approval is not configured"))
|
||||
return
|
||||
}
|
||||
rows, err := h.mcpToolApprovalService.ListByService(ctx, tenantID, serviceID)
|
||||
if err != nil {
|
||||
c.Error(errors.NewNotFoundError(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": rows})
|
||||
}
|
||||
|
||||
type setMCPToolApprovalBody struct {
|
||||
RequireApproval bool `json:"require_approval"`
|
||||
}
|
||||
|
||||
// SetMCPToolApproval sets whether a tool requires human approval before the agent may call it.
|
||||
func (h *MCPServiceHandler) SetMCPToolApproval(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
serviceID := secutils.SanitizeForLog(c.Param("id"))
|
||||
rawName := c.Param("tool_name")
|
||||
toolName, err := url.PathUnescape(rawName)
|
||||
if err != nil {
|
||||
toolName = rawName
|
||||
}
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
if h.mcpToolApprovalService == nil {
|
||||
c.Error(errors.NewInternalServerError("MCP tool approval is not configured"))
|
||||
return
|
||||
}
|
||||
var body setMCPToolApprovalBody
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
c.Error(errors.NewBadRequestError(err.Error()))
|
||||
return
|
||||
}
|
||||
if err := h.mcpToolApprovalService.SetRequireApproval(ctx, tenantID, serviceID, toolName, body.RequireApproval); err != nil {
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
type resolveToolApprovalBody struct {
|
||||
Decision string `json:"decision" binding:"required"` // approve | reject
|
||||
ModifiedArgs json.RawMessage `json:"modified_args"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// ResolveToolApproval completes a pending MCP tool approval (agent execution resumes).
|
||||
func (h *MCPServiceHandler) ResolveToolApproval(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
pendingID := c.Param("pending_id")
|
||||
tenantID := c.GetUint64(types.TenantIDContextKey.String())
|
||||
if tenantID == 0 {
|
||||
c.Error(errors.NewBadRequestError("Tenant ID cannot be empty"))
|
||||
return
|
||||
}
|
||||
if h.toolApprovalGate == nil {
|
||||
c.Error(errors.NewInternalServerError("Tool approval gate is not configured"))
|
||||
return
|
||||
}
|
||||
var body resolveToolApprovalBody
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
c.Error(errors.NewBadRequestError(err.Error()))
|
||||
return
|
||||
}
|
||||
dec := approval.Decision{Reason: body.Reason}
|
||||
switch body.Decision {
|
||||
case "approve":
|
||||
dec.Approved = true
|
||||
if len(body.ModifiedArgs) > 0 {
|
||||
var probe map[string]interface{}
|
||||
if err := json.Unmarshal(body.ModifiedArgs, &probe); err != nil {
|
||||
c.Error(errors.NewBadRequestError("modified_args must be a JSON object"))
|
||||
return
|
||||
}
|
||||
dec.ModifiedArgs = body.ModifiedArgs
|
||||
}
|
||||
case "reject":
|
||||
dec.Approved = false
|
||||
default:
|
||||
c.Error(errors.NewBadRequestError("decision must be approve or reject"))
|
||||
return
|
||||
}
|
||||
if err := h.toolApprovalGate.Resolve(tenantID, pendingID, dec); err != nil {
|
||||
if err == approval.ErrPendingNotFound {
|
||||
c.Error(errors.NewNotFoundError("pending approval not found or already completed"))
|
||||
return
|
||||
}
|
||||
if err == approval.ErrTenantMismatch {
|
||||
c.Error(errors.NewBadRequestError("tenant mismatch"))
|
||||
return
|
||||
}
|
||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{"pending_id": pendingID})
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -66,6 +67,8 @@ func (h *AgentStreamHandler) Subscribe() {
|
||||
h.eventBus.On(event.EventError, h.handleError)
|
||||
h.eventBus.On(event.EventSessionTitle, h.handleSessionTitle)
|
||||
h.eventBus.On(event.EventAgentComplete, h.handleComplete)
|
||||
h.eventBus.On(event.EventToolApprovalRequired, h.handleToolApprovalRequired)
|
||||
h.eventBus.On(event.EventToolApprovalResolved, h.handleToolApprovalResolved)
|
||||
}
|
||||
|
||||
// handleThought handles agent thought events
|
||||
@@ -210,6 +213,60 @@ func (h *AgentStreamHandler) handleToolResult(ctx context.Context, evt event.Eve
|
||||
return nil
|
||||
}
|
||||
|
||||
func toolApprovalDataToMap(v interface{}) map[string]interface{} {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(b, &m); err != nil {
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// handleToolApprovalRequired persists MCP tool human-approval prompts for SSE / replay (issue #1173).
|
||||
func (h *AgentStreamHandler) handleToolApprovalRequired(ctx context.Context, evt event.Event) error {
|
||||
data, ok := evt.Data.(event.ToolApprovalRequiredData)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
meta := toolApprovalDataToMap(data)
|
||||
meta["pending_id"] = data.PendingID
|
||||
if err := h.streamManager.AppendEvent(h.ctx, h.sessionID, h.assistantMessageID, interfaces.StreamEvent{
|
||||
ID: evt.ID,
|
||||
Type: types.ResponseTypeToolApprovalRequired,
|
||||
Content: "MCP tool requires human approval",
|
||||
Done: true,
|
||||
Timestamp: time.Now(),
|
||||
Data: meta,
|
||||
}); err != nil {
|
||||
logger.GetLogger(h.ctx).Error("Append tool approval required event failed", "error", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleToolApprovalResolved persists the outcome of a tool approval (issue #1173).
|
||||
func (h *AgentStreamHandler) handleToolApprovalResolved(ctx context.Context, evt event.Event) error {
|
||||
data, ok := evt.Data.(event.ToolApprovalResolvedData)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
meta := toolApprovalDataToMap(data)
|
||||
meta["pending_id"] = data.PendingID
|
||||
if err := h.streamManager.AppendEvent(h.ctx, h.sessionID, h.assistantMessageID, interfaces.StreamEvent{
|
||||
ID: evt.ID,
|
||||
Type: types.ResponseTypeToolApprovalResolved,
|
||||
Content: "MCP tool approval resolved",
|
||||
Done: true,
|
||||
Timestamp: time.Now(),
|
||||
Data: meta,
|
||||
}); err != nil {
|
||||
logger.GetLogger(h.ctx).Error("Append tool approval resolved event failed", "error", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleReferences handles knowledge references events
|
||||
func (h *AgentStreamHandler) handleReferences(ctx context.Context, evt event.Event) error {
|
||||
data, ok := evt.Data.(event.AgentReferencesData)
|
||||
|
||||
@@ -500,6 +500,14 @@ func RegisterMCPServiceRoutes(r *gin.RouterGroup, handler *handler.MCPServiceHan
|
||||
mcpServices.GET("/:id/tools", handler.GetMCPServiceTools)
|
||||
// Get MCP service resources
|
||||
mcpServices.GET("/:id/resources", handler.GetMCPServiceResources)
|
||||
// MCP tool human approval (issue #1173)
|
||||
mcpServices.GET("/:id/tool-approvals", handler.ListMCPToolApprovals)
|
||||
mcpServices.PUT("/:id/tool-approvals/:tool_name", handler.SetMCPToolApproval)
|
||||
}
|
||||
|
||||
agentTool := r.Group("/agent")
|
||||
{
|
||||
agentTool.POST("/tool-approvals/:pending_id", handler.ResolveToolApproval)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,10 +27,10 @@ type FunctionCall struct {
|
||||
|
||||
// ChatResponse chat response
|
||||
type ChatResponse struct {
|
||||
Content string `json:"content"`
|
||||
Content string `json:"content"`
|
||||
ToolCalls []LLMToolCall `json:"tool_calls,omitempty"`
|
||||
FinishReason string `json:"finish_reason,omitempty"`
|
||||
Usage TokenUsage `json:"usage"`
|
||||
FinishReason string `json:"finish_reason,omitempty"`
|
||||
Usage TokenUsage `json:"usage"`
|
||||
}
|
||||
|
||||
// Response type
|
||||
@@ -57,6 +57,10 @@ const (
|
||||
ResponseTypeAgentQuery ResponseType = "agent_query"
|
||||
// Complete response type (agent complete)
|
||||
ResponseTypeComplete ResponseType = "complete"
|
||||
// ToolApprovalRequired: MCP tool marked dangerous — UI must collect user approval before execution continues
|
||||
ResponseTypeToolApprovalRequired ResponseType = "tool_approval_required"
|
||||
// ToolApprovalResolved: user approved/rejected (or timeout); informational for UI replay
|
||||
ResponseTypeToolApprovalResolved ResponseType = "tool_approval_resolved"
|
||||
)
|
||||
|
||||
// StreamResponse stream response
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// MCPToolApprovalRepository persists per-tool approval requirements.
|
||||
type MCPToolApprovalRepository interface {
|
||||
ListByService(ctx context.Context, tenantID uint64, serviceID string) ([]*types.MCPToolApproval, error)
|
||||
IsRequired(ctx context.Context, tenantID uint64, serviceID, toolName string) (bool, error)
|
||||
Upsert(ctx context.Context, row *types.MCPToolApproval) error
|
||||
}
|
||||
|
||||
// MCPToolApprovalService is the business layer for MCP tool approval flags.
|
||||
type MCPToolApprovalService interface {
|
||||
ListByService(ctx context.Context, tenantID uint64, serviceID string) ([]*types.MCPToolApproval, error)
|
||||
SetRequireApproval(ctx context.Context, tenantID uint64, serviceID, toolName string, require bool) error
|
||||
IsRequired(ctx context.Context, tenantID uint64, serviceID, toolName string) (bool, error)
|
||||
}
|
||||
+25
-3
@@ -30,9 +30,9 @@ type MCPService struct {
|
||||
Headers MCPHeaders `json:"headers" gorm:"type:json"`
|
||||
AuthConfig *MCPAuthConfig `json:"auth_config" gorm:"type:json"`
|
||||
AdvancedConfig *MCPAdvancedConfig `json:"advanced_config" gorm:"type:json"`
|
||||
StdioConfig *MCPStdioConfig `json:"stdio_config,omitempty" gorm:"type:json"` // Required for stdio transport
|
||||
EnvVars MCPEnvVars `json:"env_vars,omitempty" gorm:"type:json"` // Environment variables for stdio
|
||||
IsBuiltin bool `json:"is_builtin" gorm:"default:false"` // Whether this is a builtin MCP service (visible to all tenants)
|
||||
StdioConfig *MCPStdioConfig `json:"stdio_config,omitempty" gorm:"type:json"` // Required for stdio transport
|
||||
EnvVars MCPEnvVars `json:"env_vars,omitempty" gorm:"type:json"` // Environment variables for stdio
|
||||
IsBuiltin bool `json:"is_builtin" gorm:"default:false"` // Whether this is a builtin MCP service (visible to all tenants)
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
|
||||
@@ -69,6 +69,28 @@ type MCPTool struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
InputSchema json.RawMessage `json:"inputSchema"` // JSON Schema for tool parameters
|
||||
// RequireApproval when true: agent execution pauses until the user approves in UI (issue #1173).
|
||||
RequireApproval bool `json:"require_approval,omitempty"`
|
||||
}
|
||||
|
||||
// MCPToolApproval persists per-tool "danger / needs human approval" for an MCP service.
|
||||
// Tool list itself comes from MCP ListTools; this table only stores overrides.
|
||||
type MCPToolApproval struct {
|
||||
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
||||
TenantID uint64 `json:"tenant_id" gorm:"not null;uniqueIndex:idx_mcp_tool_approvals_tenant_svc_tool"`
|
||||
ServiceID string `json:"service_id" gorm:"type:varchar(36);not null;uniqueIndex:idx_mcp_tool_approvals_tenant_svc_tool;index"`
|
||||
ToolName string `json:"tool_name" gorm:"type:varchar(512);not null;uniqueIndex:idx_mcp_tool_approvals_tenant_svc_tool"`
|
||||
RequireApproval bool `json:"require_approval" gorm:"not null;default:false"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// BeforeCreate sets ID for MCPToolApproval.
|
||||
func (m *MCPToolApproval) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.ID == "" {
|
||||
m.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MCPResource represents a resource exposed by an MCP service
|
||||
|
||||
Reference in New Issue
Block a user