-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
288 lines (246 loc) · 9.51 KB
/
server.js
File metadata and controls
288 lines (246 loc) · 9.51 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
/**
* Modular MCP Server - Refactored Architecture
* Clean module-based organization with proper initialization
*/
import express from 'express';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import path from 'path';
import { fileURLToPath } from 'url';
// Shared imports
import config from './shared/config/index.js';
import { createLogger } from './shared/utils/logger.js';
import corsMiddleware from './shared/middleware/cors.js';
import { errorHandler, notFoundHandler } from './shared/middleware/error-handler.js';
import { initializeDatabase } from './shared/database/connection.js';
import { getCustomSchema, getHistory, isUsingCustomSchema } from './modules/schema-driven-mcp/lib/schemaStorage.js';
import fs from 'fs';
// Module imports
import * as apiTestingMCP from './modules/api-testing-mcp/index.js';
import * as opendirectMCP from './modules/opendirect-mcp/index.js';
import * as schemaDrivenMCP from './modules/schema-driven-mcp/index.js';
import * as a2aProtocol from './modules/a2a-protocol/index.js';
import * as aiChat from './modules/ai-chat/index.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const log = createLogger('Server');
// ============================================
// EXPRESS APP SETUP
// ============================================
const app = express();
const PORT = parseInt(process.env.PORT || '3001', 10); // Different port for parallel testing
// Trust proxy for Cloud Run / load balancers
app.set('trust proxy', true);
// Middleware
app.use(morgan('dev'));
app.use(corsMiddleware);
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ limit: "50mb", extended: true }));
// ============================================
// DATABASE INITIALIZATION
// ============================================
try {
initializeDatabase();
log.success('Database initialized');
} catch (error) {
log.warn('Database initialization failed - API Testing features may not work');
}
// ============================================
// MODULE INITIALIZATION (BEFORE STATIC FILES!)
// ============================================
const modules = [];
log.info('Initializing modules...\n');
// 1. API Testing MCP
try {
const module = apiTestingMCP.init(app, config.modules.apiTestingMCP);
modules.push(module);
} catch (error) {
log.error(`Failed to initialize API Testing MCP: ${error.message}`);
}
// 2. OpenDirect MCP (Manual)
try {
const module = opendirectMCP.init(app, config.modules.opendirectMCP);
modules.push(module);
} catch (error) {
log.error(`Failed to initialize OpenDirect MCP: ${error.message}`);
}
// 3. Schema-Driven MCP
try {
const module = schemaDrivenMCP.init(app, config.modules.schemaDrivenMCP);
modules.push(module);
} catch (error) {
log.error(`Failed to initialize Schema-Driven MCP: ${error.message}`);
}
// 4. A2A Protocol
// Note: A2A requires schema-driven state, so we need to get it dynamically
try {
// Get the schema-driven state by importing the module's exported state
const { initializeOrReloadServer } = await import('./modules/schema-driven-mcp/server.js');
const schemaDrivenState = initializeOrReloadServer();
const a2aConfig = {
...config.modules.a2aProtocol,
basePath: config.modules.a2aProtocol,
openaiApiKey: config.ai.openai.apiKey,
enabled: true
};
const module = a2aProtocol.init(app, a2aConfig, schemaDrivenState);
modules.push(module);
} catch (error) {
log.error(`Failed to initialize A2A Protocol: ${error.message}`);
}
// 5. AI Chat
try {
const aiChatConfig = {
aiProvider: config.ai.anthropic.apiKey ? 'anthropic' :
config.ai.openai.apiKey ? 'openai' :
config.ai.gemini.apiKey ? 'gemini' : 'none',
mcpServerUrl: `http://localhost:${PORT}${config.modules.apiTestingMCP}`,
basePath: config.modules.aiChat
};
const module = aiChat.init(app, aiChatConfig);
modules.push(module);
} catch (error) {
log.error(`Failed to initialize AI Chat: ${error.message}`);
}
log.info('');
// ============================================
// REST API ROUTES (BEFORE STATIC FILES!)
// ============================================
try {
const { default: mcpRoutes } = await import('./modules/api-testing-mcp/routes/mcpRoutes.js');
app.use('/api', mcpRoutes);
log.success('REST API routes mounted at /api');
} catch (error) {
log.error(`Failed to mount REST API routes: ${error.message}`);
}
// ============================================
// SCHEMA ENDPOINTS
// ============================================
// Read-only schema endpoints (current/history)
app.get('/api/schema/current', (req, res) => {
try {
const customSchema = getCustomSchema();
const schemaData = customSchema || JSON.parse(
fs.readFileSync(path.join(__dirname, 'opendirect-mcp-schema.json'), 'utf-8')
);
res.json({
success: true,
schema: {
name: schemaData.name || 'Unknown',
version: schemaData.version || 'Unknown',
toolsCount: schemaData.tools?.length || 0,
resourcesCount: schemaData.resources?.length || 0,
source: customSchema ? customSchema.source : 'Local Default',
isCustom: isUsingCustomSchema()
}
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
app.get('/api/schema/history', (req, res) => {
res.json({
success: true,
history: getHistory()
});
});
// Schema management routes (register, fetch, reset, test) from schema-driven module
try {
const { default: schemaRoutes } = await import('./modules/schema-driven-mcp/routes/schemaRoutes.js');
app.use('/api', schemaRoutes);
log.success('Schema management routes mounted at /api/schema/*');
} catch (error) {
log.error(`Failed to mount schema management routes: ${error.message}`);
}
// MCP connector routes (validate, get-tools, call-tool) from api-testing module
try {
const { default: mcpConnectorRoutes } = await import('./modules/api-testing-mcp/routes/mcpConnectorRoutes.js');
app.use('/api', mcpConnectorRoutes);
log.success('MCP connector routes mounted at /api/mcp/*');
} catch (error) {
log.error(`Failed to mount MCP connector routes: ${error.message}`);
}
// ============================================
// STATIC FILES (AFTER MODULES AND API ROUTES!)
// ============================================
app.use(express.static(path.join(__dirname, 'client/ui')));
app.use(express.static(path.join(__dirname, 'storage')));
// ============================================
// HEALTH & INFO ENDPOINTS
// ============================================
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
service: 'MCP Config - Modular Architecture (Refactored)',
version: '2.0.0',
port: PORT,
env: config.env,
modules: modules.map(m => ({
name: m.name,
version: m.version,
basePath: m.basePath,
endpoints: m.endpoints
}))
});
});
app.get('/', (req, res) => {
res.json({
name: 'MCP Config Server - Refactored',
version: '2.0.0',
port: PORT,
modules: modules.map(m => ({
name: m.name,
endpoints: m.endpoints
})),
endpoints: {
health: '/health',
modules: modules.reduce((acc, m) => {
acc[m.name] = m.endpoints;
return acc;
}, {})
}
});
});
// Catch-all route for SPA (must be AFTER API/MCP routes)
// This regex excludes: /api, /mcp, /chat, /a2a, /health, /agenticdirect, /schema
app.get(/^\/(?!api|mcp|chat|a2a|health|agenticdirect|schema).*/, function (req, res) {
res.sendFile(path.join(__dirname, 'client/ui', 'index.html'));
});
// ============================================
// ERROR HANDLERS (MUST BE LAST!)
// ============================================
app.use(notFoundHandler);
app.use(errorHandler);
// ============================================
// START SERVER
// ============================================
app.listen(PORT, () => {
console.log('');
console.log('╔══════════════════════════════════════════════════════════════╗');
console.log('║ 🚀 MCP Config Server - Modular Architecture (NEW) ║');
console.log('╟──────────────────────────────────────────────────────────────╢');
console.log(`║ Port: ${PORT.toString().padEnd(46)} ║`);
console.log(`║ Environment: ${config.env.padEnd(46)} ║`);
console.log(`║ Modules: ${modules.length} active${' '.repeat(39)} ║`);
console.log('╟──────────────────────────────────────────────────────────────╢');
modules.forEach(m => {
console.log(`║ ✓ ${m.name.padEnd(55)} ║`);
});
console.log('╚══════════════════════════════════════════════════════════════╝');
console.log('');
log.success(`Refactored server running at http://localhost:${PORT}`);
log.info(`Original server still available at http://localhost:3000`);
log.info('');
log.info('Module Endpoints:');
modules.forEach(m => {
log.info(` ${m.name}:`);
Object.entries(m.endpoints || {}).forEach(([key, value]) => {
log.info(` - ${key}: http://localhost:${PORT}${value}`);
});
});
console.log('');
});
export default app;