@mcp-shark/mcp-shark 1.5.6 → 1.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/libraries/TransportLibrary.js +70 -0
- package/core/libraries/index.js +1 -0
- package/core/services/McpDiscoveryService.js +1 -1
- package/core/services/ServerManagementService.js +9 -17
- package/package.json +1 -1
- package/ui/server/controllers/ServerManagementController.js +3 -2
- package/ui/server/routes/composite/index.js +3 -1
- package/ui/server/setup.js +1 -4
- package/ui/server/websocket/handler.js +8 -5
- package/ui/server/routes/smartscan/transport.js +0 -53
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
2
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
3
|
+
import { WebSocketClientTransport } from '@modelcontextprotocol/sdk/client/websocket.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Library for MCP transport creation utilities
|
|
7
|
+
* Pure utility - no dependencies on services or repositories
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Create transport for MCP server based on config
|
|
12
|
+
* @param {Object} serverConfig - Server configuration
|
|
13
|
+
* @param {string} [serverConfig.type] - Transport type (stdio, http, websocket)
|
|
14
|
+
* @param {string} [serverConfig.url] - Server URL (for http/websocket)
|
|
15
|
+
* @param {Object} [serverConfig.headers] - HTTP headers
|
|
16
|
+
* @param {string} [serverConfig.command] - Command for stdio transport
|
|
17
|
+
* @param {Array} [serverConfig.args] - Command arguments
|
|
18
|
+
* @param {Object} [serverConfig.env] - Environment variables
|
|
19
|
+
* @param {string} [serverName] - Server name for error messages
|
|
20
|
+
* @returns {Object} Transport instance
|
|
21
|
+
* @throws {Error} If transport cannot be created
|
|
22
|
+
*/
|
|
23
|
+
export function createTransport(serverConfig, serverName = null) {
|
|
24
|
+
const type = serverConfig.type || (serverConfig.url ? 'http' : 'stdio');
|
|
25
|
+
const {
|
|
26
|
+
url,
|
|
27
|
+
headers: configHeaders = {},
|
|
28
|
+
command,
|
|
29
|
+
args = [],
|
|
30
|
+
env: configEnv = {},
|
|
31
|
+
} = serverConfig;
|
|
32
|
+
|
|
33
|
+
const env = {
|
|
34
|
+
...process.env,
|
|
35
|
+
...configEnv,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const requestInit = { headers: { ...configHeaders } };
|
|
39
|
+
|
|
40
|
+
const errorPrefix = serverName ? `Server ${serverName}: ` : '';
|
|
41
|
+
|
|
42
|
+
switch (type) {
|
|
43
|
+
case 'stdio':
|
|
44
|
+
if (!command) {
|
|
45
|
+
throw new Error(`${errorPrefix}command is required for stdio transport`);
|
|
46
|
+
}
|
|
47
|
+
return new StdioClientTransport({ command, args, env });
|
|
48
|
+
|
|
49
|
+
case 'http':
|
|
50
|
+
case 'sse':
|
|
51
|
+
case 'streamable-http':
|
|
52
|
+
if (!url) {
|
|
53
|
+
throw new Error(`${errorPrefix}url is required for ${type} transport`);
|
|
54
|
+
}
|
|
55
|
+
return new StreamableHTTPClientTransport(new URL(url), { requestInit });
|
|
56
|
+
|
|
57
|
+
case 'ws':
|
|
58
|
+
case 'websocket':
|
|
59
|
+
if (!url) {
|
|
60
|
+
throw new Error(`${errorPrefix}url is required for websocket transport`);
|
|
61
|
+
}
|
|
62
|
+
return new WebSocketClientTransport(new URL(url));
|
|
63
|
+
|
|
64
|
+
default:
|
|
65
|
+
if (command) {
|
|
66
|
+
return new StdioClientTransport({ command, args, env });
|
|
67
|
+
}
|
|
68
|
+
throw new Error(`${errorPrefix}unsupported transport type: ${type}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
package/core/libraries/index.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export { SerializationLibrary } from './SerializationLibrary.js';
|
|
6
6
|
export { LoggerLibrary } from './LoggerLibrary.js';
|
|
7
|
+
export { createTransport } from './TransportLibrary.js';
|
|
7
8
|
export { CompositeError, isError, getErrors } from './ErrorLibrary.js';
|
|
8
9
|
export {
|
|
9
10
|
ApplicationError,
|
|
@@ -36,13 +36,10 @@ export class ServerManagementService {
|
|
|
36
36
|
|
|
37
37
|
// If filePath is provided, restore original config if already patched
|
|
38
38
|
// This ensures processSetup reads the original config, not the patched one
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
restoreWarning = restoreResult.warning;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
39
|
+
const restoreWarning =
|
|
40
|
+
filePath && !fileContent
|
|
41
|
+
? this.configPatchingService.restoreIfPatched(filePath).warning || null
|
|
42
|
+
: null;
|
|
46
43
|
|
|
47
44
|
// Process setup
|
|
48
45
|
const setupResult = this.configService.processSetup(filePath, fileContent, selectedServices);
|
|
@@ -80,16 +77,11 @@ export class ServerManagementService {
|
|
|
80
77
|
});
|
|
81
78
|
|
|
82
79
|
// Patch the original config file if it exists
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
);
|
|
89
|
-
if (patchResult.warning) {
|
|
90
|
-
patchWarning = patchResult.warning;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
80
|
+
const patchWarning =
|
|
81
|
+
fileData.resolvedFilePath && this.configService.fileExists(fileData.resolvedFilePath)
|
|
82
|
+
? this.configPatchingService.patchConfigFile(fileData.resolvedFilePath, updatedConfig)
|
|
83
|
+
.warning || null
|
|
84
|
+
: null;
|
|
93
85
|
|
|
94
86
|
return {
|
|
95
87
|
success: true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcp-shark/mcp-shark",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.8",
|
|
4
4
|
"description": "Aggregate multiple Model Context Protocol (MCP) servers into a single unified interface with a powerful monitoring UI. Prov deep visibility into every request and response.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./bin/mcp-shark.js",
|
|
@@ -5,10 +5,11 @@ import { handleError, handleValidationError } from '../utils/errorHandler.js';
|
|
|
5
5
|
* Controller for server management HTTP endpoints
|
|
6
6
|
*/
|
|
7
7
|
export class ServerManagementController {
|
|
8
|
-
constructor(serverManagementService, logService, logger) {
|
|
8
|
+
constructor(serverManagementService, logService, logger, configService = null) {
|
|
9
9
|
this.serverManagementService = serverManagementService;
|
|
10
10
|
this.logService = logService;
|
|
11
11
|
this.logger = logger;
|
|
12
|
+
this.configService = configService;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
/**
|
|
@@ -76,7 +77,7 @@ export class ServerManagementController {
|
|
|
76
77
|
stop = async (_req, res) => {
|
|
77
78
|
try {
|
|
78
79
|
const stopped = await this.serverManagementService.stopServer();
|
|
79
|
-
const restored = this.configService.restoreOriginalConfig();
|
|
80
|
+
const restored = this.configService ? this.configService.restoreOriginalConfig() : false;
|
|
80
81
|
|
|
81
82
|
if (stopped) {
|
|
82
83
|
if (restored) {
|
|
@@ -37,6 +37,7 @@ export function createCompositeRoutes(
|
|
|
37
37
|
const serverManagementService = container.getService('serverManagement');
|
|
38
38
|
const logService = container.getService('log');
|
|
39
39
|
const logger = container.getLibrary('logger');
|
|
40
|
+
const configService = container.getService('config');
|
|
40
41
|
|
|
41
42
|
// Initialize log service with the log array
|
|
42
43
|
logService.initialize(mcpSharkLogs);
|
|
@@ -56,7 +57,8 @@ export function createCompositeRoutes(
|
|
|
56
57
|
const serverManagementController = new ServerManagementController(
|
|
57
58
|
serverManagementService,
|
|
58
59
|
logService,
|
|
59
|
-
logger
|
|
60
|
+
logger,
|
|
61
|
+
configService
|
|
60
62
|
);
|
|
61
63
|
|
|
62
64
|
// Set cleanup function for shutdown endpoint
|
package/ui/server/setup.js
CHANGED
|
@@ -24,7 +24,6 @@ import { createSmartScanRoutes } from './routes/smartscan.js';
|
|
|
24
24
|
import { createStatisticsRoutes } from './routes/statistics.js';
|
|
25
25
|
import { swaggerSpec } from './swagger/swagger.js';
|
|
26
26
|
import { performCleanup } from './utils/cleanup.js';
|
|
27
|
-
import { restoreConfig } from './utils/config.js';
|
|
28
27
|
import { getMcpSharkProcess, setMcpSharkProcess } from './utils/processState.js';
|
|
29
28
|
import { broadcastLogUpdate, notifyClients } from './websocket/broadcast.js';
|
|
30
29
|
import { handleWebSocketConnection } from './websocket/handler.js';
|
|
@@ -110,9 +109,7 @@ export function createUIServer() {
|
|
|
110
109
|
app.post('/api/config/backup/delete', backupRoutes.deleteBackup);
|
|
111
110
|
|
|
112
111
|
app.post('/api/composite/setup', compositeRoutes.setup);
|
|
113
|
-
app.post('/api/composite/stop',
|
|
114
|
-
compositeRoutes.stop(req, res, () => restoreConfig(container));
|
|
115
|
-
});
|
|
112
|
+
app.post('/api/composite/stop', compositeRoutes.stop);
|
|
116
113
|
app.get('/api/composite/status', compositeRoutes.getStatus);
|
|
117
114
|
app.get('/api/mcp-server/status', compositeRoutes.getMcpServerStatus);
|
|
118
115
|
app.post('/api/composite/shutdown', compositeRoutes.shutdown);
|
|
@@ -9,8 +9,11 @@ import { Server as ServerConstants } from '#core/constants/Server.js';
|
|
|
9
9
|
export function handleWebSocketConnection(clients, ws, logger) {
|
|
10
10
|
clients.add(ws);
|
|
11
11
|
|
|
12
|
+
// Use object to hold timeout ID (allows reassignment while keeping const)
|
|
13
|
+
const timeoutState = { id: null };
|
|
14
|
+
|
|
12
15
|
// Set up timeout to close stale connections
|
|
13
|
-
|
|
16
|
+
timeoutState.id = setTimeout(() => {
|
|
14
17
|
if (ws.readyState === 1) {
|
|
15
18
|
logger?.warn('WebSocket connection timeout, closing');
|
|
16
19
|
ws.close();
|
|
@@ -28,8 +31,8 @@ export function handleWebSocketConnection(clients, ws, logger) {
|
|
|
28
31
|
|
|
29
32
|
ws.on('pong', () => {
|
|
30
33
|
// Reset timeout on pong
|
|
31
|
-
clearTimeout(
|
|
32
|
-
|
|
34
|
+
clearTimeout(timeoutState.id);
|
|
35
|
+
timeoutState.id = setTimeout(() => {
|
|
33
36
|
if (ws.readyState === 1) {
|
|
34
37
|
logger?.warn('WebSocket connection timeout, closing');
|
|
35
38
|
ws.close();
|
|
@@ -38,14 +41,14 @@ export function handleWebSocketConnection(clients, ws, logger) {
|
|
|
38
41
|
});
|
|
39
42
|
|
|
40
43
|
ws.on('close', () => {
|
|
41
|
-
clearTimeout(
|
|
44
|
+
clearTimeout(timeoutState.id);
|
|
42
45
|
clearInterval(heartbeatInterval);
|
|
43
46
|
clients.delete(ws);
|
|
44
47
|
});
|
|
45
48
|
|
|
46
49
|
ws.on('error', (error) => {
|
|
47
50
|
logger?.error({ error: error.message }, 'WebSocket error');
|
|
48
|
-
clearTimeout(
|
|
51
|
+
clearTimeout(timeoutState.id);
|
|
49
52
|
clearInterval(heartbeatInterval);
|
|
50
53
|
clients.delete(ws);
|
|
51
54
|
});
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
2
|
-
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
3
|
-
import { WebSocketClientTransport } from '@modelcontextprotocol/sdk/client/websocket.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Create transport for MCP server based on config
|
|
7
|
-
*/
|
|
8
|
-
export function createTransport(serverConfig, serverName) {
|
|
9
|
-
const type = serverConfig.type || (serverConfig.url ? 'http' : 'stdio');
|
|
10
|
-
const {
|
|
11
|
-
url,
|
|
12
|
-
headers: configHeaders = {},
|
|
13
|
-
command,
|
|
14
|
-
args = [],
|
|
15
|
-
env: configEnv = {},
|
|
16
|
-
} = serverConfig;
|
|
17
|
-
|
|
18
|
-
const env = {
|
|
19
|
-
...process.env,
|
|
20
|
-
...configEnv,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const requestInit = { headers: { ...configHeaders } };
|
|
24
|
-
|
|
25
|
-
switch (type) {
|
|
26
|
-
case 'stdio':
|
|
27
|
-
if (!command) {
|
|
28
|
-
throw new Error(`Server ${serverName}: command is required for stdio transport`);
|
|
29
|
-
}
|
|
30
|
-
return new StdioClientTransport({ command, args, env });
|
|
31
|
-
|
|
32
|
-
case 'http':
|
|
33
|
-
case 'sse':
|
|
34
|
-
case 'streamable-http':
|
|
35
|
-
if (!url) {
|
|
36
|
-
throw new Error(`Server ${serverName}: url is required for ${type} transport`);
|
|
37
|
-
}
|
|
38
|
-
return new StreamableHTTPClientTransport(new URL(url), { requestInit });
|
|
39
|
-
|
|
40
|
-
case 'ws':
|
|
41
|
-
case 'websocket':
|
|
42
|
-
if (!url) {
|
|
43
|
-
throw new Error(`Server ${serverName}: url is required for websocket transport`);
|
|
44
|
-
}
|
|
45
|
-
return new WebSocketClientTransport(new URL(url));
|
|
46
|
-
|
|
47
|
-
default:
|
|
48
|
-
if (command) {
|
|
49
|
-
return new StdioClientTransport({ command, args, env });
|
|
50
|
-
}
|
|
51
|
-
throw new Error(`Server ${serverName}: unsupported transport type: ${type}`);
|
|
52
|
-
}
|
|
53
|
-
}
|