-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
168 lines (134 loc) Β· 4.9 KB
/
dev-server.js
File metadata and controls
168 lines (134 loc) Β· 4.9 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
#!/usr/bin/env node
/**
* Development Server Helper
*
* Enhanced development server with additional debugging capabilities,
* configuration validation, and development-specific features.
*/
import { readFileSync, existsSync } from 'fs';
import { resolve } from 'path';
import { createLogger } from './src/utils/logger.js';
const logger = createLogger('dev-server');
/**
* Development server configuration
*/
const DEV_CONFIG = {
autoRestart: true,
validateConfig: true,
showStackTraces: true,
enableDebugOutput: true,
logApiCalls: process.env.LOG_API_CALLS === 'true',
mockProviders: process.env.MOCK_PROVIDERS === 'true'
};
/**
* Check if .env file exists and provide helpful guidance
*/
function checkEnvironmentSetup() {
const envPath = resolve('.env');
const envExamplePath = resolve('.env.example');
if (!existsSync(envPath)) {
logger.warn('No .env file found');
if (existsSync(envExamplePath)) {
logger.info('Found .env.example file. Copy it to .env and add your API keys:');
logger.info(' cp .env.example .env');
} else {
logger.info('Create a .env file with your API keys. Example:');
logger.info(' OPENAI_API_KEY=sk-your-key-here');
}
logger.info('You can still run the server, but tools will fail without API keys');
return false;
}
return true;
}
/**
* Display development server information
*/
function showDevInfo() {
logger.info('π Converse MCP Server - Development Mode');
logger.info('βββββββββββββββββββββββββββββββββββββββββββββββββββ');
const hasEnv = checkEnvironmentSetup();
logger.info('Configuration:');
logger.info(` β’ Environment: ${process.env.NODE_ENV || 'development'}`);
logger.info(` β’ Log Level: ${process.env.LOG_LEVEL || 'info'}`);
logger.info(` β’ Port: ${process.env.PORT || '3157'}`);
logger.info(` β’ Auto Restart: ${DEV_CONFIG.autoRestart ? 'β' : 'β'}`);
logger.info(` β’ Environment File: ${hasEnv ? 'β' : 'β'}`);
// Check for API keys
const apiKeys = {
'OpenAI': !!process.env.OPENAI_API_KEY,
'XAI': !!process.env.XAI_API_KEY,
'Google': !!process.env.GOOGLE_API_KEY
};
logger.info('API Keys:');
Object.entries(apiKeys).forEach(([provider, hasKey]) => {
logger.info(` β’ ${provider}: ${hasKey ? 'β' : 'β'}`);
});
if (!Object.values(apiKeys).some(Boolean)) {
logger.warn('β οΈ No API keys configured - tools will fail to execute');
}
logger.info('Development Commands:');
logger.info(' β’ npm run dev - Start with debug logging');
logger.info(' β’ npm run dev:quiet - Start with minimal logging');
logger.info(' β’ npm run dev:verbose - Start with trace logging');
logger.info(' β’ npm run debug - Start with Node.js inspector');
logger.info(' β’ npm run test:watch - Run tests in watch mode');
logger.info('βββββββββββββββββββββββββββββββββββββββββββββββββββ');
}
/**
* Enhanced error handler for development
*/
function setupDevErrorHandlers() {
process.on('uncaughtException', (error) => {
logger.error('π₯ Uncaught Exception in Development Server', { error });
if (DEV_CONFIG.showStackTraces) {
console.error('\nπ Full Stack Trace:');
console.error(error.stack);
}
logger.info('π Server will restart automatically due to --watch flag');
});
process.on('unhandledRejection', (reason, promise) => {
logger.error('π₯ Unhandled Promise Rejection in Development Server', {
error: reason,
data: { promise: promise.toString() }
});
if (DEV_CONFIG.showStackTraces && reason.stack) {
console.error('\nπ Full Stack Trace:');
console.error(reason.stack);
}
logger.info('π Server will restart automatically due to --watch flag');
});
}
/**
* Main development server function
*/
async function startDevServer() {
try {
// Show development information
showDevInfo();
// Set up enhanced error handling
setupDevErrorHandlers();
// Set development environment if not already set
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
// Set debug log level if not already set
if (!process.env.LOG_LEVEL) {
process.env.LOG_LEVEL = 'debug';
}
logger.info('π― Starting MCP Server...');
// Import and start the main server
const { default: main } = await import('./src/index.js');
} catch (error) {
logger.error('π₯ Failed to start development server', { error });
if (DEV_CONFIG.showStackTraces) {
console.error('\nπ Full Stack Trace:');
console.error(error.stack);
}
process.exit(1);
}
}
// Only run if this file is executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
startDevServer();
}
export { startDevServer, DEV_CONFIG };