{"record":{"id":"51dcc746f1039f5f","repo":"CherryHQ/cherry-studio","slug":"brave-api-key-is-required-for-brave-search-mcp-ser","errorCode":null,"errorMessage":"BRAVE_API_KEY is required for Brave Search MCP server","messagePattern":"BRAVE_API_KEY is required for Brave Search MCP server","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"src/main/ai/mcp/servers/braveSearch.ts","lineNumber":297,"sourceCode":"Address: ${address}\nPhone: ${poi.phone || 'N/A'}\nRating: ${poi.rating?.ratingValue ?? 'N/A'} (${poi.rating?.ratingCount ?? 0} reviews)\nPrice Range: ${poi.priceRange || 'N/A'}\nHours: ${(poi.openingHours || []).join(', ') || 'N/A'}\nDescription: ${descData.descriptions[poi.id] || 'No description available'}\n`\n      })\n      .join('\\n---\\n') || 'No local results found'\n  )\n}\n\nclass BraveSearchServer {\n  public server: Server\n  private apiKey: string\n\n  constructor(apiKey: string) {\n    if (!apiKey) {\n      throw new Error('BRAVE_API_KEY is required for Brave Search MCP server')\n    }\n    this.apiKey = apiKey\n    this.server = new Server(\n      {\n        name: 'brave-search-server',\n        version: '0.1.0'\n      },\n      {\n        capabilities: {\n          tools: {}\n        }\n      }\n    )\n    this.initialize()\n  }\n\n  initialize() {\n    // Tool handlers","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/mcp/servers/braveSearch.ts#L279-L315","documentation":"Constructor guard on BraveSearchServer: if the apiKey argument is empty/undefined, it refuses to construct. The factory at factory.ts:32 passes envs.BRAVE_API_KEY, so a missing or empty env var triggers this at server-creation time, before any tool call.","triggerScenarios":"User enabled the Brave Search MCP server in settings but did not supply a BRAVE_API_KEY env var, or supplied an empty string. The in-memory MCP factory is invoked and BraveSearchServer construction throws synchronously.","commonSituations":"MCP server config UI saved without the key; key field named incorrectly (e.g. API_KEY instead of BRAVE_API_KEY); env var unset in the launched environment; user expected a trial key to be preconfigured.","solutions":["Obtain a Brave Search API subscription token from https://brave.com/search/api/.","In the MCP server configuration, set the BRAVE_API_KEY env var to that token (the factory reads envs.BRAVE_API_KEY).","Restart/reload the MCP server after saving so the factory re-instantiates BraveSearchServer.","Verify the key has no surrounding whitespace or quotes."],"exampleFix":"// before — server config missing the key\n// envs: {}\n\n// after — provide the key in the server's env config\n// envs: { \"BRAVE_API_KEY\": \"BSA<your-token>\" }","handlingStrategy":"validation","validationCode":"// Validate the key before enabling the Brave Search server\nfunction validateBraveKey(envs: Record<string, string>) {\n  const key = envs.BRAVE_API_KEY\n  if (typeof key !== 'string' || !key.trim()) {\n    throw new Error('BRAVE_API_KEY env var is required to enable the Brave Search MCP server')\n  }\n  if (!/^BSA[A-Za-z0-9]+$/.test(key.trim())) {\n    throw new Error('BRAVE_API_KEY does not look like a Brave Search token')\n  }\n  return key.trim()\n}","typeGuard":"function hasBraveKey(envs: unknown): envs is { BRAVE_API_KEY: string } {\n  return typeof envs === 'object' && envs !== null && typeof (envs as any).BRAVE_API_KEY === 'string' && (envs as any).BRAVE_API_KEY.trim().length > 0\n}","tryCatchPattern":"try {\n  server = new BraveSearchServer(validateBraveKey(envs)).server\n} catch (e) {\n  logger.error('Brave Search server disabled', { error: e.message })\n  // surface to UI: prompt user to set BRAVE_API_KEY\n}","preventionTips":["Set BRAVE_API_KEY in the MCP server env config before enabling.","Trim whitespace; avoid wrapping quotes.","Gate server enablement on the key being present to fail at config time, not call time."],"tags":["config","brave-search","api-key","mcp-server"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}