

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

# 搭配 Kiro 和其他編碼助理使用 MCP 伺服器部署即時編碼安全驗證
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants"></a>

*Ivan Girardi 和 Iker Reina Fuente，Amazon Web Services*

## 摘要
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-summary"></a>

此模式說明如何實作模型內容通訊協定 (MCP) 伺服器，整合三種業界標準的安全性掃描工具，以提供全面的程式碼安全分析。伺服器可讓 AI 編碼助理 （例如 Kiro、Amazon Q Developer 和 Cline) 自動將程式碼片段和基礎設施掃描為程式碼 (IaC) 組態。透過這些掃描，編碼助理可協助識別安全漏洞、設定錯誤和違規。

在數百萬個程式碼片段上訓練的 AI 程式碼產生器會建立安全盲點，該訓練資料的安全性如何？ 此模式可在程式碼產生期間提供即時安全驗證，協助開發人員在程式碼過程中識別和了解潛在的安全問題。此方法可協助開發人員解決直接漏洞和來自相依性的繼承風險。透過彌補 AI 效率和安全合規之間的差距，此模式有助於安全採用 AI 驅動的開發工具。

此模式可協助組織透過 AI 輔助編碼工具增強其開發安全實務，提供跨多種程式設計語言和基礎設施定義的持續安全掃描功能。解決方案結合了下列工具的功能：
+ 用於掃描 IaC 檔案的 Checkov，包括 Terraform AWS CloudFormation和 Kubernetes 資訊清單
+ Semgrep 用於分析多種程式設計語言，例如 Python、JavaScript、Java 等
+ 專用 Python 安全性掃描的 Bandit 

