MCP ExplorerExplorer

Cdk Serverless Mcp Server

@elevaon 10 months ago
2 MIT
FreeCommunity
AI Systems
A minimal Model Context Protocol (MCP) server on AWS Lambda with API Gateway.

Overview

What is Cdk Serverless Mcp Server

cdk-serverless-mcp-server is a minimal Model Context Protocol (MCP) server that is deployed on AWS Lambda and exposed via Amazon API Gateway, utilizing AWS CDK for deployment.

Use cases

Use cases include building serverless applications that need to process JSON-RPC requests, integrating with other AWS services, and developing prototypes for MCP-based solutions.

How to use

To use cdk-serverless-mcp-server, ensure you have Node.js v22+ and AWS CDK installed. Clone the repository, install dependencies, and deploy the stack using AWS CDK commands.

Key features

Key features include a minimal MCP server setup using @modelcontextprotocol/sdk, deployment as a single AWS Lambda function, an HTTP POST endpoint exposed via API Gateway, support for local development testing with Jest, and a simple example tool for JSON-RPC interaction.

Where to use

cdk-serverless-mcp-server can be used in cloud-based applications that require a lightweight and serverless architecture for handling Model Context Protocol interactions.

Content

🧠 cdk-serverless-mcp-server

A super simple Model Context Protocol (MCP) server deployed on AWS Lambda and exposed via Amazon API Gateway, deployed with AWS CDK.
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

🛠 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 testing with jest
  • 🧪 Includes a simple example tool (add) with JSON-RPC interaction

📦 Project Structure

cdk-serverless-mcp-server/
├── __tests__/                              # Jest tests
├── bin/                                    # CDK entry point
├── cdk-serverless-mcp-server.ts                # CDK app
├── lib/                                    # CDK stack
│   └── cdk-serverless-mcp-server-stack.ts      # CDK stack
├── src/                                    # Source code
│   └── index.mjs                               # MCP server handler
├── .gitignore                              # Git ignore file
├── cdk.json                                # CDK Project config
├── package.json                            # Project dependencies
├── package-lock.json                       # Project lock file
├── README.md                               # This documentation file

🛠 Prerequisites

🚀 Getting Started

  1. Install dependencies:
npm install
  1. Install AWS CDK globally (if not already installed):
npm install -g aws-cdk
  1. Test Locally with jest
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:

npm run layer-dependencies-install # install dependencies for the layer

Then, deploy the stack:

cdk bootstrap
cdk deploy

After deployment, the MCP server will be live at the URL output by the command.

🧪 Once deployed, test with curl requests

List tools

curl --location 'http://your-endpoint/dev/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://your-endpoint/dev/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
    }
  }
}'

📘 License

MIT — feel free to fork, tweak, and deploy your own version!

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers