Repository navigation
Conversation
anilonayy
force-pushed
the
perf/lazy-tool-schemas
branch
from
October 12, 2026 00:19
c7d829b to
f95ec8a
Compare
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
force-pushed
the
perf/lazy-tool-schemas
branch
from
October 12, 2026 07:08
f95ec8a to
f8b88bb
Compare
Author
|
Hi @Siumauricio, friendly ping on this one. It addresses #63 by building tool schemas on demand: on the |
Author
|
Hi @Siumauricio, following up on this. It is still mergeable against |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.tsconstructs all 604z.object(...)schemas at import time, even for presets that load a handful of tools.createServer()registers each tool throughMcpServer.tool(...), so the SDK builds and retains its own validator per tool, and the code also keeps 604 converted JSON schemas.Change
Lazy schemas (generator + generated file). Each tool's
schemais now a factory() => z.object(...)instead of a built schema, so importing the module no longer constructs 604 schema trees.ToolDefinition.schemais typed accordingly, and the generator emits the factory.Build schemas on demand (
server.ts). Instead ofMcpServer.tool()for all tools, the server registers thetoolscapability and thetools/list/tools/callhandlers 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/liststill returns the draft-2020-12 schemas (so the regex/$schemahandling from #32/#58/#68 is untouched).Measurements
Node v26.7.0, full toolset (
DOKPLOY_TOOL_PRESET=all), RSS andheapUsedaftertools/listand a forced GC, median of 5 runs:heapUsed(live, retained)The retained live heap is the memory the tool definitions actually hold; it drops ~39%. RSS falls less because
tools/liststill 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 insrc/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-checkpasses.biome checkis clean on the changed source files. (pnpm run precommitalso reports a pre-existing format error insrc/utils/redactSensitive.test.tsand anoStaticOnlyClasswarning inresponseFormatter.tsthat are already onmain— see chore: format redactSensitive.test.ts so precommit passes #75 — I left those alone.)pnpm generatereproduces the generated file.