feat(mcp): prompt and resume OAuth authorization mid-conversation

When an agent invokes an OAuth-enabled MCP service that the current user has
not authorized yet, the tool call now pauses and surfaces an "Authorize" card
in the chat instead of failing with an opaque error. After the user completes
the per-user OAuth popup, the backend verifies a token exists and resumes the
paused tool call automatically.

Backend:
- Add mcp_oauth_required / mcp_oauth_resolved events and response types.
- Add OAuthPendingRequest + Gate.RequestOAuthAndWait, reusing the existing
  approval waiter, Resolve, Redis cross-instance fan-out and timeout.
- MCPTool.Execute pauses on an authorization-required error for OAuth services
  and retries once (with a fresh ctx and recycled connection) after the user
  authorizes.
- Forward the new events to the SSE stream.
- Add ResolveMCPOAuth handler + route; it verifies authorization before
  unblocking so a premature resolve cannot resume into another failure.

Frontend:
- Add McpOAuthCard with an Authorize button reusing the popup + status-poll
  flow, then resolve the paused run.
- Handle the new stream events (dedup multiple pending cards per service).
- Add resolveMCPOAuth API client and i18n keys (zh/en/ko/ru).
This commit is contained in:
wizardchen
2026-06-29 22:37:02 +08:00
committed by lyingbug
parent f2686e9e39
commit 9fd7a7f522
16 changed files with 862 additions and 34 deletions
+7
View File
@@ -60,6 +60,13 @@ const (
EventToolApprovalRequired EventType = "tool_approval_required"
EventToolApprovalResolved EventType = "tool_approval_resolved"
// MCP OAuth in-conversation authorization prompt: emitted when an
// OAuth-enabled MCP service is invoked but the current user has not
// authorized it yet. The agent pauses until the user authorizes (or the
// wait times out / is canceled).
EventMCPOAuthRequired EventType = "mcp_oauth_required"
EventMCPOAuthResolved EventType = "mcp_oauth_resolved"
// Error events
EventError EventType = "error" // 错误事件
+28
View File
@@ -237,3 +237,31 @@ type ToolApprovalResolvedData struct {
TimedOut bool `json:"timed_out,omitempty"`
Canceled bool `json:"canceled,omitempty"`
}
// MCPOAuthRequiredData is emitted when an OAuth-enabled MCP service is invoked
// during a conversation but the current user has not authorized it yet. The
// UI surfaces an "Authorize" card; the agent pauses until the user authorizes.
type MCPOAuthRequiredData 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"`
TimeoutSeconds int `json:"timeout_seconds"`
RequestedAtUnix int64 `json:"requested_at"`
ToolCallID string `json:"tool_call_id"`
RequestID string `json:"request_id,omitempty"`
}
// MCPOAuthResolvedData confirms the outcome of an in-conversation OAuth prompt
// (authorized / timeout / cancel).
type MCPOAuthResolvedData struct {
PendingID string `json:"pending_id"`
ServiceID string `json:"service_id"`
Authorized bool `json:"authorized"`
Reason string `json:"reason,omitempty"`
TimedOut bool `json:"timed_out,omitempty"`
Canceled bool `json:"canceled,omitempty"`
}