config.ts•2.33 kB
import dotenv from 'dotenv';
import { readFileSync } from 'fs';
import { resolve } from 'path';
dotenv.config();
function loadConfigFromFile(): { apiKey?: string; model?: string; baseURL?: string } {
const keyFilePath = '/Users/a019051/.keys/openai_gpt.key';
try {
const content = readFileSync(keyFilePath, 'utf-8').trim();
const config: { apiKey?: string; model?: string; baseURL?: string } = {};
// Parse environment variable format
const lines = content.split('\n');
for (const line of lines) {
const trimmedLine = line.trim();
if (trimmedLine.startsWith('OPENAI_API_KEY=')) {
config.apiKey = trimmedLine.split('=')[1]?.replace(/"/g, '');
} else if (trimmedLine.startsWith('OPENAI_MODEL=')) {
config.model = trimmedLine.split('=')[1]?.replace(/"/g, '');
} else if (trimmedLine.startsWith('OPENAI_BASE_URL=')) {
config.baseURL = trimmedLine.split('=')[1]?.replace(/"/g, '');
}
}
// Fallback: if file contains just the API key without variable name
if (!config.apiKey && content.startsWith('sk-')) {
config.apiKey = content;
}
return config;
} catch (error) {
return {};
}
}
export interface Config {
openai: {
apiKey: string;
model: string;
baseURL?: string;
};
server: {
name: string;
version: string;
debug: boolean;
};
}
const fileConfig = loadConfigFromFile();
export const config: Config = {
openai: {
apiKey: process.env.OPENAI_API_KEY || fileConfig.apiKey || '',
model: process.env.OPENAI_MODEL || fileConfig.model || 'gpt-5', // GPT-5 as default
baseURL: process.env.OPENAI_BASE_URL || fileConfig.baseURL
},
server: {
name: 'openai-mcp-server',
version: '1.0.0',
debug: process.env.DEBUG === 'true'
}
};
export function validateConfig(): void {
if (!config.openai.apiKey) {
console.error('ERROR: OPENAI_API_KEY is required');
console.error('Please set environment variable or add to /Users/a019051/.keys/openai_gpt.key');
process.exit(1);
}
if (config.server.debug) {
console.error('Config loaded:', {
model: config.openai.model,
hasApiKey: !!config.openai.apiKey,
baseURL: config.openai.baseURL || 'default',
source: process.env.OPENAI_API_KEY ? 'env' : 'file'
});
}
}