@aws/run-mcp-servers-with-aws-lambda 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +213 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,213 @@
1
+ # Run Model Context Protocol (MCP) servers with AWS Lambda
2
+
3
+ This project enables you to run [Model Context Protocol](https://modelcontextprotocol.io) stdio-based servers in AWS Lambda functions.
4
+
5
+ Currently, most implementations of MCP servers and clients are entirely local on a single machine.
6
+ A desktop application such as an IDE or Claude Desktop initiates MCP servers locally as child processes
7
+ and communicates with each of those servers over a long-running stdio stream.
8
+
9
+ ```mermaid
10
+ flowchart LR
11
+ subgraph "Your Laptop"
12
+ Host["Desktop Application<br>with MCP Clients"]
13
+ S1["MCP Server A<br>(child process)"]
14
+ S2["MCP Server B<br>(child process)"]
15
+ Host <-->|"MCP Protocol<br>(over stdio stream)"| S1
16
+ Host <-->|"MCP Protocol<br>(over stdio stream)"| S2
17
+ end
18
+ ```
19
+
20
+ This MCP server adapter for AWS Lambda helps you to wrap existing stdio MCP servers into Lambda functions.
21
+ You can invoke these function-based MCP servers from your application using the MCP protocol
22
+ over short-lived connections.
23
+ Your application can then be a desktop-based app, a distributed system running in the cloud,
24
+ or any other architecture.
25
+ Your application must have access to invoke your Lambda functions,
26
+ and use the custom MCP client transport that invokes the Lambda functions.
27
+
28
+ ```mermaid
29
+ flowchart LR
30
+ subgraph "Distributed System"
31
+ App["Your Application<br>with MCP Clients"]
32
+ S3["MCP Server A<br>(Lambda function)"]
33
+ S4["MCP Server B<br>(Lambda function)"]
34
+ App <-->|"MCP Protocol<br>with custom transport<br>(invoke function)"| S3
35
+ App <-->|"MCP Protocol<br>with custom transport<br>(invoke function)"| S4
36
+ end
37
+ ```
38
+
39
+ ## Considerations
40
+
41
+ - If you are looking for a way to invoke existing Lambda functions as tools through MCP,
42
+ see the [AWS Lambda MCP Server project](https://awslabs.github.io/mcp/servers/lambda-mcp-server/).
43
+ - This package currently requires using a custom MCP client transport to communicate with the MCP
44
+ server by invoking the Lambda function. Existing applications with MCP support such as
45
+ Amazon Q Developer CLI, Cline, etc do not have this custom transport, and cannot communicate with
46
+ MCP servers adapted into Lambda functions.
47
+ Note: with [upcoming changes to the MCP protocol](https://github.com/modelcontextprotocol/specification/pull/206),
48
+ we expect that this limitation will be removed in the future.
49
+ - This package currently supports MCP servers and clients written in Python and Typescript.
50
+ Other languages such as Kotlin are not supported.
51
+ - The server adapters only adapt stdio MCP servers, not servers written for other protocols such as SSE.
52
+ - The server adapters do not maintain any MCP server state across Lambda function invocations.
53
+ Only stateless MCP servers are a good fit for using this adapter. For example, MCP servers
54
+ that invoke stateless tools like the [time MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/time)
55
+ or make stateless web requests like the [fetch MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/fetch).
56
+ Stateful MCP servers are not a good fit, because they will lose their state on every request.
57
+ For example, MCP servers that manage data on disk or in memory such as
58
+ the [sqlite MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/sqlite),
59
+ the [filesystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem),
60
+ and the [git MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/git).
61
+ - The server adapters ignore any MCP protocol notifications from the client to the server.
62
+ - The server adapters do not provide mechanisms for managing any secrets needed by the wrapped
63
+ MCP server. For example, the [GitHub MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/github)
64
+ and the [Brave search MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search)
65
+ require API keys to make requests to third-party APIs.
66
+ You can configure these API keys as
67
+ [encrypted environment variables](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars-encryption.html)
68
+ in the Lambda function's configuration. However, note that anyone with access to invoke the Lambda function
69
+ will then have access to use your API key to call the third-party APIs by invoking the function.
70
+ We recommend limiting access to the Lambda function using
71
+ [least-privilege IAM policies](https://docs.aws.amazon.com/lambda/latest/dg/security-iam.html).
72
+
73
+ ## Examples
74
+
75
+ ### Python server example
76
+
77
+ This project includes an
78
+ [example Python Lambda function](examples/servers/time/function/index.py)
79
+ that runs the simple
80
+ [MCP 'time' reference server](https://github.com/modelcontextprotocol/servers/tree/main/src/time).
81
+ The Lambda function bundles the [mcp-server-time package](https://pypi.org/project/mcp-server-time/).
82
+ On each function invocation, the Lambda function will manage the lifecycle of the bundled MCP server.
83
+ It will:
84
+
85
+ 1. start the 'time' MCP server as a child process
86
+ 1. initialize the MCP server
87
+ 1. forward the incoming request to the local server
88
+ 1. return the server's response to the function caller
89
+ 1. shut down the MCP server child process
90
+
91
+ ```python
92
+ import sys
93
+ from mcp.client.stdio import StdioServerParameters
94
+ from mcp_lambda import stdio_server_adapter
95
+
96
+ server_params = StdioServerParameters(
97
+ command=sys.executable,
98
+ args=[
99
+ "-m",
100
+ "mcp_server_time",
101
+ "--local-timezone",
102
+ "America/New_York",
103
+ ],
104
+ )
105
+
106
+
107
+ def handler(event, context):
108
+ return stdio_server_adapter(server_params, event, context)
109
+ ```
110
+
111
+ ### Typescript server example
112
+
113
+ This project includes an
114
+ [example Node.js Lambda function](examples/servers/weather-alerts/lib/weather-alerts-mcp-server.function.ts)
115
+ that runs an [OpenAPI MCP server](https://github.com/snaggle-ai/openapi-mcp-server/)
116
+ to provide a single API from [weather.gov](https://www.weather.gov/documentation/services-web-api) as a tool.
117
+ The Lambda function bundles the [openapi-mcp-server package](https://www.npmjs.com/package/openapi-mcp-server).
118
+ On each function invocation, the Lambda function will manage the lifecycle of the bundled MCP server.
119
+ It will:
120
+
121
+ 1. start the 'openapi-mcp-server' MCP server as a child process
122
+ 1. initialize the MCP server
123
+ 1. forward the incoming request to the local server
124
+ 1. return the server's response to the function caller
125
+ 1. shut down the MCP server child process
126
+
127
+ ```typescript
128
+ import { Handler, Context } from "aws-lambda";
129
+
130
+ const serverParams = {
131
+ command: "npx",
132
+ args: ["--offline", "openapi-mcp-server", "./weather-alerts-openapi.json"],
133
+ };
134
+
135
+ export const handler: Handler = async (event, context: Context) => {
136
+ // Dynamically import ES module into CommonJS Lambda function
137
+ const { stdioServerAdapter } = await import(
138
+ "@aws/run-mcp-servers-with-aws-lambda"
139
+ );
140
+
141
+ return await stdioServerAdapter(serverParams, event, context);
142
+ };
143
+ ```
144
+
145
+ ### Python client example
146
+
147
+ This project includes an
148
+ [example Python MCP client](examples/chatbots/python/server_clients/lambda_function.py)
149
+ that invokes the 'time' MCP server function from above.
150
+ The client invokes a Lambda function named "mcp-server-time" with a payload that is compliant
151
+ with the MCP protocol and returns the function's response to the caller.
152
+
153
+ ```python
154
+ from mcp import ClientSession
155
+ from mcp_lambda import LambdaFunctionParameters, lambda_function_client
156
+
157
+ server_params = LambdaFunctionParameters(
158
+ function_name="mcp-server-time",
159
+ region_name="us-east-2",
160
+ )
161
+
162
+ read, write = await lambda_function_client(server_params)
163
+ session = ClientSession(read, write)
164
+ await session.initialize()
165
+ ```
166
+
167
+ ### Typescript client example
168
+
169
+ This project includes an
170
+ [example Typescript MCP client](examples/chatbots/typescript/src/server_clients/lambda_function.ts)
171
+ that invokes the 'time' MCP server function from above.
172
+ The client invokes a Lambda function named "mcp-server-time" with a payload that is compliant
173
+ with the MCP protocol and returns the function's response to the caller.
174
+
175
+ ```typescript
176
+ import {
177
+ LambdaFunctionParameters,
178
+ LambdaFunctionClientTransport,
179
+ } from "@aws/run-mcp-servers-with-aws-lambda";
180
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
181
+
182
+ const serverParams: LambdaFunctionParameters = {
183
+ functionName: "mcp-server-time",
184
+ regionName: "us-east-2",
185
+ };
186
+
187
+ const client = new Client(
188
+ {
189
+ name: "my-client",
190
+ version: "0.0.1",
191
+ },
192
+ {
193
+ capabilities: {
194
+ sampling: {},
195
+ },
196
+ }
197
+ );
198
+
199
+ const transport = new LambdaFunctionClientTransport(serverParams);
200
+ await client.connect(transport);
201
+ ```
202
+
203
+ ### Deploy and run the examples
204
+
205
+ See the [development guide](DEVELOP.md) for instructions to deploy and run the examples in this repository.
206
+
207
+ ## Security
208
+
209
+ See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
210
+
211
+ ## License
212
+
213
+ This project is licensed under the Apache-2.0 License.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aws/run-mcp-servers-with-aws-lambda",
3
3
  "description": "Run Model Context Protocol (MCP) servers with AWS Lambda",
4
- "version": "0.1.0",
4
+ "version": "0.1.2",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
7
7
  "author": {