

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

# 設定 AWS MCP 伺服器
<a name="aws-mcp-server"></a>

 AWS MCP 伺服器是 Agent Toolkit for 的受管模型內容通訊協定伺服器 AWS。它可讓您的 AI 代理程式驗證存取 AWS - 代表您執行 AWS API 呼叫、在沙盒環境中執行指令碼，以及隨需提供 AWS 技能 （包括 [amazon-opensearch-service 技能](amazon-opensearch-service-skill.md))。使用它來從您的代理程式使用 OpenSearch Service 網域和 OpenSearch Serverless 集合。

**注意**  
 AWS MCP Server 會連線至公有 Amazon OpenSearch Service 端點。它不支援部署在 VPC 中的網域或集合。如果您的 OpenSearch Service 網域或 OpenSearch Serverless 集合使用 VPC 存取，請改用開放原始碼 [opensearch-mcp-server-py](https://github.com/opensearch-project/opensearch-mcp-server-py)，其支援透過本機組態的 VPC 連線。

## 先決條件
<a name="aws-mcp-server-prerequisites"></a>
+ 支援 Agent Toolkit for AWS （例如 Kiro、Claude Code、Cursor 和 Codex) 的 AI 編碼代理程式。
+ AWS 在本機設定的登入資料 （例如 CLI AWS 設定檔或環境登入資料），具有您要執行之 OpenSearch 操作的許可。對於網域，請參閱 [身分型政策](ac.md#ac-types-identity)；對於無伺服器，請參閱 [Amazon OpenSearch Serverless 的資料存取控制](serverless-data-access.md)。

## 安裝 aws-data-analytics 外掛程式 （建議）
<a name="aws-mcp-server-install-plugin"></a>

`aws-data-analytics` 外掛程式在單一安裝中綁定 AWS MCP Server 組態和`amazon-opensearch-service`技能，因此您不需要設定 MCP 端點或單獨安裝技能。

**Claude 程式碼**

```
/plugin install aws-data-analytics@claude-plugins-official
/reload-plugins
```

**Codex**

```
codex plugin marketplace add aws/agent-toolkit-for-aws
```

然後啟動 Codex 並執行 `/plugins`以瀏覽和安裝`aws-data-analytics`外掛程式。

## 直接設定 AWS MCP 伺服器
<a name="aws-mcp-server-configure-directly"></a>

如果您的代理程式不支援外掛程式，請直接設定 AWS MCP 伺服器。遵循 代理程式工具組 AWS 文件中的[設定 AWS MCP 伺服器](https://docs.aws.amazon.com/agent-toolkit/latest/userguide/getting-started-aws-mcp-server.html)，然後向您的代理程式詢問 OpenSearch 問題 - 透過伺服器在執行時間探索`amazon-opensearch-service`技能。

## 在沒有技能的情況下使用 AWS MCP 伺服器
<a name="aws-mcp-server-without-skills"></a>

您可以直接使用 AWS MCP 伺服器，無需載入`amazon-opensearch-service`技能。在此模式中，代理程式會使用伺服器的內建`call_aws`工具直接叫用 AWS API 操作。這對於臨機操作或當您想要精確控制代理程式進行的 API 呼叫時非常有用。

例如，若要建立 OpenSearch Serverless 集合：

```
Create an OpenSearch Serverless search collection named "my-collection" in us-east-1
```

代理程式會使用您設定的登入資料代表您呼叫 OpenSearch Serverless `CreateCollection` API：

```
{
  "name": "my-collection",
  "type": "SEARCH",
  "description": "My search collection"
}
```

您也可以針對現有資源執行操作，無需技能。例如：

```
List all my OpenSearch Service domains in us-east-1
Show the cluster health for my domain named production-domain
Create an index called products in my collection endpoint
```

 AWS MCP 伺服器會處理身分驗證，並將您的自然語言請求轉譯為適當的 AWS API 或 OpenSearch API 呼叫。

## 驗證設定
<a name="aws-mcp-server-verify"></a>

安裝之後，請重新啟動您的代理程式，以便載入新的組態。然後詢問：

```
What AWS skills do you have available for OpenSearch?
```

客服人員應報告`amazon-opensearch-service`技能。嘗試任務，例如*「列出我的 OpenSearch Service 網域」* – 代理程式會使用您設定的登入資料透過 AWS MCP 伺服器執行呼叫。

## 安全考量
<a name="aws-mcp-server-security"></a>

 AWS MCP Server 會使用您提供的登入資料執行。請遵循下列實務：
+ **使用最低權限憑證。**將專用 IAM 主體範圍限定為客服人員所需的 OpenSearch 資源和動作。避免管理員登入資料。
+ **分開開發和生產。**將伺服器指向非生產資源進行探索，並在生產變更之前進行確認。
+ **保護登入資料。**偏好透過靜態存取金鑰的 IAM 角色和 AWS CLI 設定檔，且絕不會將秘密遞交至來源控制中的組態檔案。
+ **檢閱工具輸出。**MCP 工具回應會傳回模型做為內容。避免針對含有您不希望向模型提供者公開之敏感資料的索引執行伺服器。

## 疑難排解
<a name="aws-mcp-server-troubleshooting"></a>
+ **客服人員看不到任何 OpenSearch 技能或工具** – 確認外掛程式或 MCP 組態有效，並完全重新啟動您的客服人員。大多數代理程式只會在啟動時載入 MCP 伺服器。
+ **呼叫傳回 403 禁止** – 您的登入資料缺少呼叫 API 的許可。對於網域，請檢閱主體上的網域存取政策和 IAM 政策。對於無伺服器，請檢閱集合的資料存取政策。

## 搭配代理程式架構使用
<a name="aws-mcp-server-frameworks"></a>

您可以將 AWS MCP 伺服器整合到程式設計工作流程的 Python 代理程式架構中。下列範例會連線至 AWS MCP Server 端點，該端點提供對 OpenSearch 和其他 AWS 服務的已驗證存取權。

### Strands 代理程式
<a name="aws-mcp-server-strands"></a>

[Strands Agents](https://strandsagents.com) 是 AWS原生代理程式 SDK，具有內建的 MCP 支援，並以 Amazon Bedrock 作為預設模型提供者。

```
from strands import Agent
from strands.tools.mcp import MCPClient
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client

ENDPOINT = "https://aws-mcp.us-east-1.api.aws/mcp"
AWS_REGION = "us-east-1"

opensearch_client = MCPClient(
    lambda: aws_iam_streamablehttp_client(
        endpoint=ENDPOINT,
        aws_service="aws-mcp",
        aws_region=AWS_REGION,
    )
)

with opensearch_client:
    agent = Agent(tools=opensearch_client.list_tools_sync())
    response = agent("What OpenSearch Service domains do I have?")
    print(response)
```

安裝必要的套件：

```
pip install strands-agents mcp-proxy-for-aws
```

### LangGraph
<a name="aws-mcp-server-langgraph"></a>

[LangGraph](https://github.com/langchain-ai/langgraph) 是用於建置具狀態代理程式的低階協同運作架構。下列範例使用 `mcp-proxy-for-aws`提供已驗證的傳輸`langchain-mcp-adapters`，並將 AWS MCP 工具載入 Amazon Bedrock 支援的 LangChain 代理程式。

```
import asyncio
from mcp import ClientSession
from mcp_proxy_for_aws.client import aws_iam_streamablehttp_client
from langchain_mcp_adapters.tools import load_mcp_tools
from langchain_aws import ChatBedrock
from langchain.agents import create_agent

ENDPOINT = "https://aws-mcp.us-east-1.api.aws/mcp"
AWS_REGION = "us-east-1"

async def main():
    async with aws_iam_streamablehttp_client(
        endpoint=ENDPOINT,
        aws_service="aws-mcp",
        aws_region=AWS_REGION,
    ) as (read_stream, write_stream, _get_session_id):
        async with ClientSession(read_stream, write_stream) as session:
            await session.initialize()
            tools = await load_mcp_tools(session)
            model = ChatBedrock(
                model_id="us.anthropic.claude-opus-4-8",
                region_name=AWS_REGION,
            )
            agent = create_agent(model, tools)
            result = await agent.ainvoke(
                {"messages": [{"role": "user", "content": "What OpenSearch Service domains do I have?"}]}
            )
            print(result["messages"][-1].content)

asyncio.run(main())
```

安裝必要的套件：

```
pip install mcp-proxy-for-aws langchain langchain-aws langchain-mcp-adapters langgraph
```

## 其他資訊
<a name="aws-mcp-server-more-information"></a>
+ [適用於 AWS 元件的 Agent Toolkit](https://docs.aws.amazon.com/agent-toolkit/latest/userguide/components.html)