

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# MCP 服务器集成模板
<a name="cfn-template-mcp-server"></a>

使用模型上下文协议 (MCP) 服务器模板，您可以在 Amazon Bedrock 上部署 OpenSearch 托管的 MCP 服务器 AgentCore，从而降低 AI 代理和工具之间的集成复杂性。 OpenSearch 有关更多信息，请参阅[什么是 Amazon Bedrock AgentCore](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/what-is-bedrock-agentcore.html)？ 。

## 模板功能
<a name="template-mcp-server-features"></a>

此模板包含以下用于部署和管理 MCP 服务器的关键功能。

**托管的 MCP 服务器部署**  
**opensearch-mcp-server-py**使用 Amazon Bedrock AgentCore Runtime 进行部署，并提供代理终端节点，用于代理对底层 MCP 服务器的请求。有关更多信息，请参阅[opensearch-mcp-server-py](https://github.com/opensearch-project/opensearch-mcp-server-py)上的*GitHub*。

**身份验证和安全**  
处理入站身份验证（从用户到 MCP 服务器）和出站身份验证（从 MCP 服务器到 OpenSearch），并 OAuth 支持企业身份验证。

**注意**  
MCP 服务器模板仅在以下 AWS 区域中可用：  
美国东部（弗吉尼亚州北部）
美国西部（俄勒冈州）
欧洲地区（法兰克福）
亚太地区（悉尼）

## 使用 MCP 服务器模板
<a name="template-mcp-server-procedure"></a>

按照以下步骤部署 MCP 服务器模板并将其连接到您的 OpenSearch 域。

1. 打开[亚马逊 OpenSearch 服务控制台](https://console.aws.amazon.com//aos/home )。

1. 在左侧导航窗格中，选择**集成**。

1. 查找 **MCP 服务器集成**模板。

1. 选择**配置域**。然后，输入您的 OpenSearch 域终端节点。

如果未指定相应的可选参数，则模板将创建 AgentCore 运行时和以下组件：
+ Amazon ECR 存储库
+ Amazon Cognito 用户池作为授权方 OAuth 
+  AgentCore 运行时使用的执行角色

完成此流程后，请执行以下创建后步骤：

1. **对于 Amazon OpenSearch 服务**：将您的执行角色 ARN 映射到 OpenSearch 后端角色以控制对您的域的访问。

   **对 OpenSearch 于 Amazon Serverless**：创建数据访问策略，允许您的执行角色访问您的馆藏。

1. 从您的授权者那里获取 OAuth 访问令牌。然后使用此令牌通过 CloudFormation 堆栈输出中列出的 URL 访问 MCP 服务器。

有关更多信息，请参阅 [OpenSearch 无服务器的策略操作](security-iam-serverless.md#security-iam-serverless-id-based-policies-actions)。

## 与人工智能代理集成
<a name="cfn-template-mcp-agent-integrations"></a>

部署后，您可以将 MCP 服务器与任何兼容 MCP 的代理进行集成。有关更多信息，请参阅《Amazon Bedrock 开发人员指南》**中的 [Invoke your deployed MCP server](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-mcp.html#runtime-mcp-invoke-server)。

**开发人员集成**  
可将 MCP 服务器端点添加到代理配置中。也可以将其与 Amazon Q 开发者版 CLI、自定义代理或其他兼容 MCP 的代理结合使用。

**企业部署**  
中央托管的代理可以 OpenSearch 作为一个组件连接到多个服务。该代理支持 OAuth 和企业身份验证系统，并可扩展以支持多个用户和用例。

### 使用 Strands Agents 框架的示例
<a name="strands-agent-integration-id"></a>

```
import os
import requests
from strands import Agent
from strands.tools.mcp import MCPClient
from mcp.client.streamable_http import streamablehttp_client

def get_bearer_token(discovery_url: str, client_id: str, client_secret: str):
    response = requests.get(discovery_url)
    discovery_data = response.json()
    token_endpoint = discovery_data['token_endpoint']

    data = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }

    response = requests.post(token_endpoint, data=data, headers=headers)
    token_data = response.json()
    return token_data['access_token']

if __name__ == "__main__":
    discovery_url = os.environ["DISCOVERY_URL"]
    client_id = os.environ["CLIENT_ID"]
    client_secret = os.environ["CLIENT_SECRET"]
    mcp_url = os.environ["MCP_URL"]

    bearer_token = get_bearer_token(discovery_url, client_id, client_secret)

    opensearch_mcp_client = MCPClient(lambda: streamablehttp_client(mcp_url, {
        "authorization": f"Bearer {bearer_token}",
        "Content-Type": "application/json"
    }))

    with opensearch_mcp_client:
        tools = opensearch_mcp_client.list_tools_sync()
        agent = Agent(tools=tools)
        agent("list indices")
```

*有关更多信息，请参阅网站上的 “[使用 Amazon Bedro OpenSearch ck AgentCore 托管 MCP 服务器](https://opensearch.org/blog/hosting-opensearch-mcp-server-with-amazon-bedrock-agentcore/)” OpenSearch 。*