Skip to content

Improve MCP server lifecycle: caching and auto-restart#1872

Merged
dgageot merged 1 commit intodocker:mainfrom
dgageot:mcp
Feb 27, 2026
Merged

Improve MCP server lifecycle: caching and auto-restart#1872
dgageot merged 1 commit intodocker:mainfrom
dgageot:mcp

Conversation

@dgageot
Copy link
Member

@dgageot dgageot commented Feb 27, 2026

  • Cache tool and prompt lists in MCP Toolset, invalidated via ToolListChanged/PromptListChanged server notifications instead of re-fetching on every agent iteration
  • Auto-restart MCP servers that die unexpectedly with exponential backoff (up to 5 attempts), distinguishing crashes from intentional Stop() calls via a stopping flag

Assisted-By: cagent

@dgageot dgageot requested a review from a team as a code owner February 27, 2026 14:23
Copy link

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

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

Review Summary

This PR implements MCP server lifecycle improvements with caching and auto-restart functionality. The implementation is generally sound, but I found one concurrency issue with the notification handler callback:

Issue: The emitToolsChanged callback (invoked when MCP servers notify of tool list changes) blocks on r.CurrentAgentTools(context.Background()), which can take significant time if the MCP server is slow or unresponsive. Multiple rapid notifications could spawn concurrent blocking calls without rate limiting, potentially exhausting resources. While this affects sidebar updates rather than core functionality, it should be addressed to prevent resource exhaustion.

Positive findings: The caching mechanism with generation counters is well-designed, the restart logic with exponential backoff is correct, and the lock ordering avoids deadlocks.

- Cache tool and prompt lists in MCP Toolset, invalidated via
  ToolListChanged/PromptListChanged server notifications instead of
  re-fetching on every agent iteration
- Auto-restart MCP servers that die unexpectedly with exponential
  backoff (up to 5 attempts), distinguishing crashes from intentional
  Stop() calls via a stopping flag

Assisted-By: cagent

Signed-off-by: David Gageot <david.gageot@docker.com>
@dgageot dgageot merged commit da2fb08 into docker:main Feb 27, 2026
5 checks passed
@dgageot dgageot deleted the mcp branch February 27, 2026 19:37
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.

2 participants