Skip to content

perf: build tool schemas on demand to cut per-process memory - #92

Open
anilonayy wants to merge 2 commits into
Dokploy:mainfrom
anilonayy:perf/lazy-tool-schemas
Open

anilonayy wants to merge 2 commits into
Dokploy:mainfrom
anilonayy:perf/lazy-tool-schemas

Conversation

@anilonayy

Copy link
Copy Markdown

Addresses #63.

Problem

The server builds a Zod schema for every one of the 604 tools at startup and keeps them for the life of the process:

  • src/generated/tools.ts constructs all 604 z.object(...) schemas at import time, even for presets that load a handful of tools.
  • createServer() registers each tool through McpServer.tool(...), so the SDK builds and retains its own validator per tool, and the code also keeps 604 converted JSON schemas.

Change

  1. Lazy schemas (generator + generated file). Each tool's schema is now a factory () => z.object(...) instead of a built schema, so importing the module no longer constructs 604 schema trees. ToolDefinition.schema is typed accordingly, and the generator emits the factory.

  2. Build schemas on demand (server.ts). Instead of McpServer.tool() for all tools, the server registers the tools capability and the tools/list / tools/call handlers directly. The advertised draft-2020-12 JSON schemas are built once at startup and the Zod schemas are released; each tool's Zod schema is rebuilt and cached only when that tool is actually called.

Behavior is unchanged: input is still validated against the same Zod schema before dispatch, GET/POST routing and annotations are the same, and tools/list still returns the draft-2020-12 schemas (so the regex/$schema handling from #32/#58/#68 is untouched).

Measurements

Node v26.7.0, full toolset (DOKPLOY_TOOL_PRESET=all), RSS and heapUsed after tools/list and a forced GC, median of 5 runs:

Metric Before After Change
heapUsed (live, retained) 33.7 MB 20.7 MB −39%
RSS 146.4 MB 132.6 MB −9%

The retained live heap is the memory the tool definitions actually hold; it drops ~39%. RSS falls less because tools/list still builds all 604 Zod schemas transiently to convert them, and V8 does not return that peak to the OS. Precomputing the JSON schemas at generate time would remove that transient and cut RSS further; I kept this PR to the lazy/on-demand change so it does not touch the schema-conversion path other open PRs edit — happy to follow up with the build-time precompute if you'd like it.

The absolute numbers are higher than the 89 MB in the issue because this was measured on Node 26 (Node itself is ~70 MB here before any tools); the relevant figure is the proportional reduction.

Testing

  • pnpm test — 50 passing (45 existing + 5 new in src/handler.call.test.ts: valid input dispatches to the API client, POST body dispatch, invalid input is rejected before the client is called, a tool outside the active preset returns an error, schemas are built lazily).
  • pnpm run type-check passes. biome check is clean on the changed source files. (pnpm run precommit also reports a pre-existing format error in src/utils/redactSensitive.test.ts and a noStaticOnlyClass warning in responseFormatter.ts that are already on main — see chore: format redactSensitive.test.ts so precommit passes #75 — I left those alone.)
  • pnpm generate reproduces the generated file.

@anilonayy
anilonayy force-pushed the perf/lazy-tool-schemas branch from c7d829b to f95ec8a Compare October 12, 2026 00:19
The generated tools module built a Zod schema for every one of the 604
tools at import time, regardless of which preset was active. Emit each
schema as a factory (`() => z.object(...)`) instead, so importing the
module no longer constructs 604 schema trees and a tool's schema is only
built when it is needed.
createServer() registered every tool through McpServer.tool(), which made
the SDK build and retain a Zod schema per tool for the whole process
lifetime, and it also kept 604 converted JSON schemas. Register the tools
capability and the list/call handlers directly instead: the advertised
JSON schemas are built once and the Zod schemas are released, and each
tool's Zod schema is rebuilt and cached only when that tool is called.

Input validation, GET/POST dispatch, annotations and the draft-2020-12
list schemas are unchanged; new tests cover the call path (valid input,
invalid input, and a tool outside the active toolset).

Addresses Dokploy#63.
@anilonayy
anilonayy force-pushed the perf/lazy-tool-schemas branch from f95ec8a to f8b88bb Compare October 12, 2026 07:08
@anilonayy

Copy link
Copy Markdown
Author

Hi @Siumauricio, friendly ping on this one. It addresses #63 by building tool schemas on demand: on the all preset the live heap went from ~33.7 MB to ~20.7 MB (median of 5 runs), with 5 new CallTool tests and type-check clean. Still mergeable against main. Glad to split it or change the approach if you prefer.

@anilonayy

Copy link
Copy Markdown
Author

Hi @Siumauricio, following up on this. It is still mergeable against main and addresses #63. If building schemas on demand is not the direction you want, let me know and I will rework or close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant