Skip to content

Commit 19d2fb4

Browse files
committed
feat: Add separate handlers for API Gateway (REST APIs), API Gateway V2 (HTTP APIs), and Lambda function URLs to Typescript impl
1 parent 69208ba commit 19d2fb4

10 files changed

+1358
-886
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
2+
import {
3+
StreamableHttpHandler,
4+
ParsedHttpRequest,
5+
HttpResponse,
6+
} from "./streamable_http_handler.js";
7+
import { RequestHandler } from "./request_handler.js";
8+
9+
/**
10+
* Handler for API Gateway V1 events (REST APIs)
11+
*
12+
* This handler processes APIGatewayProxyEvent events (Lambda proxy integration behind API Gateway REST API)
13+
* and returns APIGatewayProxyResult responses.
14+
*
15+
* This class handles all the generic JSON-RPC protocol aspects of the MCP Streamable HTTP transport:
16+
* - HTTP method validation (POST, OPTIONS, GET)
17+
* - Content-Type and Accept header validation
18+
* - JSON parsing and validation
19+
* - Batch request handling
20+
* - CORS headers
21+
* - Error response formatting
22+
* This class does not implement session management.
23+
*
24+
* The specific business logic is delegated to a provided RequestHandler implementation.
25+
*/
26+
export class APIGatewayProxyEventHandler extends StreamableHttpHandler<
27+
APIGatewayProxyEvent,
28+
APIGatewayProxyResult
29+
> {
30+
constructor(requestHandler: RequestHandler) {
31+
super(requestHandler);
32+
}
33+
34+
/**
35+
* Parse APIGatewayProxyEvent into common HTTP request format
36+
*/
37+
protected parseEvent(event: APIGatewayProxyEvent): ParsedHttpRequest {
38+
return {
39+
method: event.httpMethod,
40+
headers: event.headers || {},
41+
body: event.body || null,
42+
};
43+
}
44+
45+
/**
46+
* Format HTTP response as APIGatewayProxyResult
47+
*/
48+
protected formatResponse(response: HttpResponse): APIGatewayProxyResult {
49+
return {
50+
statusCode: response.statusCode,
51+
headers: response.headers,
52+
body: response.body,
53+
};
54+
}
55+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from "aws-lambda";
2+
import {
3+
StreamableHttpHandler,
4+
ParsedHttpRequest,
5+
HttpResponse,
6+
} from "./streamable_http_handler.js";
7+
import { RequestHandler } from "./request_handler.js";
8+
9+
/**
10+
* Handler for API Gateway V2 events (HTTP APIs)
11+
*
12+
* This handler processes APIGatewayProxyEventV2 events and returns APIGatewayProxyResultV2 responses.
13+
*
14+
* This class handles all the generic JSON-RPC protocol aspects of the MCP Streamable HTTP transport:
15+
* - HTTP method validation (POST, OPTIONS, GET)
16+
* - Content-Type and Accept header validation
17+
* - JSON parsing and validation
18+
* - Batch request handling
19+
* - CORS headers
20+
* - Error response formatting
21+
* This class does not implement session management.
22+
*
23+
* The specific business logic is delegated to a provided RequestHandler implementation.
24+
*/
25+
export class APIGatewayProxyEventV2Handler extends StreamableHttpHandler<
26+
APIGatewayProxyEventV2,
27+
APIGatewayProxyResultV2
28+
> {
29+
constructor(requestHandler: RequestHandler) {
30+
super(requestHandler);
31+
}
32+
33+
/**
34+
* Parse APIGatewayProxyEventV2 into common HTTP request format
35+
*/
36+
protected parseEvent(event: APIGatewayProxyEventV2): ParsedHttpRequest {
37+
return {
38+
method: event.requestContext.http.method,
39+
headers: event.headers || {},
40+
body: event.body || null,
41+
};
42+
}
43+
44+
/**
45+
* Format HTTP response as APIGatewayProxyResultV2
46+
*/
47+
protected formatResponse(response: HttpResponse): APIGatewayProxyResultV2 {
48+
return {
49+
statusCode: response.statusCode,
50+
headers: response.headers,
51+
body: response.body,
52+
};
53+
}
54+
}

0 commit comments

Comments
 (0)