-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
537 lines (466 loc) · 16.6 KB
/
index.ts
File metadata and controls
537 lines (466 loc) · 16.6 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#!/usr/bin/env node --experimental-strip-types
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
import { z } from 'zod';
import { pino } from 'pino';
import { tmpdir } from 'os';
import { join } from 'path';
import { createServer } from 'http';
import { readFileSync } from 'fs';
// Import platform adapters
import { CloudflareAdapter } from './src/platforms/cloudflare.ts';
import { VercelAdapter } from './src/platforms/vercel.ts';
import { RailwayAdapter } from './src/platforms/railway.ts';
// Import types
import {
CloudPlatform,
DeploymentConfig,
DeploymentConfigSchema,
CostCompareInputSchema,
PlatformRecommendation,
CloudPlatformAdapter
} from './src/types.ts';
// Get version from package.json
const __dirname = import.meta.dirname;
const packageJson = JSON.parse(readFileSync(join(__dirname, './package.json'), 'utf8'));
const VERSION = packageJson.version || '1.0.0';
// Configure logging
const logger = pino({
level: 'info',
transport: {
targets: [
{
target: 'pino/file',
options: { destination: join(tmpdir(), 'chittycloud-mcp-error.log') },
level: 'error'
},
{
target: 'pino/file',
options: { destination: join(tmpdir(), 'chittycloud-mcp.log') },
level: 'info'
}
]
}
});
// Platform adapters
const platformAdapters = new Map<CloudPlatform, CloudPlatformAdapter>();
platformAdapters.set(CloudPlatform.CLOUDFLARE, new CloudflareAdapter());
platformAdapters.set(CloudPlatform.VERCEL, new VercelAdapter());
platformAdapters.set(CloudPlatform.RAILWAY, new RailwayAdapter());
// Authentication store (in production, this would be encrypted/secured)
const authStore = new Map<CloudPlatform, Record<string, string>>();
// Create MCP server
const server = new McpServer({
name: 'chittycloud',
version: VERSION,
description: 'Universal Cloud Platform Operations - Deploy to Cloudflare, Vercel, Railway, and more',
capabilities: {
resources: {},
tools: {},
streaming: {}
}
});
// Deploy tool - Deploy to any cloud platform
server.tool(
'deploy',
'Deploy a project to a specified cloud platform (Cloudflare, Vercel, Railway). Requires authentication for the target platform.',
{
config: DeploymentConfigSchema,
authenticate: z.boolean().optional().describe('Whether to prompt for authentication if not already authenticated'),
},
async ({ config, authenticate = true }) => {
logger.info(`Deploy request for ${config.projectName} on ${config.platform}`);
try {
const adapter = platformAdapters.get(config.platform);
if (!adapter) {
return {
content: [{ type: 'text', text: `Platform ${config.platform} not supported` }],
isError: true
};
}
// Check authentication
if (!adapter.isAuthenticated()) {
if (!authenticate) {
return {
content: [{ type: 'text', text: `Not authenticated with ${config.platform}. Use the 'authenticate' tool first.` }],
isError: true
};
}
return {
content: [{
type: 'text',
text: `Authentication required for ${config.platform}. Please use the 'authenticate' tool first with your API credentials.`
}],
isError: true
};
}
// Perform deployment
const result = await adapter.deploy(config);
return {
content: [{
type: 'text',
text: `🚀 Deployment initiated successfully!
Platform: ${result.platform}
Project: ${config.projectName}
Deployment ID: ${result.deploymentId}
URL: ${result.url}
Status: ${result.status}
Region: ${result.region}
Build Time: ${result.buildTime}ms
Use the 'deployment-status' tool to check progress.`
}]
};
} catch (error) {
logger.error(`Deployment failed: ${error}`);
return {
content: [{ type: 'text', text: `Deployment failed: ${error}` }],
isError: true
};
}
}
);
// Authentication tool
server.tool(
'authenticate',
'Authenticate with a cloud platform using API credentials',
{
platform: z.enum(['cloudflare', 'vercel', 'railway', 'netlify', 'render', 'fly']),
credentials: z.record(z.string(), z.string()).describe('Platform-specific credentials (e.g., apiToken, accountId for Cloudflare)'),
},
async ({ platform, credentials }) => {
logger.info(`Authentication request for ${platform}`);
try {
const adapter = platformAdapters.get(platform);
if (!adapter) {
return {
content: [{ type: 'text', text: `Platform ${platform} not supported` }],
isError: true
};
}
const success = await adapter.authenticate(credentials);
if (success) {
authStore.set(platform, credentials);
return {
content: [{
type: 'text',
text: `✅ Successfully authenticated with ${platform}`
}]
};
} else {
return {
content: [{ type: 'text', text: `❌ Authentication failed for ${platform}` }],
isError: true
};
}
} catch (error) {
logger.error(`Authentication failed: ${error}`);
return {
content: [{ type: 'text', text: `Authentication failed: ${error}` }],
isError: true
};
}
}
);
// Deployment status tool
server.tool(
'deployment-status',
'Check the status of a deployment',
{
platform: z.enum(['cloudflare', 'vercel', 'railway', 'netlify', 'render', 'fly']),
deploymentId: z.string(),
},
async ({ platform, deploymentId }) => {
logger.info(`Status check for deployment ${deploymentId} on ${platform}`);
try {
const adapter = platformAdapters.get(platform);
if (!adapter) {
return {
content: [{ type: 'text', text: `Platform ${platform} not supported` }],
isError: true
};
}
if (!adapter.isAuthenticated()) {
return {
content: [{ type: 'text', text: `Not authenticated with ${platform}` }],
isError: true
};
}
const status = await adapter.getStatus(deploymentId);
const statusEmoji = {
'pending': '⏳',
'building': '🔨',
'ready': '✅',
'error': '❌',
'canceled': '🚫'
}[status.status];
return {
content: [{
type: 'text',
text: `${statusEmoji} Deployment Status
Platform: ${status.platform}
Deployment ID: ${status.deploymentId}
URL: ${status.url}
Status: ${status.status}
Region: ${status.region}
${status.buildTime ? `Build Time: ${status.buildTime}ms` : ''}
Last Updated: ${status.timestamp}`
}]
};
} catch (error) {
logger.error(`Status check failed: ${error}`);
return {
content: [{ type: 'text', text: `Status check failed: ${error}` }],
isError: true
};
}
}
);
// Cost comparison tool
server.tool(
'cost-compare',
'Compare deployment costs across different cloud platforms',
CostCompareInputSchema,
async ({ config, platforms = ['cloudflare', 'vercel', 'railway'] }) => {
logger.info(`Cost comparison for ${config.projectName} across platforms`);
try {
const results = [];
for (const platform of platforms) {
const adapter = platformAdapters.get(platform);
if (!adapter) continue;
try {
if (!adapter.isAuthenticated()) {
results.push(`${platform}: ⚠️ Not authenticated`);
continue;
}
const analytics = await adapter.getAnalytics(config.projectName);
results.push(`${platform}:
💰 Estimated Cost: $${analytics.totalCost.toFixed(2)}/month
📊 Performance Score: ${analytics.performanceScore}/100
⚡ Avg Build Time: ${Math.round(analytics.averageBuildTime / 1000)}s
✅ Success Rate: ${Math.round(analytics.successRate * 100)}%`);
} catch (error) {
results.push(`${platform}: ❌ Error getting data - ${error}`);
}
}
return {
content: [{
type: 'text',
text: `💰 Cost Comparison for "${config.projectName}"
${results.join('\n\n')}
💡 Recommendations:
• Cloudflare: Best for static sites and edge computing (generous free tier)
• Vercel: Best for Next.js and React apps (excellent DX)
• Railway: Best for full-stack apps with databases (simple pricing)`
}]
};
} catch (error) {
logger.error(`Cost comparison failed: ${error}`);
return {
content: [{ type: 'text', text: `Cost comparison failed: ${error}` }],
isError: true
};
}
}
);
// List deployments tool
server.tool(
'list-deployments',
'List all deployments across platforms or for a specific platform',
{
platform: z.enum(['cloudflare', 'vercel', 'railway', 'netlify', 'render', 'fly']).optional().describe('Optional: filter by specific platform'),
limit: z.number().min(1).max(100).optional().describe('Maximum number of deployments to return (default: 20)'),
},
async ({ platform, limit = 20 }) => {
logger.info(`Listing deployments${platform ? ` for ${platform}` : ' across all platforms'}`);
try {
const allDeployments = [];
const platformsToCheck = platform ? [platform] : Array.from(platformAdapters.keys());
for (const platformName of platformsToCheck) {
const adapter = platformAdapters.get(platformName);
if (!adapter) continue;
if (!adapter.isAuthenticated()) {
allDeployments.push({
platform: platformName,
error: 'Not authenticated'
});
continue;
}
try {
const deployments = await adapter.getDeployments();
allDeployments.push(...deployments);
} catch (error) {
allDeployments.push({
platform: platformName,
error: error
});
}
}
// Sort by timestamp (newest first)
const validDeployments = allDeployments.filter(d => !('error' in d));
const errorDeployments = allDeployments.filter(d => 'error' in d);
validDeployments.sort((a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime());
const deployments = validDeployments.slice(0, limit);
if (deployments.length === 0) {
return {
content: [{
type: 'text',
text: `No deployments found${platform ? ` on ${platform}` : ''}.\n${errorDeployments.length > 0 ? '\nErrors:\n' + errorDeployments.map(e => `${e.platform}: ${e.error}`).join('\n') : ''}`
}]
};
}
const deploymentsList = deployments.map(d => {
const statusEmoji = {
'pending': '⏳',
'building': '🔨',
'ready': '✅',
'error': '❌',
'canceled': '🚫'
}[d.status];
const timeAgo = new Date(Date.now() - new Date(d.timestamp).getTime()).toISOString().substr(11, 8);
return `${statusEmoji} ${d.platform.toUpperCase()} | ${d.deploymentId.slice(0, 8)}... | ${d.url} | ${timeAgo} ago`;
}).join('\n');
return {
content: [{
type: 'text',
text: `🚀 Recent Deployments (${deployments.length}/${allDeployments.length})
${deploymentsList}
${errorDeployments.length > 0 ? '\n⚠️ Platform Errors:\n' + errorDeployments.map(e => `${e.platform}: ${e.error}`).join('\n') : ''}`
}]
};
} catch (error) {
logger.error(`List deployments failed: ${error}`);
return {
content: [{ type: 'text', text: `Failed to list deployments: ${error}` }],
isError: true
};
}
}
);
// Platform analytics tool
server.tool(
'platform-analytics',
'Get detailed analytics for a project across all platforms',
{
projectName: z.string(),
platforms: z.array(z.enum(['cloudflare', 'vercel', 'railway', 'netlify', 'render', 'fly'])).optional().describe('Platforms to analyze (default: all authenticated platforms)'),
},
async ({ projectName, platforms }) => {
logger.info(`Getting analytics for project ${projectName}`);
try {
const platformsToCheck = platforms || Array.from(platformAdapters.keys());
const analytics = [];
for (const platform of platformsToCheck) {
const adapter = platformAdapters.get(platform);
if (!adapter || !adapter.isAuthenticated()) continue;
try {
const data = await adapter.getAnalytics(projectName);
analytics.push(data);
} catch (error) {
logger.warn(`Failed to get analytics for ${platform}: ${error}`);
}
}
if (analytics.length === 0) {
return {
content: [{
type: 'text',
text: `No analytics data found for project "${projectName}". Make sure you're authenticated with the platforms and have deployments.`
}]
};
}
const analyticsReport = analytics.map(data => `
📊 ${data.platform.toUpperCase()}
• Total Deployments: ${data.totalDeployments}
• Success Rate: ${Math.round(data.successRate * 100)}%
• Avg Build Time: ${Math.round(data.averageBuildTime / 1000)}s
• Monthly Cost: $${data.totalCost.toFixed(2)}
• Performance Score: ${data.performanceScore}/100
${data.lastDeployment ? `• Last Deploy: ${new Date(data.lastDeployment).toLocaleDateString()}` : ''}
`).join('\n');
// Calculate totals
const totalDeployments = analytics.reduce((sum, a) => sum + a.totalDeployments, 0);
const totalCost = analytics.reduce((sum, a) => sum + a.totalCost, 0);
const avgSuccessRate = analytics.reduce((sum, a) => sum + a.successRate, 0) / analytics.length;
return {
content: [{
type: 'text',
text: `📈 Analytics Report for "${projectName}"
${analyticsReport}
🏆 SUMMARY
• Total Deployments: ${totalDeployments}
• Average Success Rate: ${Math.round(avgSuccessRate * 100)}%
• Total Monthly Cost: $${totalCost.toFixed(2)}
• Platforms Active: ${analytics.length}
💡 Best Performing Platform: ${analytics.sort((a, b) => b.performanceScore - a.performanceScore)[0]?.platform.toUpperCase() || 'N/A'}`
}]
};
} catch (error) {
logger.error(`Analytics failed: ${error}`);
return {
content: [{ type: 'text', text: `Analytics failed: ${error}` }],
isError: true
};
}
}
);
// Determine transport mode
const useHttp = process.env['MCP_HTTP_MODE'] === 'true' || process.argv.includes('--http');
const port = parseInt(process.env['MCP_PORT'] || '3000', 10);
if (useHttp) {
// HTTP mode
logger.info(`Starting ChittyCloud MCP HTTP server on port ${port}`);
const httpServer = createServer(async (req, res) => {
// Basic CORS
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// Health check endpoint
if (req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'healthy',
service: 'chittycloud-mcp',
version: VERSION,
timestamp: new Date().toISOString(),
platforms: {
cloudflare: platformAdapters.get(CloudPlatform.CLOUDFLARE)?.isAuthenticated() || false,
vercel: platformAdapters.get(CloudPlatform.VERCEL)?.isAuthenticated() || false,
railway: platformAdapters.get(CloudPlatform.RAILWAY)?.isAuthenticated() || false,
}
}));
return;
}
// Handle MCP requests
if (req.url === '/' && req.method === 'POST') {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: () => `session-${Date.now()}`
});
await server.connect(transport);
await transport.handleRequest(req, res);
} else {
res.writeHead(404);
res.end('Not Found');
}
});
httpServer.listen(port, () => {
logger.info(`ChittyCloud MCP server version ${VERSION} started on port ${port}`);
logger.info(`Health check: http://localhost:${port}/health`);
});
} else {
// Stdio mode (default)
logger.info('Starting ChittyCloud MCP in stdio mode');
const transport = new StdioServerTransport();
server.connect(transport)
.then(() => {
logger.info(`ChittyCloud MCP server version ${VERSION} started successfully`);
})
.catch((error: Error) => {
logger.error(`Failed to start ChittyCloud MCP server: ${error.message}`);
process.exit(1);
});
}