跳到內容
AggregaAggregaAggregaAggrega
選擇語言: 繁體中文
本頁為機器翻譯。閱讀英文原文。

工具的位址來自 Secret——一個從自己的位址讀取金鑰的 Model Context Protocol 伺服器

某些由他人執行的 Model Context Protocol 伺服器,從它們自己的位址——https://mcp.example.com/mcp?api_key=<key>——讀取金鑰,而不是從請求標頭。這樣的位址無法像一般位址那樣寫進工具:工具會原樣發布它的位址,所以金鑰會到達位址所到達的每個檔案。

這個工作區把位址分成兩部分保存。工具 core/ai-tool/mcp 保存不含攜帶金鑰的那一部分的位址。代理程式連到工具的連線將整個位址當作機密保存,而 Release 指名它所在之處。它為 Kubernetes 與 Docker Compose 建置,而且在兩者上都沒有任何渲染出的檔案帶有金鑰。

代理程式宣告在這個工作區自己的目錄中,所以保存位址的插槽在這裡完整寫出。標準程式庫的代理程式執行環境 core/ai-agent/openai-agents-sdk、claude-agent-sdk、agent-framework、google-adk 與 mastra,以及平台 core/ai-agent/flowise 與 n8n,宣告具有相同兩個槽位的 tools 插槽。工具、兩個認證 Features 與它們的實現隨附於標準程式庫目錄。

它建置什麼

實例目錄 Component它在這裡是什麼
searchcore/ai-tool/mcp由他人執行的 Model Context Protocol 伺服器,對應到 mcp-remote,它不執行任何東西
agentlocal/ai-agent/mcp-client代理程式,透過它的 tools 插槽連接到 search

兩個 Releases 共用同一個 Aggregate,各自帶有自己的 Manifest:

ReleaseManifest目標整個位址的來源
release-helm.yamlmanifest-helm.yamlKubernetes命名空間已持有的 Secret:繫結到 helm/k8s-existing-secret 的 core/referenced-secret
release-compose.yamlmanifest-compose.yamlDocker Compose一個 Compose 變數參照:繫結到 compose/dotenv-secret 的 core/secret-value

草擬範例

你可以用下列命令建置這個範例,或是讓你的 AI 助理替你建置。

把範例寫入一個以它命名的新資料夾:

Terminal window
aggrega draft --example mcp-address-from-secret

它接著會印出要接著執行的命令。第一個會切換到新資料夾:

Terminal window
cd mcp-address-from-secret

建置

Terminal window
aggrega build --release release-helm.yaml --auto-pull # → dist/kubernetes/
aggrega build --release release-compose.yaml --auto-pull # → dist/local/

每個 Release 都寫入各自的目錄,因此兩棵渲染樹可以並存,並能並排比較。

.aggrega-config.yaml 列出此工作區自己的 catalog/,其中存放代理程式及其兩個 Recipes;其他一切都從預設登錄庫 hub.aggrega.ai(由它指名)解析。若有標準程式庫目錄的簽出內容(取得方式),並以 --catalog 指名,每個實體都會從這兩個目錄解析,因此建置在使用 --offline 時也能完成,該選項會拒絕任何網路連線。--auto-pull 會在建置期間寫入每個 Release 的鎖定檔;鎖定檔與渲染輸出都是產生出來的,不隨附。

Kubernetes 建置會印出以下內容,其中 <workspace> 代表此範例的資料夾:

Pull complete: release-helm.lock (entities=11, source_artifacts=0, external_inputs=0)
notice: LINT: Feature "core/referenced-secret" at core/referenced-secret:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)
[… omitted: a total build time, which differs per run]
Build completed: 2 files written to <workspace>/dist

compose 建置會為 release-compose.yaml 印出相同的幾行,其通知指名 core/secret-value,並寫入一個檔案。notice: 該行與該 Release 所附加的認證 Feature 有關:它接受機密的定位器 origin,可來自開發者的 values 或操作員的 infra,而提示會回報有一個葉節點在兩個支柱中都有宣告。它不改變任何判定,也不改變任何渲染出來的位元組。

搭配 AI 助理

以 Aggrega 的工具設定好的 AI 助理可以做同樣的事:把這個提示貼給它。

只使用 Aggrega 工具,並且不要自己建立任何資料夾。若工作區資料夾中尚未有 `mcp-address-from-secret` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `mcp-address-from-secret`,且不指定 `dir`:它會把範例寫入新的 `mcp-address-from-secret` 資料夾。接著依此順序為每個 Release 各呼叫一次 `build` 工具 — `release-helm.yaml` 與 `release-compose.yaml` — 每次都將 `workspace_root` 設為 `mcp-address-from-secret`、`release` 設為該檔案、`auto_pull` 設為 true、`write` 設為 true。告訴我每次建置寫入了哪些檔案;它們位於 `mcp-address-from-secret/dist/` 之下。

工具持有位址,但不含其金鑰

來自 release-helm.yaml:

