Guidance for Deploying Model Context Protocol (MCP) Servers on AWS

repository·main·Indexed 41 days ago

https://github.com/aws-solutions-library-samples/guidance-for-deploying-model-context-protocol-servers-on-aws

Infrastructure guidance and code for deploying Model Context Protocol (MCP) servers on AWS using ECS Fargate and Lambda. Implements the 2025-06-18 MCP specification with StreamableHTTP transport and secure remote access via OAuth 2.0 (RFC9728) using Amazon Cognito, CloudFront, and WAF.

Tokens
15.2K
Snippets
35
Records
54
Agent score
100%

What's inside MCP on AWS

  1. Overview of Deploying MCP Servers on AWS

    main

    This guidance provides a pattern for deploying Model Context Protocol (MCP) servers on AWS using secure authentication via Amazon Cognito. It implements the 2025-06-18 MCP specification with OAuth 2.0 Protected Resource Metadata (RFC9728).

    Key architectural components include:

    • CloudFront & WAF: Global delivery and security.
    • Application Load Balancer (ALB): Traffic distribution and SSL termination.
    • ECS Fargate & Lambda: Containerized and serverless hosting for MCP servers.
    • Amazon Cognito: Acts as the OAuth 2.0 authorization server.
    • StreamableHTTP transport: Enables remote access via stateless request handling.

    The deployment is organized into four CDK stacks: VPC, Security, CloudFront WAF, and MCP Server stacks.

  2. Deployment Architecture for Cognito-integrated MCP Servers

    main

    A production-ready deployment on AWS involves several CDK stacks to manage networking, security, and compute:

    • VPC Stack: Manages the VPC, subnets, and security groups.
    • Security Stack: Provisions the AWS Cognito User Pool and App Client.
    • CloudFront WAF Stack: Deploys a WAF Web ACL for CloudFront (Note: currently limited to us-east-1).
    • MCP Server Stack: Deploys the core compute (ECS Fargate or Lambda), the Application Load Balancer (ALB), and the CloudFront distribution.
    • Cross-Region Sync: Ensures WAF parameters are synchronized across regions if necessary.
  3. Use StreamableHTTP Transport for MCP Communication

    main

    The implementation uses StreamableHTTP transport for MCP communication, which follows these patterns:

    • Protocol: HTTP/HTTPS using JSON-RPC 2.0 over POST requests.
    • Statelessness: Each request is treated as a new instance to support concurrent client access.
    • Authentication: Every request must include a Bearer token for validation.
    • Primary Endpoints:
      • POST /mcp: The main endpoint for MCP tool calls and resource requests.
      • GET /.well-known/oauth-protected-resource: The endpoint for OAuth discovery.
  4. Implement OAuth 2.0 Protected Resource Metadata (RFC9728) in MCP Servers

    main

    To support standards-compliant authentication, MCP servers should implement the OAuth 2.0 Protected Resource Metadata specification (RFC9728). This allows clients to discover how to authenticate against the resource.

    Key Requirements:

    • Metadata Endpoint: The server must expose a GET endpoint at /.well-known/oauth-protected-resource.
    • Purpose: This endpoint advertises the resource identifier and the supported authorization servers (e.g., AWS Cognito).
    • Discovery Flow: When a client receives a 401 Unauthorized response, it uses the resource_metadata URL provided in the WWW-Authenticate header to fetch this configuration.
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: Bearer realm="mcp-server", resource_metadata="https://example.com/.well-known/oauth-protected-resource"
  5. Understand the OAuth 2.0 Authorization Flow for MCP Clients

    main

    The integration follows a specific sequence to move from an unauthenticated state to a secure MCP session:

    1. Trigger: Client sends an MCP request without a token.
    2. Challenge: MCP Server returns 401 Unauthorized with a WWW-Authenticate header containing the resource_metadata URL.
    3. Discovery: Client fetches metadata from /.well-known/oauth-protected-resource and identifies the Authorization Server (AS).
    4. Configuration: Client fetches the AS metadata via /.well-known/openid-configuration.
    5. Authorization: Client initiates an OAuth 2.0 Authorization Code flow with PKCE, including the resource parameter.
    6. Token Exchange: After user authorization, the client exchanges the code for an access token.
    7. Authenticated Request: Client retries the MCP request using the access token as a Bearer token.
  6. Deploy MCP Server stacks using AWS CDK

    main

    Follow these steps to deploy the infrastructure. The deployment process involves cloning the repository, installing dependencies, authenticating with ECR, and deploying the CDK stacks.

    1. Clone and Navigate:
      git clone <repository-url>
      cd guidance-for-deploying-model-context-protocol-servers-on-aws
      cd source/cdk/ecs-and-lambda
    2. Install Dependencies:
      npm install
    3. Login to Public ECR:
      aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
    4. Deploy Stacks: Choose one of the following deployment patterns based on your domain requirements.
    # Standard deployment without custom domain
    cdk deploy --all
    
    # Deployment with custom domain (single region - us-east-1)
    cdk deploy --all --context cdnCertificateArn=arn:aws:acm:us-east-1:123456789012:certificate/abc123 --context albCertificateArn=arn:aws:acm:us-east-1:123456789012:certificate/abc123 --context customDomain=mcp-server.example.com
    
    # Deployment with multi-region certificates
    cdk deploy --all --context cdnCertificateArn=arn:aws:acm:us-east-1:123456789012:certificate/abc123 --context albCertificateArn=arn:aws:acm:eu-west-1:123456789012:certificate/def456 --context customDomain=mcp-server.example.com
    
    # CloudFront HTTPS only (ALB remains HTTP)
    cdk deploy --all --context cdnCertificateArn=arn:aws:acm:us-east-1:123456789012:certificate/abc123 --context customDomain=mcp-server.example.com
  7. Prepare your environment for deployment

    main

    Before deploying, ensure your environment meets the following requirements:

    Operating System

    • Optimized for Amazon Linux 2 AMI.

    Required Tools

    AWS CDK Bootstrap If using AWS CDK for the first time in your account, run:

    cdk bootstrap
    npm install -g aws-cdk
    cdk bootstrap
  8. Update MCP server configurations

    main

    To update only the MCP server component (e.g., after changing server code or configuration) without redeploying the entire infrastructure, use the following commands:

    Without domain configuration:

    cdk deploy MCP-Server

    With domain configuration:

    cdk deploy MCP-Server --context cdnCertificateArn=arn:aws:acm:us-east-1:123456789012:certificate/abc123 --context albCertificateArn=arn:aws:acm:us-east-1:123456789012:certificate/abc123 --context customDomain=mcp-server.example.com
  9. Run the Simple Auth Client

    main

    The client can be started using uv run mcp-simple-auth-client. You can customize the connection behavior using environment variables:

    • To connect to a server on a specific port, set MCP_SERVER_PORT.
    • To use SSE transport instead of the default, set MCP_TRANSPORT_TYPE=sse.
    # Standard run
    uv run mcp-simple-auth-client
    
    # Run with custom server port
    MCP_SERVER_PORT=3001 uv run mcp-simple-auth-client
    
    # Run using SSE transport
    MCP_TRANSPORT_TYPE=sse uv run mcp-simple-auth-client
  10. Optimize AWS MCP Server infrastructure costs

    main

    To manage and reduce the cost of your MCP server deployment, follow these recommendations:

    Immediate Actions

    • Monitor Cognito MAU: Keep an eye on Monthly Active Users to stay within the free tier (which supports up to 50,000 MAUs).
    • Evaluate NAT Gateway: Determine if you need a NAT Gateway in both availability zones or if one is sufficient for your requirements.
    • Reserved Capacity: For predictable ECS workloads, consider using reserved capacity to lower costs.
    • CloudFront Patterns: Evaluate your CloudFront data transfer patterns to ensure they are optimal.

    Best Practices

    • Resource Monitoring: Use CloudWatch to monitor resource utilization and adjust capacity as needed.
    • Cost Alerts: Set up AWS Budgets to receive alerts on unexpected cost increases.
    • Long-term Savings: Consider Reserved Instances for long-term ECS usage.
    • Data Lifecycle: Implement lifecycle policies for the DynamoDB token table to automatically expire old tokens and manage storage costs.