feat(server): MCP 工具参数运行时校验与统一错误处理

This commit is contained in:
gitea committed 2026-07-17 17:33:14 +08:00
1 parent 49693bdbd7
commit 31c2a112de
2 files changed
+96 -31

No files matched your search

+76 -22
View File
@@ -4,14 +4,59 @@ import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
import { loadCorpConfigs } from "./core/config/loader.js";
import { CorpContext } from "./core/config/manager.js";
import { createEkbClient } from "./core/auth/ekb.js";
import { getEasoClient } from "./core/auth/easo.js";
import { createSealaiClient } from "./core/auth/sealai.js";
import { AuthenticationError, EkbError, NotLoggedInError } from "./core/errors.js";
import { sealaiTools } from "./domains/sealai/tools.js";
import { platformTools } from "./domains/platform/tools.js";
const SwitchCorpSchema = z.object({
corpId: z.string().describe("企业 ID"),
});
function formatError(error: unknown): {
content: Array<{ type: "text"; text: string }>;
isError: true;
} {
if (error instanceof NotLoggedInError) {
return {
content: [{ type: "text", text: "未配置企业,请先添加企业配置" }],
isError: true,
};
}
if (error instanceof AuthenticationError) {
return {
content: [{ type: "text", text: "认证失败,请检查企业凭证或 token 是否过期" }],
isError: true,
};
}
if (error instanceof EkbError) {
return {
content: [{ type: "text", text: `错误 [${error.code}]: ${error.message}` }],
isError: true,
};
}
if (error instanceof z.ZodError) {
return {
content: [{ type: "text", text: `参数错误: ${error.message}` }],
isError: true,
};
}
console.error(error);
return {
content: [{ type: "text", text: "发生内部错误,请稍后重试" }],
isError: true,
};
}
async function main() {
// 加载企业配置
const corps = loadCorpConfigs();
@@ -37,16 +82,7 @@ async function main() {
{
name: "switch_corp",
description: "切换到指定企业",
inputSchema: {
type: "object",
properties: {
corpId: {
type: "string",
description: "企业 ID",
},
},
required: ["corpId"],
},
inputSchema: (SwitchCorpSchema as any).toJSONSchema(),
},
// SealAI 工具
...sealaiTools.map((t) => ({
@@ -70,7 +106,19 @@ async function main() {
// 企业切换
if (name === "switch_corp") {
const { corpId } = args as { corpId: string };
const parsed = SwitchCorpSchema.safeParse(args);
if (!parsed.success) {
return {
content: [
{
type: "text",
text: `参数错误: ${parsed.error.message}`,
},
],
isError: true,
};
}
const { corpId } = parsed.data;
const success = corpContext.switchCorp(corpId);
return {
content: [
@@ -102,8 +150,15 @@ async function main() {
// SealAI 工具
const sealaiTool = sealaiTools.find((t) => t.name === name);
if (sealaiTool) {
const parsed = sealaiTool.parameters.safeParse(args);
if (!parsed.success) {
return {
content: [{ type: "text", text: `参数错误: ${parsed.error.message}` }],
isError: true,
};
}
const client = await createSealaiClient(currentCorp);
const result = await sealaiTool.handler(client, args as never);
const result = await sealaiTool.handler(client, parsed.data as never);
return {
content: [
{
@@ -117,11 +172,18 @@ async function main() {
// Platform 工具
const platformTool = platformTools.find((t) => t.name === name);
if (platformTool) {
const parsed = platformTool.parameters.safeParse(args);
if (!parsed.success) {
return {
content: [{ type: "text", text: `参数错误: ${parsed.error.message}` }],
isError: true,
};
}
const ekbClient = await createEkbClient(currentCorp);
const { client: easoClient } = await getEasoClient(currentCorp);
const result = await platformTool.handler(
{ corp: currentCorp, ekbClient, easoClient },
args as never
parsed.data as never
);
return {
content: [
@@ -143,15 +205,7 @@ async function main() {
isError: true,
};
} catch (error) {
return {
content: [
{
type: "text",
text: `错误: ${error instanceof Error ? error.message : String(error)}`,
},
],
isError: true,
};
return formatError(error);
}
});