{"record":{"id":"4f948e179af6455a","repo":"koala73/worldmonitor","slug":"could-not-resolve-json-stringify-echocountryinpu","errorCode":null,"errorMessage":"Could not resolve ${JSON.stringify(echoCountryInput(raw))} to a country. ${COUNTRY_ARG_HINT}","messagePattern":"Could not resolve (.+?) to a country\\. (.+?)","errorType":"validation","errorClass":"RpcValidationError","httpStatus":400,"severity":"error","filePath":"api/mcp/registry/rpc-tools.ts","lineNumber":90,"sourceCode":"  staleAgeSeconds?: number;\n  staleReason?: string;\n  attemptedAt?: string;\n};\n\n// Corroboration for the country brief cannot ride on `sources`: that array is\n// the proto BriefSource shape (title/source/url/publishedAt) returned by the\n// gateway, so widening it would be a proto change, and on the common path the\n// server-side sources win anyway. A sibling field keeps the citation list\n// exactly as it is. (#4925 item 3)\ntype McpBriefGroundingStory = {\n  title: string;\n  source: string;\n  url?: string;\n  publishedAt?: string;\n  corroborationCount: number;\n  mentionCount?: number;\n  storyPhase?: string;\n};\n\n// Keep the optional grounding copy smaller than the primary citation list.\n// An oversized URL is omitted rather than truncated because a clipped URL is\n// no longer a valid citation target. The primary `sources` field remains the\n// canonical citation surface.\nconst MAX_COUNTRY_BRIEF_GROUNDING_URL_LENGTH = 2_000;\n\nfunction clipBriefText(value: unknown, maxLen: number): string {\n  if (typeof value !== 'string') return '';\n  const text = value.replace(/\\s+/g, ' ').trim();\n  return text.length > maxLen ? `${text.slice(0, maxLen - 1).trim()}...` : text;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n  return !!value && typeof value === 'object' && !Array.isArray(value);\n}\n\n/**","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/koala73/worldmonitor/blob/e586b8b4b80f595aa7ece295eec10d76f2921240/api/mcp/registry/rpc-tools.ts#L72-L108","documentation":"requireCountryCode() resolves a caller-supplied country argument (name, code, alias) via resolveCountryCode(); if resolution fails it throws RpcValidationError with a field-level description including the echoed (safe, JSON-stringified) input and COUNTRY_ARG_HINT. It fails loudly so an agent can self-correct its arguments.","triggerScenarios":"Calling any MCP RPC tool whose handler calls requireCountryCode (directly or via countryCode()/code()) with a raw value that resolveCountryCode cannot map — misspelled country names, lowercase or non-alpha-2 codes, unknown aliases, non-string types like numbers or objects.","commonSituations":"An LLM agent passing 'Swiss' or 'CHN'-style typo codes; passing a numeric ISO 3166-1 numeric code; passing localized country names; passing null/undefined when the parameter was optional upstream.","solutions":["Pass a valid ISO 3166-1 alpha-2 code (e.g. \"CH\", \"US\") — the most reliable form.","Use the exact country name or alias supported by resolveCountryCode; consult COUNTRY_ARG_HINT in the error message for accepted formats.","Trim whitespace and fix casing (resolution is typically case-insensitive but exact spelling matters).","If the entity genuinely has no country code (e.g. an EU-wide query), use the tool's non-country variant or preset parameter instead."],"exampleFix":"// before\ncallTool('get-country-brief', { country_code: 'Swiss' })\n// after\ncallTool('get-country-brief', { country_code: 'CH' })","handlingStrategy":"validation","validationCode":"const resolved = resolveCountryCode(raw);\nif (!resolved) {\n  throw new Error(`\"${raw}\" is not a recognized country; pass an ISO 3166-1 alpha-2 code like \"CH\"`);\n}","typeGuard":"function isAlpha2(v: unknown): v is string {\n  return typeof v === 'string' && /^[A-Za-z]{2}$/.test(v);\n}","tryCatchPattern":"try {\n  const cc = requireCountryCode(raw, 'get-country-brief');\n} catch (err) {\n  if (err instanceof RpcValidationError) {\n    // inspect err.fields / description; retry once with a normalized alpha-2 code\n  } else throw err;\n}","preventionTips":["Always pass uppercase ISO 3166-1 alpha-2 codes to MCP country tools.","Normalize user/agent input through resolveCountryCode before dispatching RPCs.","Read COUNTRY_ARG_HINT in the error for accepted formats.","Trim and uppercase strings at the input boundary."],"tags":["validation","mcp","rpc","country-code","input"],"backgroundTag":"rpc-validation-error","analyzedSha":"e586b8b4b80f595aa7ece295eec10d76f2921240","analyzedAt":"2026-09-01T10:32:37.851Z","contentChangedAt":"2026-09-01T10:32:37.851Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}