-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathrouter.ts
More file actions
615 lines (540 loc) · 20.3 KB
/
router.ts
File metadata and controls
615 lines (540 loc) · 20.3 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* Copyright Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { MiddlewareFactory } from '@backstage/backend-defaults/rootHttpRouter';
import { NotAllowedError } from '@backstage/errors';
import { createPermissionIntegrationRouter } from '@backstage/plugin-permission-node';
import express, { Router } from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import {
lightspeedChatCreatePermission,
lightspeedChatDeletePermission,
lightspeedChatReadPermission,
lightspeedMcpManagePermission,
lightspeedMcpReadPermission,
lightspeedPermissions,
} from '@red-hat-developer-hub/backstage-plugin-lightspeed-common';
import { Readable } from 'node:stream';
import { McpUserSettingsStore } from './mcp-server-store';
import {
McpServerResponse,
McpServerStatus,
McpValidationResult,
} from './mcp-server-types';
import { McpServerValidator } from './mcp-server-validator';
import { userPermissionAuthorization } from './permission';
import {
DEFAULT_HISTORY_LENGTH,
QueryRequestBody,
RouterOptions,
} from './types';
import { validateCompletionsRequest } from './validation';
const SKIP_USER_ID_ENDPOINTS = new Set(['/v1/models', '/v1/shields']);
interface StaticMcpServer {
name: string;
token?: string;
}
/**
* Build MCP-HEADERS for LCS. Format matches the LCS "client" auth model:
* { "server-name": { "Authorization": "Bearer <token>" } }
*
* For each admin-configured server, includes the user's override token if
* present in the DB, falling back to the admin default from app-config.
* Servers the user has disabled are excluded.
*/
async function buildMcpHeaders(
servers: StaticMcpServer[],
store: McpUserSettingsStore,
userEntityRef: string,
): Promise<string> {
const headers: Record<string, { Authorization: string }> = {};
const userSettings = await store.listByUser(userEntityRef);
const settingsMap = new Map(userSettings.map(s => [s.server_name, s]));
for (const server of servers) {
const setting = settingsMap.get(server.name);
const enabled = setting ? Boolean(setting.enabled) : true;
if (!enabled) continue;
// User's personal token (DB) takes precedence over admin default (app-config).
// If the user hasn't set one, falls back to the config token.
// If neither exists, the server is excluded from MCP-HEADERS.
const token = setting?.token || server.token;
if (token) {
headers[server.name] = { Authorization: `Bearer ${token}` };
}
}
return Object.keys(headers).length > 0 ? JSON.stringify(headers) : '';
}
/**
* @public
* The lightspeed backend router
*/
export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger, config, database, httpAuth, userInfo, permissions } = options;
const router = Router();
router.use(express.json());
const port = config.getOptionalNumber('lightspeed.servicePort') ?? 8080;
const system_prompt = config.getOptionalString('lightspeed.systemPrompt');
// Parse admin-configured MCP servers from app-config.
// Only name is required; token is optional (users can provide their own via the UI).
// URLs come from LCS (GET /v1/mcp-servers), not from app-config.
const mcpServersConfig = config.getOptionalConfigArray(
'lightspeed.mcpServers',
);
const staticServers: StaticMcpServer[] = [];
if (mcpServersConfig) {
for (const mcpServer of mcpServersConfig) {
staticServers.push({
name: mcpServer.getString('name'),
token: mcpServer.getOptionalString('token'),
});
}
}
// Initialize database-backed store for per-user preferences and validator
const dbClient = await database.getClient();
const settingsStore = new McpUserSettingsStore(dbClient);
const mcpValidator = new McpServerValidator(logger);
// URL cache populated from LCS GET /v1/mcp-servers.
// The canonical URL for each MCP server lives in LCS config, not app-config.
const lcsUrlCache = new Map<string, string>();
async function refreshLcsUrlCache(): Promise<void> {
try {
const response = await fetch(`http://0.0.0.0:${port}/v1/mcp-servers`);
if (!response.ok) {
logger.warn(
`Failed to fetch MCP server URLs from LCS: HTTP ${response.status}`,
);
return;
}
const data = (await response.json()) as {
servers: Array<{ name: string; url: string }>;
};
for (const s of data.servers) {
lcsUrlCache.set(s.name, s.url);
}
logger.info(`Cached ${lcsUrlCache.size} MCP server URL(s) from LCS`);
} catch (error) {
logger.warn(`Failed to fetch MCP server URLs from LCS: ${error}`);
}
}
function resolveServerUrl(serverName: string): string | undefined {
return lcsUrlCache.get(serverName);
}
// Best-effort URL cache on startup (non-blocking)
refreshLcsUrlCache().catch(() => {});
router.get('/health', (_, response) => {
response.json({ status: 'ok' });
});
const permissionIntegrationRouter = createPermissionIntegrationRouter({
permissions: lightspeedPermissions,
});
router.use(permissionIntegrationRouter);
const authorizer = userPermissionAuthorization(permissions);
// ─── MCP Server Management Endpoints ────────────────────────────────
// All MCP servers are admin-configured (static). Users can view the
// list, toggle servers on/off, and provide personal access tokens.
router.get('/mcp-servers', async (req, res) => {
try {
const credentials = await httpAuth.credentials(req);
await authorizer.authorizeUser(lightspeedMcpReadPermission, credentials);
const user = await userInfo.getUserInfo(credentials);
const userSettings = await settingsStore.listByUser(user.userEntityRef);
const settingsMap = new Map(userSettings.map(s => [s.server_name, s]));
const servers: McpServerResponse[] = staticServers.map(server => {
const setting = settingsMap.get(server.name);
return {
name: server.name,
url: resolveServerUrl(server.name),
enabled: setting ? Boolean(setting.enabled) : true,
status: (setting?.status as McpServerStatus) ?? 'unknown',
toolCount: setting?.tool_count ?? 0,
hasToken: !!(setting?.token || server.token),
};
});
res.json({ servers });
} catch (error) {
if (error instanceof NotAllowedError) {
res.status(403).json({ error: error.message });
} else {
logger.error(`Error listing MCP servers: ${error}`);
res.status(500).json({ error: 'Failed to list MCP servers' });
}
}
});
router.post('/mcp-servers/validate', async (req, res) => {
try {
const credentials = await httpAuth.credentials(req);
await authorizer.authorizeUser(lightspeedMcpReadPermission, credentials);
const { url, token } = req.body;
if (!url || !token) {
res.status(400).json({ error: 'url and token are required' });
return;
}
const result = await mcpValidator.validate(url, token);
res.json(result);
} catch (error) {
if (error instanceof NotAllowedError) {
res.status(403).json({ error: error.message });
} else {
logger.error(`Error validating MCP credentials: ${error}`);
res.status(500).json({ error: 'Validation failed' });
}
}
});
router.post('/mcp-servers/:name/validate', async (req, res) => {
try {
const credentials = await httpAuth.credentials(req);
await authorizer.authorizeUser(lightspeedMcpReadPermission, credentials);
const user = await userInfo.getUserInfo(credentials);
const { name } = req.params;
const server = staticServers.find(s => s.name === name);
if (!server) {
res.status(404).json({
error: `MCP server '${name}' not found in configuration`,
});
return;
}
// Resolve URL: config override → LCS cache → fresh LCS fetch
let serverUrl = resolveServerUrl(server.name);
if (!serverUrl) {
await refreshLcsUrlCache();
serverUrl = resolveServerUrl(server.name);
}
if (!serverUrl) {
res
.status(400)
.json({ error: 'Server has no URL — not found in LCS or config' });
return;
}
const setting = await settingsStore.get(name, user.userEntityRef);
const effectiveToken = setting?.token || server.token;
if (!effectiveToken) {
res
.status(400)
.json({ error: 'No token available — provide one first' });
return;
}
const validation = await mcpValidator.validate(serverUrl, effectiveToken);
const status: McpServerStatus = validation.valid ? 'connected' : 'error';
await settingsStore.updateStatus(
name,
user.userEntityRef,
status,
validation.toolCount,
);
res.json({
name,
status,
toolCount: validation.toolCount,
validation,
});
} catch (error) {
if (error instanceof NotAllowedError) {
res.status(403).json({ error: error.message });
} else {
logger.error(`Error validating MCP server: ${error}`);
res.status(500).json({ error: 'Validation failed' });
}
}
});
router.patch('/mcp-servers/:name', async (req, res) => {
try {
const credentials = await httpAuth.credentials(req);
await authorizer.authorizeUser(
lightspeedMcpManagePermission,
credentials,
);
const user = await userInfo.getUserInfo(credentials);
const { name } = req.params;
const server = staticServers.find(s => s.name === name);
if (!server) {
res.status(404).json({
error: `MCP server '${name}' not found in configuration`,
});
return;
}
const { enabled, token } = req.body;
if (enabled === undefined && token === undefined) {
res.status(400).json({
error: 'At least one of enabled or token must be provided',
});
return;
}
const setting = await settingsStore.upsert(name, user.userEntityRef, {
enabled,
token,
});
let validation: McpValidationResult | undefined;
const serverUrl = resolveServerUrl(server.name);
if (token && serverUrl) {
validation = await mcpValidator.validate(serverUrl, token);
const newStatus: McpServerStatus = validation.valid
? 'connected'
: 'error';
await settingsStore.updateStatus(
name,
user.userEntityRef,
newStatus,
validation.toolCount,
);
setting.status = newStatus;
setting.tool_count = validation.toolCount;
}
const result: Record<string, unknown> = {
server: {
name: server.name,
url: resolveServerUrl(server.name),
enabled: Boolean(setting.enabled),
status: setting.status as McpServerStatus,
toolCount: setting.tool_count,
hasToken: !!(setting.token || server.token),
} as McpServerResponse,
};
if (validation) result.validation = validation;
res.json(result);
} catch (error) {
if (error instanceof NotAllowedError) {
res.status(403).json({ error: error.message });
} else {
logger.error(`Error updating MCP server settings: ${error}`);
res.status(500).json({ error: 'Failed to update MCP server settings' });
}
}
});
// ─── Proxy Middleware (existing) ────────────────────────────────────
router.use('/', async (req, res, next) => {
const passthroughPaths = ['/v1/query', '/v1/feedback'];
if (passthroughPaths.includes(req.path) || req.method === 'PUT') {
return next();
}
// TODO: parse server_id from req.body and get URL and token when multi-server is supported
const credentials = await httpAuth.credentials(req);
const user = await userInfo.getUserInfo(credentials);
const userEntity = user.userEntityRef;
logger.info(`receives call from user: ${userEntity}`);
try {
if (req.method === 'GET') {
await authorizer.authorizeUser(
lightspeedChatReadPermission,
credentials,
);
} else if (req.method === 'DELETE') {
await authorizer.authorizeUser(
lightspeedChatDeletePermission,
credentials,
);
}
} catch (error) {
if (error instanceof NotAllowedError) {
logger.error(error.message);
return res.status(403).json({ error: error.message });
}
}
// Proxy middleware configuration
const apiProxy = createProxyMiddleware({
target: `http://0.0.0.0:${port}`,
changeOrigin: true,
pathRewrite: (path, _) => {
const isSkippable = Array.from(SKIP_USER_ID_ENDPOINTS).some(endpoint =>
path.startsWith(endpoint),
);
if (isSkippable) {
return path;
}
let newPath = path;
// Add user_id
const userQueryParam = `user_id=${encodeURIComponent(userEntity)}`;
newPath = path.includes('?')
? `${path}&${userQueryParam}`
: `${path}?${userQueryParam}`;
// Add history_length if needed
if (
!path.includes('history_length') &&
path.includes('conversation_id')
) {
const historyLengthQuery = `history_length=${DEFAULT_HISTORY_LENGTH}`;
newPath = newPath.includes('?')
? `${newPath}&${historyLengthQuery}`
: `${newPath}?${historyLengthQuery}`;
}
logger.info(`Rewriting path from ${path} to ${newPath}`);
return newPath;
},
});
return apiProxy(req, res, next);
});
router.post('/v1/feedback', async (request, response) => {
try {
const credentials = await httpAuth.credentials(request);
const userEntity = await userInfo.getUserInfo(credentials);
const user_id = userEntity.userEntityRef;
logger.info(`/v1/feedback receives call from user: ${user_id}`);
await authorizer.authorizeUser(
lightspeedChatCreatePermission,
credentials,
);
const userQueryParam = `user_id=${encodeURIComponent(user_id)}`;
const requestBody = JSON.stringify(request.body);
const fetchResponse = await fetch(
`http://0.0.0.0:${port}/v1/feedback?${userQueryParam}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: requestBody,
},
);
if (!fetchResponse.ok) {
// Read the error body
const errorBody = await fetchResponse.json();
const errormsg = `Error from lightspeed-core server: ${errorBody.error?.message || errorBody?.detail?.cause || 'Unknown error'}`;
logger.error(errormsg);
// Return a 500 status for any upstream error
response.status(500).json({
error: errormsg,
});
}
const data = await fetchResponse.json();
response.status(fetchResponse.status).json(data);
} catch (error) {
const errormsg = `Error while sending feedback: ${error}`;
logger.error(errormsg);
if (error instanceof NotAllowedError) {
response.status(403).json({ error: error.message });
} else {
response.status(500).json({ error: errormsg });
}
}
});
router.post(
'/v1/query',
validateCompletionsRequest,
async (request, response) => {
const { provider }: Pick<QueryRequestBody, 'provider'> = request.body;
try {
const credentials = await httpAuth.credentials(request);
const userEntity = await userInfo.getUserInfo(credentials);
const user_id = userEntity.userEntityRef;
logger.info(`/v1/query receives call from user: ${user_id}`);
await authorizer.authorizeUser(
lightspeedChatCreatePermission,
credentials,
);
const userQueryParam = `user_id=${encodeURIComponent(user_id)}`;
request.body.media_type = 'application/json'; // set media_type to receive start and end event
// if system_prompt is defined in lightspeed config
// set system_prompt to override the default rhdh system prompt
if (system_prompt && system_prompt.trim().length > 0) {
request.body.system_prompt = system_prompt;
}
const requestBody = JSON.stringify(request.body);
// Build MCP headers from config servers + this user's preferences
const mcpHeadersValue = await buildMcpHeaders(
staticServers,
settingsStore,
user_id,
);
const fetchResponse = await fetch(
`http://0.0.0.0:${port}/v1/streaming_query?${userQueryParam}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'MCP-HEADERS': mcpHeadersValue,
},
body: requestBody,
},
);
if (!fetchResponse.ok) {
// Read the error body
const errorBody = await fetchResponse.json();
const errormsg = `Error from lightspeed-core server: ${errorBody.error?.message || errorBody?.detail?.cause || 'Unknown error'}`;
logger.error(errormsg);
// Return a 500 status for any upstream error
response.status(500).json({
error: errormsg,
});
return;
}
// Pipe the response back to the original response
if (fetchResponse.body) {
const nodeStream = Readable.fromWeb(fetchResponse.body as any);
nodeStream.pipe(response);
}
} catch (error) {
const errormsg = `Error fetching completions from ${provider}: ${error}`;
logger.error(errormsg);
if (error instanceof NotAllowedError) {
response.status(403).json({ error: error.message });
} else {
response.status(500).json({ error: errormsg });
}
}
},
);
router.put(
'/v2/conversations/:conversation_id',
async (request, response) => {
try {
const credentials = await httpAuth.credentials(request);
const userEntity = await userInfo.getUserInfo(credentials);
const user_id = userEntity.userEntityRef;
const conversation_id = request.params.conversation_id;
const requestBody = JSON.stringify(request.body);
await authorizer.authorizeUser(
lightspeedChatCreatePermission,
credentials,
);
const userQueryParam = `user_id=${encodeURIComponent(user_id)}`;
const fetchResponse = await fetch(
`http://0.0.0.0:${port}/v2/conversations/${conversation_id}?${userQueryParam}`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: requestBody,
},
);
if (!fetchResponse.ok) {
// Read the error body
const errorBody = await fetchResponse.json();
const errormsg = `Error from lightspeed-core server: ${errorBody.error?.message || errorBody?.detail?.cause || 'Unknown error'}`;
logger.error(errormsg);
// Return a 500 status for any upstream error
response.status(500).json({
error: errormsg,
});
return;
}
const data = await fetchResponse.json();
response.status(fetchResponse.status).json(data);
} catch (error) {
const errormsg = `Error while updating topic summary: ${error}`;
logger.error(errormsg);
if (error instanceof NotAllowedError) {
response.status(403).json({ error: error.message });
} else {
response.status(500).json({ error: errormsg });
}
}
},
);
const middleware = MiddlewareFactory.create({ logger, config });
router.use(middleware.error());
return router;
}