mcp icon indicating copy to clipboard operation
mcp copied to clipboard

Invalid regex in generated tool schemas (`[` unescaped inside character class) — MCP unusable with LLM clients that strictly validate tool schemas

Open HosenAlshukri opened this issue 3 months ago • 2 comments

Environment

  • @dokploy/mcp: 0.29.14 (latest on npm as of Aug 2026)
  • Node.js: v24.15.0
  • Dependencies: zod 3.25.28, zod-to-json-schema 3.25.2, @modelcontextprotocol/sdk 1.12.0
  • MCP transport: stdio
  • Client: opencode (also reproducible via any MCP client whose LLM provider strictly validates tool input schemas)
  • OS: Linux

Describe the bug

The MCP server fails to connect with a schema-validation error from the LLM provider. The whole tool list is rejected because one generated tool schema contains an invalid regex.

Full error:

[invalid_request_error] Invalid schema for function 'dokploy-mcp_libsql-create':
"^[a-zA-Z0-9@#%^&*()_+\\-=[\\]{}|;:,.<>?~`]*$" is not a "regex"

Root cause

build/generated/tools.js (auto-generated from openapi.json) declares database password fields with:

z.string().regex(new RegExp("^[a-zA-Z0-9@#%^&*()_+\\-=[\\]{}|;:,.<>?~`]*$"))

The [ inside the character class is unescaped, so this is not a valid ECMA-262 regex. Node's V8 engine is lenient and accepts it at runtime, which is why it passes local testing — but strict validators (JSON Schema pattern validators / LLM provider tool-schema validation) reject it. zod-to-json-schema serializes it verbatim into the JSON Schema pattern keyword.

Corrected pattern: ^[a-zA-Z0-9@#%^&*()_+\-=\[\]{}|;:,.<>?~]*$(escape[and]`).

Steps to reproduce

  1. Install and run the MCP server in stdio mode.
  2. Connect from any MCP client that forwards tool schemas to an LLM provider with strict schema validation.
  3. Observe the connection failing with the error above.

Or without a provider:

// build/generated/tools.js → libsql-create tool
const { generatedTools } = await import("@dokploy/mcp/build/generated/tools.js");
import zodToJsonSchema from "zod-to-json-schema";

const schema = zodToJsonSchema(generatedTools.find(t => t.name === "libsql-create").schema);
console.log(schema.properties.databasePassword.pattern);
// => "^[a-zA-Z0-9@#%^&*()_+\\-=[\\]{}|;:,.<>?~`]*$"  (invalid — unescaped "[" inside class)

Expected behavior

All 546 tool schemas should contain valid ECMA-262 regexes so the server works with any LLM client.

Affected tools — 19 occurrences across 15 tools

Tool Line (build/generated/tools.js)
libsql-create 1836
libsql-update 1956
mariadb-create (×2: databasePassword, databaseRootPassword) 2004
mariadb-update (×2) 2124
mariadb-changePassword 2136
mongo-create 2196
mongo-update 2316
mongo-changePassword 2328
mysql-create (×2) 2460
mysql-update (×2) 2580
mysql-changePassword 2592
postgres-create 3192
postgres-update 3312
postgres-changePassword 3324
redis-changePassword 3720

Suggested fix

  1. Fix the source of truth (the upstream openapi.json / the generator) so the pattern is emitted with escaped brackets: ^[a-zA-Z0-9@#%^&*()_+\-=\[\]{}|;:,.<>?~]*$`
  2. Regenerate via pnpm generate (file header says it's auto-generated, so editing the built file alone will be overwritten).
  3. Consider a CI check that validates every generated pattern compiles as strict ECMA-262 regex (new RegExp(pattern, "u") in Node, or a strict validator) to prevent regressions.

Workaround

Patch the built file before running (19 occurrences of the same string), e.g. replace \\-=[\\]{} with \\-=\\[\\]{} in build/generated/tools.js.

HosenAlshukri avatar Aug 08 '26 15:08 HosenAlshukri

Opened a fix in #71 -- adds a sanitizeRegexPattern pass to the tool-schema generator that escapes bare [, (, ), {, }, and | characters inside character classes, making generated schemas valid under the ECMAScript v (Unicode Sets) flag that strict LLM-client validators use.

Christian-Sidak avatar Aug 09 '26 04:08 Christian-Sidak

Opened a fix in #71 -- Escapes special characters (parentheses, braces, pipe, brackets) inside regex character classes in 19 database password pattern fields across openapi.json and tools.ts generated files, and adds sanitizeRegexPattern/sanitizeSchemaPatterns helpers to generate-tools.ts so future pnpm generate runs emit ECMAScript v-flag-valid patterns. Affects libsql, mariadb, mongo, mysql, postgres, and redis tools..

Christian-Sidak avatar Aug 09 '26 04:08 Christian-Sidak