Build long-running MCP servers on Amazon Bedrock AgentCore with Strands Agents integration
Summary
AI agents are evolving into autonomous workers capable of handling complex, time-intensive tasks like ML model training and large dataset processing. The Model Context Protocol (MCP) is a standard for agent-server integrations, but long-running operations (minutes to hours) pose a challenge due to typical session timeouts. This article presents two architectural patterns to enable seamless, cross-session task execution: context messaging and asynchronous task management. Context messaging maintains continuous communication for tasks up to 15 minutes, while asynchronous task management decouples initiation from execution, allowing tasks to run for hours with client disconnection. The article demonstrates how to implement these using Amazon Bedrock AgentCore and Strands Agents, specifically leveraging AgentCore Memory for persistent state management to prevent data loss and ensure result retrieval even after server restarts or instance terminations.
Key takeaway
For AI Engineers building agents that perform multi-hour data processing or model training, you should adopt an asynchronous task management pattern with persistent external memory like Amazon Bedrock AgentCore Memory. This ensures tasks complete reliably, results are preserved across sessions, and users can disconnect and reconnect without losing progress or data, transforming agents into robust autonomous workers.
Key insights
Persistent state management enables AI agents to execute long-running tasks reliably across disconnected sessions.
Principles
- Decouple task execution from client connection for long-running operations.
- Use persistent storage for task state and results to ensure resilience.
Method
Implement context messaging for short-to-moderate tasks (1-15 min) or asynchronous task management with external memory (e.g., Amazon Bedrock AgentCore Memory) for tasks spanning hours or requiring client disconnection.
In practice
- Use `ctx.report_progress()` for context messaging heartbeats.
- Implement `asyncio.create_task()` for background processing.
- Store task results in AgentCore Memory using `create_event()`.
Topics
- AI Agents
- Long-Running Tasks
- Model Context Protocol
- Amazon Bedrock AgentCore
- Persistent State Management
Code references
- aws/bedrock-agentcore-sdk-python
- aws-samples/sample-mcp-for-long-runing-tasks-with-amazon-bedrock-agentcore
Best for: AI Engineer, Machine Learning Engineer, MLOps Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by Artificial Intelligence.