此解決方案的主要功能包括下列項目：
+ Delta 掃描新的程式碼區段，降低運算開銷
+ 隔離的安全工具環境，防止跨工具污染
+ 與 AI 編碼助理 (Kiro、Amazon Q Developer、Cline 等） 無縫整合
+ 程式碼產生期間的即時安全意見回饋
+ 適用於組織合規的自訂掃描規則

模式為具有標準化回應格式的安全掃描提供統一界面，讓您更輕鬆地將安全檢查整合到開發工作流程中。模式使用 Python 和 MCP 架構來提供自動化安全意見回饋。此方法可協助開發人員在開發程序初期識別和解決安全問題，同時透過詳細的調查結果了解安全最佳實務。

## 先決條件和限制
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-prereqs"></a>

**先決條件**
+ 如果您想要使用其中一個編碼助理， AWS 帳戶 則具有使用 Kiro 或 Amazon Q Developer 之存取權的作用中
+ [已安裝](https://www.python.org/downloads/) Python 3.10 版或更新版本
+ `uv` 套件管理員[已安裝](https://docs.astral.sh/uv/getting-started/installation/)
+ 熟悉安全掃描工具和概念
+ 對 IaC 和應用程式安全性的基本了解

**限制**
+ Bandit 掃描僅限於 Python 檔案。
+ 即時掃描可能會影響大型程式碼基底的效能。
+ 工具特定的限制是以支援的檔案格式和語言為基礎。
+ 需要手動檢閱才能驗證安全性問題清單。
+ 安全掃描結果需要安全專業知識才能正確解譯。
+ 有些 AWS 服務 不適用於所有 AWS 區域。如需區域可用性，請參閱[AWS 依區域的服務](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。如需特定端點，請參閱[服務端點和配額](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)，然後選擇服務的連結。

**產品版本**
+ Python 3.10 版或更新版本
+ Checkov 3.0.0 版或更新版本
+ Semgrep 1.45.0 版或更新版本
+ Bandit 1.7.5 版或更新版本
+ MCP【cli】 1.11.0 版或更新版本
+ Pydantic 1.10.0 版或更新版本
+ Loguru 0.6.0 版或更新版本

## Architecture
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-architecture"></a>

下圖顯示此解決方案的架構。

![AI 助理會將程式碼傳送至 MCP 安全掃描器伺服器，以路由至專用掃描器；掃描結果會傳送至開發人員。](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/fa623544-4d54-48af-a4e4-9b6a0624e776/images/9c881f95-76d0-40f6-983e-d987fd2097b8.png)


該圖顯示以下工作流程：

1. 開發人員使用 AI 助理 （例如 Kiro、Cline、Amazon Q Developer 或 Roo Code) 來產生或分析程式碼。AI 助理會傳送程式碼以進行安全性掃描。

1. MCP 安全掃描器伺服器會透過將請求路由到適當的專用掃描器來處理請求：IaC 檔案的 Checkov、多個程式設計語言分析的 Semgrep，或 Python 特定安全性掃描的 Bandit。

1. 掃描器結果包含安全性調查結果、嚴重性等級、詳細說明和建議的修正，會透過 AI 助理傳回開發人員。

1. 建立持續的意見回饋迴圈，開發人員會在其中接收即時安全驗證，透過 AI 助理實現自動修正，並在開發期間提升安全最佳實務。

架構可降低下列常見的安全風險：
+ 命令注入
+ 提示注入
+ 路徑周遊
+ 相依性攻擊
+ 資源耗盡 

架構實作下列最佳實務，以降低這些常見的安全風險：
+ 所有使用者和 AI 模型輸入都會寫入暫存檔案。
+ 命令列界面 (CLI) 命令不會直接輸入。
+ 檔案系統存取僅限於暫時目錄和檔案。
+ 臨時檔案會自動清除。
+ 掃描回應已淨化。
+ 會強制執行限制程序功能的程序隔離。
+ 會記錄所有掃描活動。

**自動化和擴展**

模式透過下列功能支援自動化：
+ 與 AI 編碼助理整合，以進行自動程式碼掃描
+ 用於自動化處理的標準化 API 回應
+ 透過 MCP 組態檔案的組態
+ 支援批次處理多個檔案
+ 跨多種程式設計語言和 IaC 格式的可擴展掃描

掃描程序可以透過提供的 API 端點自動化：
+ `scan_with_checkov` 用於 IaC 掃描
+ `scan_with_semgrep` 適用於多語言程式碼掃描
+ `scan_with_bandit` 適用於 Python 特定掃描
+ `get_supported_formats` 用於格式驗證

擴充掃描工具時，請遵循本節先前所述的設計原則和最佳實務。另請參閱[最佳實務](#deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-best-practices)。

## 工具
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-tools"></a>

**AWS 服務**
+ [Kiro](https://aws.amazon.com/documentation-overview/kiro/) 是一種代理程式編碼服務，與開發人員一起將提示轉換為詳細規格，然後轉換為工作程式碼、文件和測試。Kiro 代理程式可協助開發人員解決具有挑戰性的問題，並自動化產生文件和單元測試等任務。
+ [Amazon Q Developer](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/what-is.html) 是採用生成式 AI 技術的對話式助理，可協助您了解、建置、擴展和操作 AWS 應用程式。

**其他工具**
+ [Bandit](https://bandit.readthedocs.io/en/latest/) 是一種專門的 Python 安全掃描器工具。它可偵測常見的 Python 安全問題，例如不安全的 函數、硬式編碼秘密和注入漏洞。Bandit 提供詳細的可信度和嚴重性評分。
+ [Checkov](https://github.com/bridgecrewio/checkov) 是一種靜態程式碼分析工具，可檢查 IaC 的安全性和合規性設定錯誤。此外，Checkov 會偵測合規違規和安全最佳實務。
+ [Cline](https://cline.bot/) 是採用 AI 技術的編碼助理，可在 VS Code 中執行。
+ [Loguru](https://loguru.readthedocs.io/en/stable/) 是 Python 的資料驗證程式庫。
+ [模型內容通訊協定 (MCP)](https://modelcontextprotocol.io/docs/getting-started/intro) 是用於建置 AI 輔助開發工具的開放原始碼架構。
+ [Pydantic](https://docs.pydantic.dev/latest/) 是 Python 的資料驗證程式庫。
+ [Semgrep](https://semgrep.dev/docs/introduction) 會分析原始程式碼是否有安全漏洞和錯誤。它支援多種程式設計語言。Semgrep 使用以安全為重心的規則集進行全面分析。它提供詳細的可信度和嚴重性評分。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [MCP 安全掃描器：即時保護 AI 程式碼助理](https://github.com/aws-samples/sample-mcp-security-scanner)儲存庫中取得。儲存庫包含 MCP 伺服器實作、Kiro、Amazon Q Developer、Cline 等 MCP 組態的詳細資訊、組態範例和測試公用程式。

儲存庫結構包括：
+ `security_scanner_mcp_server/` - 主要伺服器實作
+ `docs/` - 文件和示範資料
+ `tests/` - 測試檔案
+ `mcp-config-example.json` - 範例 MCP 組態
+ `requirements.txt` - 專案相依性

## 最佳實務
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-best-practices"></a>

**安全性掃描實作**
+ 檢閱安全性調查結果，以驗證問題並排定優先順序。
+ 保持掃描工具 (Checkov、Semgrep 和 Bandit) 更新至最新版本。
+ 將此模式的 MCP 安全工具與其他安全措施和工具搭配使用。
+ 定期更新安全規則集和政策。

**組態管理**
+ 在官方版本控制來源中使用 MCP 組態檔案。
+ 記錄自訂規則和組態。

**整合**
+ 在開發週期的早期整合安全性掃描。
+ 在預先遞交勾點或持續整合和持續部署 (CI/CD) 管道中設定自動掃描。
+ 為您的環境設定適當的嚴重性閾值。
+ 建立明確的程序來處理安全問題清單。

**營運考量事項**
+ 監控掃描效能和資源用量。
+ 實作適當的錯誤處理和記錄。
+ 維護自訂組態的文件。
+ 建立檢閱和更新安全規則的程序。

此外，請記住下列最佳實務：
+ 一律在特定內容中驗證安全調查結果。
+ 將安全工具和相依性保持在最新狀態。
+ 使用多個安全工具進行全面涵蓋。
+ 遵循開發程序中的安全最佳實務。

## 史詩
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-epics"></a>

### (Kiro 使用者） 設定 MCP 安全掃描器伺服器
<a name="kiro-users-set-up-the-mcp-security-scanner-server"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 MCP 設定。 | 您可以使用 Kiro IDE，透過 （選項 1) 手動尋找組態檔案或 （選項 2) 在 Kiro 中編輯組態檔案。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html)<pre>{<br />  "mcpServers": {<br />    "security-scanner": {<br />      "command": "uvx",<br />      "args": [<br />        "--from",<br />        "git+https://github.com/aws-samples/sample-mcp-security-scanner.git@main",<br />        "security_scanner_mcp_server"<br />      ],<br />      "env": {<br />        "FASTMCP_LOG_LEVEL": "ERROR"<br />      },<br />      "disabled": false,<br />      "autoApprove": []<br />    }<br />  }<br />}</pre> | 應用程式開發人員 | 

### (Amazon Q Developer 使用者） 設定 MCP 安全掃描器伺服器
<a name="qdevlong-users-set-up-the-mcp-security-scanner-server"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 MCP 設定。 | 若要手動設定 MCP 設定，請使用下列步驟：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html)<pre>{<br />  "mcpServers": {<br />    "security-scanner": {<br />      "command": "uvx",<br />      "args": [<br />        "--from",<br />        "git+https://github.com/aws-samples/sample-mcp-security-scanner.git@main",<br />        "security_scanner_mcp_server"<br />      ],<br />      "env": {<br />        "FASTMCP_LOG_LEVEL": "ERROR"<br />      }<br />    }<br />  }<br />}</pre> | 應用程式開發人員 | 

### (Cline 使用者） 設定 MCP 安全掃描器伺服器
<a name="cline-users-set-up-the-mcp-security-scanner-server"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 MCP 設定。 | 若要手動設定 MCP 設定，請使用下列步驟：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html)<pre>{<br />  "mcpServers": {<br />    "security-scanner": {<br />      "command": "uvx",<br />      "args": [<br />        "--from",<br />        "git+https://github.com/aws-samples/sample-mcp-security-scanner.git@main",<br />        "security_scanner_mcp_server"<br />      ],<br />      "env": {<br />        "FASTMCP_LOG_LEVEL": "ERROR"<br />      },<br />      "disabled": false,<br />      "autoApprove": []<br />    }<br />  }<br />}</pre> | 應用程式開發人員 | 

### 使用 Python 和 Bandit 的程式碼分析範例
<a name="example-of-code-analysis-using-python-and-bandit"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行程式碼分析。 | 若要使用 Python 和 Bandit 執行程式碼分析，請使用下列步驟：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 應用程式開發人員 | 

### 使用 Terraform 和 Checkov 的程式碼分析範例
<a name="example-of-code-analysis-using-terraform-and-checkov"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行程式碼分析。 | 若要使用 Terraform 和 Checkov 執行程式碼分析，請使用下列步驟：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 應用程式開發人員 | 

### 進階掃描功能的範例
<a name="example-of-advanced-scanning-capabilities"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 執行目標掃描。 | 以下是可用來執行目標掃描的請求範例：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 應用程式開發人員 | 
| 使用安全掃描與程式碼產生。 | 若要使用程式碼產生迴圈來解決安全性問題清單，請使用下列步驟 （此範例使用 Kiro 作為編碼助理）：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 應用程式開發人員 | 

## 疑難排解
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 環境設定問題 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 
| 掃描器問題 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 
| 整合問題 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 
| 其他支援 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants.html) | 

## 相關資源
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-resources"></a>

**AWS 文件**
+ [基礎設施即程式碼](https://docs.aws.amazon.com/whitepapers/latest/introduction-devops-aws/infrastructure-as-code.html) (AWS 白皮書*介紹 DevOps on AWS*)

**其他 AWS 資源**
+ [安全性、身分與合規的最佳實務](https://aws.amazon.com/architecture/security-identity-compliance/)

**其他資源**
+ [Bandit 文件](https://aws.amazon.com/architecture/security-identity-compliance/)
+ [Checkov 文件](https://aws.amazon.com/architecture/security-identity-compliance/)
+ [模型內容通訊協定 (MCP) 文件](https://aws.amazon.com/architecture/security-identity-compliance/)
+ [OWASP 安全編碼實務](https://owasp.org/www-project-secure-coding-practices-quick-reference-guide/) (OWASP Foundation 網站）
+ [Semgrep 文件](https://aws.amazon.com/architecture/security-identity-compliance/)

## 其他資訊
<a name="deploy-real-time-coding-security-validation-by-using-an-mcp-server-with-kiro-and-other-coding-assistants-additional"></a>

**啟用自動核准的範例 MCP 組態**

如果未`autoApprove`設定，使用者必須授予許可，才能將程式碼傳送至 MCP 安全伺服器進行掃描。設定 `autoApprove` 時，程式碼助理無需使用者核准即可叫用工具。這些工具會在機器本機執行，不會傳送任何資料，而且只會執行程式碼掃描。

下列組態可自動執行所有安全掃描函數：

```
{
  "mcpServers": {
    "security-scanner": {
      "command": "uvx",
      "args": [
        "--from",
        "git+https://github.com/aws-samples/sample-mcp-security-scanner.git@main",
        "security_scanner_mcp_server"
      ],
      "env": {
        "FASTMCP_LOG_LEVEL": "ERROR"
      },
      "disabled": false,
      "autoApprove": [
        "scan_with_checkov",
        "scan_with_semgrep", 
        "scan_with_bandit",
        "get_supported_formats"
      ]
    }
  }
}
```

若要啟用偵錯記錄，請將 `"FASTMCP_LOG_LEVEL"`設定為 `"DEBUG"`。

**安全性掃描工具支援的檔案格式**

此解決方案中的每個安全掃描工具都支援下列檔案格式：

*Checkov (IaC)*
+ Terraform – .tf、.tfvars、.tfstate
+ CloudFormation – .yaml、.yml、.json、.template
+ Kubernetes – .yaml、.yml
+ Dockerfile – Dockerfile
+ ARM – .json (Azure Resource Manager)
+ Bicep – .bicep
+ 無伺服器 – .yml、.yaml
+ Helm – .yaml、.yml、.tpl
+ GitHub 動作 – .yml、.yaml
+ GitLab\_ci – .yml、.yaml
+ Ansible – .yml、.yaml

*Semgrep （原始程式碼）*
+ Python – .py
+ JavaScript – .js
+ TypeScript – .ts
+ Java – .java
+ Go – .go
+ C – .c
+ C\+\+ – .cpp
+ C\# – .cs
+ Ruby – .rb
+ PHP – .php
+ Scala – .scala
+ Kotlin – .kt
+ Rust – .rs

*Bandit （僅限 Python)*
+ Python – .py

**示範**

針對程式碼掃描，請向 AI 助理嘗試下列範例提示：
+ 「掃描目前的指令碼並告訴我結果。」
+ 「掃描第 20-60 行並告訴我結果。」
+ 「掃描此 Amazon DynamoDB 資料表資源並告訴我結果。」

如需詳細資訊，請參閱此模式 GitHub 儲存庫中的此[程式碼掃描示範](https://github.com/aws-samples/sample-mcp-security-scanner/blob/main/docs/demo_code_scan.gif)。

若要產生安全程式碼，請嘗試下列範例提示：
+ 「產生 Terraform 組態以建立已啟用加密的 DynamoDB 資料表，並掃描是否有安全問題。」
+ 「建立寫入 DynamoDB 的 Python Lambda 函數，並掃描是否有漏洞。」
+ 「使用適當的安全性設定產生 S3 儲存貯體的 CloudFormation 範本，並確認它通過安全性檢查。」
+ 「撰寫 Python 指令碼以使用分頁查詢 DynamoDB，並掃描安全性最佳實務。」
+ 「為具有安全強化的微服務建立 Kubernetes 部署資訊清單並進行驗證。」

如需詳細資訊，請參閱此模式 GitHub 儲存庫中的此[程式碼產生與安全性掃描](https://github.com/aws-samples/sample-mcp-security-scanner/blob/main/docs/demo_code_generation.gif)示範。