feat(agent): add configurable MCP OAuth wait timeout per agent
Allow agents to set how long in-conversation OAuth prompts wait before auto-skipping, with UI in the agent editor and bounded gate behavior.
This commit is contained in:
@@ -117,6 +117,10 @@ type OAuthPendingRequest struct {
|
||||
ServiceName string
|
||||
MCPToolName string
|
||||
ToolCallID string
|
||||
// WaitTimeout overrides the gate's default wait timeout when > 0. The wait
|
||||
// is always bounded (either by this value, the gate default, or ctx
|
||||
// cancellation) so the blocked goroutine never leaks.
|
||||
WaitTimeout time.Duration
|
||||
}
|
||||
|
||||
var _ MCPApproval = (*Gate)(nil)
|
||||
@@ -446,7 +450,12 @@ func (g *Gate) RequestOAuthAndWait(ctx context.Context, req OAuthPendingRequest)
|
||||
g.mu.Unlock()
|
||||
}()
|
||||
|
||||
timeoutSec := int(g.timeout / time.Second)
|
||||
waitTimeout := g.timeout
|
||||
if req.WaitTimeout > 0 {
|
||||
waitTimeout = req.WaitTimeout
|
||||
}
|
||||
|
||||
timeoutSec := int(waitTimeout / time.Second)
|
||||
if timeoutSec < 1 {
|
||||
timeoutSec = 1
|
||||
}
|
||||
@@ -497,7 +506,7 @@ func (g *Gate) RequestOAuthAndWait(ctx context.Context, req OAuthPendingRequest)
|
||||
})
|
||||
}
|
||||
|
||||
timer := time.NewTimer(g.timeout)
|
||||
timer := time.NewTimer(waitTimeout)
|
||||
defer timer.Stop()
|
||||
|
||||
var d Decision
|
||||
|
||||
@@ -15,6 +15,18 @@ import (
|
||||
|
||||
const defaultMCPToolExecTimeout = 60 * time.Second
|
||||
|
||||
// oauthWaitTimeout derives the in-conversation OAuth wait timeout from the
|
||||
// agent's user-configured value (carried on the session, in seconds). The wait
|
||||
// is ALWAYS bounded to avoid leaking the blocked goroutine when the user
|
||||
// neither authorizes nor skips: a value <= 0 returns 0, which tells the gate to
|
||||
// fall back to its configured default timeout.
|
||||
func oauthWaitTimeout(sess *MCPOAuthSession) time.Duration {
|
||||
if sess == nil || sess.AuthWaitTimeoutSeconds <= 0 {
|
||||
return 0
|
||||
}
|
||||
return time.Duration(sess.AuthWaitTimeoutSeconds) * time.Second
|
||||
}
|
||||
|
||||
// MCPOAuthSession carries chat/session metadata so MCP connect and tool
|
||||
// registration can pause for in-conversation OAuth. Nil disables the prompt.
|
||||
type MCPOAuthSession struct {
|
||||
@@ -28,6 +40,9 @@ type MCPOAuthSession struct {
|
||||
ApprovalCtx context.Context
|
||||
// ExecTimeout, when >0, caps the retry ctx after a successful authorization.
|
||||
ExecTimeout time.Duration
|
||||
// AuthWaitTimeoutSeconds is the agent-level, user-configured number of
|
||||
// seconds to wait for in-conversation OAuth authorization. See oauthWaitTimeout.
|
||||
AuthWaitTimeoutSeconds int
|
||||
}
|
||||
|
||||
// oauthSessionFromToolExec builds an OAuth session from per-tool execution metadata.
|
||||
@@ -54,6 +69,16 @@ func oauthSessionFromToolExec(ctx context.Context, meta *ToolExecContext) *MCPOA
|
||||
}
|
||||
}
|
||||
|
||||
// withAuthWaitTimeout returns sess with the agent-level OAuth wait timeout
|
||||
// (seconds) applied. Safe on a nil session.
|
||||
func (s *MCPOAuthSession) withAuthWaitTimeout(seconds int) *MCPOAuthSession {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
s.AuthWaitTimeoutSeconds = seconds
|
||||
return s
|
||||
}
|
||||
|
||||
// oauthSessionForRegistration builds an OAuth session for tool discovery at agent startup.
|
||||
func oauthSessionForRegistration(ctx context.Context, sess *MCPOAuthSession, retryTimeout time.Duration) *MCPOAuthSession {
|
||||
if sess == nil || sess.EventBus == nil {
|
||||
@@ -73,13 +98,14 @@ func oauthSessionForRegistration(ctx context.Context, sess *MCPOAuthSession, ret
|
||||
requestID, _ = types.RequestIDFromContext(ctx)
|
||||
}
|
||||
return &MCPOAuthSession{
|
||||
EventBus: sess.EventBus,
|
||||
SessionID: sess.SessionID,
|
||||
AssistantMessageID: sess.AssistantMessageID,
|
||||
UserID: userID,
|
||||
RequestID: requestID,
|
||||
ApprovalCtx: approvalCtx,
|
||||
ExecTimeout: retryTimeout,
|
||||
EventBus: sess.EventBus,
|
||||
SessionID: sess.SessionID,
|
||||
AssistantMessageID: sess.AssistantMessageID,
|
||||
UserID: userID,
|
||||
RequestID: requestID,
|
||||
ApprovalCtx: approvalCtx,
|
||||
ExecTimeout: retryTimeout,
|
||||
AuthWaitTimeoutSeconds: sess.AuthWaitTimeoutSeconds,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,6 +187,7 @@ func waitForMCPOAuthAuthorization(
|
||||
ServiceName: service.Name,
|
||||
MCPToolName: mcpToolName,
|
||||
ToolCallID: toolCallID,
|
||||
WaitTimeout: oauthWaitTimeout(sess),
|
||||
})
|
||||
if waitErr != nil || !decision.Approved {
|
||||
return ctx, noop, false
|
||||
|
||||
@@ -22,15 +22,24 @@ type MCPTool struct {
|
||||
mcpTool *types.MCPTool
|
||||
mcpManager *mcp.MCPManager
|
||||
gate approval.MCPApproval // optional human approval before CallTool (issue #1173)
|
||||
// authWaitTimeoutSeconds carries the agent-level, user-configured OAuth wait
|
||||
// timeout (seconds) applied when a tool call triggers in-conversation auth.
|
||||
// <=0 uses the gate's configured default.
|
||||
authWaitTimeoutSeconds int
|
||||
}
|
||||
|
||||
// NewMCPTool creates a new MCP tool wrapper
|
||||
func NewMCPTool(service *types.MCPService, mcpTool *types.MCPTool, mcpManager *mcp.MCPManager, gate approval.MCPApproval) *MCPTool {
|
||||
// NewMCPTool creates a new MCP tool wrapper. authWaitTimeoutSeconds carries the
|
||||
// agent-level OAuth wait timeout applied when a tool call triggers in-conversation auth.
|
||||
func NewMCPTool(
|
||||
service *types.MCPService, mcpTool *types.MCPTool,
|
||||
mcpManager *mcp.MCPManager, gate approval.MCPApproval, authWaitTimeoutSeconds int,
|
||||
) *MCPTool {
|
||||
return &MCPTool{
|
||||
service: service,
|
||||
mcpTool: mcpTool,
|
||||
mcpManager: mcpManager,
|
||||
gate: gate,
|
||||
service: service,
|
||||
mcpTool: mcpTool,
|
||||
mcpManager: mcpManager,
|
||||
gate: gate,
|
||||
authWaitTimeoutSeconds: authWaitTimeoutSeconds,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +184,7 @@ func (t *MCPTool) Execute(ctx context.Context, args json.RawMessage) (*types.Too
|
||||
|
||||
isStdio := t.service.TransportType == types.MCPTransportStdio
|
||||
meta, _ := ToolExecFromContext(ctx)
|
||||
oauthSess := oauthSessionFromToolExec(ctx, meta)
|
||||
oauthSess := oauthSessionFromToolExec(ctx, meta).withAuthWaitTimeout(t.authWaitTimeoutSeconds)
|
||||
toolCallID := ""
|
||||
if meta != nil {
|
||||
toolCallID = meta.ToolCallID
|
||||
@@ -424,6 +433,10 @@ func RegisterMCPTools(
|
||||
}
|
||||
|
||||
registered := 0
|
||||
authWaitTimeoutSeconds := 0
|
||||
if oauthSess != nil {
|
||||
authWaitTimeoutSeconds = oauthSess.AuthWaitTimeoutSeconds
|
||||
}
|
||||
regOAuth := oauthSessionForRegistration(ctx, oauthSess, listToolsTimeout)
|
||||
for _, service := range services {
|
||||
if !service.Enabled {
|
||||
@@ -479,7 +492,7 @@ func RegisterMCPTools(
|
||||
|
||||
// Register each tool
|
||||
for _, mcpTool := range mcpTools {
|
||||
tool := NewMCPTool(service, mcpTool, mcpManager, gate)
|
||||
tool := NewMCPTool(service, mcpTool, mcpManager, gate, authWaitTimeoutSeconds)
|
||||
toolName := tool.Name()
|
||||
|
||||
// Check for name collision before registering (first-wins policy).
|
||||
|
||||
Reference in New Issue
Block a user