sam-serverless-mcp-server
The sam-serverless-mcp-server is a minimal Model Context Protocol (MCP) server that runs on AWS Lambda and is accessible through Amazon API Gateway, using the Serverless Application Model for deployment. It supports local development and includes a simple example tool for JSON-RPC interaction.
π§ sam-serverless-mcp-server
A super simple Model Context Protocol (MCP) server deployed on AWS Lambda and exposed via Amazon API Gateway, deployed with Serverless Application Model (SAM). This skeleton is based on the awesome work of FrΓ©dΓ©ric Barthelet: which has developed a middy middleware for Model Context Protocol (MCP) server integration with AWS Lambda functions in this repo
Long story
π Read the article of this series here on dev.to
π Features
- πͺ Minimal MCP server setup using @modelcontextprotocol/sdk
- π Deployed as a single AWS Lambda function
- π HTTP POST endpoint exposed via API Gateway at /mcp
- π Supports local development via SAM
- π§ͺ Includes a simple example tool (add) with JSON-RPC interaction
π¦ Project Structure
sam-serverless-mcp-server/
βββ __tests__/ # Jest tests
βββ src/ # Source code
β βββ index.js # MCP server handler
βββ .gitignore # Git ignore file
βββ buildspec.yml # Buildspec file for AWS CodeBuild and CodePipeline (CI/CD)
βββ jest.config.mjs # Jest config file
βββ package.json # Project dependencies
βββ package-lock.json # Project lock file
βββ README.md # This documentation file
βββ samconfig.toml # Serverless Application Model config
βββ template.yml # Serverless Application Model template
π Prerequisites
π Getting Started
- Install dependencies:
npm install
- Run Locally with SAM
sam local start-api
Local endpoint will be available at:
POST http://localhost:3000/mcp
Switch to Api Gateway V2 (HTTP API)
If you want to use API Gateway V2, you can change the template.yml
file to use HttpApi
instead of Api
in the Events
section. This will allow you to use HTTP APIs instead of REST APIs.
This will allow you to use HTTP APIs instead of REST APIs.
Events:
McpHttpApi:
Type: HttpApi # π This switches to HTTP API (v2)
Properties:
Path: /mcp
Method: POST
π§ͺ Test with curl requests
List tools
curl --location 'http://localhost:3000/mcp' \
--header 'content-type: application/json' \
--header 'accept: application/json' \
--header 'jsonrpc: 2.0' \
--data '{
"jsonrpc": "2.0",
"method": "tools/list",
"id": 1
}'
β Use the add Tool
curl --location 'http://localhost:3000/mcp' \
--header 'content-type: application/json' \
--header 'accept: application/json' \
--header 'jsonrpc: 2.0' \
--data '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "add",
"arguments": {
"a": 5,
"b": 3
}
}
}'
π§ͺ Test with jest
There are some basic tests included in the __tests__
folder. You can run them with:
npm run test
𧬠Code Breakdown
This code is based on the awesome work of FrΓ©dΓ©ric Barthelet: which has developed a middy middleware for Model Context Protocol (MCP) server integration with AWS Lambda functions in this repo
src/index.js
import middy from "@middy/core";
import httpErrorHandler from "@middy/http-error-handler";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import mcpMiddleware from "middy-mcp";
const server = new McpServer({
name: "Lambda hosted MCP Server",
version: "1.0.0",
});
server.tool("add", { a: z.number(), b: z.number() }, async ({ a, b }) => ({
content: [{ type: "text", text: String(a + b) }],
}));
export const handler = middy()
.use(mcpMiddleware({ server }))
.use(httpErrorHandler());
π‘ Deploy to AWS
Just run:
sam build
sam deploy --guided
After deployment, the MCP server will be live at the URL output by the command.
π License
MIT β feel free to fork, tweak, and deploy your own version!