@aiondadotcom/mcp-openai-image
Version:
MCP server for OpenAI image generation with STDIO transport
123 lines (122 loc) • 4.03 kB
JavaScript
import { promises as fs } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
import { SUPPORTED_MODELS } from './types.js';
export class ConfigManager {
configPath;
config = null;
constructor() {
// Store config in user's home directory
this.configPath = join(homedir(), '.mcp-openai-image.json');
}
async loadConfig() {
if (this.config) {
return this.config;
}
try {
const data = await fs.readFile(this.configPath, 'utf-8');
this.config = JSON.parse(data);
return this.config;
}
catch (error) {
// Config file doesn't exist or is invalid, create default
this.config = this.createDefaultConfig();
await this.saveConfig(this.config);
return this.config;
}
}
async saveConfig(config) {
try {
config.updatedAt = new Date().toISOString();
await fs.writeFile(this.configPath, JSON.stringify(config, null, 2));
this.config = config;
}
catch (error) {
throw new Error(`Failed to save configuration: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
async validateApiKey(apiKey) {
try {
// Basic validation - check if it's a non-empty string starting with 'sk-'
if (!apiKey || typeof apiKey !== 'string' || !apiKey.startsWith('sk-')) {
return false;
}
// Additional validation could be added here to test the key with OpenAI API
return true;
}
catch (error) {
return false;
}
}
async getConfigStatus() {
const config = await this.loadConfig();
return {
configured: !!config.apiKey,
hasApiKey: !!config.apiKey,
model: config.model,
organization: config.organization,
lastUsed: config.lastUsed
};
}
async updateApiKey(apiKey, organization) {
const isValid = await this.validateApiKey(apiKey);
if (!isValid) {
throw new Error('Invalid API key format. API key must start with "sk-"');
}
if (!organization) {
throw new Error('Organization ID is required for image generation');
}
const config = await this.loadConfig();
config.apiKey = apiKey;
config.organization = organization;
await this.saveConfig(config);
}
async updateModel(model) {
if (!SUPPORTED_MODELS.includes(model)) {
throw new Error(`Unsupported model: ${model}. Supported models: ${SUPPORTED_MODELS.join(', ')}`);
}
const config = await this.loadConfig();
config.model = model;
await this.saveConfig(config);
}
async updateLastUsed() {
const config = await this.loadConfig();
config.lastUsed = new Date().toISOString();
await this.saveConfig(config);
}
createDefaultConfig() {
const now = new Date().toISOString();
return {
model: 'gpt-4.1',
defaultSize: '1024x1024',
defaultQuality: 'standard',
defaultFormat: 'png',
createdAt: now,
updatedAt: now
};
}
async getApiKey() {
const config = await this.loadConfig();
return config.apiKey;
}
async getOrganization() {
const config = await this.loadConfig();
return config.organization;
}
async getModel() {
const config = await this.loadConfig();
return config.model;
}
async getDefaultSize() {
const config = await this.loadConfig();
return config.defaultSize;
}
async getDefaultQuality() {
const config = await this.loadConfig();
return config.defaultQuality;
}
async getDefaultFormat() {
const config = await this.loadConfig();
return config.defaultFormat;
}
}