fix: use detached context for ask_user_question MCP tool (#178)

This commit is contained in:
João Salavisa
2026-02-04 11:51:37 +00:00
committed by GitHub
parent be4ec541ee
commit d7fc77a1d9
@@ -4,11 +4,17 @@ import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// askUserQuestionTimeout is the maximum time to wait for a user response.
// This needs to be long enough for users to read and respond to questions.
// Note: This must match or exceed the clarification store timeout (10 minutes).
const askUserQuestionTimeout = 12 * time.Minute
// MCP action constants for backend WS requests
const (
ActionMCPListWorkspaces = "mcp.list_workspaces"
@@ -145,7 +151,7 @@ func (s *Server) updateTaskHandler() server.ToolHandlerFunc {
}
func (s *Server) askUserQuestionHandler() server.ToolHandlerFunc {
return func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
return func(_ context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
prompt, err := req.RequireString("prompt")
if err != nil {
return mcp.NewToolResultError("prompt is required"), nil
@@ -205,8 +211,16 @@ func (s *Server) askUserQuestionHandler() server.ToolHandlerFunc {
"question": question,
"context": questionCtx,
}
// Use a detached context with our own timeout for waiting on user input.
// The MCP request context may have a short timeout from the agent's MCP client,
// but we need to wait longer for users to read and respond to questions.
// This is similar to how script_message_handler.go handles long-running scripts.
askCtx, cancel := context.WithTimeout(context.Background(), askUserQuestionTimeout)
defer cancel()
var result map[string]interface{}
if err := s.backend.RequestPayload(ctx, ActionMCPAskUserQuestion, payload, &result); err != nil {
if err := s.backend.RequestPayload(askCtx, ActionMCPAskUserQuestion, payload, &result); err != nil {
return mcp.NewToolResultError(err.Error()), nil
}