1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
# Build MCP Server for Production
Builds and prepares your MCP server for production deployment.
## Usage
```
/build [target] [options]
```
## Targets
- `node` - Build for Node.js (default)
- `docker` - Build Docker image
- `npm` - Prepare for npm publishing
## Options
- `--minify` - Minify output
- `--sourcemap` - Include source maps
- `--analyze` - Analyze bundle size
## Implementation
```typescript
import { exec } from 'child_process';
import { promisify } from 'util';
import * as fs from 'fs/promises';
import * as path from 'path';
const execAsync = promisify(exec);
async function buildServer(
target: 'node' | 'docker' | 'npm' = 'node',
options: {
minify?: boolean;
sourcemap?: boolean;
analyze?: boolean;
} = {}
) {
console.log('šØ Building MCP Server for Production');
console.log('='.repeat(50));
// Pre-build checks
await runPreBuildChecks();
// Build based on target
switch (target) {
case 'node':
await buildForNode(options);
break;
case 'docker':
await buildForDocker(options);
break;
case 'npm':
await buildForNpm(options);
break;
}
// Post-build validation
await validateBuild(target);
console.log('\nā
Build completed successfully!');
}
async function runPreBuildChecks() {
console.log('\nš Running pre-build checks...');
// Check for uncommitted changes
try {
const { stdout: gitStatus } = await execAsync('git status --porcelain');
if (gitStatus.trim()) {
console.warn('ā ļø Warning: You have uncommitted changes');
}
} catch {
// Git not available or not a git repo
}
// Run tests
console.log(' Running tests...');
try {
await execAsync('npm test');
console.log(' ā
Tests passed');
} catch (error) {
console.error(' ā Tests failed');
throw new Error('Build aborted: tests must pass');
}
// Check dependencies
console.log(' Checking dependencies...');
try {
await execAsync('npm audit --production');
console.log(' ā
No vulnerabilities found');
} catch (error) {
console.warn(' ā ļø Security vulnerabilities detected');
console.log(' Run "npm audit fix" to resolve');
}
}
async function buildForNode(options: any) {
console.log('\nš¦ Building for Node.js...');
// Clean previous build
await fs.rm('dist', { recursive: true, force: true });
// Update tsconfig for production
const tsConfig = JSON.parse(await fs.readFile('tsconfig.json', 'utf-8'));
const prodConfig = {
...tsConfig,
compilerOptions: {
...tsConfig.compilerOptions,
sourceMap: options.sourcemap || false,
inlineSources: false,
removeComments: true,
},
};
await fs.writeFile('tsconfig.prod.json', JSON.stringify(prodConfig, null, 2));
// Build with TypeScript
console.log(' Compiling TypeScript...');
await execAsync('npx tsc -p tsconfig.prod.json');
// Minify if requested
if (options.minify) {
console.log(' Minifying code...');
await minifyCode();
}
// Copy package files
console.log(' Copying package files...');
await fs.copyFile('package.json', 'dist/package.json');
await fs.copyFile('README.md', 'dist/README.md').catch(() => {});
await fs.copyFile('LICENSE', 'dist/LICENSE').catch(() => {});
// Create production package.json
const pkg = JSON.parse(await fs.readFile('package.json', 'utf-8'));
const prodPkg = {
...pkg,
scripts: {
start: 'node index.js',
},
devDependencies: undefined,
};
await fs.writeFile('dist/package.json', JSON.stringify(prodPkg, null, 2));
// Analyze bundle if requested
if (options.analyze) {
await analyzeBundleSize();
}
console.log(' ā
Node.js build complete');
console.log(' Output: ./dist');
}
async function buildForDocker(options: any) {
console.log('\nš Building Docker image...');
// Build Node.js first
await buildForNode(options);
// Create Dockerfile if it doesn't exist
const dockerfilePath = 'Dockerfile';
if (!await fs.access(dockerfilePath).then(() => true).catch(() => false)) {
await createDockerfile();
}
// Build Docker image
console.log(' Building Docker image...');
const imageName = 'mcp-server';
const version = JSON.parse(await fs.readFile('package.json', 'utf-8')).version;
await execAsync(`docker build -t ${imageName}:${version} -t ${imageName}:latest .`);
// Test the image
console.log(' Testing Docker image...');
const { stdout } = await execAsync(`docker run --rm ${imageName}:latest node index.js --version`);
console.log(` Version: ${stdout.trim()}`);
console.log(' ā
Docker build complete');
console.log(` Image: ${imageName}:${version}`);
console.log(' Run: docker run -it ' + imageName + ':latest');
}
async function buildForNpm(options: any) {
console.log('\nš¦ Preparing for npm publishing...');
// Build Node.js first
await buildForNode(options);
// Validate package.json
const pkg = JSON.parse(await fs.readFile('package.json', 'utf-8'));
if (!pkg.name) {
throw new Error('package.json must have a name field');
}
if (!pkg.version) {
throw new Error('package.json must have a version field');
}
if (!pkg.description) {
console.warn('ā ļø Warning: package.json should have a description');
}
// Create .npmignore if needed
const npmignorePath = '.npmignore';
if (!await fs.access(npmignorePath).then(() => true).catch(() => false)) {
await createNpmIgnore();
}
// Run npm pack to test
console.log(' Creating package tarball...');
const { stdout } = await execAsync('npm pack --dry-run');
// Parse package contents
const files = stdout.split('\n').filter(line => line.includes('npm notice'));
console.log(` Package will include ${files.length} files`);
// Check package size
const sizeMatch = stdout.match(/npm notice ([\d.]+[kMG]B)/);
if (sizeMatch) {
console.log(` Package size: ${sizeMatch[1]}`);
// Warn if package is large
if (sizeMatch[1].includes('M') && parseFloat(sizeMatch[1]) > 10) {
console.warn('ā ļø Warning: Package is larger than 10MB');
}
}
console.log(' ā
npm package ready');
console.log(' Publish with: npm publish');
}
async function createDockerfile() {
const dockerfile = `
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage
FROM node:20-alpine
WORKDIR /app
# Install dumb-init for signal handling
RUN apk add --no-cache dumb-init
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \\
adduser -S nodejs -u 1001
# Copy built application
COPY --from=builder /app/dist ./
COPY --from=builder /app/node_modules ./node_modules
# Change ownership
RUN chown -R nodejs:nodejs /app
USER nodejs
EXPOSE 3000
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "index.js"]
`;
await fs.writeFile('Dockerfile', dockerfile);
console.log(' Created Dockerfile');
}
async function createNpmIgnore() {
const npmignore = `
# Source files
src/
tests/
# Config files
.eslintrc*
.prettierrc*
tsconfig*.json
vitest.config.ts
# Development files
*.log
.env
.env.*
!.env.example
# Build artifacts
*.tsbuildinfo
coverage/
.nyc_output/
# Docker
Dockerfile
docker-compose.yml
# Git
.git/
.gitignore
# CI/CD
.github/
.gitlab-ci.yml
# IDE
.vscode/
.idea/
# Misc
.DS_Store
Thumbs.db
`;
await fs.writeFile('.npmignore', npmignore);
console.log(' Created .npmignore');
}
async function minifyCode() {
// Use esbuild or terser to minify
try {
await execAsync('npx esbuild dist/**/*.js --minify --outdir=dist --allow-overwrite');
} catch {
console.warn(' Minification skipped (esbuild not available)');
}
}
async function analyzeBundleSize() {
console.log('\nš Analyzing bundle size...');
const files = await fs.readdir('dist', { recursive: true });
let totalSize = 0;
const fileSizes: Array<{ name: string; size: number }> = [];
for (const file of files) {
const filePath = path.join('dist', file as string);
const stat = await fs.stat(filePath);
if (stat.isFile()) {
totalSize += stat.size;
fileSizes.push({ name: file as string, size: stat.size });
}
}
// Sort by size
fileSizes.sort((a, b) => b.size - a.size);
console.log(' Largest files:');
fileSizes.slice(0, 5).forEach(file => {
console.log(` ${file.name}: ${(file.size / 1024).toFixed(2)}KB`);
});
console.log(` Total size: ${(totalSize / 1024).toFixed(2)}KB`);
}
async function validateBuild(target: string) {
console.log('\nš Validating build...');
// Check that main file exists
const mainFile = 'dist/index.js';
if (!await fs.access(mainFile).then(() => true).catch(() => false)) {
throw new Error('Build validation failed: main file not found');
}
// Try to load the server
try {
await import(path.resolve(mainFile));
console.log(' ā
Server module loads successfully');
} catch (error) {
throw new Error(`Build validation failed: ${error.message}`);
}
}
```
|