mcp-execution-codegen
Progressive loading TypeScript code generation for MCP tools. Achieves 98% token savings by generating one file per tool.
Installation
[]
= "0.9"
Or with cargo-add:
[!IMPORTANT] Requires Rust 1.91 or later.
Usage
Progressive Loading Generation
use ProgressiveGenerator;
use Introspector;
use ;
async
[!TIP] Generated files include: one
.tsfile per tool,index.tsre-exports, and_runtime/mcp-bridge.tshelper.
Generated Files: tsconfig.json is a Leaf Configuration
The generator produces package.json, tsconfig.json, and one .ts file per tool. Both JSON files are regenerated on every generate call — manual edits will be lost.
Important: The generated tsconfig.json is a leaf configuration not intended to be extends-ed by other TypeScript configs. If your own tsconfig.json extends the generated one, "noEmit": true will be inherited, which silently prevents your TypeScript build from emitting output.
Do not extend the generated tsconfig.json. The generated TypeScript files are a standalone package meant to be executed or type-checked separately from your own build:
- Execute directly via a TS-aware runtime:
tsx,deno, or Node.js's native type-stripping. - Or type-check independently: run
tsc -p <generated-dir>as a separate build step. - If using a bundler (esbuild, swc, Vite, etc.) that doesn't enforce TypeScript's
noEmitconstraint, consult your bundler's documentation for mixingnoEmitand emitting configurations.
Token Savings
| Approach | Tokens | Savings |
|---|---|---|
| Traditional (all tools) | ~30,000 | - |
| Progressive (1 tool) | ~500-1,500 | 98% |
Generated TypeScript Structure
Each tool file includes full TypeScript interfaces:
/**
* Creates a new issue in a GitHub repository
* @param params - Tool parameters
* @returns Tool execution result
*/
export async function createIssue(
params: CreateIssueParams
): Promise<CreateIssueResult> {
return (await callMCPTool('github', 'create_issue', params)) as CreateIssueResult;
}
export type CreateIssueParams = {
/** Repository in format "owner/repo" */
repo: string;
/** Issue title */
title: string;
/** Issue body (optional) */
body?: string;
};
Features
- One File Per Tool: Separate TypeScript file for each MCP tool
- Type-Safe Interfaces: Full TypeScript parameter and result types
- JSDoc Documentation: Complete documentation from MCP schemas
- 98% Token Savings: Load only the tools you need
- Handlebars Templates: Customizable code generation
Type Conversion
JSON Schema types are converted to TypeScript:
| JSON Schema | TypeScript |
|---|---|
string |
string |
number |
number |
boolean |
boolean |
array |
T[] |
object |
{ [key: string]: T } |
[!NOTE] Optional parameters use
?suffix in TypeScript interfaces.
Performance
| Metric | Target | Achieved |
|---|---|---|
| 10 tools | <100ms | 0.19ms (526x faster) |
| 50 tools | <20ms | 0.97ms (20.6x faster) |
| VFS export | <10ms | 1.2ms (8.3x faster) |
Security
[!IMPORTANT] All server-controlled strings are sanitized before interpolation into generated TypeScript.
The code generator applies JSDoc sanitization to every field that originates from an MCP server:
- Tool
nameanddescription(truncated to 256 chars,*/escaped, CR/LF stripped) - Server
nameandversion(same rules,versiontruncated to 64 chars) - Every
descriptionfield insideinput_schemaJSON recursively - Categorization fields:
category(128 chars),keywords,short_description(256 chars each) - Non-string
descriptionvalues in schemas are replaced withnull
This prevents malicious MCP servers from injecting arbitrary TypeScript by embedding */ in their metadata.
Related Crates
This crate is part of the mcp-execution workspace:
mcp-execution-core- Foundation typesmcp-execution-introspector- MCP server analysismcp-execution-files- Virtual filesystem for output
MSRV Policy
Minimum Supported Rust Version: 1.91
MSRV increases are considered minor version bumps.
License
Licensed under either of Apache License 2.0 or MIT license at your option.