@zhangzhao1102/yapi-mcp-server
Version:
YApi MCP Server - 用于与YApi接口管理平台交互的MCP服务器
85 lines • 3.12 kB
JavaScript
import { readFileSync, existsSync } from 'fs';
import path from 'path';
export class ConfigManager {
constructor() {
this.config = this.loadConfig();
}
static getInstance() {
if (!ConfigManager.instance) {
ConfigManager.instance = new ConfigManager();
}
return ConfigManager.instance;
}
loadConfig() {
let config = {
baseUrl: undefined,
username: undefined,
password: undefined,
token: undefined,
uid: undefined,
};
// 从环境变量获取配置文件路径
const homeDir = process.env.HOME || process.env.USERPROFILE;
if (!homeDir) {
console.error('无法获取用户主目录');
return config;
}
const mcpConfigPath = path.join(homeDir, '.cursor', 'mcp.json');
// 尝试从配置文件读取yapi-mcp-server配置
try {
if (existsSync(mcpConfigPath)) {
const rawContent = readFileSync(mcpConfigPath, 'utf-8').replace(/^\uFEFF/, '');
let mcpConfig;
try {
mcpConfig = JSON.parse(rawContent);
}
catch (error) {
console.warn('无法解析 MCP 配置(JSON.parse 失败):', error);
return config;
}
if (typeof mcpConfig !== 'object' || mcpConfig === null) {
console.warn('MCP 配置不是对象,实际类型:', typeof mcpConfig);
return config;
}
// 查找yapi-mcp-server配置
if (mcpConfig.mcpServers && typeof mcpConfig.mcpServers === 'object') {
const yapiServer = mcpConfig.mcpServers['yapi-mcp-server'];
if (yapiServer && yapiServer.env) {
config = {
baseUrl: yapiServer.env.YAPI_BASE_URL || config.baseUrl,
username: yapiServer.env.YAPI_USERNAME || undefined,
password: yapiServer.env.YAPI_PASSWORD || undefined,
token: undefined,
uid: undefined,
};
}
}
}
}
catch (error) {
console.warn('无法读取MCP配置文件:', error);
}
return config;
}
getConfig() {
return this.config;
}
updateConfig(newConfig) {
this.config = { ...this.config, ...newConfig };
}
validateConfig() {
const errors = [];
if (!this.config.baseUrl) {
errors.push('YAPI_BASE_URL 环境变量未设置');
}
if (!this.config.password && !this.config.username) {
errors.push('YAPI_USERNAME 和 YAPI_PASSWORD 环境变量是必需的');
}
return {
isValid: errors.length === 0,
errors
};
}
}
export const configManager = ConfigManager.getInstance();
//# sourceMappingURL=config.js.map