This project is a serverless application that implements a Streamable HTTP API using the Model Context Protocol (MCP) on AWS Lambda.
This application deploys a Lambda function that supports response streaming using AWS SAM (Serverless Application Model). The main features include:
- Tool invocation functionality using the Model Context Protocol (MCP)
- Real-time responses utilizing Lambda Response Streaming
- Implementation examples of simple greeting tools and notification capabilities
This project uses the Model Context Protocol (MCP) TypeScript library.
- Repository: github.com/anthropics/model-context-protocol
This application consists of the following components:
- MCPStreamableFunction: Lambda function using Node.js 22.x runtime
- Lambda Adapter Layer: Layer to support response streaming
- Function URL: Endpoint for direct access to the Lambda function without authentication
To use this project, you need:
# Clone the repository
git clone https://github.com/moritalous/mcp-streamablehttp-lambda-sample.git
# Navigate to the project directory
cd mcp-streamablehttp-lambda-sample
# Install dependencies
cd mcp-function
npm install
cd ..
# Build the application
sam build
# Deploy the application (interactive for the first time)
sam deploy --guided
During deployment, you will be prompted to enter the following information:
- Stack Name: Name of the CloudFormation stack (e.g., mcp-server-streamable-http)
- AWS Region: Region to deploy to
- Confirm changes before deploy: Whether to confirm changes
- Allow SAM CLI IAM role creation: Whether to allow IAM role creation
- Save arguments to samconfig.toml: Whether to save the configuration
After deployment completes, the Lambda Function URL will be output. You can use this URL to access the API.
This application provides the following endpoints:
- POST /mcp: Initialize and send MCP requests
- GET /mcp: Method not allowed (returns 405)
- DELETE /mcp: Method not allowed (returns 405)
This sample application implements the following tools:
-
greet: A simple greeting tool
- Parameter:
name
(string)
- Parameter:
-
multi-greet: A tool that sends multiple greetings with notifications
- Parameter:
name
(string)
- Parameter:
-
greeting-template: A prompt template for generating greetings
- Parameter:
name
(string)
- Parameter:
-
greeting-resource: A resource that provides a default greeting
A client implementation example is available in mcp-function/src/client.ts
. This client provides an interactive command-line interface to:
- Connect to the server
- List available tools, prompts, and resources
- Call tools with arguments
- Get prompts with arguments
- Handle notifications from the server
- Manage session termination and reconnection
cd mcp-function
MCP_SERVER_URL=https://*****.lambda-url.us-east-1.on.aws/mcp npm run dev:client
To remove resources when no longer needed:
sam delete --stack-name <your-stack-name>