Fix tool call timeout by implementing chunked response with keepalive

- Added special handling for HumanAgent_Chat tool calls to prevent Node.js undici 5-minute timeout
- Send HTTP headers immediately with Transfer-Encoding: chunked to stop headersTimeout
- Implement 4-minute keepalive mechanism writing space character to reset bodyTimeout
- Allows indefinite wait times for human responses in tool calls
- Tested successfully with 35+ minute wait time without timeout

Resolves issue where VS Code MCP client would cancel tool calls after ~5-6 minutes
with error 'The user cancelled the tool call' due to undici hardcoded timeouts.
This commit is contained in:
B Harper
2025-11-19 15:32:59 +11:00
parent 164b6b9f1e
commit accdbdc602
4 changed files with 310 additions and 1 deletions
+182
View File
@@ -0,0 +1,182 @@
# Taking Back Control: Why Human-in-the-Loop AI Beats Fully Autonomous Agents
*How HumanAgent MCP transforms AI coding from guesswork to conversation*
# The Frustration We All Know
Picture this: You ask your AI coding assistant a simple question expecting some discussion and instead of answering or planning with you it confidently rewrites half your codebase, breaks your tests, and introduces subtle bugs that take hours to debug. Generally starting with the dreaded "Sure Let me..."
Sound familiar?
The promise of AI coding assistants was supposed to be increased productivity and reduced cognitive load. Instead, many developers find themselves in a constant battle:
AI agents making decisions and changes without asking based on outdated training data or just a whim.
Trying so hard to please that a simple request for discussion results in 15 minutes of "Let Me, no wait, actually if i try this, no that didnt work" and never undoing any of the half baked ideas that are now littered around in code.
We're stuck between two extremes: passive suggestion tools that can't engage, or overly aggressive agents that won't stop to ask. There's no middle ground—until now.
Keep reading till the end - there is a free ready made solution for you detailed in this article.
# Why AI Assistants Keep Guessing Wrong
The fundamental problem isn't the AI's intelligence—it's the communication model.
Worse still is VSCode's Copilot Chat's built-in default prompt which is so strongly and poorly worded that it forces the agent to spare no effort and not accept defeat or bother you with any discussion before coming back with a complete solution that you did not ask for.
Following are some real and very problematic statements taken directly from GitHub Copilot Chat's main system prompt:
* "implement changes rather than only suggesting them."
* "If the users intent is unclear, infer the most useful likely action and proceed"
* “Continue working until the users request is completely resolved before ending your turn and yielding back to the user. Only terminate your turn when you are certain the task is complete.”
When AI can't ask questions, it fills the gaps with assumptions:
- Should I use TypeScript or JavaScript? *Guess.*
- Should I optimize for speed or readability? *Guess.*
- Should I update the tests too? *Guess.*
These guesses waste expensive API calls, generate incorrect code, and burn your time debugging. More importantly, they break the collaborative flow that makes human pair programming so effective.
# Enter Human-in-the-Loop: The Best of Both Worlds
What if your AI assistant could:
- Ask clarifying questions before making changes?
- Discuss approach options and get your input?
- Confirm destructive actions before executing them?
- Have real conversations instead of one-way commands?
This is the human-in-the-loop approach—and it's where AI tooling is heading.
# The Model Context Protocol (MCP) Changes Everything
Anthropic's [Model Context Protocol](https://modelcontextprotocol.io/) provides the infrastructure for this new paradigm. MCP allows AI models to interact with external tools and services in a standardized way—including asking humans for input.
Instead of AI making isolated decisions, MCP enables:
- Bidirectional communication between AI and humans
- Real-time interaction without leaving your development environment
- Rich context sharing that keeps everyone on the same page
# Introducing HumanAgent MCP
HumanAgent MCP is a VS Code extension that brings true human-in-the-loop AI to your development workflow. Built on the Model Context Protocol, it creates a persistent chat interface where your AI assistant can ask questions, discuss options, and get real-time guidance.
# How It Works
Install the HumanAgent MCP server - no configuration required. [Install HumanAgent MCP](vscode:extension/3DTek-xyz.humanagent-mcp)
Start your agent discussion in the usual manner with something resembling "Lets chat via Human Agent Tool".
What happens next:
1. A chat panel opens in your VS Code sidebar
2. The AI's question appears with context
3. You respond directly in the chat (or use quick reply buttons)
4. The AI receives your answer and continues with clarity
None of the back and forth interaction with human agent chat are treated as premium requests $$$!
Yes thats correct - you can run an entire days planning & coding session on 1 premium request.
The agent is encouraged to keep the discussion up until a plan is reached and its time to code. Its also encouraged to come back with any extra questions it needs answered.
An additional prompt can be setup to be included in your every response such as "Remember to read the copilot-instructions file regularly and dont iterate and change your direction - if its not working come back and we can make a new plan"
# Key Features
* Native VS Code Integration + Web Interface
- Dockable chat panel that stays visible while coding
- Multiple session support for parallel AI in multiple vscode workspaces
- Web interface option for monitoring and interacting with all running VSCode workspaces. Manage many VSCode agents working on multiple projects from one place
* Quick Replies
- Pre-defined responses for common questions
- Customizable via workspace settings
- "Yes Please Proceed" / "Explain in more detail please" by default
* Smart Notifications
- Tab indicators show which sessions need attention
- Visual cues for pending AI questions
- Sound notifications (optional)
* Flexible Configuration
- Per-workspace tool customization
- Override default behavior for specific projects
- Session naming for easy tracking
# The Benefits: More Than Just Better Code
Reduced Costs
Stop wasting expensive API calls on AI generating code based on wrong assumptions. One quick question saves thousands of tokens of regeneration.
Fewer Errors
Clarification before action means less incorrect code, fewer bugs, less debugging time and less orphaned AI code littered around.
Faster Development Cycles
A 10-second clarification beats an hour of debugging wrong guesses. Keep your flow state intact.
Better Collaboration
Turns one-way instructions into dialogue. You stay in control while the AI handles the grunt work.
# Getting Started
# Installation
1. Install from VS Code Marketplace: [Install HumanAgent MCP](vscode:extension/3DTek-xyz.humanagent-mcp)
2. The MCP server starts automatically on extension activation
3. That's it - nothing further. No MCP server setup, MCP.json files. Just install and start working.
# Using with GitHub Copilot
GitHub Copilot in VS Code automatically discovers MCP servers. Once HumanAgent MCP is installed, Copilot can invoke the chat tool when it needs input.
Try asking Copilot: "Refactor my authentication system, but check with me before making any database schema changes"
# The Future is Collaborative, Not Autonomous
The vision of fully autonomous AI coding—where you describe what you want and AI builds it without supervision—is seductive but flawed. Software development isn't just about writing code; it's about:
- Making informed tradeoffs (speed vs quality, simple vs scalable)
- Understanding business context (deadlines, user needs, technical debt)
- Maintaining coherent architecture across teams and time
- Learning and adapting from mistakes and feedback
These require human judgment, but they don't require humans to write every line of code.
Human-in-the-loop AI hits the sweet spot:
- AI handles the repetitive, time-consuming coding work
- Humans make the strategic decisions and provide domain knowledge
- Conversation ensures alignment and catches errors early
- Both parties learn and improve from the interaction
This isn't a compromise—it's the optimal collaboration model.
# Join the Conversation
HumanAgent MCP is open source and actively developed. We're building features based on real developer feedback:
- GitHub: [3DTek-xyz/HumanAgent-MCP](https://github.com/3DTek-xyz/HumanAgent-MCP)
- VS Code Marketplace: [HumanAgent MCP](https://marketplace.visualstudio.com/items?itemName=3DTek-xyz.humanagent-mcp)
- Documentation: See the README for advanced configuration
# What's Next
We're working on:
- Image support for pasting screenshots and diagrams
- Multi-agent agentic orchestration for complex workflows. Multiple agents with narrow scope working together.
- Team collaboration features for shared AI sessions
# Try It Today
Stop fighting with autonomous AI that guesses wrong. Stop context-switching to ChatGPT. Start having real conversations with your AI coding assistant.
Install HumanAgent MCP and experience what coding with AI should feel like—a true collaboration between human insight and machine productivity.
---
*HumanAgent MCP is free and open source. Built by developers, for developers who want AI to augment their skills—not replace their judgment.*
Tags: #AI #Development #VSCode #ModelContextProtocol #Copilot #HumanInTheLoop
+5 -1
View File
@@ -13,4 +13,8 @@ For more information see https://github.com/3DTek-xyz/HumanAgent-MCP/blob/master
![HumanAgent MCP Extension Demo](high-res-demo.gif)
*Complete demonstration of the HumanAgent MCP extension in action - showing real-time human-AI collaboration*
*Complete demonstration of the HumanAgent MCP extension in action - showing real-time human-AI collaboration*
## Medium Article
https://medium.com/@harperbenwilliam/stop-the-ai-chaos-why-human-in-the-loop-beats-fully-autonomous-coding-agents-eeb0ae17fde9
+87
View File
@@ -691,3 +691,90 @@
2025-11-18T02:22:23.278Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763432483320
2025-11-18T02:26:48.410Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763432790349
2025-11-18T02:27:14.305Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763432819507
2025-11-18T02:39:58.506Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763433568966
2025-11-18T02:53:30.480Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763434329608
2025-11-18T02:56:15.460Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763434424942
2025-11-18T03:38:45.925Z - RESPONSE ENDPOINT CALLED - RequestID: 5-1763434725081
2025-11-18T03:40:19.015Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763437187183
2025-11-18T03:41:59.378Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763437242001
2025-11-18T03:43:01.827Z - RESPONSE ENDPOINT CALLED - RequestID: 5-1763437369466
2025-11-18T06:14:00.728Z - RESPONSE ENDPOINT CALLED - RequestID: 6-1763437423019
2025-11-18T06:18:02.912Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763446658929
2025-11-18T06:23:47.631Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763447021849
2025-11-18T06:25:57.558Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763447105247
2025-11-18T06:27:25.839Z - RESPONSE ENDPOINT CALLED - RequestID: 5-1763447231081
2025-11-18T06:32:46.263Z - RESPONSE ENDPOINT CALLED - RequestID: 6-1763447483088
2025-11-18T06:34:23.905Z - RESPONSE ENDPOINT CALLED - RequestID: 7-1763447622207
2025-11-18T06:35:14.021Z - RESPONSE ENDPOINT CALLED - RequestID: 8-1763447690581
2025-11-18T06:36:31.582Z - RESPONSE ENDPOINT CALLED - RequestID: 9-1763447750484
2025-11-18T06:37:44.404Z - RESPONSE ENDPOINT CALLED - RequestID: 10-1763447818173
2025-11-18T06:38:38.546Z - RESPONSE ENDPOINT CALLED - RequestID: 11-1763447875648
2025-11-18T06:39:48.141Z - RESPONSE ENDPOINT CALLED - RequestID: 12-1763447932022
2025-11-18T06:43:40.511Z - RESPONSE ENDPOINT CALLED - RequestID: 13-1763448001173
2025-11-18T06:45:17.457Z - RESPONSE ENDPOINT CALLED - RequestID: 14-1763448230522
2025-11-18T06:46:27.529Z - RESPONSE ENDPOINT CALLED - RequestID: 15-1763448329021
2025-11-18T06:48:28.407Z - RESPONSE ENDPOINT CALLED - RequestID: 16-1763448405443
2025-11-18T06:50:29.838Z - RESPONSE ENDPOINT CALLED - RequestID: 17-1763448526213
2025-11-18T06:52:12.050Z - RESPONSE ENDPOINT CALLED - RequestID: 18-1763448650631
2025-11-19T01:24:09.277Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763515019650
2025-11-19T01:25:33.021Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763515517453
2025-11-19T01:26:25.579Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763515548791
2025-11-19T01:27:28.483Z - RESPONSE ENDPOINT CALLED - RequestID: 5-1763515595531
2025-11-19T01:30:18.613Z - RESPONSE ENDPOINT CALLED - RequestID: 6-1763515656709
2025-11-19T01:32:02.270Z - RESPONSE ENDPOINT CALLED - RequestID: 7-1763515831756
2025-11-19T01:35:55.654Z - RESPONSE ENDPOINT CALLED - RequestID: 8-1763515932210
2025-11-19T01:36:23.176Z - RESPONSE ENDPOINT CALLED - RequestID: 9-1763516165498
2025-11-19T01:37:00.835Z - RESPONSE ENDPOINT CALLED - RequestID: 10-1763516192706
2025-11-19T01:39:10.905Z - RESPONSE ENDPOINT CALLED - RequestID: 11-1763516230904
2025-11-19T01:39:49.004Z - RESPONSE ENDPOINT CALLED - RequestID: 12-1763516367404
2025-11-19T01:40:17.739Z - RESPONSE ENDPOINT CALLED - RequestID: 13-1763516398238
2025-11-19T01:41:36.756Z - RESPONSE ENDPOINT CALLED - RequestID: 14-1763516481876
2025-11-19T01:42:38.294Z - RESPONSE ENDPOINT CALLED - RequestID: 15-1763516526387
2025-11-19T01:45:01.737Z - RESPONSE ENDPOINT CALLED - RequestID: 16-1763516585390
2025-11-19T01:45:35.148Z - RESPONSE ENDPOINT CALLED - RequestID: 17-1763516718714
2025-11-19T01:46:43.539Z - RESPONSE ENDPOINT CALLED - RequestID: 18-1763516759168
2025-11-19T01:47:16.740Z - RESPONSE ENDPOINT CALLED - RequestID: 19-1763516833761
2025-11-19T01:51:18.447Z - RESPONSE ENDPOINT CALLED - RequestID: 20-1763517065160
2025-11-19T01:53:24.021Z - RESPONSE ENDPOINT CALLED - RequestID: 21-1763517148133
2025-11-19T01:55:22.395Z - RESPONSE ENDPOINT CALLED - RequestID: 22-1763517220485
2025-11-19T01:55:56.343Z - RESPONSE ENDPOINT CALLED - RequestID: 23-1763517336062
2025-11-19T02:01:39.996Z - RESPONSE ENDPOINT CALLED - RequestID: 24-1763517693982
2025-11-19T02:02:56.332Z - RESPONSE ENDPOINT CALLED - RequestID: 25-1763517710116
2025-11-19T02:03:13.157Z - RESPONSE ENDPOINT CALLED - RequestID: 26-1763517783066
2025-11-19T02:05:42.415Z - RESPONSE ENDPOINT CALLED - RequestID: 27-1763517881460
2025-11-19T02:10:16.438Z - RESPONSE ENDPOINT CALLED - RequestID: 28-1763517957632
2025-11-19T02:11:54.688Z - RESPONSE ENDPOINT CALLED - RequestID: 29-1763518226131
2025-11-19T02:12:39.655Z - RESPONSE ENDPOINT CALLED - RequestID: 30-1763518329883
2025-11-19T02:15:30.961Z - RESPONSE ENDPOINT CALLED - RequestID: 31-1763518367194
2025-11-19T02:16:41.402Z - RESPONSE ENDPOINT CALLED - RequestID: 32-1763518543112
2025-11-19T02:17:43.201Z - RESPONSE ENDPOINT CALLED - RequestID: 33-1763518614727
2025-11-19T02:19:21.697Z - RESPONSE ENDPOINT CALLED - RequestID: 34-1763518676927
2025-11-19T02:20:18.802Z - RESPONSE ENDPOINT CALLED - RequestID: 35-1763518774294
2025-11-19T02:43:24.964Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763520157655
2025-11-19T02:45:01.206Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763520219515
2025-11-19T02:46:19.104Z - RESPONSE ENDPOINT CALLED - RequestID: 5-1763520313847
2025-11-19T02:57:37.343Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763521014023
2025-11-19T02:59:52.681Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763521113125
2025-11-19T03:02:14.607Z - RESPONSE ENDPOINT CALLED - RequestID: 5-1763521251826
2025-11-19T03:03:54.164Z - RESPONSE ENDPOINT CALLED - RequestID: 6-1763521353229
2025-11-19T03:06:19.281Z - RESPONSE ENDPOINT CALLED - RequestID: 7-1763521454858
2025-11-19T03:07:37.298Z - RESPONSE ENDPOINT CALLED - RequestID: 8-1763521598807
2025-11-19T03:11:39.988Z - RESPONSE ENDPOINT CALLED - RequestID: 9-1763521816638
2025-11-19T03:13:32.720Z - RESPONSE ENDPOINT CALLED - RequestID: 10-1763521924945
2025-11-19T03:15:58.163Z - RESPONSE ENDPOINT CALLED - RequestID: 11-1763522057738
2025-11-19T03:17:35.340Z - RESPONSE ENDPOINT CALLED - RequestID: 12-1763522181228
2025-11-19T03:18:55.097Z - RESPONSE ENDPOINT CALLED - RequestID: 13-1763522268824
2025-11-19T03:19:49.257Z - RESPONSE ENDPOINT CALLED - RequestID: 14-1763522377448
2025-11-19T03:21:18.464Z - RESPONSE ENDPOINT CALLED - RequestID: 15-1763522444120
2025-11-19T03:23:03.579Z - RESPONSE ENDPOINT CALLED - RequestID: 16-1763522504241
2025-11-19T03:23:50.363Z - RESPONSE ENDPOINT CALLED - RequestID: 17-1763522596596
2025-11-19T03:25:51.736Z - RESPONSE ENDPOINT CALLED - RequestID: 18-1763522683032
2025-11-19T03:36:07.974Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763523299064
2025-11-19T03:37:27.924Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763523384331
2025-11-19T03:38:54.188Z - RESPONSE ENDPOINT CALLED - RequestID: 5-1763523460761
2025-11-19T03:45:59.295Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763523836543
2025-11-19T03:46:34.788Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763523964130
2025-11-19T03:52:41.873Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763524321397
2025-11-19T04:29:21.690Z - RESPONSE ENDPOINT CALLED - RequestID: 4-1763524368377
2025-11-19T04:29:57.436Z - RESPONSE ENDPOINT CALLED - RequestID: 5-1763526575920
2025-11-19T04:32:45.235Z - RESPONSE ENDPOINT CALLED - RequestID: 3-1763526722087
+36
View File
@@ -636,6 +636,42 @@ export class McpServer extends EventEmitter {
this.debugLogger.log('HTTP', `Added sessionId ${sessionId} to MCP message params`);
}
// Special handling for HumanAgent_Chat tool to prevent undici timeout
// This tool can wait indefinitely for human response, so we need to:
// 1. Send HTTP headers immediately (stops undici's 5-minute headersTimeout)
// 2. Send keepalive data every 4 minutes (resets undici's 5-minute bodyTimeout)
if (message.method === 'tools/call' && message.params?.name === 'HumanAgent_Chat') {
this.debugLogger.log('HTTP', 'HumanAgent_Chat detected - using streaming response to prevent timeout');
// Send headers immediately to stop headersTimeout
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.setHeader('Transfer-Encoding', 'chunked');
this.debugLogger.log('HTTP', 'Sent immediate headers for HumanAgent_Chat');
// Start keepalive to reset bodyTimeout every 4 minutes
// Note: We write a space character which is valid JSON whitespace and gets ignored
const keepaliveInterval = setInterval(() => {
if (!res.destroyed) {
res.write(' '); // Write whitespace to reset bodyTimeout
this.debugLogger.log('HTTP', 'Sent keepalive for HumanAgent_Chat');
} else {
clearInterval(keepaliveInterval);
}
}, 4 * 60 * 1000); // 4 minutes (undici timeout is 5 minutes)
// Wait for human response (this can take any amount of time now)
const response = await this.handleMessage(message);
// Cleanup keepalive and send final response
clearInterval(keepaliveInterval);
this.debugLogger.log('HTTP', 'HumanAgent_Chat response received, sending to client');
const responseJson = JSON.stringify(response);
res.end(responseJson); // This sends the actual JSON response
return;
}
// Normal handling for all other tools/methods
const response = await this.handleMessage(message);
this.debugLogger.log('HTTP', 'Response from handleMessage:', response);