search:
infra:
endpoint: https://mcp.example.com/mcp

helm/mcp-remote 會從該位址讀取它所發布的主機、連接埠與通訊協定,並為該工具輸出一個檔案:一個 ExternalName Service,把叢集內的名稱對應到該主機。dist/kubernetes/helm/search-service.yaml 的整個 spec:

spec:
type: ExternalName
externalName: mcp.example.com
ports:
- port: 443

因此工具所持有的位址至少需要通訊協定與主機,並且完全不含攜帶金鑰的部分。compose/mcp-remote 根本不會為該工具定義任何服務。

代理程式的連線持有完整位址

代理程式的 Component 在其 tools 插槽上宣告兩個機密槽位。取自 catalog/component/local/ai-agent/mcp-client/1.0.0/component.yaml,省略了描述:

tools:
selector: "component.aggrega.ai/category=ai-tool, tool.aggrega.ai/transport=http"
valueSchema:
unauthenticated:
type: boolean
default: false
featuresSchema:
credential:
selector: "feature.aggrega.ai/kind=secret"
sensitive: true
inject: [env]
endpoint:
selector: "feature.aggrega.ai/kind=secret"
sensitive: true
inject: [env]

credential 保存代理程式在請求標頭中出示的金鑰;endpoint 保存完整位址,供從位址本身讀取金鑰的伺服器使用。unauthenticated 是連線自己的聲明,表示其工具不需要認證,且不改變代理程式渲染的任何內容。這三者都位於連線上,因此連到多個伺服器的代理程式會把每個伺服器的機密保存在該伺服器的連線上。

Release 會在 Aggregate 所連接的連線上填入 endpoint。取自 release-helm.yaml:

agent:
uses:
tools:
search:
features:
endpoint:
type: core/referenced-secret
infra:
origin: search-mcp-address

Release 不寫 values,所以 origin 位於 infra 中,而從 Secret 讀取的資料鍵是 Feature 的預設值 value。Manifest 把 core/referenced-secret 繫結到 helm/k8s-existing-secret,後者將 origin 讀作命名空間中已存在的 Secret 名稱,且不渲染任何內容。該 Secret 由部署者在 Release 之外建立:

Terminal window
kubectl -n agents create secret generic search-mcp-address \
--from-literal=value='https://mcp.example.com/mcp?api_key=<key>'

代理程式的容器從中讀取位址。dist/kubernetes/helm/agent-deployment.yaml 的 env:

env:
- name: MCP_URL_SEARCH
valueFrom:
secretKeyRef:
name: search-mcp-address
key: value
- name: MCP_HOST_SEARCH
value: mcp.example.com

MCP_URL_SEARCH 是完整位址,依名稱與資料鍵從 Secret 讀取;MCP_HOST_SEARCH 是工具發布的主機。dist/kubernetes/helm/ 只有代理程式的 Deployment 與工具的 Service,別無其他:沒有渲染任何 Secret,也沒有任何檔案含有金鑰。

On Docker Compose

Compose 沒有機密參照,所以 compose Release 用 core/secret-value 填入同一個槽位,其 origin 是一個 Compose 變數參照。取自 release-compose.yaml:

agent:
uses:
tools:
search:
features:
endpoint:
type: core/secret-value
infra:
origin: "${SEARCH_MCP_URL}"

compose/dotenv-secret 會照原樣傳遞該參照,代理程式的服務則攜帶它。dist/local/compose/docker-compose.yaml 中 agent 服務的 environment,該檔案只定義該服務,沒有其他服務:

environment:
MCP_HOST_SEARCH: mcp.example.com
MCP_URL_SEARCH: ${SEARCH_MCP_URL}

Docker Compose 會在堆疊啟動時代換 SEARCH_MCP_URL — 來源可以是 shell 環境、以 --env-file 指定的檔案,或專案目錄中的 .env 檔案 — 因此檔案中只帶有該參照。

代理程式的 Recipes 如何讀取槽位

tools 插槽上的每個連線都會變成環境變數,以連線別名的大寫形式命名:MCP_HOST_<ALIAS> 來自工具發布的主機,MCP_URL_<ALIAS> 來自工具發布的位址 — 除非該連線帶有 endpoint 機密,這時 MCP_URL_<ALIAS> 就是該機密,而發布的位址完全不會傳遞。帶有 credential 的連線會多加 MCP_CREDENTIAL_<ALIAS>,以及 MCP_CREDENTIAL_HEADER_<ALIAS>,即工具發布的、出示該認證時所用的請求標頭。

  • 在 Kubernetes 上,機密是一筆附加項目,指名連線與槽位 — connection: search、from: endpoint、as: env — 提供者會把它轉成上方的 secretKeyRef,並從 helm/k8s-existing-secret 所發布的內容讀取 Secret 的名稱與資料鍵。
  • 在 Docker Compose 上,機密會以其自身附加項目傳遞時所用的鍵 values.key,抵達代理程式的注入管道,而 Recipe 會把該值寫入服務的環境。

