What Is schemas.ts in Desktop Commander MCP? Purpose and Implementation Guide
schemas.ts serves as the central validation hub where Desktop Commander MCP defines Zod-based schemas for every tool argument, exporting a toolArgSchemas map that enables runtime type-safety and automatic validation of incoming MCP tool calls.
The src/tools/schemas.ts file in the Desktop Commander MCP repository encapsulates all argument validation logic for the Model Context Protocol (MCP) server. By consolidating validation schemas in a single location, the project ensures consistent type checking across UI components, LLM prompts, and server-side execution logic.
Core Purpose of schemas.ts
Centralized Zod Schema Definitions
The primary responsibility of schemas.ts is defining the shape, types, defaults, and constraints for every tool argument using the Zod validation library. Each schema describes exactly what parameters a tool expects, from required file paths to optional numeric offsets.
According to the source code, the file defines schemas ranging from configuration tools to complex PDF manipulation operations. For example, GetConfigArgsSchema (lines 4-8) validates configuration retrieval arguments, while ReadFileArgsSchema (lines 56-68) enforces file reading parameters including path, offset, length, and origin fields.
The toolArgSchemas Lookup Map
Beyond individual schema definitions, the file exports a toolArgSchemas record (lines 49-77) that maps string tool names to their corresponding Zod schemas. This lookup table enables the server dispatcher to dynamically validate incoming requests without hardcoding validation logic for each tool.
export const toolArgSchemas: Record<string, z.ZodTypeAny> = {
read_file: ReadFileArgsSchema,
write_file: WriteFileArgsSchema,
start_process: StartProcessArgsSchema,
// ... additional tool mappings
};
Key Schemas Defined in schemas.ts
The validation logic covers four major functional areas of the Desktop Commander MCP server:
Configuration Tools
GetConfigArgsSchema– Validates arguments for retrieving configuration values (lines 4-8)SetConfigValueArgsSchema– Validates key-value pairs for configuration updates
Process Management Tools
ListProcessesArgsSchema– Defines an empty schema (z.object({})) for parameter-less process listing (line 24)StartProcessArgsSchema– Enforces command strings and working directory paths (lines 27-35)ReadProcessOutputArgsSchema– Validates process ID references for output retrieval
Filesystem Operations
ReadFileArgsSchema– Requirespathstrings with optionaloffsetandlengthnumbers (lines 56-68)WriteFileArgsSchema– Validates file paths and content stringsCreateDirectoryArgsSchema– Ensures directory path parameters meet type requirements
PDF Manipulation
WritePdfArgsSchema– Complex object validation for PDF writing operations (lines 99-120)PdfInsertOperationSchema– Validates insertion points and content blocks within PDF documents
How schemas.ts Integrates with the Server
The src/server.ts dispatcher consumes the toolArgSchemas map to validate incoming tool-call payloads before execution. When the MCP server receives a request, it looks up the appropriate schema by tool name and parses the raw arguments against the Zod definition.
This integration provides three critical benefits:
- Runtime type-safety – Invalid parameters trigger Zod errors before reaching tool implementations
- Clear error messages – Validation failures return descriptive messages indicating exactly which fields failed constraints
- Single source of truth – Changes to tool signatures require updates only in
schemas.ts, automatically propagating to all consumers
Practical Implementation Examples
Validating Tool Calls Manually
You can import individual schemas to validate arguments in isolation:
import { ReadFileArgsSchema } from './tools/schemas';
const payload = {
path: '/tmp/example.txt',
offset: 0,
length: 200,
origin: 'ui',
};
try {
const args = ReadFileArgsSchema.parse(payload);
console.log('Validated args:', args);
} catch (e) {
console.error('Invalid arguments:', e.errors);
}
Server Dispatcher Integration
The toolArgSchemas map enables dynamic validation in the request handler:
import { toolArgSchemas } from './tools/schemas';
function validateToolCall(toolName: string, rawArgs: unknown) {
const schema = toolArgSchemas[toolName];
if (!schema) {
throw new Error(`Unknown tool: ${toolName}`);
}
return schema.parse(rawArgs);
}
// In request handler:
const { tool, args } = req.body;
const validatedArgs = validateToolCall(tool, args);
Extending with New Tools
Adding support for new capabilities requires only two steps in schemas.ts:
// 1. Define the schema
export const MyNewToolArgsSchema = z.object({
target: z.string(),
priority: z.number().default(1),
});
// 2. Register in the map
toolArgSchemas['my_new_tool'] = MyNewToolArgsSchema;
Summary
- schemas.ts acts as the single source of truth for all tool argument validation in Desktop Commander MCP.
- The file defines Zod schemas for configuration, process management, filesystem, and PDF tools.
- The exported
toolArgSchemasmap (lines 49-77) enables the server dispatcher to validate incoming MCP requests dynamically. - Runtime validation occurs in
src/server.tsbefore tool execution, ensuring type safety and clear error reporting. - New tools integrate seamlessly by adding schemas to
src/tools/schemas.tsand registering them in the lookup map.
Frequently Asked Questions
What validation library does schemas.ts use?
The file uses Zod, a TypeScript-first schema validation library with static type inference. Zod enables both runtime validation and automatic TypeScript type generation, ensuring that validated data matches expected interfaces without redundant type definitions.
How does schemas.ts handle tools without parameters?
For tools requiring no arguments, such as process listing operations, the file exports empty schemas using z.object({}). The ListProcessesArgsSchema (line 24) demonstrates this pattern, allowing the tool to be called with an empty object while still passing validation.
Where is the toolArgSchemas map consumed?
The toolArgSchemas map is consumed primarily in src/server.ts, where the MCP server dispatcher uses it to validate incoming tool invocations. Before routing a request to the concrete tool implementation in src/tools/*, the server retrieves the appropriate schema from this map and parses the arguments against it.
Can I extend schemas.ts with custom tool definitions?
Yes, extension requires defining a new Zod schema constant and adding it to the toolArgSchemas record. The schema should follow the naming convention [ToolName]ArgsSchema, and the map entry must use the exact tool name string expected by MCP clients. This maintains consistency with the server's validation pipeline.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →