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

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 +211 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,211 @@
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
+ - This package currently requires using a custom MCP client transport to communicate with the MCP
42
+ server by invoking the Lambda function. Existing applications with MCP support such as
43
+ Amazon Q Developer CLI, Cline, etc do not have this custom transport, and cannot communicate with
44
+ MCP servers adapted into Lambda functions.
45
+ Note: with [upcoming changes to the MCP protocol](https://github.com/modelcontextprotocol/specification/pull/206),
46
+ we expect that this limitation will be removed in the future.
47
+ - This package currently supports MCP servers and clients written in Python and Typescript.
48
+ Other languages such as Kotlin are not supported.
49
+ - The server adapters only adapt stdio MCP servers, not servers written for other protocols such as SSE.
50
+ - The server adapters do not maintain any MCP server state across Lambda function invocations.
51
+ Only stateless MCP servers are a good fit for using this adapter. For example, MCP servers
52
+ that invoke stateless tools like the [time MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/time)
53
+ or make stateless web requests like the [fetch MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/fetch).
54
+ Stateful MCP servers are not a good fit, because they will lose their state on every request.
55
+ For example, MCP servers that manage data on disk or in memory such as
56
+ the [sqlite MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/sqlite),
57
+ the [filesystem MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem),
58
+ and the [git MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/git).
59
+ - The server adapters ignore any MCP protocol notifications from the client to the server.
60
+ - The server adapters do not provide mechanisms for managing any secrets needed by the wrapped
61
+ MCP server. For example, the [GitHub MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/github)
62
+ and the [Brave search MCP server](https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search)
63
+ require API keys to make requests to third-party APIs.
64
+ You can configure these API keys as
65
+ [encrypted environment variables](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars-encryption.html)
66
+ in the Lambda function's configuration. However, note that anyone with access to invoke the Lambda function
67
+ will then have access to use your API key to call the third-party APIs by invoking the function.
68
+ We recommend limiting access to the Lambda function using
69
+ [least-privilege IAM policies](https://docs.aws.amazon.com/lambda/latest/dg/security-iam.html).
70
+
71
+ ## Examples
72
+
73
+ ### Python server example
74
+
75
+ This project includes an
76
+ [example Python Lambda function](examples/servers/time/function/index.py)
77
+ that runs the simple
78
+ [MCP 'time' reference server](https://github.com/modelcontextprotocol/servers/tree/main/src/time).
79
+ The Lambda function bundles the [mcp-server-time package](https://pypi.org/project/mcp-server-time/).
80
+ On each function invocation, the Lambda function will manage the lifecycle of the bundled MCP server.
81
+ It will:
82
+
83
+ 1. start the 'time' MCP server as a child process
84
+ 1. initialize the MCP server
85
+ 1. forward the incoming request to the local server
86
+ 1. return the server's response to the function caller
87
+ 1. shut down the MCP server child process
88
+
89
+ ```python
90
+ import sys
91
+ from mcp.client.stdio import StdioServerParameters
92
+ from mcp_lambda import stdio_server_adapter
93
+
94
+ server_params = StdioServerParameters(
95
+ command=sys.executable,
96
+ args=[
97
+ "-m",
98
+ "mcp_server_time",
99
+ "--local-timezone",
100
+ "America/New_York",
101
+ ],
102
+ )
103
+
104
+
105
+ def handler(event, context):
106
+ return stdio_server_adapter(server_params, event, context)
107
+ ```
108
+
109
+ ### Typescript server example
110
+
111
+ This project includes an
112
+ [example Node.js Lambda function](examples/servers/weather-alerts/lib/weather-alerts-mcp-server.function.ts)
113
+ that runs an [OpenAPI MCP server](https://github.com/snaggle-ai/openapi-mcp-server/)
114
+ to provide a single API from [weather.gov](https://www.weather.gov/documentation/services-web-api) as a tool.
115
+ The Lambda function bundles the [openapi-mcp-server package](https://www.npmjs.com/package/openapi-mcp-server).
116
+ On each function invocation, the Lambda function will manage the lifecycle of the bundled MCP server.
117
+ It will:
118
+
119
+ 1. start the 'openapi-mcp-server' MCP server as a child process
120
+ 1. initialize the MCP server
121
+ 1. forward the incoming request to the local server
122
+ 1. return the server's response to the function caller
123
+ 1. shut down the MCP server child process
124
+
125
+ ```typescript
126
+ import { Handler, Context } from "aws-lambda";
127
+
128
+ const serverParams = {
129
+ command: "npx",
130
+ args: ["--offline", "openapi-mcp-server", "./weather-alerts-openapi.json"],
131
+ };
132
+
133
+ export const handler: Handler = async (event, context: Context) => {
134
+ // Dynamically import ES module into CommonJS Lambda function
135
+ const { stdioServerAdapter } = await import(
136
+ "@aws/run-mcp-servers-with-aws-lambda"
137
+ );
138
+
139
+ return await stdioServerAdapter(serverParams, event, context);
140
+ };
141
+ ```
142
+
143
+ ### Python client example
144
+
145
+ This project includes an
146
+ [example Python MCP client](examples/chatbots/python/server_clients/lambda_function.py)
147
+ that invokes the 'time' MCP server function from above.
148
+ The client invokes a Lambda function named "mcp-server-time" with a payload that is compliant
149
+ with the MCP protocol and returns the function's response to the caller.
150
+
151
+ ```python
152
+ from mcp import ClientSession
153
+ from mcp_lambda import LambdaFunctionParameters, lambda_function_client
154
+
155
+ server_params = LambdaFunctionParameters(
156
+ function_name="mcp-server-time",
157
+ region_name="us-east-2",
158
+ )
159
+
160
+ read, write = await lambda_function_client(server_params)
161
+ session = ClientSession(read, write)
162
+ await session.initialize()
163
+ ```
164
+
165
+ ### Typescript client example
166
+
167
+ This project includes an
168
+ [example Typescript MCP client](examples/chatbots/typescript/src/server_clients/lambda_function.ts)
169
+ that invokes the 'time' MCP server function from above.
170
+ The client invokes a Lambda function named "mcp-server-time" with a payload that is compliant
171
+ with the MCP protocol and returns the function's response to the caller.
172
+
173
+ ```typescript
174
+ import {
175
+ LambdaFunctionParameters,
176
+ LambdaFunctionClientTransport,
177
+ } from "@aws/run-mcp-servers-with-aws-lambda";
178
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
179
+
180
+ const serverParams: LambdaFunctionParameters = {
181
+ functionName: "mcp-server-time",
182
+ regionName: "us-east-2",
183
+ };
184
+
185
+ const client = new Client(
186
+ {
187
+ name: "my-client",
188
+ version: "0.0.1",
189
+ },
190
+ {
191
+ capabilities: {
192
+ sampling: {},
193
+ },
194
+ }
195
+ );
196
+
197
+ const transport = new LambdaFunctionClientTransport(serverParams);
198
+ await client.connect(transport);
199
+ ```
200
+
201
+ ### Deploy and run the examples
202
+
203
+ See the [development guide](DEVELOP.md) for instructions to deploy and run the examples in this repository.
204
+
205
+ ## Security
206
+
207
+ See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.
208
+
209
+ ## License
210
+
211
+ 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.1",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
7
7
  "author": {