feat(fleet-mcp): Add workspace update & restart functionality#841
Open
feat(fleet-mcp): Add workspace update & restart functionality#841
Conversation
Implements programmatic workspace update and restart operations for Coder workspaces, addressing the lack of a single "update & restart" endpoint in Coder's REST API (see coder/coder#19331). ## Core Changes ### Client Layer - Add `update_workspace()` method to CoderClient for two-step update workflow - Stop workspace and wait for completion - Start with new template version - Configurable timeouts for both operations - Add `get_template_version()` method for version details lookup - Enhance `_wait_for_build_completion()` to handle both stop and start states - Recognizes "running" as completion state for start operations - Recognizes "stopped" as completion state for stop operations ### Repository Layer - Add `update()` method to AgentRepository - Supports explicit template version ID - Auto-detects active template version when no ID provided - Validates template availability ### Service Layer - Add `update_agent()` method to AgentService - Business logic and validation - Case-insensitive agent name handling - Template version ID validation ### MCP Tool - Add `update_agent` MCP tool for fleet management - Exposed via FastMCP server - Supports both explicit and automatic version selection - Returns updated agent state with success message ### Standalone CLI - Add `scripts/update_workspace.py` for direct usage - Supports workspace ID or name lookup - Configurable stop/start timeouts - Environment variable configuration - Comprehensive logging and error handling - Executable with --help documentation ## Testing - Add comprehensive test suite for CoderClient.update_workspace() - Add test suite for AgentRepository.update() - All tests passing with proper mock scenarios - Edge cases covered (not found, timeouts, version detection) ## Usage Examples Via MCP tool: ```python await update_agent(agent_service, agent_name="my-agent") ``` Via CLI: ```bash ./scripts/update_workspace.py --workspace-name my-agent ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This is a minor version bump as it adds new functionality without breaking existing APIs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR implements programmatic workspace update and restart operations for Coder workspaces, providing a robust solution for the two-step workflow required by Coder's REST API.
Context
Coder does not provide a single "update & restart" endpoint. Instead, updates require:
This implementation provides a clean abstraction over this workflow with proper error handling, polling, and timeout management.
Related Issue: coder/coder#19331
Implementation
Core Components
1. CoderClient Layer (
coder_client.py)update_workspace()- Orchestrates the two-step update workflowget_template_version()- Retrieves template version details by ID_wait_for_build_completion()- Smart polling for build operations2. AgentRepository Layer (
agent_repository.py)update()- Updates agent workspace to new template version3. AgentService Layer (
agent_service.py)update_agent()- Business logic with validation4. MCP Tool (
update_agent.py)update_agenttool via FastMCP__main__.pywith proper routingUpdateAgentResponsewith agent state and success message5. Standalone CLI (
scripts/update_workspace.py)New Models
UpdateAgentResponse- Response model for update operationsTesting
Test Coverage
test_coder_client_update.py(5 tests)test_agent_repository_update.py(4 tests)Test Results:
Usage Examples
Via MCP Tool
Via Standalone CLI
Code Quality
trunk fmttrunk checkFiles Changed
Modified
src/fleet_mcp/__main__.py- Register update_agent MCP toolsrc/fleet_mcp/clients/coder_client.py- Add update methodssrc/fleet_mcp/models/__init__.py- Export UpdateAgentResponsesrc/fleet_mcp/models/responses.py- Add UpdateAgentResponsesrc/fleet_mcp/repositories/agent_repository.py- Add update methodsrc/fleet_mcp/services/agent_service.py- Add update_agent methodCreated
src/fleet_mcp/tools/update_agent.py- MCP tool implementationscripts/update_workspace.py- Standalone CLI script (executable)tests/clients/test_coder_client_update.py- Client layer teststests/repositories/test_agent_repository_update.py- Repository layer testsImpact
Benefits
Breaking Changes
Checklist
Test Plan
nx test fleet-mcp🤖 Generated with Claude Code