{"record":{"id":"aa0464cc1b0c2839","repo":"farion1231/cc-switch","slug":"error-aa0464","errorCode":null,"errorMessage":"服务器配置必须是对象","messagePattern":"服务器配置必须是对象","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/utils/tomlUtils.ts","lineNumber":103,"sourceCode":"      if (serverIds.length > 0) {\n        const firstServer = mcpObj.servers[serverIds[0]];\n        return normalizeServerConfig(firstServer);\n      }\n    }\n  }\n\n  throw new Error(\n    \"无法识别的 TOML 格式。请提供单个 MCP 服务器配置，或使用 [mcp_servers.<id>] 格式\",\n  );\n};\n\n/**\n * 规范化服务器配置对象为 McpServer 格式\n * 保留所有字段（包括扩展字段如 timeout_ms）\n */\nfunction normalizeServerConfig(config: any): McpServerSpec {\n  if (!config || typeof config !== \"object\") {\n    throw new Error(\"服务器配置必须是对象\");\n  }\n\n  const type = (config.type as string) || \"stdio\";\n\n  // 已知字段列表（用于后续排除）\n  const knownFields = new Set<string>();\n\n  if (type === \"stdio\") {\n    if (!config.command || typeof config.command !== \"string\") {\n      throw new Error(\"stdio 类型的 MCP 服务器必须包含 command 字段\");\n    }\n\n    const server: McpServerSpec = {\n      type: \"stdio\",\n      command: config.command,\n    };\n    knownFields.add(\"type\");\n    knownFields.add(\"command\");","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/farion1231/cc-switch/blob/0b5da510168914b251481654a568c3ffacd62cf4/src/utils/tomlUtils.ts#L85-L121","documentation":"normalizeServerConfig receives the extracted candidate server object and rejects anything that is not an object ('服务器配置必须是对象' — 'server config must be an object'). This is a defensive gate inside the converter: extraction steps usually yield TOML tables, but a scalar or array can slip through when a section like mcp_servers is mapped to a plain value instead of tables.","triggerScenarios":"TOML where the value flowing into normalizeServerConfig is a scalar or array — e.g. 'mcp_servers = \"fetch\"' or a mixed paste whose first extracted entry is a list.","commonSituations":"A table header [mcp_servers.name] was omitted so the value became a plain key/value; converting JSON fixtures to TOML incorrectly.","solutions":["Make the server entry a TOML table: put it under [mcp_servers.<id>] or as top-level keys","Pre-parse with the same parseToml call and verify the extracted value is an object before importing","Show the extracted shape (table vs string vs array) in the import preview"],"exampleFix":"# before\nmcp_servers = \"fetch\"\n\n# after\n[mcp_servers.fetch]\ntype = \"stdio\"\ncommand = \"uvx\"","handlingStrategy":"type-guard","validationCode":"const candidate = extractFirstServer(parseToml(normalizeTomlText(tomlText)));\nif (!isRecord(candidate)) {\n  // tell the user the server entry must be a table\n}","typeGuard":"function isRecord(value: unknown): value is Record<string, unknown> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}","tryCatchPattern":"try {\n  const server = tomlToMcpServer(tomlText);\n} catch (error) {\n  if (error instanceof Error && error.message === '服务器配置必须是对象') {\n    // hint: wrap keys under a [mcp_servers.<id>] table header\n  } else {\n    throw error;\n  }\n}","preventionTips":["Always use table headers for server entries","Preview the extracted shape (table vs scalar) in the import UI","Validate converted JSON→TOML fixtures in tests"],"tags":["toml","mcp","type-validation","import"],"backgroundTag":"schema-validation-failed","analyzedSha":"0b5da510168914b251481654a568c3ffacd62cf4","analyzedAt":"2026-08-20T14:29:00.113Z","contentChangedAt":"2026-08-20T14:29:00.113Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}