若改寫入工具的 infra.endpoint,金鑰會跟著發布的位址一起走:代理程式的 MCP_URL_SEARCH 會以字面值攜帶它,在 Kubernetes 上位於 Deployment 中,在 Compose 檔案中則位於服務的環境,因為參考實現會照原樣發布位址。

限制

  • 在 Docker Compose 上,僅由 Release 附加到同一個代理程式的兩個機密會發生衝突。 Release 所做的每個 core/secret-value 附加項目,都以 Feature 的預設鍵 value 傳遞,因此同一個代理程式上的第二個 — 另一個伺服器的位址,或這個旁邊的認證 — 會中止建置。Release 若對 search 同時附加 endpoint 與 credential,會印出:

    [… omitted: what resolving the lockfile prints, the same halt among it, and the notice about core/secret-value]
    [OPS_ERROR E0318] release-compose.yaml at .spec.boundaries.main.components.agent.uses.tools.search.features.credential
    duplicate delivery key across two attachments: an attachment's FeatureRecipe writes a delivery key another attachment already delivered ("value", mechanism "env", attachment "search.credential", delivered first by attachment "search.endpoint"). Give one of the two attachments a key of its own, or attach one fewer: the key this attachment delivers under is chosen by its FeatureRecipe, which can read the attachment's own values and infra, and the other's is chosen the same way or by its own inject block where it delivered through one. A Release sets an attachment's infra and never its values, so where a key is chosen by a value, attach that attachment in the Aggregate with the value, and keep giving the rest from the Release.

    該鍵是 values 的葉節點,只有 Aggregate 能寫入,因此,宣告這兩個附加項目其中之一並指名其 values.key 的 Aggregate 就能避開此問題:代理程式的 compose Recipe 會依各自附加項目所指名的鍵讀取每個機密。Kubernetes 沒有這種限制,因為那裡的每個機密都是依名稱與資料鍵對 Secret 的參照,而不是在某個鍵之下傳遞的值。

  • 位於位址路徑中的金鑰。 當伺服器從路徑而非查詢字串讀取金鑰時,工具的位址是移除了該部分路徑的位址,而 Secret 保存完整內容。

  • 主機被寫了兩次,一次明文寫在工具的位址中,一次在 Secret 內,而且沒有任何東西檢查兩者是否一致,因為編譯器從不讀取 Secret。

在執行階段只用金鑰組合位址,並在一般變數中使用 $(VAR) 參照,僅在 Kubernetes 上可行:Kubernetes 會從容器中排在它之前的項目展開這類參照,而 helm 提供者會把它從 Secret 讀取的項目列在一般變數之前;Docker Compose 則不會在服務的環境項目之間展開任何 $(VAR) 參照。

調整它

  • 同時檢查標頭的伺服器。 在同一連線上附加 credential。代理程式會收到 MCP_CREDENTIAL_SEARCH,以及出示它時所用的標頭 MCP_CREDENTIAL_HEADER_SEARCH:Authorization,除非 Release 在工具的 infra.credential_header 中指名伺服器自己的標頭。在 Docker Compose 上,如上所述,請在 Aggregate 中指名這兩者之一的鍵。
  • 多個伺服器。 在 tools 插槽上為每個伺服器連接一個連線。每個連線的變數都帶有其別名,且每個連線保存自己的機密。
  • 只從標頭讀取金鑰的伺服器。 省略 endpoint,MCP_URL_<ALIAS> 就是工具發布的位址。

確定性

兩個 Releases 都在 --verify-determinism 之下建置,它會把每個節點渲染兩次,並比較發出的位元組:

Terminal window
aggrega build --release release-helm.yaml --verify-determinism

從乾淨的樹重建任一個,都會逐位元組重現其渲染輸出。

檔案

  • aggregate.yaml — 工具與代理程式,透過 tools 插槽連接,沒有附加任何機密。
  • manifest-helm.yaml、manifest-compose.yaml — 工具對應到 mcp-remote,代理程式對應到它自己的 Recipe,以及各目標所用的認證 Feature。
  • release-helm.yaml、release-compose.yaml — 不含金鑰的工具位址、代理程式的映像檔,以及代理程式連線上的 endpoint 機密。
  • catalog/component/local/ai-agent/mcp-client/1.0.0/component.yaml — 代理程式:其 tools 插槽,帶有 credential 與 endpoint 槽位。
  • catalog/recipe/helm/mcp-client/1.0.0/recipe.yaml、catalog/recipe/compose/mcp-client/1.0.0/recipe.yaml — 各目標上的代理程式。
  • .aggrega-config.yaml — 目錄搜尋堆疊與登錄庫。
  • build-release.sh — 建置一個 Release,並回顯等效的 aggrega build 命令。

對於代理程式在標頭中出示、並保存在其出示所在連線上的金鑰,請見 connection-credentials。兩者背後的模型是認證模型,而工具的實現則連同工具傳輸標籤,在 AI 相關 Features 中說明。