Skip to content

fix(web,mcp): gracefully degrade when MCP loading fails in Web UI worker#1816

Open
AI-fullcup wants to merge 1 commit intoMoonshotAI:mainfrom
AI-fullcup:fix/mcp-graceful-degradation-web-ui
Open

fix(web,mcp): gracefully degrade when MCP loading fails in Web UI worker#1816
AI-fullcup wants to merge 1 commit intoMoonshotAI:mainfrom
AI-fullcup:fix/mcp-graceful-degradation-web-ui

Conversation

@AI-fullcup
Copy link
Copy Markdown

@AI-fullcup AI-fullcup commented Apr 9, 2026

Problem

When an MCP server fails to connect (e.g., due to a port conflict when switching from TUI to Web UI), the Web UI session worker crashes entirely. This causes messages to get stuck in the "thinking" state indefinitely, and the frontend becomes unresponsive.

This is described in detail in #1766.

Root Cause

  1. In kimi_cli/soul/kimisoul.py, _agent_loop() calls wait_for_background_mcp_loading() inside a try/finally block. The finally only ensures MCPLoadingEnd is sent, but does not catch MCPRuntimeError. The exception bubbles up and crashes the agent loop.

  2. In kimi_cli/web/runner/process.py, _read_loop() catches unexpected exceptions but:

    • Does not clear _in_flight_prompt_ids
    • Does not emit "error" or "idle" status to WebSockets
    • The frontend has no way to know the worker died

Changes

  • src/kimi_cli/soul/kimisoul.py:

    • Import MCPRuntimeError
    • Catch MCPRuntimeError during wait_for_background_mcp_loading() and log a warning, allowing the conversation to continue without MCP tools.
  • src/kimi_cli/web/runner/process.py:

    • On unexpected exceptions in _read_loop(), clear _in_flight_prompt_ids and emit an "error" status so the frontend can recover instead of leaving prompts stuck in "thinking".

Testing

Fixes #1766


Open with Devin

When an MCP server fails to connect (e.g., due to a port conflict),
the Web UI session worker previously crashed entirely, causing
messages to get stuck in thinking state indefinitely.

Changes:
- In `_agent_loop()`, catch `MCPRuntimeError` during
  `wait_for_background_mcp_loading()` and log a warning instead of
  letting the exception crash the agent loop.
- In `_read_loop()`, when an unexpected exception occurs, clear
  `_in_flight_prompt_ids` and emit an error status so the
  frontend can recover instead of leaving prompts stuck in
  thinking state.

Fixes MoonshotAI#1766
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP connection failure crashes Web UI worker instead of graceful degradation

1 participant