Skip to content

Commit 13810b5

Browse files
zyn080302yanan.zhangyn
andauthored
fix(studio): harden managed Sidecar MCP routing and build status (#976)
* fix(studio): route managed MCP tools through sidecar * fix(sidecar): stabilize managed MCP runtime routing --------- Co-authored-by: yanan.zhangyn <yanan.zhangyn@bytedance.com>
1 parent 01d3b2b commit 13810b5

87 files changed

Lines changed: 1886 additions & 439 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎frontend/src/create/CustomCreate.tsx‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import {
9999
prepareMcpAuth,
100100
updateMcpAuthTokenInput,
101101
} from "./mcpAuth";
102+
import { resolveMcpGatewayEnv } from "./mcpGatewayEnv";
102103
import {
103104
normalizeDraft,
104105
sanitizeGeneratedDraftCapabilities,
@@ -2606,24 +2607,40 @@ function collectDeploymentEnv(root: AgentDraft): RuntimeEnvConfiguration {
26062607
if (
26072608
selectedHarnessOptimizations(prepared.draft).includes("mcp_resilience")
26082609
) {
2610+
const gatewayEnv = resolveMcpGatewayEnv(
2611+
prepared.draft,
2612+
prepared.envValues,
2613+
);
2614+
const gatewayError = gatewayEnv.ok ? undefined : gatewayEnv.message;
26092615
selections.push({
26102616
env: [
26112617
{
26122618
key: "MCP_URLS",
26132619
required: true,
2614-
comment: "MCP 统一网关地址",
2615-
placeholder: "https://example.com/mcp",
2620+
comment: "由已添加的 MCP 工具注入",
2621+
placeholder: "由已添加的 HTTP MCP 工具自动生成",
2622+
help: "由已添加的 HTTP MCP 工具自动注入。",
2623+
readOnly: true,
26162624
requiredBy: [harnessSidecarOptionLabel("mcp_resilience")],
2625+
missingError: gatewayError,
26172626
},
26182627
{
26192628
key: "MCP_API_KEY",
26202629
required: true,
2621-
comment: "MCP 统一网关 API Key",
2630+
comment: "由已添加的 MCP 工具注入",
2631+
placeholder: "由已添加的 HTTP MCP 工具自动生成",
2632+
help: "由已添加的 HTTP MCP 工具自动注入。",
26222633
secret: true,
2634+
readOnly: true,
26232635
requiredBy: [harnessSidecarOptionLabel("mcp_resilience")],
2636+
missingError: gatewayError,
26242637
},
26252638
],
26262639
});
2640+
if (gatewayEnv.ok) {
2641+
fixedValues.MCP_URLS = gatewayEnv.urls.join(",");
2642+
fixedValues.MCP_API_KEY = gatewayEnv.apiKey;
2643+
}
26272644
}
26282645
const config = runtimeEnvConfiguration(selections);
26292646
return {

‎frontend/src/create/deploymentEnv.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface RuntimeEnvSpec {
1818
hidden?: boolean;
1919
/** User-facing optimization names that require this Runtime setting. */
2020
requiredBy?: string[];
21+
/** Actionable error shown when a derived required value cannot be produced. */
22+
missingError?: string;
2123
}
2224

2325
export interface RuntimeEnvSelection {
@@ -122,12 +124,16 @@ export function runtimeEnvRequirementHint(
122124
spec: RuntimeEnvSpec,
123125
): string | undefined {
124126
const labels = runtimeEnvRequirementLabels(spec);
125-
return labels.length
127+
const requirement = labels.length
126128
? `优化项「${labels.join("、")}」依赖此配置。`
127-
: undefined;
129+
: "";
130+
return (
131+
[requirement, spec.help?.trim()].filter(Boolean).join(" ") || undefined
132+
);
128133
}
129134

130135
export function runtimeEnvMissingError(spec: RuntimeEnvSpec): string {
136+
if (spec.missingError?.trim()) return spec.missingError.trim();
131137
const labels = runtimeEnvRequirementLabels(spec);
132138
return labels.length
133139
? `优化项「${labels.join("、")}」依赖此配置,请填写 ${spec.key}。`
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

‎frontend/src/ui/ProjectPreview.tsx‎

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ const DEPLOY_PHASE_ORDER: Record<string, number> = {
158158
github: 8,
159159
};
160160

161+
export const BUILD_STATUS_CONFIRMATION_ERROR_MESSAGE =
162+
"构建任务已经提交,但暂时无法确认最终状态。请稍后在 Code Pipeline 查看构建结果,避免重复部署。";
163+
164+
export function isBuildStatusConfirmationError(error: unknown): boolean {
165+
const message = error instanceof Error ? error.message : String(error);
166+
return /RunPipeline result could not be reconciled|Polling build status failed/i.test(
167+
message,
168+
);
169+
}
170+
161171
function advanceDeploymentPhase(
162172
current: string | undefined,
163173
next: string | undefined,
@@ -1964,7 +1974,12 @@ export function ProjectPreview({
19641974
});
19651975
return;
19661976
}
1967-
if (mountedRef.current) setDeployError(message);
1977+
const buildStatusUnconfirmed =
1978+
latestPhase === "build" && isBuildStatusConfirmationError(err);
1979+
const displayMessage = buildStatusUnconfirmed
1980+
? BUILD_STATUS_CONFIRMATION_ERROR_MESSAGE
1981+
: message;
1982+
if (mountedRef.current) setDeployError(displayMessage);
19681983
if (mountedRef.current) setDeployResult(null);
19691984
const buildLog = finalizeBuildFailureLog();
19701985
operation.fail({
@@ -1983,17 +1998,21 @@ export function ProjectPreview({
19831998
startedAt: taskStartedAt,
19841999
status: "error",
19852000
phase: latestPhase,
1986-
label: "部署失败",
1987-
message: failedInBuild
1988-
? "构建镜像失败,详见构建日志。"
1989-
: failedInGithub
1990-
? "挂载 GitHub 持续交付失败,详见 GitHub 日志。"
1991-
: message,
2001+
label: buildStatusUnconfirmed ? "构建状态待确认" : "部署失败",
2002+
message: buildStatusUnconfirmed
2003+
? BUILD_STATUS_CONFIRMATION_ERROR_MESSAGE
2004+
: failedInBuild
2005+
? "构建镜像失败,详见构建日志。"
2006+
: failedInGithub
2007+
? "挂载 GitHub 持续交付失败,详见 GitHub 日志。"
2008+
: message,
19922009
...(buildLog ? { buildLog } : terminalBuildLogUpdate("complete")),
19932010
...(failedInGithub
19942011
? { githubDelivery: true, githubLog: latestGithubLog }
19952012
: {}),
1996-
retry: requestDeploymentConfirmation,
2013+
...(buildStatusUnconfirmed
2014+
? {}
2015+
: { retry: requestDeploymentConfirmation }),
19972016
});
19982017
} finally {
19992018
if (mountedRef.current) setDeploying(false);
@@ -2893,7 +2912,10 @@ export function ProjectPreview({
28932912
className="pp-env-value pp-env-json-value"
28942913
value={row.value}
28952914
placeholder={
2896-
row.required ? "必填,尚未填写" : "可选,尚未填写"
2915+
row.placeholder ||
2916+
(row.required
2917+
? "必填,尚未填写"
2918+
: "可选,尚未填写")
28972919
}
28982920
readOnly={fixed}
28992921
disabled={
@@ -2949,9 +2971,10 @@ export function ProjectPreview({
29492971
}
29502972
value={displayedValue}
29512973
placeholder={
2952-
row.required
2974+
row.placeholder ||
2975+
(row.required
29532976
? "必填,尚未填写"
2954-
: "可选,尚未填写"
2977+
: "可选,尚未填写")
29552978
}
29562979
readOnly={fixed}
29572980
disabled={
@@ -3199,13 +3222,22 @@ export function ProjectPreview({
31993222
{deployError && (
32003223
<DeploymentErrorMessage
32013224
className="pp-error"
3202-
message={`${activePhase
3203-
? `${isRuntimeUpdate ? "更新" : "部署"}失败(${
3204-
deploymentSteps.find((step) => step.phase === activePhase)?.label ??
3205-
activePhase
3206-
}阶段):`
3207-
: ""}${deployError}`}
3208-
onRetry={requestDeploymentConfirmation}
3225+
message={
3226+
deployError === BUILD_STATUS_CONFIRMATION_ERROR_MESSAGE
3227+
? `构建状态待确认:${deployError}`
3228+
: `${activePhase
3229+
? `${isRuntimeUpdate ? "更新" : "部署"}失败(${
3230+
deploymentSteps.find(
3231+
(step) => step.phase === activePhase,
3232+
)?.label ?? activePhase
3233+
}阶段):`
3234+
: ""}${deployError}`
3235+
}
3236+
onRetry={
3237+
deployError === BUILD_STATUS_CONFIRMATION_ERROR_MESSAGE
3238+
? undefined
3239+
: requestDeploymentConfirmation
3240+
}
32093241
retryLabel={
32103242
isRuntimeUpdate ? "重试更新" : "重试部署"
32113243
}

‎frontend/tests/agentWorkspace.test.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ test("workspace publish flow restores PR 748 deployment lifecycle hooks", () =>
410410
assert.match(projectPreviewSource, /setActivePhase\(latestPhase\)/);
411411
assert.match(
412412
projectPreviewSource,
413-
/label: "部署失败"[\s\S]*?message: failedInBuild[\s\S]*?\.\.\.\(buildLog/,
413+
/label: buildStatusUnconfirmed[\s\S]*?"构建状态待确认"[\s\S]*?"部署失败"[\s\S]*?message: buildStatusUnconfirmed[\s\S]*?failedInBuild[\s\S]*?\.\.\.\(buildLog/,
414414
);
415415
assert.match(projectPreviewSource, /const failedInGithub = latestPhase === "github" && Boolean\(latestGithubLog\)/);
416416
assert.match(projectPreviewSource, /failedInBuild[\s\S]*?"构建镜像失败,详见构建日志。"[\s\S]*?failedInGithub[\s\S]*?"挂载 GitHub 持续交付失败,详见 GitHub 日志。"/);

‎frontend/tests/debugErrorPresentation.test.mjs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,34 @@ test("creation and deployment keep friendly context and the original error", ()
7272
assert.match(clientSource, /原始响应:\\n\$\{text\}/);
7373
assert.match(
7474
projectPreviewSource,
75-
/label: "部署失败"[\s\S]*?message: failedInBuild[\s\S]*?\.\.\.\(buildLog/,
75+
/label: buildStatusUnconfirmed[\s\S]*?"构建状态待确认"[\s\S]*?"部署失败"[\s\S]*?message: buildStatusUnconfirmed[\s\S]*?failedInBuild[\s\S]*?\.\.\.\(buildLog/,
7676
);
7777
assert.match(
7878
projectPreviewSource,
7979
/failedInBuild[\s\S]*?"构建镜像失败,详见构建日志。"[\s\S]*?failedInGithub[\s\S]*?"挂载 GitHub 持续交付失败,详见 GitHub 日志。"[\s\S]*?: message/,
8080
);
81+
assert.match(
82+
projectPreviewSource,
83+
/isBuildStatusConfirmationError[\s\S]*?RunPipeline result could not be reconciled[\s\S]*?Polling build status failed/,
84+
);
85+
assert.doesNotMatch(
86+
projectPreviewSource.match(
87+
/export function isBuildStatusConfirmationError[\s\S]*?\n}/,
88+
)?.[0] ?? "",
89+
/network error|fetch failed|Volcengine request timed out/i,
90+
);
91+
assert.match(
92+
projectPreviewSource,
93+
/buildStatusUnconfirmed[\s\S]*?BUILD_STATUS_CONFIRMATION_ERROR_MESSAGE[\s\S]*?failedInBuild/,
94+
);
95+
assert.match(
96+
projectPreviewSource,
97+
/deployError === BUILD_STATUS_CONFIRMATION_ERROR_MESSAGE[\s\S]*?undefined[\s\S]*?: requestDeploymentConfirmation/,
98+
);
99+
assert.match(
100+
projectPreviewSource,
101+
/deployError === BUILD_STATUS_CONFIRMATION_ERROR_MESSAGE[\s\S]*?`构建状态待确认:\$\{deployError\}`/,
102+
);
81103
});
82104

83105
test("generated-agent debug requests preserve backend error details", () => {

‎frontend/tests/deploymentEnv.test.mjs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,23 @@ test("explains optimization dependencies and reports every missing runtime setti
352352
}),
353353
[],
354354
);
355+
356+
const derivedSpec = {
357+
key: "MCP_URLS",
358+
required: true,
359+
readOnly: true,
360+
requiredBy: ["MCP 稳定性治理"],
361+
help: "由已添加的 HTTP MCP 工具自动注入。",
362+
missingError: "请返回“添加 MCP 工具”补充配置。",
363+
};
364+
assert.equal(
365+
runtimeEnvRequirementHint(derivedSpec),
366+
"优化项「MCP 稳定性治理」依赖此配置。 由已添加的 HTTP MCP 工具自动注入。",
367+
);
368+
assert.equal(
369+
runtimeEnvMissingError(derivedSpec),
370+
"请返回“添加 MCP 工具”补充配置。",
371+
);
355372
});
356373

357374
test("marks missing optimization env inputs invalid and focuses the first error", () => {
@@ -363,7 +380,21 @@ test("marks missing optimization env inputs invalid and focuses the first error"
363380
customCreateSource,
364381
/requiredBy:\s*\[harnessSidecarOptionLabel\("mcp_resilience"\)\]/,
365382
);
383+
assert.match(customCreateSource, /resolveMcpGatewayEnv\(/);
384+
assert.match(
385+
customCreateSource,
386+
/fixedValues\.MCP_URLS = gatewayEnv\.urls\.join\(","\)/,
387+
);
388+
assert.match(
389+
customCreateSource,
390+
/fixedValues\.MCP_API_KEY = gatewayEnv\.apiKey/,
391+
);
392+
assert.match(
393+
customCreateSource,
394+
/key: "MCP_API_KEY",[\s\S]*?secret: true,[\s\S]*?readOnly: true/,
395+
);
366396
assert.match(projectPreviewSource, /missingRuntimeEnvs\(/);
397+
assert.match(projectPreviewSource, /row\.placeholder \|\|/);
367398
assert.match(projectPreviewSource, /setDeploymentEnvErrors\(/);
368399
assert.match(projectPreviewSource, /focusDeploymentEnv\(/);
369400
assert.match(

0 commit comments

Comments
 (0)