Files
hose-use/src/core/auth/tools.ts
T

80 lines
2.4 KiB
TypeScript

import { z } from "zod";
import { CorpContext } from "../config/manager.js";
import { createEkbClient } from "./ekb.js";
// ── 精简 MCP Auth Tools (其余复杂登录/刷新均由 CLI 直接提供) ───────────────────
export const authTools = [
{
name: "list_corps",
description: "列出当前所有已配置的企业列表",
parameters: z.object({}),
handler: async ({ corpContext }: { corpContext: CorpContext }) => {
return {
activeCorp: corpContext.getCurrent()?.id,
corporations: corpContext.listCorps(),
};
},
},
{
name: "switch_corp",
description: "切换当前活跃工作企业",
parameters: z.object({
corpId: z.string().describe("目标企业 ID"),
}),
handler: async (
{ corpContext }: { corpContext: CorpContext },
params: { corpId: string }
) => {
const success = corpContext.switchCorp(params.corpId);
if (!success) {
throw new Error(`企业 ${params.corpId} 不存在或未配置`);
}
const current = corpContext.getCurrent();
return {
success: true,
message: `已切换到企业: ${current?.name} (${current?.id})`,
corp: current,
};
},
},
{
name: "whoami",
description: "查看当前操作员身份、当前活跃企业与账号状态",
parameters: z.object({}),
handler: async ({ corpContext }: { corpContext: CorpContext }) => {
const current = corpContext.getCurrent();
if (!current) {
return {
authenticated: false,
message: "未配置任何企业,请先运行 hose-use auth login",
};
}
let sessionValid = false;
let sessionData: unknown = null;
try {
const client = await createEkbClient(current);
const res = await client.get("api/account/v2/session").json<{ value?: unknown }>();
sessionData = res.value;
sessionValid = !!res.value;
} catch {
sessionValid = false;
}
return {
authenticated: sessionValid,
currentCorp: {
id: current.id,
name: current.name,
staffId: current.credentials.staffId,
source: current.source,
accountId: current.accountId,
baseUrl: current.baseUrl,
},
session: sessionData,
};
},
},
];