|
| 1 | +// Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import type { AgentDraft } from "./types"; |
| 16 | + |
| 17 | +export type McpGatewayEnvErrorReason = |
| 18 | + | "missing_http_tool" |
| 19 | + | "missing_url" |
| 20 | + | "missing_api_key" |
| 21 | + | "conflicting_api_keys"; |
| 22 | + |
| 23 | +export type McpGatewayEnvResolution = |
| 24 | + | { ok: true; urls: string[]; apiKey: string } |
| 25 | + | { |
| 26 | + ok: false; |
| 27 | + reason: McpGatewayEnvErrorReason; |
| 28 | + message: string; |
| 29 | + }; |
| 30 | + |
| 31 | +const ERROR_MESSAGES: Record<McpGatewayEnvErrorReason, string> = { |
| 32 | + missing_http_tool: |
| 33 | + "请返回“添加 MCP 工具”并添加至少一个 HTTP MCP 服务;MCP 稳定性治理不支持 stdio 服务。", |
| 34 | + missing_url: |
| 35 | + "已添加的 HTTP MCP 工具缺少有效服务地址,请返回“添加 MCP 工具”补充后再发布。", |
| 36 | + missing_api_key: |
| 37 | + "已添加的 HTTP MCP 工具缺少 Bearer Token,请返回“添加 MCP 工具”补充后再发布。", |
| 38 | + conflicting_api_keys: |
| 39 | + "多个 HTTP MCP 工具使用了不同凭证,而 MCP 稳定性治理当前只支持一个共享凭证;请统一凭证或改用统一网关。", |
| 40 | +}; |
| 41 | + |
| 42 | +function failure(reason: McpGatewayEnvErrorReason): McpGatewayEnvResolution { |
| 43 | + return { ok: false, reason, message: ERROR_MESSAGES[reason] }; |
| 44 | +} |
| 45 | + |
| 46 | +function isHttpUrl(value: string): boolean { |
| 47 | + try { |
| 48 | + const url = new URL(value); |
| 49 | + return url.protocol === "http:" || url.protocol === "https:"; |
| 50 | + } catch { |
| 51 | + return false; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** Resolve Sidecar gateway inputs from HTTP MCP tools configured earlier. */ |
| 56 | +export function resolveMcpGatewayEnv( |
| 57 | + root: AgentDraft, |
| 58 | + injectedEnvValues: Record<string, string>, |
| 59 | +): McpGatewayEnvResolution { |
| 60 | + const envValues: Record<string, string> = {}; |
| 61 | + const nodes: AgentDraft[] = []; |
| 62 | + const visited = new Set<AgentDraft>(); |
| 63 | + |
| 64 | + const visit = (node: AgentDraft) => { |
| 65 | + if (visited.has(node)) return; |
| 66 | + visited.add(node); |
| 67 | + nodes.push(node); |
| 68 | + Object.assign(envValues, node.deployment?.envValues ?? {}); |
| 69 | + node.subAgents.forEach(visit); |
| 70 | + node.workflow?.nodes.forEach((workflowNode) => visit(workflowNode.agent)); |
| 71 | + }; |
| 72 | + visit(root); |
| 73 | + Object.assign(envValues, injectedEnvValues); |
| 74 | + |
| 75 | + const httpTools = nodes.flatMap((node) => |
| 76 | + (node.mcpTools ?? []).filter((tool) => tool.transport === "http"), |
| 77 | + ); |
| 78 | + if (httpTools.length === 0) return failure("missing_http_tool"); |
| 79 | + |
| 80 | + const urls: string[] = []; |
| 81 | + const credentials = new Set<string>(); |
| 82 | + for (const tool of httpTools) { |
| 83 | + const url = tool.url?.trim() ?? ""; |
| 84 | + if (!url || !isHttpUrl(url)) return failure("missing_url"); |
| 85 | + |
| 86 | + const envName = tool.authTokenEnv?.trim() ?? ""; |
| 87 | + const apiKey = envName ? (envValues[envName] ?? "").trim() : ""; |
| 88 | + if (!apiKey) return failure("missing_api_key"); |
| 89 | + |
| 90 | + urls.push(url); |
| 91 | + credentials.add(apiKey); |
| 92 | + } |
| 93 | + if (credentials.size !== 1) return failure("conflicting_api_keys"); |
| 94 | + |
| 95 | + return { ok: true, urls, apiKey: [...credentials][0] }; |
| 96 | +} |
0 commit comments