内容
# 无服务器 MCP 框架



MCP Server 使用 AWS 无服务器服务的实现。
## 创建的资源
一组连接到 EventBridge 规则的 Lambda 函数,用于处理启动、停止和任务启动。创建 StepFunction 状态机以等待特定日期并发送事件。

## 安装
使用 [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html):
```bash
sam build
sam deploy --guided
```
## 参数
在部署此应用程序时需要一些参数:
- **EventBusName**: 连接集成的事件总线名称
- **Project**: 项目标识符
- **Environment**: 环境标识符
- **ServerName**: MCP 服务器名称
- **ServerVersion**: MCP 服务器版本
- **Instructions**: MCP 服务器说明
- **DomainName**: 端点的域名
- **AcmCertificateArn**: 提供的域名的证书 ARN
- **AuthenticationType**: MCP 服务器的认证类型
- **AuthenticationToken**: MCP 服务器的认证令牌
- **OAuthIssuerUrl**: OAuth 认证的发行者 URL
- **OAuthAuthorizationUrl**: OAuth 认证的授权 URL
- **OAuthTokenUrl**: OAuth 认证的令牌 URL
- **OAuthRevocationUrl**: OAuth 认证的撤销 URL
- **OAuthRedirectUris**: OAuth 认证的重定向 URI
## 输出
- **ServerEndpoint**: MCP 服务器端点
## 开发
要求:
- [NodeJS](https://nodejs.org/en/download) >= 22.x
安装 NPM 模块
```bash
npm install
```
使用 MCP 服务器规格配置 .env 文件:
```
SERVER_NAME=local
SERVER_VERSION=1.0.0
INSTRUCTIONS=""
```
使用 AWS 环境配置配置 .env 文件:
```
AWS_PROFILE=<你的 AWS 配置文件>
AWS_REGION=<选择的 AWS 区域>
SSM_PREFIX=
TOOLS_SSM_PREFIX=
RESOURCES_SSM_PREFIX=
PROMPTS_SSM_PREFIX=
TOOL_LAMBDA_PREFIX=
```
运行本地 MCP 服务器
```bash
npm run dev
```
MCP 服务器运行在 `http://localhost:3000/mcp`。
### 工具检查器
运行 [MCP Inspector](https://github.com/modelcontextprotocol/inspector)
```bash
npm run inspector
```
使用以下配置检查器:
- 传输类型: `Streamable HTTP`
- URL: `http://localhost:3000/mcp`
使用“连接”按钮连接到 MCP 服务器。
### Claude Desktop
使用 [mcp-remote](https://www.npmjs.com/package/mcp-remote) 将 HTTP 代理到 STDIO:
```json
{
"mcpServers": {
"example": {
"command": "npx",
"args": [
"-y",
"mcp-remote",
"https://xxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/dev/mcp",
"--transport",
"http-only"
]
}
}
}
```
## 部署
要求:
- [SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
- [NodeJS](https://nodejs.org/en/download) >= 22.x
构建项目
```bash
sam build
```
部署堆栈
```bash
sam deploy --profile <你的 AWS 配置文件> --guided
```
## 使用
一旦解决方案安装完成,您可以使用 SSM 参数和 Lambda 函数扩展 MCP 服务器的资源、提示或工具。
SSM 参数必须以前缀 `/<项目名称>/<环境名称>/` 开头,例如 `/mcp/dev/tools/echo`。
作为集成的示例,您可以参考模板:[template.integration.yaml](./template.integration.yaml)。
### 资源
SSM 参数必须以前缀 `/<项目名称>/<环境名称>/resources/` 开头,例如 `/mcp/dev/resources/example`。
内容必须是有效的 JSON 格式,并具有以下属性:
- name: 资源名称
- uri: 资源 URI,例如 "config://example"
- content: 资源文本内容
可以在 SAM 模板中声明为:
```yaml
ExampleResourceParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/${Project}/${Environment}/resources/example"
Type: String
Value: |
{
"name": "example",
"uri": "config://example",
"content": "Just an example resource"
}
```
### 提示
SSM 参数必须以前缀 `/<项目名称>/<环境名称>/prompts/` 开头,例如 `/mcp/dev/prompts/echo`。
内容必须是有效的 JSON 格式,并具有以下属性:
- name: 提示名称
- description: 提示描述
- inputSchema.json: [JSON schema](https://json-schema.org/)
- content: 提示模板内容
内容模板将使用 [ejs](https://ejs.co/) 进行输入数据插值。
可以在 SAM 模板中声明为:
```yaml
EchoPromptParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/${Project}/${Environment}/prompts/echo"
Type: String
Value: |
{
"name": "echo",
"description": "Execute the example tool",
"inputSchema": {
"json": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
}
},
"content": "Execute the tool 'echo' with the message '<%= message %>'"
}
```
### 工具
SSM 参数必须以前缀 `/<项目名称>/<环境名称>/tools/` 开头,例如 `/mcp/dev/tools/echo`。
内容必须是有效的 JSON 格式,并具有以下属性:
- name: 提示名称
- description: 提示描述
- inputSchema.json: [JSON schema](https://json-schema.org/)
相关的 Lambda 函数必须命名为 `<项目名称>-<环境名称>-tools-<工具名称>`,例如 `mcp-dev-tools-echo`。
函数的输入和输出必须遵循 [Converse API](https://docs.aws.amazon.com/bedrock/latest/userguide/tool-use-inference-call.html) 模式。
该参数可以在 SAM 模板中声明为:
```yaml
EchoToolParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub "/${Project}/${Environment}/tools/echo"
Type: String
Value: |
{
"name": "echo",
"description": "Print the message provided in input",
"inputSchema": {
"json": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
}
}
}
```
Lambda 函数的实现可以是:
```js
exports.handler = async ({ toolUseId, name, input }, context) => {
return {
name,
toolUseId,
status: 'success',
content: [{
text: input.message
}]
}
}
```
在 SAM 模板中内联声明:
```yaml
EchoToolFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "${Project}-${Environment}-tools-echo"
Handler: index.handler
InlineCode: |
exports.handler = async ({ toolUseId, name, input }, context) => {
return {
name,
toolUseId,
status: 'success',
content: [{
text: input.message
}]
}
}
```
## 贡献
- 仓库徽章由 [Shields.io](https://shields.io/) 提供
- 基础设施架构图由 [Cloudcraft](https://www.cloudcraft.co/) 提供