import { Server } from "@modelcontextprotocol/sdk/server/index.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; 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 { AuthenticationError, EkbError, NotLoggedInError } from "./core/errors.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(); const corpContext = new CorpContext(corps); const server = new Server( { name: "ekb-mcp", version: "0.1.0", }, { capabilities: { tools: {}, }, } ); // 注册工具列表 server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ // 企业切换工具 { name: "switch_corp", description: "切换到指定企业", inputSchema: (SwitchCorpSchema as any).toJSONSchema(), }, // Platform 工具 ...platformTools.map((t) => ({ name: t.name, description: t.description, inputSchema: (t.parameters as any).toJSONSchema(), })), ], }; }); // 处理工具调用 server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; // 企业切换 if (name === "switch_corp") { 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: [ { type: "text", text: success ? `已切换到企业: ${corpId}` : `企业 ${corpId} 不存在`, }, ], }; } // 获取当前企业 const currentCorp = corpContext.getCurrent(); if (!currentCorp) { return { content: [ { type: "text", text: "未配置企业,请先添加企业配置", }, ], isError: true, }; } try { // 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 }, parsed.data as never ); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } return { content: [ { type: "text", text: `未知工具: ${name}`, }, ], isError: true, }; } catch (error) { return formatError(error); } }); const transport = new StdioServerTransport(); await server.connect(transport); console.error("EKB MCP Server running on stdio"); } main().catch(console.error);