turul-mcp-aws-lambda
AWS Lambda integration for the turul-mcp-framework, enabling serverless deployment of MCP servers with full protocol compliance.
Overview
turul-mcp-aws-lambda provides seamless integration between the turul-mcp-framework and AWS Lambda runtime, enabling serverless MCP servers with proper session management, CORS handling, and SSE streaming support.
Features
- ✅ Zero-Cold-Start Architecture - Optimized Lambda integration
- ✅ MCP 2025-06-18 Compliance - Full protocol support with SSE streaming
- ✅ DynamoDB Session Storage - Persistent session management across invocations
- ✅ CORS Support - Automatic CORS header injection for browser clients
- ✅ Type Conversion Layer - Clean
lambda_http↔hyperconversion - ✅ Streaming Responses - SSE notifications through Lambda streaming
- ✅ Builder Pattern - Familiar API matching
McpServer::builder()
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.1"
= "0.1.1"
= "0.13"
= { = "1.0", = ["macros"] }
Minimal Lambda MCP Server
use ;
use LambdaMcpServerBuilder;
use McpTool;
use ;
async
Architecture
Framework Integration
The crate bridges AWS Lambda's HTTP execution model with the turul-mcp-framework:
┌─────────────────────────┐
│ AWS Lambda Runtime │
├─────────────────────────┤
│ turul-mcp-aws-lambda │ ← This crate
│ ├─ Type Conversion │ ← lambda_http ↔ hyper
│ ├─ CORS Integration │ ← Automatic header injection
│ ├─ SSE Adaptation │ ← Lambda streaming responses
│ └─ Session Management │ ← DynamoDB persistence
├─────────────────────────┤
│ turul-mcp-server │ ← Core framework
└─────────────────────────┘
Three-Layer Discovery
Through lambda development, we discovered the framework's 3-layer architecture:
- Layer 1:
McpServer- High-level builder and handler management - Layer 2:
HttpMcpServer- TCP server (incompatible with Lambda) - Layer 3:
SessionMcpHandler- Request handler (what Lambda needs)
This crate skips Layer 2 and provides clean integration to Layer 3.
DynamoDB Session Storage
Automatic Table Creation
use LambdaMcpServerBuilder;
use DynamoDbSessionStorage;
use Arc;
let storage = new;
let server = new
.name
.storage // Persistent session management
.tool
.build
.await?;
Session Persistence
Sessions automatically persist across Lambda invocations:
;
CORS Configuration
Automatic CORS for Browser Clients
let server = new
.cors_allow_all_origins // Enable CORS for all origins
.build
.await?;
Custom CORS Configuration
use ;
let cors = new
.allow_origins
.allow_credentials;
let server = new
.cors
.build
.await?;
SSE Streaming in Lambda
Real-time Notifications
Lambda streaming responses enable real-time SSE notifications:
;
Deployment
Local Testing with cargo-lambda
# Install cargo-lambda
# Run locally for testing
RUST_LOG=debug
# Test with MCP Inspector
# Connect to: http://localhost:9000/lambda-url/my-lambda-server
Deploy to AWS Lambda
# Build for Lambda
# Deploy to AWS
Environment Configuration
# Required environment variables
# DynamoDB table name
Examples
Complete AWS Integration Server
See examples/lambda-mcp-server for a production-ready example with:
- DynamoDB query tools
- SNS publishing
- SQS message sending
- CloudWatch metrics
- Full session persistence
- SSE streaming
Builder Pattern Examples
use LambdaMcpServerBuilder;
use ToolBuilder;
// Runtime tool creation
let dynamic_tool = new
.description
.number_param
.number_param
.execute
.build?;
let server = new
.tool
.build
.await?;
Testing
Unit Tests
The crate includes comprehensive test coverage:
# Run all tests
# Test specific modules
Integration Testing
# Test with local Lambda runtime
&
Performance Optimization
Cold Start Optimization
// Cache expensive operations at module level
static SHARED_STORAGE: OnceLock = new;
async
Memory Management
Lambda functions benefit from efficient memory usage:
let server = new
.tool // Use Default for zero-sized types
.build
.await?;
Feature Flags
[]
= { = "0.1.1", = ["cors", "sse", "dynamodb"] }
default- Includescorsandssecors- CORS header injection for Lambda responsessse- Server-Sent Events stream adaptationdynamodb- DynamoDB session storage backend
Limitations
Lambda-Specific Considerations
- Request Timeout: Lambda has 15-minute maximum execution time
- Payload Size: 6MB maximum payload size for synchronous invocations
- Concurrent Executions: Subject to AWS Lambda concurrency limits
- Cold Starts: First invocation may have higher latency
SSE Streaming Notes
- Lambda streaming responses have size and time limits
- Long-running SSE connections may be terminated by Lambda
- Consider using API Gateway WebSocket for persistent connections
Error Handling
Lambda Error Patterns
// Proper error handling for Lambda
handler.handle.await
.map_err
License
Licensed under the MIT License. See LICENSE for details.