Skip to content

fix(agent-core-v2): make MCP initial connect non-blocking during startup - #2586

Merged
7Sageer merged 7 commits into
mainfrom
fix/mcp-nonblocking-startup
Aug 4, 2026
Merged

fix(agent-core-v2): make MCP initial connect non-blocking during startup#2586
7Sageer merged 7 commits into
mainfrom
fix/mcp-nonblocking-startup

Conversation

@7Sageer

@7Sageer 7Sageer commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below.

Problem

In the v2 engine (kap-server / agent-core-v2), session creation synchronously awaited the workspace MCP initial connect: materializeSession awaited ISessionMcpHandle.ready, which only resolves after every configured MCP server has connected or timed out (default 30s per server). The TUI awaits session creation before starting its UI event loop, so a slow or hung MCP server left users staring at a blank terminal on startup — create, resume, and fork were all affected. Agent creation paid the same wait a second time via await mcpReady in the agent lifecycle.

The v1 engine does not have this problem: its Session constructor kicks off MCP loading fire-and-forget and only the first turn waits for the initial load, so the UI comes up immediately and the MCP latency overlaps with the user's reading/typing time.

What changed

Adopt the v1 timing in v2 — startup no longer waits for MCP; the first LLM step does:

  • SessionLifecycleService.materializeSession no longer awaits this.mcp.ready (nor the ephemeral overlay's ready). Workspace MCP connection still starts immediately in the background, and status keeps flowing through the existing mcp.server.status / tool.list.updated events, so clients can observe readiness.
  • AgentLifecycleService no longer captures/awaits the session MCP handle's ready before creating an agent; the now-unused ISessionMcpHandle injection is dropped.
  • AgentMcpService registers an onWillBeginStep hook that awaits waitForInitialLoad(ctx.signal) before each LLM request, so the first step still sees the complete MCP tool list. The cost is one-time — the promise is already resolved for subsequent steps — and the hook is interruptible via the turn signal. The existing per-server waitUntil gate on MCP tool calls stays as a fallback.

Tests: flipped the three blocking assertions (session create/resume, agent create) to assert immediate return, re-gated the resume-hiding test on main-agent creation, added coverage that the first step waits for the initial MCP load while the second runs straight through, and made the shared-manager workspace-resources test await the seeded readiness explicitly. Full agent-core-v2 suite (289 files / 4512 tests) green, tsc --noEmit clean.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: de1cf8c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Signed-off-by: 7Sageer <sag77r@hotmail.com>
@pkg-pr-new

pkg-pr-new Bot commented Aug 4, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@de1cf8c
npx https://pkg.pr.new/@moonshot-ai/kimi-code@de1cf8c

commit: de1cf8c

@7Sageer
7Sageer merged commit 278b6af into main Aug 4, 2026
14 checks passed
@7Sageer
7Sageer deleted the fix/mcp-nonblocking-startup branch August 4, 2026 08:47
@github-actions github-actions Bot mentioned this pull request Aug 4, 2026
mbuckaway pushed a commit to mbuckaway/kimi-code that referenced this pull request Aug 4, 2026
…tup (MoonshotAI#2586)

* fix(agent-core-v2): make MCP initial connect non-blocking during startup

* Delete .changeset/mcp-nonblocking-startup.md

Signed-off-by: 7Sageer <sag77r@hotmail.com>

* fix(agent-core-v2): wait for MCP readiness before first turn

* fix(klient): wait for MCP startup before listing

* fix(klient): keep MCP server listing non-blocking

* test(acp-server): allow pending MCP snapshot

---------

Signed-off-by: 7Sageer <sag77r@hotmail.com>
daofazhiran pushed a commit to daofazhiran/kimi-code that referenced this pull request Aug 4, 2026
…tup (MoonshotAI#2586)

* fix(agent-core-v2): make MCP initial connect non-blocking during startup

* Delete .changeset/mcp-nonblocking-startup.md

Signed-off-by: 7Sageer <sag77r@hotmail.com>

* fix(agent-core-v2): wait for MCP readiness before first turn

* fix(klient): wait for MCP startup before listing

* fix(klient): keep MCP server listing non-blocking

* test(acp-server): allow pending MCP snapshot

---------

Signed-off-by: 7Sageer <sag77r@hotmail.com>
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.

1 participant