linshenkx/prompt-optimizer · critical · Error

Core services initialization failed: ${(error as Error).mess

Error message

Core services initialization failed: ${(error as Error).message}

What it means

Thrown by CoreServicesAdapter.initialize when the underlying Core services (model manager, providers) fail to boot. The original error is logged and preserved as the cause property, so the real reason (missing API key, unreachable provider endpoint, invalid model config) is in error.cause, not in this message itself.

Source

Thrown at packages/mcp-server/src/adapters/core-services.ts:113

        this.templateManager,
        this.historyManager,
        createImageUnderstandingService(),
      );

      // 10. 验证服务健康状态
      await this.validateServices();

      this.initialized = true;
      logger.info('Core services initialized successfully');

    } catch (error) {
      // 记录详细错误信息
      logger.error('Failed to initialize Core services', error as Error);

      // 检查是否有任何可用的模型配置
      this.showEnvironmentHint();

      throw new Error(`Core services initialization failed: ${(error as Error).message}`, { cause: error });
    }
  }

  private async setupDefaultModel(config: MCPServerConfig): Promise<void> {
    if (!this.modelManager) {
      throw new Error('ModelManager not initialized');
    }

    try {
      // 使用重构后的 setupDefaultModel 函数,只传递 preferredProvider
      await setupDefaultModel(
        this.modelManager,
        config.preferredModelProvider
      );

      // 获取并显示当前使用的模型信息
      const mcpModel = await this.modelManager.getModel('mcp-default');
      if (mcpModel) {

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Read error.cause and the preceding 'Failed to initialize Core services' log line to identify the root cause
  2. Verify required API keys are set in the environment (echo $API_KEY, check .env loading)
  3. Check the environment hint printed by showEnvironmentHint() for available model configs
  4. Validate network reachability of the provider base URL (curl) and fix proxy/firewall settings
  5. Verify model configuration files are valid and reference existing models

Example fix

// before
await adapter.initialize(config);

// after
try {
  await adapter.initialize(config);
} catch (e) {
  console.error('root cause:', (e as Error).cause);
  throw e;
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await adapter.initialize(config);
} catch (e) {
  const cause = (e as Error & { cause?: Error }).cause;
  if (cause) logger.error('Root cause:', cause.message);
  process.exit(1);
}

Prevention

When it happens

Trigger: Calling initialize() when required model provider configuration is absent (no API keys in env), when a provider base URL is unreachable, or when model configuration files are malformed. The adapter logs 'Failed to initialize Core services' and shows an environment hint before rethrowing.

Common situations: Missing OPENAI/ANTHROPIC API key environment variables in deployment; corporate proxy blocking the provider endpoint; config referencing a model ID that no longer exists after a provider API change; version upgrade renaming config keys.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/011d09f5bdff127a. Report an issue: GitHub.