基本語法
Go Template 以{{}} 作為分隔符,可在其中使用多種函式與變數。於 MCP 閘道中主要會用到以下變數:
設定變數
.Config:服務層級設定.Args:請求參數
執行時變數
.Request:原始請求資料.Response:上游服務回應資料
常見應用場景
1. 從環境變數讀取設定
使用
env 函式可安全地從環境變數取得敏感資訊,避免在設定檔中硬編碼。Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
在 MCP 閘道中使用 Go Template 處理請求與回應資料
{{}} 作為分隔符,可在其中使用多種函式與變數。於 MCP 閘道中主要會用到以下變數:
.Config:服務層級設定.Args:請求參數.Request:原始請求資料.Response:上游服務回應資料config:
Authorization: 'Bearer {{ env "AUTH_TOKEN" }}' # 從環境變數讀取授權資訊
env 函式可安全地從環境變數取得敏感資訊,避免在設定檔中硬編碼。headers:
Authorization: "{{.Request.Headers.Authorization}}" # 傳遞用戶端的 Authorization header