- Newest
- Most votes
- Most comments
When working with MCP servers in Amazon Bedrock AgentCore Runtime, session state management is handled through specific mechanisms designed for stateless environments.
The key to maintaining session state across HTTP requests is using the built-in session management capabilities of AgentCore Runtime. Here's how it works:
-
Session ID Management: AgentCore Runtime automatically adds an
Mcp-Session-Idheader to any request that doesn't include one. This header allows MCP clients to maintain connection continuity to the same AgentCore Runtime session. -
Runtime Session ID: When invoking an agent through the
InvokeAgentRuntimeoperation, you should use theruntimeSessionIdparameter. By providing the same session identifier across multiple requests, you can maintain conversation context, allowing the agent to reference previous interactions. -
External State Storage: Since AgentCore Runtime provides session isolation by default and requires stateless streamable-HTTP servers, you should externalize your agent state. This means persisting state after each interaction and restoring it before the next one.
For your specific implementation:
- Make sure your MCP server is truly stateless and relies on the session identifiers provided by AgentCore Runtime rather than in-memory state.
- Use the
runtimeSessionIdparameter consistently across your requests to maintain session context. - Consider implementing external state storage using AgentCore Memory, which is specifically designed to maintain session context using its short-term memory capabilities.
To implement AgentCore Memory:
from bedrock_agentcore.memory import MemoryClient memory_client = MemoryClient(region_name="us-east-1") memory = memory_client.create_memory_and_wait( name="YourMemoryStore", description="Your session descriptions", strategies=[] )
This approach will allow your MCP server to maintain session state across multiple HTTP requests, even when different container instances handle those requests.
Sources
Invoke an AgentCore Runtime agent - Amazon Bedrock AgentCore
Introducing Amazon Bedrock AgentCore: Securely deploy and operate AI agents at any scale (preview) | AWS News Blog
Deploy MCP servers in AgentCore Runtime - Amazon Bedrock AgentCore
Amazon Bedrock AgentCore Runtime for AWS Marketplace - AWS Marketplace
Effectively building AI agents on AWS Serverless | AWS Compute Blog
answered a year ago
AgentCore Runtime routes requests based on the mcp-session-id header value. Different session ids will be served by different containers to ensure session isolation. The same session id will be served by the same container for the lifetime of the container. A container will be recycled after 15 min of inactivity and cannot last longer than 8hrs.
If you need to persist data for longer than 8hrs you need to consider some external persistency, like DynamoDB.
https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-mcp-protocol-contract.html
If you do not specify an mcp-session-id header in your invocation, AgentCore will generate one.
https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/gateway-sessions.html
Hope the above helps "Stateful MCP server target interactions" should answer the issue.
answered 4 months ago
The challenge here is that MCP session state cannot safely live inside a single runtime container in AgentCore Runtime. AgentCore does not guarantee sticky routing of HTTP requests to a particular container instance. AWS explicitly requires MCP servers running under AgentCore to be stateless between HTTP requests, which means you can’t rely on process-local RAM for protocol-level state.
If your MCP implementation requires persistent session state (not all do), that state must live somewhere external and consistent — e.g., AgentCore Memory, DynamoDB, S3, Redis, etc. That’s the only way to ensure correctness when the runtime container is replaced, scaled, or recycled.
AgentCore will horizontally scale by launching multiple runtime instances. And yes, there is session affinity: if AgentCore spins up three instances, runtimeSessionId = 1234 will usually stay pinned to “Runtime A” for as long as that session exists — up until the microVM hits its maxLifetime or idleRuntimeSessionTimeout. But you have no guarantee when AgentCore will tear down or restart that instance, so you cannot rely on in-RAM state for a “stateful MCP” design. It’s an optimization only, not a correctness mechanism.
This is different from the process-level persistence you get inside a single running agent instance. For example, in Lambda I often use a statically scoped module-level variable as a lightweight cache for data that rarely changes (like a DynamoDB read). If the Lambda is “warm,” that cache survives between invocations and saves a round-trip. But that behavior is only a best-effort optimization — scaling events, cold starts, and timeouts all wipe that state out unpredictably.
AgentCore runtimes appear to have a similar cold/warm dynamic (a microVM stays alive for the duration of a session until idle timeout or maxLifetime), but even if you did rely on process-local caching there, it wouldn’t solve the underlying MCP issue. MCP requires consistent session state, and process-level RAM inside a single runtime instance is never durable or reliable enough for that — especially given horizontal scaling and runtime recycling.
Finally, using an ECR-based agent doesn’t change these semantics. You get the same lifecycle rules, the same per-session microVM isolation, and the same requirement to externalize MCP state if you need persistence.
I hope that helps. I am not the expert, and I don't work for AWS, but sometimes I don't like how the bot answers and I have been dancing around this issue as well, so I thought maybe I could help with your question.
answered a year ago
Relevant content
asked 4 months ago
asked a year ago
asked 4 months ago
