{"record":{"id":"011d09f5bdff127a","repo":"linshenkx/prompt-optimizer","slug":"core-services-initialization-failed-error-as-e","errorCode":null,"errorMessage":"Core services initialization failed: ${(error as Error).message}","messagePattern":"Core services initialization failed: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/mcp-server/src/adapters/core-services.ts","lineNumber":113,"sourceCode":"        this.templateManager,\n        this.historyManager,\n        createImageUnderstandingService(),\n      );\n\n      // 10. 验证服务健康状态\n      await this.validateServices();\n\n      this.initialized = true;\n      logger.info('Core services initialized successfully');\n\n    } catch (error) {\n      // 记录详细错误信息\n      logger.error('Failed to initialize Core services', error as Error);\n\n      // 检查是否有任何可用的模型配置\n      this.showEnvironmentHint();\n\n      throw new Error(`Core services initialization failed: ${(error as Error).message}`, { cause: error });\n    }\n  }\n\n  private async setupDefaultModel(config: MCPServerConfig): Promise<void> {\n    if (!this.modelManager) {\n      throw new Error('ModelManager not initialized');\n    }\n\n    try {\n      // 使用重构后的 setupDefaultModel 函数，只传递 preferredProvider\n      await setupDefaultModel(\n        this.modelManager,\n        config.preferredModelProvider\n      );\n\n      // 获取并显示当前使用的模型信息\n      const mcpModel = await this.modelManager.getModel('mcp-default');\n      if (mcpModel) {","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/mcp-server/src/adapters/core-services.ts#L95-L131","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read error.cause and the preceding 'Failed to initialize Core services' log line to identify the root cause","Verify required API keys are set in the environment (echo $API_KEY, check .env loading)","Check the environment hint printed by showEnvironmentHint() for available model configs","Validate network reachability of the provider base URL (curl) and fix proxy/firewall settings","Verify model configuration files are valid and reference existing models"],"exampleFix":"// before\nawait adapter.initialize(config);\n\n// after\ntry {\n  await adapter.initialize(config);\n} catch (e) {\n  console.error('root cause:', (e as Error).cause);\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await adapter.initialize(config);\n} catch (e) {\n  const cause = (e as Error & { cause?: Error }).cause;\n  if (cause) logger.error('Root cause:', cause.message);\n  process.exit(1);\n}","preventionTips":["Set and verify all provider API keys before starting the server","Run a connectivity check to provider endpoints during deploy smoke tests","Log error.cause, not just the wrapper message"],"tags":["initialization","configuration","api-key","mcp-server"],"backgroundTag":"service-initialization-failed","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}