Add stateful and stateless enterprise selection

This commit is contained in:
gitea committed 2026-08-12 11:38:25 +08:00
1 parent d383b3aad8
commit 0959cef1cc
22 files changed
+609 -187

No files matched your search

+55 -7
View File
@@ -5,7 +5,12 @@ import {
ListToolsRequestSchema
} from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
import { getEnterprisesDirCandidates, loadCorpConfigs } from "./core/config/loader.js";
import {
getEnterprisesDirCandidates,
loadCorpConfigs,
loadGlobalConfig,
saveGlobalConfig
} from "./core/config/loader.js";
import { CorpContext } from "./core/config/manager.js";
import { createSealClient } from "./core/http/factory.js";
import { resolveLiveSealEnterpriseConfig } from "./domains/seal/source.js";
@@ -14,7 +19,7 @@ import { VERSION } from "./version.js";
async function main() {
const corps = loadCorpConfigs();
const corpContext = new CorpContext(corps);
const corpContext = new CorpContext(corps, loadGlobalConfig());
const server = new Server(
{
@@ -52,14 +57,15 @@ async function main() {
...sealMcpTools.map((tool) => ({
name: tool.name,
description: tool.description,
inputSchema: z.toJSONSchema(tool.parameters)
inputSchema: mcpInputSchema(tool.parameters, loadGlobalConfig().stateful)
}))
]
}));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
corpContext.refresh(loadCorpConfigs());
const globalConfig = loadGlobalConfig();
corpContext.refresh(loadCorpConfigs(), globalConfig);
if (name === "seal_corps_list") {
return textResult(corpContext.listCorps());
@@ -67,19 +73,33 @@ async function main() {
if (name === "seal_corp_switch") {
const { corpId } = args as { corpId: string };
if (!globalConfig.stateful) {
return textResult({
success: false,
message: "Cannot switch the default enterprise while stateless mode is enabled. " +
"Set config mode to stateful first."
}, true);
}
const success = corpContext.switchCorp(corpId);
if (success) {
saveGlobalConfig({ ...globalConfig, stateful: true, defaultCorpId: corpId });
}
return textResult(
success ? { success: true, current: corpId } : { success: false, message: `Unknown corp: ${corpId}` },
!success
);
}
const currentCorp = corpContext.getCurrent();
const requestedCorpId = getCorpId(args);
const currentCorp = corpContext.resolve(requestedCorpId);
if (!currentCorp) {
return textResult(
{
error:
"No enterprise config loaded. Add a non-example JSON file to an enterprises config directory.",
error: requestedCorpId
? `No enterprise config found for ${requestedCorpId}`
: globalConfig.stateful
? "No enterprise config loaded. Add a non-example JSON file to an enterprises config directory."
: "Stateless mode is enabled. Every enterprise tool call requires corpId.",
candidates: getEnterprisesDirCandidates()
},
true
@@ -123,6 +143,34 @@ function textResult(value: unknown, isError = false) {
};
}
function getCorpId(args: unknown): string | undefined {
if (!args || typeof args !== "object" || Array.isArray(args)) return undefined;
const value = (args as Record<string, unknown>).corpId;
return typeof value === "string" && value.trim() ? value.trim() : undefined;
}
function mcpInputSchema(parameters: z.ZodType, stateful: boolean) {
const schema = z.toJSONSchema(parameters) as {
type?: string;
properties?: Record<string, unknown>;
required?: string[];
};
schema.type = "object";
schema.properties = {
...(schema.properties ?? {}),
corpId: {
type: "string",
description: stateful
? "可选:企业配置 ID;省略时使用当前默认企业"
: "必填:企业配置 ID"
}
};
if (!stateful) {
schema.required = [...(schema.required ?? []), "corpId"];
}
return schema;
}
main().catch((error) => {
console.error(error);
process.exit(1);