A TypeScript SDK for creating Model Context Protocol (MCP) servers that run on AWS Lambda using decorators.
The MCP Lambda SDK to NodeJs provides a simple, decorator-based approach to create MCP servers that run as AWS Lambda functions. With just a few decorators, you can expose your Lambda functions as MCP tools that can be called by AI systems following the Model Context Protocol specification.
npm install mcp-lambda-nodejsimport { MCPServer, MCPTool, z } from 'mcp-lambda-nodejs';
@MCPServer({
name: 'my-calculator-server',
version: '1.0.0'
})
export class CalculatorServer {
@MCPTool({
title: 'Add Numbers',
description: 'Adds two numbers together',
inputSchema: {
a: z.number().describe('First number'),
b: z.number().describe('Second number')
},
outputSchema: {
result: z.number().describe('The sum of the two numbers'),
operation: z.string().describe('Description of the operation')
}
})
async add(params: { a: number; b: number }) {
return {
result: params.a + params.b,
operation: `Added ${params.a} + ${params.b}`
};
}
@MCPTool({
title: 'Multiply Numbers',
description: 'Multiplies two numbers',
inputSchema: {
a: z.number().describe('First number'),
b: z.number().describe('Second number')
},
outputSchema: {
result: z.number().describe('The product of the two numbers')
}
})
async multiply(params: { a: number; b: number }) {
return {
result: params.a * params.b
};
}
}import { MCPHandlerFactory, APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'mcp-lambda-nodejs';
import { CalculatorServer } from './calculator-server';
// Create the handler using the factory
export const calculatorHandler = MCPHandlerFactory.createHandler(CalculatorServer, 'calculator');
// Export the main function for the serverless framework
export async function main(event: APIGatewayProxyEventV2): Promise<APIGatewayProxyResultV2> {
return await calculatorHandler(event);
}Configure your serverless.yml (or preferred deployment method):
service: my-mcp-server
provider:
name: aws
runtime: nodejs18.x
functions:
calculator:
handler: dist/calculator-handler.main
events:
- httpApi:
path: /calculator
method: post
- httpApi:
path: /calculator
method: optionsMarks a class as an MCP server.
Parameters:
config.name: Server name identifierconfig.version: Server version
Marks a method as an MCP tool.
Parameters:
config.title: Human-readable tool titleconfig.description: Tool descriptionconfig.inputSchema: Zod schema object for input validationconfig.outputSchema: Zod schema object for output validation
Creates a Lambda handler function for an MCP server class.
Parameters:
ServerClass: The decorated MCP server classserverName: Optional server instance name
Returns: AWS Lambda handler function
The SDK includes built-in session management to maintain state across multiple tool calls from the same client session.
import { MCPSessionManager } from 'mcp-lambda-nodejs';
@MCPServer({
name: 'stateful-server',
version: '1.0.0'
})
export class StatefulServer {
private sessionManager = new MCPSessionManager();
@MCPTool({
title: 'Store Value',
description: 'Stores a value in the session',
inputSchema: {
key: z.string(),
value: z.string(),
sessionId: z.string().optional()
},
outputSchema: {
success: z.boolean()
}
})
async storeValue(params: { key: string; value: string; sessionId?: string }) {
if (params.sessionId) {
await this.sessionManager.updateSessionState(params.sessionId, {
[params.key]: params.value
});
}
return { success: true };
}
}You can use any Zod schema for input and output validation:
@MCPTool({
title: 'Process User',
description: 'Processes user data',
inputSchema: {
user: z.object({
name: z.string().min(1),
email: z.string().email(),
age: z.number().int().min(0).max(120)
})
},
outputSchema: {
processed: z.boolean(),
userId: z.string().uuid()
}
})
async processUser(params: { user: { name: string; email: string; age: number } }) {
// Process user logic here
return {
processed: true,
userId: crypto.randomUUID()
};
}The SDK automatically handles errors and returns them in the proper MCP format:
@MCPTool({
title: 'Divide Numbers',
description: 'Divides two numbers',
inputSchema: {
dividend: z.number(),
divisor: z.number()
},
outputSchema: {
result: z.number()
}
})
async divide(params: { dividend: number; divisor: number }) {
if (params.divisor === 0) {
throw new Error('Division by zero is not allowed');
}
return {
result: params.dividend / params.divisor
};
}See the /examples directory for complete working examples:
- Calculator Server: Basic arithmetic operations
- Data Processing Server: Advanced data manipulation tools
- Node.js 18+
- TypeScript 5.0+
- AWS Lambda (for deployment)
@modelcontextprotocol/sdk: MCP protocol implementationzod: Schema validationreflect-metadata: Decorator metadata supportuuid: Session ID generation
MIT
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
- Create an issue on GitHub for bug reports
- Check the examples for usage patterns
- Review the MCP specification at modelcontextprotocol.io
In order to deploy the example, you need to run the following command:
serverless deploy
After running deploy, you should see output similar to:
Deploying "serverless-http-api" to stage "dev" (us-east-1)
✔ Service deployed to stack serverless-http-api-dev (91s)
endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
hello: serverless-http-api-dev-hello (1.6 kB)
Note: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to HTTP API (API Gateway V2) event docs.
After successful deployment, you can call the created application via HTTP:
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
Which should result in response similar to:
{ "message": "Go Serverless v4! Your function executed successfully!" }The easiest way to develop and test your function is to use the dev command:
serverless dev
This will start a local emulator of AWS Lambda and tunnel your requests to and from AWS Lambda, allowing you to interact with your function as if it were running in the cloud.
Now you can invoke the function as before, but this time the function will be executed locally. Now you can develop your function locally, invoke it, and see the results immediately without having to re-deploy.
When you are done developing, don't forget to run serverless deploy to deploy the function to the cloud.