openapi_mcp_stack.py•4.05 kB
"""CDK Stack for OpenAPI MCP Server deployment."""
from pathlib import Path
import aws_cdk as cdk
from aws_cdk import Duration, Stack
from aws_cdk import aws_apigatewayv2 as apigw
from aws_cdk.aws_apigatewayv2_integrations import HttpLambdaIntegration
from aws_cdk import aws_lambda as lambda_
from constructs import Construct
class OpenApiMcpStack(Stack):
"""CDK Stack that deploys OpenAPI MCP Server to Lambda with API Gateway.
Uses AWS Lambda Web Adapter for HTTP server deployment.
"""
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
"""Initialize the stack.
Args:
scope: CDK app
construct_id: Stack identifier
**kwargs: Additional stack arguments
"""
super().__init__(scope, construct_id, **kwargs)
# Get project root directory
project_root = Path(__file__).parent.parent
# Lambda function using Docker image with Lambda Web Adapter
mcp_function = lambda_.DockerImageFunction(
self,
'OpenApiMcpFunction',
code=lambda_.DockerImageCode.from_image_asset(
str(project_root),
file="Dockerfile",
platform=cdk.aws_ecr_assets.Platform.LINUX_AMD64,
exclude=[
"cdk.out",
"cdk/cdk.out",
".cdk.staging",
".venv",
".git",
"node_modules",
".pytest_cache",
"__pycache__",
"*.pyc",
".DS_Store",
".vscode",
".idea",
],
),
timeout=Duration.seconds(30),
memory_size=512,
environment={
'LOG_LEVEL': 'INFO',
'PORT': '8080', # Lambda Web Adapter traffic port
'AWS_LWA_READINESS_CHECK_PATH': '/', # Health check endpoint
'API_NAME': cdk.CfnParameter(
self,
'ApiName',
type='String',
description='Name of the API to expose',
default='MyAPI',
).value_as_string,
'API_BASE_URL': cdk.CfnParameter(
self,
'ApiBaseUrl',
type='String',
description='Base URL of the API',
).value_as_string,
'API_SPEC_URL': cdk.CfnParameter(
self,
'ApiSpecUrl',
type='String',
description='URL of the OpenAPI specification',
).value_as_string,
'AUTH_TYPE': cdk.CfnParameter(
self,
'AuthType',
type='String',
description='Authentication type (none, basic, bearer, api_key, cognito, zoho)',
default='none',
allowed_values=['none', 'basic', 'bearer', 'api_key', 'cognito', 'zoho'],
).value_as_string,
},
)
# API Gateway HTTP API
http_api = apigw.HttpApi(
self,
'OpenApiMcpHttpApi',
api_name='openapi-mcp-server',
description='HTTP API for OpenAPI MCP Server',
)
# Add Lambda integration
http_api.add_routes(
path='/{proxy+}',
methods=[apigw.HttpMethod.ANY],
integration=HttpLambdaIntegration(
'OpenApiMcpIntegration', mcp_function
),
)
# Outputs
cdk.CfnOutput(
self,
'ApiUrl',
value=http_api.url or '',
description='URL of the HTTP API Gateway',
)
cdk.CfnOutput(
self,
'FunctionName',
value=mcp_function.function_name,
description='Name of the Lambda function',
)