Lesson 2 of 3 Model Context Protocol and Amazon Bedrock AgentCore

Hosting MCP Servers on Lambda and Amazon ECS

This lesson compares hosting options for remote MCP servers: lightweight, stateless tools fit serverless functions, and complex, stateful or long-running tools fit containers.

Learning outcomes

After completing this lesson, you will be able to:

  • Host stateless MCP servers on Lambda.
  • Host complex or stateful MCP servers on Amazon ECS.
  • Use MCP client libraries for consistent access.

Stateless MCP servers on Lambda

A stateless MCP server answers each request independently: it lists its tools, runs a tool and returns the result, keeping nothing between requests. This model fits AWS Lambda well, behind Amazon API Gateway or a Lambda function URL, using the Streamable HTTP transport in stateless mode.

Lambda scales automatically with the number of requests, costs nothing when no agent is calling the server, and needs no servers to manage. It suits lightweight tools, such as lookups, calculations and calls to AWS or internal APIs, that complete within seconds. Authentication is enforced at API Gateway, for example with an Amazon Cognito or Lambda authoriser, or in the function itself, and each tool's permissions come from the function's execution role, which should grant only what the tools need. Servers that hold long-lived connections or session state are better hosted on Amazon ECS, as described next.

The following diagram shows the hosting chosen for each of two MCP servers with different run times.

Choosing hosting for two MCP servers. The exchange-rate server is stateless and answers in under a second, so Lambda behind API Gateway fits. The analysis server streams progress for up to 40 minutes and keeps a database connection pool, beyond Lambda's 15-minute limit, so ECS on Fargate behind a load balancer fits.
Amazon EKS is an alternative container platform for the second server.

Complex MCP servers on Amazon ECS

Some servers are stateful or heavy: they keep sessions, stream progress for long operations, hold connection pools to databases, load large libraries or models, or run longer than Lambda's 15-minute limit. These fit containers on Amazon ECS with AWS Fargate behind an Application Load Balancer, with service auto scaling, or on Amazon EKS. Session affinity or a shared session store (for example ElastiCache or DynamoDB) is needed when several tasks serve the same sessions.

Amazon Bedrock AgentCore Runtime can also host MCP servers with managed scaling and session isolation, and AgentCore Gateway can turn existing Lambda functions and REST APIs into MCP tools without writing a server, as described in the next lesson.

Sort into groups

Lambda or ECS?

Exam skills 2.1.7

Choose where each MCP server should be hosted.

Log in to try this activity Interactive activities are free with an account.

MCP client libraries for consistent access

On the consuming side, applications and agents connect to MCP servers through MCP client libraries rather than hand-written HTTP calls. Official SDKs exist for Python, TypeScript and other languages, and agent frameworks such as Strands Agents include MCP clients.

A client library handles connection set-up, capability negotiation, tool discovery, authentication tokens and error handling in the same way for every server. As a result, adding a new server to an agent requires only its address and credentials, and every server behaves consistently from the agent's point of view, which is the consistency of access that the exam guide refers to. The following table compares the hosting options for MCP servers.

HostingFitsConsiderations
Lambda + API Gateway or function URLStateless, lightweight, short tools15-minute limit; cold starts; scales to zero
ECS on Fargate + load balancerStateful, long-running or heavy toolsAlways-on cost; session affinity
AgentCore RuntimeManaged hosting with session isolationManaged service limits and pricing
AgentCore GatewayExisting APIs and Lambda functions as MCP toolsNo server code to write
True or false

MCP clients

Exam skills 2.1.7 · 1.5.6

Check three claims about consuming MCP servers.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing hosting

A company needs two MCP servers: one that looks up exchange rates from an internal API in under a second, and one that runs data quality analyses on large datasets, streaming progress for up to 40 minutes and keeping a database connection pool.

The exchange-rate server is stateless and fast, so Lambda behind API Gateway fits. The analysis server is long-running and stateful, exceeding Lambda's limit, so ECS on Fargate behind a load balancer fits.

Check yourself

How can existing APIs be exposed as MCP tools without writing a new MCP server?

Show answer

With Amazon Bedrock AgentCore Gateway, which turns Lambda functions and APIs into MCP tools.

Exam Tips

Remember for the exam

  • Stateless, lightweight tools: MCP server on Lambda.
  • Stateful, long-running or heavy tools: MCP server on ECS with Fargate.
  • Existing APIs as MCP tools without new code: AgentCore Gateway.
  • Use MCP client libraries for consistent access patterns.