

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# MCP 伺服器整合範本
<a name="cfn-template-mcp-server"></a>

使用模型內容通訊協定 (MCP) 伺服器範本，您可以在 Amazon Bedrock AgentCore 上部署 OpenSearch 託管 MCP 伺服器，從而降低 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 伺服器部署**  
使用 Amazon Bedrock AgentCore 執行期部署 **opensearch-mcp-server-py**，並提供代理程式端點，將請求代理到基礎 MCP 伺服器。如需詳細資訊，請參閱 *GitHub* 上的 [opensearch-mcp-server-py](https://github.com/opensearch-project/opensearch-mcp-server-py)。

**身分驗證和安全性**  
同時處理傳入身分驗證 （從使用者到 MCP 伺服器） 和傳出身分驗證 （從 MCP 伺服器到 OpenSearch)，並支援企業身分驗證的 OAuth。

**注意**  
MCP 伺服器範本僅適用於下列 AWS 區域：  
美國東部 (維吉尼亞北部)
美國西部 (奧勒岡)
歐洲 (法蘭克福)
亞太地區 (悉尼)

## 使用 MCP 伺服器範本
<a name="template-mcp-server-procedure"></a>

請依照下列步驟部署 MCP 伺服器範本，並將其連接到您的 OpenSearch 網域。

1. 開啟 [Amazon OpenSearch Service 主控台](https://console.aws.amazon.com//aos/home )。

1. 在左側導覽窗格中，選擇**整合**。

1. 找到 **MCP 伺服器整合**範本。

1. 選擇**設定網域**。然後，輸入您的 OpenSearch 網域端點。

如果未指定對應的選用參數，範本會建立 AgentCore 執行期和下列元件：
+ Amazon ECR 儲存庫
+ 做為 OAuth 授權方的 Amazon Cognito 使用者集區
+ AgentCore 執行期使用的執行角色

完成此程序後，您應該遵循這些建立後步驟：

1. **對於 Amazon OpenSearch Service**：將您的執行角色 ARN 映射至 OpenSearch 後端角色，以控制對網域的存取。

   **對於 Amazon OpenSearch Serverless**：建立允許執行角色存取集合的資料存取政策。

1. 從您的授權方取得 OAuth 存取權杖。然後，使用此字符在 CloudFormation 堆疊輸出中列出的 URL 存取 MCP 伺服器。

如需詳細資訊，請參閱[OpenSearch Serverless 的政策動作](security-iam-serverless.md#security-iam-serverless-id-based-policies-actions)。

## 與 AI 代理器整合
<a name="cfn-template-mcp-agent-integrations"></a>

部署之後，您可以將 MCP 伺服器與任何 MCP 相容代理程式整合。如需詳細資訊，請參閱《*Amazon Bedrock 開發人員指南*》中的[叫用部署的 MCP 伺服器](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-mcp.html#runtime-mcp-invoke-server)。

**開發人員整合**  
您可以將 MCP 伺服器端點新增至您的代理程式組態。您也可以將其與 Amazon Q Developer 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")
```

如需詳細資訊，請參閱 [ OpenSearch 網站上的使用 Amazon Bedrock AgentCore 託管 OpenSearch MCP 伺服器](https://opensearch.org/blog/hosting-opensearch-mcp-server-with-amazon-bedrock-agentcore/)。 *OpenSearch *