diff --git a/src/core/config/loader.ts b/src/core/config/loader.ts index 1a9fdba..49ef057 100644 --- a/src/core/config/loader.ts +++ b/src/core/config/loader.ts @@ -5,17 +5,28 @@ import { CorpConfig, CorpConfigSchema } from "./types"; const ENTERPRISES_DIR = "./enterprises"; export function loadCorpConfigs(): CorpConfig[] { + let files: string[]; try { - const files = readdirSync(ENTERPRISES_DIR).filter((f) => - f.endsWith(".json") + files = readdirSync(ENTERPRISES_DIR).filter((f) => f.endsWith(".json")); + } catch (error) { + console.error( + `[config] failed to read enterprises directory: ${error instanceof Error ? error.message : String(error)}` ); - - return files.map((file) => { - const content = readFileSync(join(ENTERPRISES_DIR, file), "utf-8"); - const parsed = JSON.parse(content); - return CorpConfigSchema.parse(parsed); - }); - } catch { return []; } + + const configs: CorpConfig[] = []; + for (const file of files) { + try { + const content = readFileSync(join(ENTERPRISES_DIR, file), "utf-8"); + const parsed = JSON.parse(content); + configs.push(CorpConfigSchema.parse(parsed)); + } catch (error) { + console.error( + `[config] failed to load ${file}: ${error instanceof Error ? error.message : String(error)}` + ); + } + } + + return configs; } diff --git a/src/server.ts b/src/server.ts index 3d8d4d6..166874e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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); } });