Skip to content

Commit 082d4dc

Browse files
author
CodeBuddy Attribution Bot
committed
fix(attribution): MCP 查询云函数日志工具参数校验错误或提示不清晰 (issue_mo8z2h9t_o13t7d)
1 parent e2ca73f commit 082d4dc

1 file changed

Lines changed: 67 additions & 17 deletions

File tree

mcp/src/tools/functions.ts

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ export function registerFunctionTools(server: ExtendedMcpServer) {
493493
}
494494
};
495495

496+
const TIME_FORMAT_REGEX = /^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}$/;
497+
496498
const validateLogRange = (
497499
startTime?: string,
498500
endTime?: string,
@@ -503,6 +505,17 @@ export function registerFunctionTools(server: ExtendedMcpServer) {
503505
throw new Error("offset+limit 不能大于 10000");
504506
}
505507

508+
if (startTime && !TIME_FORMAT_REGEX.test(startTime)) {
509+
throw new Error(
510+
`startTime 格式错误: "${startTime}"。必须使用 YYYY-MM-DD HH:mm:ss 格式(如 2024-01-01 00:00:00)`,
511+
);
512+
}
513+
if (endTime && !TIME_FORMAT_REGEX.test(endTime)) {
514+
throw new Error(
515+
`endTime 格式错误: "${endTime}"。必须使用 YYYY-MM-DD HH:mm:ss 格式(如 2024-01-01 23:59:59)`,
516+
);
517+
}
518+
506519
if (startTime && endTime) {
507520
const start = new Date(startTime).getTime();
508521
const end = new Date(endTime).getTime();
@@ -610,15 +623,30 @@ export function registerFunctionTools(server: ExtendedMcpServer) {
610623
input.limit,
611624
);
612625
const cloudbase = await getManager();
613-
const result = await cloudbase.functions.getFunctionLogsV2({
614-
name: input.functionName,
615-
offset: input.offset,
616-
limit: input.limit,
617-
startTime: input.startTime,
618-
endTime: input.endTime,
619-
requestId: input.requestId,
620-
qualifier: input.qualifier,
621-
});
626+
let result;
627+
try {
628+
result = await cloudbase.functions.getFunctionLogsV2({
629+
name: input.functionName,
630+
offset: input.offset,
631+
limit: input.limit,
632+
startTime: input.startTime,
633+
endTime: input.endTime,
634+
requestId: input.requestId,
635+
qualifier: input.qualifier,
636+
});
637+
} catch (error) {
638+
const errMsg = error instanceof Error ? error.message : String(error);
639+
if (/invalid parameter/i.test(errMsg)) {
640+
throw new Error(
641+
`${errMsg}\n\n常见原因:\n` +
642+
`1. startTime/endTime 格式错误,必须为 YYYY-MM-DD HH:mm:ss(如 2024-01-01 00:00:00),不支持 ISO 8601 或时间戳\n` +
643+
`2. startTime 和 endTime 间隔超过一天\n` +
644+
`3. functionName 不存在或格式不正确\n` +
645+
`建议:不传 startTime/endTime 时默认查询最近一天的日志。`,
646+
);
647+
}
648+
throw error;
649+
}
622650
logCloudBaseResult(server.logger, result);
623651
return buildEnvelope(
624652
{
@@ -644,11 +672,25 @@ export function registerFunctionTools(server: ExtendedMcpServer) {
644672
}
645673
validateLogRange(input.startTime, input.endTime);
646674
const cloudbase = await getManager();
647-
const result = await cloudbase.functions.getFunctionLogDetail({
648-
startTime: input.startTime,
649-
endTime: input.endTime,
650-
logRequestId: input.requestId,
651-
});
675+
let result;
676+
try {
677+
result = await cloudbase.functions.getFunctionLogDetail({
678+
startTime: input.startTime,
679+
endTime: input.endTime,
680+
logRequestId: input.requestId,
681+
});
682+
} catch (error) {
683+
const errMsg = error instanceof Error ? error.message : String(error);
684+
if (/invalid parameter/i.test(errMsg)) {
685+
throw new Error(
686+
`${errMsg}\n\n常见原因:\n` +
687+
`1. startTime/endTime 格式错误,必须为 YYYY-MM-DD HH:mm:ss(如 2024-01-01 00:00:00),不支持 ISO 8601 或时间戳\n` +
688+
`2. startTime 和 endTime 间隔超过一天\n` +
689+
`建议:不传 startTime/endTime 时默认查询最近一天的日志。`,
690+
);
691+
}
692+
throw error;
693+
}
652694
logCloudBaseResult(server.logger, result);
653695
return buildEnvelope(
654696
{
@@ -1417,12 +1459,20 @@ export function registerFunctionTools(server: ExtendedMcpServer) {
14171459
action: z
14181460
.enum(QUERY_FUNCTION_ACTIONS)
14191461
.describe("只读操作类型,例如 listFunctions、getFunctionDetail、listFunctionLogs"),
1420-
functionName: z.string().optional().describe("函数名称。函数相关 action 必填"),
1462+
functionName: z.string().optional().describe(
1463+
"函数名称。listFunctionLogs、getFunctionDetail、listFunctionLayers、listFunctionTriggers、getFunctionDownloadUrl 时必填",
1464+
),
14211465
limit: z.number().optional().describe("分页数量。列表类 action 可选"),
14221466
offset: z.number().optional().describe("分页偏移。列表类 action 可选"),
14231467
codeSecret: z.string().optional().describe("代码保护密钥"),
1424-
startTime: z.string().optional().describe("日志查询开始时间"),
1425-
endTime: z.string().optional().describe("日志查询结束时间"),
1468+
startTime: z.string().optional().describe(
1469+
"日志查询开始时间,格式必须为 YYYY-MM-DD HH:mm:ss(如 2024-01-01 00:00:00)。" +
1470+
"与 endTime 间隔不能超过一天。不传时默认查询最近一天",
1471+
),
1472+
endTime: z.string().optional().describe(
1473+
"日志查询结束时间,格式必须为 YYYY-MM-DD HH:mm:ss(如 2024-01-01 23:59:59)。" +
1474+
"与 startTime 间隔不能超过一天。不传时默认为当前时间",
1475+
),
14261476
requestId: z.string().optional().describe("日志 requestId。获取日志详情时必填"),
14271477
qualifier: z.string().optional().describe("函数版本,日志查询时可选"),
14281478
runtime: z.string().optional().describe("层查询的运行时筛选"),

0 commit comments

Comments
 (0)