Skip to content

Commit 338505f

Browse files
committed
feat: Typescript-based cat-facts MCP server accessible behind Lambda function URL using streamable HTTP transport and Sigv4
1 parent 901b1a0 commit 338505f

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

examples/servers/cat-facts/README.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
Sample inputs:
1+
# Cat Facts MCP Server
22

3-
```bash
4-
$ npm run build
5-
$ export LOG_LEVEL=debug
6-
7-
# Initialize
8-
$ node -e 'require("./lib/cat-facts-mcp-server.function.js").handler({"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"roots":{"listChanged":true}},"clientInfo":{"name":"mcp","version":"0.1.0"}},"jsonrpc":"2.0","id":0}, "")'
9-
10-
# List tools
11-
$ node -e 'require("./lib/cat-facts-mcp-server.function.js").handler({"method":"tools/list","params":{"clientInfo":{"name":"mcp","version":"0.1.0"}},"jsonrpc":"2.0","id":0}, "")'
12-
13-
# Get a random cat fact
14-
$ node -e 'require("./lib/cat-facts-mcp-server.function.js").handler({"method":"tools/call","params":{"name":"getRandomFact","arguments":{}},"jsonrpc":"2.0","id":0}, "")'
15-
```
3+
This example demonstrates an MCP server that provides cat facts through a Lambda Function URL and IAM authentication.
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { Handler, Context } from "aws-lambda";
1+
import {
2+
Handler,
3+
Context,
4+
APIGatewayProxyEventV2WithIAMAuthorizer,
5+
APIGatewayProxyResultV2,
6+
} from "aws-lambda";
7+
import {
8+
LambdaFunctionURLEventHandler,
9+
StdioServerAdapterRequestHandler,
10+
} from "@aws/run-mcp-servers-with-aws-lambda";
211

312
const serverParams = {
413
command: "node",
@@ -11,11 +20,16 @@ const serverParams = {
1120
],
1221
};
1322

14-
export const handler: Handler = async (event, context: Context) => {
15-
// Dynamically import ES module into CommonJS Lambda function
16-
const { stdioServerAdapter } = await import(
17-
"@aws/run-mcp-servers-with-aws-lambda"
18-
);
23+
const requestHandler = new LambdaFunctionURLEventHandler(
24+
new StdioServerAdapterRequestHandler(serverParams)
25+
);
1926

20-
return await stdioServerAdapter(serverParams, event, context);
27+
export const handler: Handler = async (
28+
event: APIGatewayProxyEventV2WithIAMAuthorizer,
29+
context: Context
30+
): Promise<APIGatewayProxyResultV2> => {
31+
// To customize the handler based on the caller's identity, you can use:
32+
// event.requestContext.authorizer.iam
33+
34+
return requestHandler.handle(event, context);
2135
};

examples/servers/cat-facts/lib/cat-facts-mcp-server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as cdk from "aws-cdk-lib";
22
import { Construct } from "constructs";
3-
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
3+
import { NodejsFunction, OutputFormat } from "aws-cdk-lib/aws-lambda-nodejs";
44
import {
55
Code,
66
LayerVersion,
@@ -59,6 +59,8 @@ export class CatFactsMcpServer extends cdk.Stack {
5959
},
6060
layers: [mcpLambdaLayer],
6161
bundling: {
62+
format: OutputFormat.ESM,
63+
mainFields: ["module", "main"],
6264
nodeModules: ["@ivotoby/openapi-mcp-server"],
6365
// For testing, the @aws/run-mcp-servers-with-aws-lambda package is bundled from local files using the Lambda layer above.
6466
// Remove the layer and this externalModules configuration if using the @aws/run-mcp-servers-with-aws-lambda package from npm.

0 commit comments

Comments
 (0)