Skip to content

Commit 9b83da6

Browse files
committed
refactor(mcp): remove redundant try-catch in tools, let wrapper handle errors
- Remove inner try-catch from storage, databaseSQL, databaseNoSQL, security-rule, cloudrun, miniprogram, download, invite-code, dataModel - Errors now propagate to tool-wrapper for unified handling (telemetry, enhanced error message, GitHub Issue link) - Keep business-return only (e.g. force confirmation, path validation)
1 parent 01648ab commit 9b83da6

File tree

9 files changed

+824
-1236
lines changed

9 files changed

+824
-1236
lines changed

mcp/src/tools/cloudrun.ts

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,17 @@ export function registerCloudRunTools(server: ExtendedMcpServer) {
192192
}
193193
},
194194
async (args: queryCloudRunInput) => {
195-
try {
196-
const input = args;
197-
const manager = await getManager();
195+
const input = args;
196+
const manager = await getManager();
198197

199-
if (!manager) {
200-
throw new Error("Failed to initialize CloudBase manager. Please check your credentials and environment configuration.");
201-
}
198+
if (!manager) {
199+
throw new Error("Failed to initialize CloudBase manager. Please check your credentials and environment configuration.");
200+
}
202201

203-
const cloudrunService = manager.cloudrun;
202+
const cloudrunService = manager.cloudrun;
204203

205-
switch (input.action) {
206-
case 'list': {
204+
switch (input.action) {
205+
case 'list': {
207206
const listParams: any = {
208207
pageSize: input.pageSize,
209208
pageNum: input.pageNum,
@@ -295,23 +294,8 @@ export function registerCloudRunTools(server: ExtendedMcpServer) {
295294
};
296295
}
297296

298-
default:
299-
throw new Error(`Unsupported action: ${input.action}`);
300-
}
301-
302-
} catch (error: any) {
303-
return {
304-
content: [
305-
{
306-
type: "text",
307-
text: JSON.stringify({
308-
success: false,
309-
error: error.message || 'Unknown error occurred',
310-
message: "Failed to query CloudRun information. Please check your permissions and try again."
311-
}, null, 2)
312-
}
313-
]
314-
};
297+
default:
298+
throw new Error(`Unsupported action: ${input.action}`);
315299
}
316300
}
317301
);
@@ -335,24 +319,22 @@ export function registerCloudRunTools(server: ExtendedMcpServer) {
335319
}
336320
},
337321
async (args: ManageCloudRunInput) => {
338-
try {
339-
const input = args;
340-
const manager = await getManager();
322+
const input = args;
323+
const manager = await getManager();
341324

342-
if (!manager) {
343-
throw new Error("Failed to initialize CloudBase manager. Please check your credentials and environment configuration.");
344-
}
325+
if (!manager) {
326+
throw new Error("Failed to initialize CloudBase manager. Please check your credentials and environment configuration.");
327+
}
345328

346-
const cloudrunService = manager.cloudrun;
347-
let targetPath: string | undefined;
329+
const cloudrunService = manager.cloudrun;
330+
let targetPath: string | undefined;
348331

349-
// Validate and normalize path for operations that require it
350-
if (input.targetPath) {
351-
targetPath = validateAndNormalizePath(input.targetPath);
352-
}
332+
if (input.targetPath) {
333+
targetPath = validateAndNormalizePath(input.targetPath);
334+
}
353335

354-
switch (input.action) {
355-
case 'createAgent': {
336+
switch (input.action) {
337+
case 'createAgent': {
356338
if (!targetPath) {
357339
throw new Error("targetPath is required for createAgent operation");
358340
}
@@ -939,23 +921,8 @@ for await (let x of res.textStream) {
939921
};
940922
}
941923

942-
default:
943-
throw new Error(`Unsupported action: ${input.action}`);
944-
}
945-
946-
} catch (error: any) {
947-
return {
948-
content: [
949-
{
950-
type: "text",
951-
text: JSON.stringify({
952-
success: false,
953-
error: error.message || 'Unknown error occurred',
954-
message: `Failed to ${args.action} CloudRun service. Please check your permissions and parameters.`
955-
}, null, 2)
956-
}
957-
]
958-
};
924+
default:
925+
throw new Error(`Unsupported action: ${input.action}`);
959926
}
960927
}
961928
);

mcp/src/tools/dataModel.ts

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,12 @@ export function registerDataModelTools(server: ExtendedMcpServer) {
655655
name?: string;
656656
names?: string[];
657657
}) => {
658-
try {
659-
const cloudbase = await getManager();
660-
let currentEnvId = await getEnvId(cloudBaseOptions);
658+
const cloudbase = await getManager();
659+
let currentEnvId = await getEnvId(cloudBaseOptions);
661660

662-
let result;
661+
let result;
663662

664-
switch (action) {
663+
switch (action) {
665664
case "get":
666665
if (!name) {
667666
throw new Error("获取数据模型需要提供模型名称");
@@ -982,28 +981,8 @@ export function registerDataModelTools(server: ExtendedMcpServer) {
982981
throw error;
983982
}
984983

985-
default:
986-
throw new Error(`不支持的操作类型: ${action}`);
987-
}
988-
} catch (error: any) {
989-
return {
990-
content: [
991-
{
992-
type: "text",
993-
text: JSON.stringify(
994-
{
995-
success: false,
996-
action,
997-
error: error.message || error.original?.Message || "未知错误",
998-
code: error.original?.Code,
999-
message: "数据模型操作失败",
1000-
},
1001-
null,
1002-
2
1003-
),
1004-
},
1005-
],
1006-
};
984+
default:
985+
throw new Error(`不支持的操作类型: ${action}`);
1007986
}
1008987
}
1009988
);
@@ -1092,12 +1071,10 @@ classDiagram
10921071
publish?: boolean;
10931072
dbInstanceType?: string;
10941073
}) => {
1095-
try {
1096-
const cloudbase = await getManager();
1097-
let currentEnvId = await getEnvId(cloudBaseOptions);
1074+
const cloudbase = await getManager();
1075+
let currentEnvId = await getEnvId(cloudBaseOptions);
10981076

1099-
// 使用mermaidToJsonSchema转换Mermaid图表
1100-
const schemas = mermaidToJsonSchema(mermaidDiagram);
1077+
const schemas = mermaidToJsonSchema(mermaidDiagram);
11011078

11021079
if (!schemas || Object.keys(schemas).length === 0) {
11031080
return {
@@ -1226,25 +1203,6 @@ classDiagram
12261203
},
12271204
],
12281205
};
1229-
} catch (error: any) {
1230-
return {
1231-
content: [
1232-
{
1233-
type: "text",
1234-
text: JSON.stringify(
1235-
{
1236-
success: false,
1237-
error: error.message || error.original?.Message || "未知错误",
1238-
code: error.original?.Code,
1239-
message: "数据模型修改操作失败",
1240-
},
1241-
null,
1242-
2
1243-
),
1244-
},
1245-
],
1246-
};
1247-
}
12481206
}
12491207
);
12501208
}

0 commit comments

Comments
 (0)