Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
feat(mcp): accept a coarse error_code in the MCP action-log ingest (#5239)
Erik Engervall8a42723e937d
diff --git a/apps/api/src/services/mcp/action-logs.test.ts b/apps/api/src/services/mcp/action-logs.test.tsindex 4e571b960..d50692051 100644--- a/apps/api/src/services/mcp/action-logs.test.ts+++ b/apps/api/src/services/mcp/action-logs.test.ts@@ -205,6 +205,32 @@ describe("MCP action log contract", () => {);});+ it("stores an optional coarse error code and rejects anything but snake_case", () => {+ expect(input().error_code).toBeNull();+ expect(+ input({ status: "error", error_code: "rate_limited" }).error_code,+ ).toBe("rate_limited");+ expect(+ input({ status: "error", error_code: "scrape_all_engines_failed" })+ .error_code,+ ).toBe("scrape_all_engines_failed");+ expect(input({ error_code: "" }).error_code).toBeNull();+ for (const value of [+ "Rate limit exceeded",+ "https://example.com",+ "RATE_LIMITED",+ "rate_",+ "rate__limited",+ "fcmcp_abc123",+ "x".repeat(65),+ 429,+ ]) {+ expect(() => input({ error_code: value })).toThrow(+ "error_code must be a lowercase snake_case code",+ );+ }+ });+it("stores the optional per-call fields the hosted MCP reads itself", () => {expect(input()).toMatchObject({mcp_origin: null,@@ -341,6 +367,7 @@ describe("MCP action log contract", () => {input({objective: "Find the refund policy",api_job_id: REQUEST_ID,+ error_code: "rate_limited",user_agent: "Claude/1.0",mcp_origin: "mcp-ua-codex-cli-rs",mcp_client: "codex-cli-rs",@@ -359,6 +386,7 @@ describe("MCP action log contract", () => {api_key_id: "123",objective: "Find the refund policy",api_job_id: REQUEST_ID,+ error_code: "rate_limited",mcp_origin: "mcp-ua-codex-cli-rs",mcp_client: "codex-cli-rs",mcp_client_source: "user_agent",diff --git a/apps/api/src/services/mcp/action-logs.ts b/apps/api/src/services/mcp/action-logs.tsindex fb5061e6f..6eb415bf5 100644--- a/apps/api/src/services/mcp/action-logs.ts+++ b/apps/api/src/services/mcp/action-logs.ts@@ -19,6 +19,7 @@ const SECRET_PATTERN =/(?:\bBearer\s+\S+|\beyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\b|\b(?:sk-|fc-|fco_|fcr_|fcmcp_)[A-Za-z0-9_-]+)/i;const SECRET_PATTERN_ALL = new RegExp(SECRET_PATTERN.source, "gi");const OBJECTIVE_MAX_LENGTH = 1000;+const ERROR_CODE_PATTERN = /^(?=.{2,64}$)[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/;const MCP_CLIENT_SOURCES = ["client_info", "user_agent", "oauth"] as const;const DURATION_MS_MAX = 2_147_483_647;const INVALID_ARGUMENTS_MAX = 10;@@ -38,6 +39,7 @@ const ALLOWED_FIELDS = new Set(["client_name","client_version","error_class",+ "error_code","resource","objective","api_job_id",@@ -95,6 +97,18 @@ function text(value: unknown, field: string, max = 128, required = false) {return normalized || null;}+function errorCode(value: unknown) {+ if (value == null || value === "") return null;+ if (+ typeof value !== "string" ||+ !ERROR_CODE_PATTERN.test(value) ||+ SECRET_PATTERN.test(value)+ ) {+ invalid("error_code must be a lowercase snake_case code");+ }+ return value;+}+function objective(value: unknown) {if (value == null) return null;if (typeof value !== "string") invalid("objective must be a string");@@ -152,6 +166,7 @@ type McpActionLogInput = {client_name: string | null;client_version: string | null;error_class: string | null;+ error_code: string | null;resource: string;objective: string | null;api_job_id: string | null;@@ -233,6 +248,7 @@ export function normalizeMcpActionLogInput(client_name: text(payload.client_name, "client_name"),client_version: text(payload.client_version, "client_version"),error_class: text(payload.error_class, "error_class"),+ error_code: errorCode(payload.error_code),resource,objective: objective(payload.objective),api_job_id: text(payload.api_job_id, "api_job_id"),