@mcp-shark/mcp-shark 1.4.0
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/LICENSE +85 -0
- package/README.md +724 -0
- package/bin/mcp-shark.js +93 -0
- package/mcp-server/.editorconfig +15 -0
- package/mcp-server/.prettierignore +11 -0
- package/mcp-server/.prettierrc +12 -0
- package/mcp-server/README.md +280 -0
- package/mcp-server/commitlint.config.cjs +42 -0
- package/mcp-server/eslint.config.js +131 -0
- package/mcp-server/lib/auditor/audit.js +228 -0
- package/mcp-server/lib/common/error.js +15 -0
- package/mcp-server/lib/server/external/all.js +32 -0
- package/mcp-server/lib/server/external/config.js +59 -0
- package/mcp-server/lib/server/external/kv.js +102 -0
- package/mcp-server/lib/server/external/single/client.js +35 -0
- package/mcp-server/lib/server/external/single/request.js +49 -0
- package/mcp-server/lib/server/external/single/run.js +75 -0
- package/mcp-server/lib/server/external/single/transport.js +57 -0
- package/mcp-server/lib/server/internal/handlers/common.js +20 -0
- package/mcp-server/lib/server/internal/handlers/error.js +7 -0
- package/mcp-server/lib/server/internal/handlers/prompts-get.js +22 -0
- package/mcp-server/lib/server/internal/handlers/prompts-list.js +12 -0
- package/mcp-server/lib/server/internal/handlers/resources-list.js +12 -0
- package/mcp-server/lib/server/internal/handlers/resources-read.js +19 -0
- package/mcp-server/lib/server/internal/handlers/tools-call.js +37 -0
- package/mcp-server/lib/server/internal/handlers/tools-list.js +14 -0
- package/mcp-server/lib/server/internal/run.js +49 -0
- package/mcp-server/lib/server/internal/server.js +63 -0
- package/mcp-server/lib/server/internal/session.js +39 -0
- package/mcp-server/mcp-shark.js +72 -0
- package/mcp-server/package-lock.json +4784 -0
- package/mcp-server/package.json +30 -0
- package/package.json +103 -0
- package/ui/README.md +212 -0
- package/ui/index.html +16 -0
- package/ui/package-lock.json +3574 -0
- package/ui/package.json +12 -0
- package/ui/paths.js +282 -0
- package/ui/public/og-image.png +0 -0
- package/ui/server/routes/backups.js +251 -0
- package/ui/server/routes/composite.js +244 -0
- package/ui/server/routes/config.js +175 -0
- package/ui/server/routes/conversations.js +25 -0
- package/ui/server/routes/help.js +43 -0
- package/ui/server/routes/logs.js +32 -0
- package/ui/server/routes/playground.js +152 -0
- package/ui/server/routes/requests.js +235 -0
- package/ui/server/routes/sessions.js +27 -0
- package/ui/server/routes/smartscan/discover.js +117 -0
- package/ui/server/routes/smartscan/scans/clearCache.js +22 -0
- package/ui/server/routes/smartscan/scans/createBatchScans.js +123 -0
- package/ui/server/routes/smartscan/scans/createScan.js +42 -0
- package/ui/server/routes/smartscan/scans/getCachedResults.js +51 -0
- package/ui/server/routes/smartscan/scans/getScan.js +41 -0
- package/ui/server/routes/smartscan/scans/listScans.js +24 -0
- package/ui/server/routes/smartscan/scans.js +13 -0
- package/ui/server/routes/smartscan/token.js +56 -0
- package/ui/server/routes/smartscan/transport.js +53 -0
- package/ui/server/routes/smartscan.js +24 -0
- package/ui/server/routes/statistics.js +83 -0
- package/ui/server/utils/config-update.js +212 -0
- package/ui/server/utils/config.js +98 -0
- package/ui/server/utils/paths.js +23 -0
- package/ui/server/utils/port.js +28 -0
- package/ui/server/utils/process.js +80 -0
- package/ui/server/utils/scan-cache/all-results.js +180 -0
- package/ui/server/utils/scan-cache/directory.js +35 -0
- package/ui/server/utils/scan-cache/file-operations.js +104 -0
- package/ui/server/utils/scan-cache/hash.js +47 -0
- package/ui/server/utils/scan-cache/server-operations.js +80 -0
- package/ui/server/utils/scan-cache.js +12 -0
- package/ui/server/utils/serialization.js +13 -0
- package/ui/server/utils/smartscan-token.js +42 -0
- package/ui/server.js +199 -0
- package/ui/src/App.jsx +153 -0
- package/ui/src/CompositeLogs.jsx +164 -0
- package/ui/src/CompositeSetup.jsx +285 -0
- package/ui/src/HelpGuide/HelpGuideContent.jsx +118 -0
- package/ui/src/HelpGuide/HelpGuideFooter.jsx +58 -0
- package/ui/src/HelpGuide/HelpGuideHeader.jsx +56 -0
- package/ui/src/HelpGuide.jsx +65 -0
- package/ui/src/IntroTour.jsx +140 -0
- package/ui/src/LogDetail.jsx +122 -0
- package/ui/src/LogTable.jsx +242 -0
- package/ui/src/PacketDetail.jsx +190 -0
- package/ui/src/PacketFilters.jsx +222 -0
- package/ui/src/PacketList.jsx +183 -0
- package/ui/src/SmartScan.jsx +178 -0
- package/ui/src/TabNavigation.jsx +143 -0
- package/ui/src/components/App/HelpButton.jsx +64 -0
- package/ui/src/components/App/TrafficTab.jsx +69 -0
- package/ui/src/components/App/useAppState.js +163 -0
- package/ui/src/components/BackupList.jsx +192 -0
- package/ui/src/components/CollapsibleSection.jsx +82 -0
- package/ui/src/components/ConfigFileSection.jsx +84 -0
- package/ui/src/components/ConfigViewerModal.jsx +141 -0
- package/ui/src/components/ConfirmationModal.jsx +129 -0
- package/ui/src/components/DetailsTab/BodySection.jsx +27 -0
- package/ui/src/components/DetailsTab/CollapsibleRequestResponse.jsx +70 -0
- package/ui/src/components/DetailsTab/HeadersSection.jsx +25 -0
- package/ui/src/components/DetailsTab/InfoSection.jsx +28 -0
- package/ui/src/components/DetailsTab/NetworkInfoSection.jsx +63 -0
- package/ui/src/components/DetailsTab/ProtocolInfoSection.jsx +75 -0
- package/ui/src/components/DetailsTab/RequestDetailsSection.jsx +46 -0
- package/ui/src/components/DetailsTab/ResponseDetailsSection.jsx +66 -0
- package/ui/src/components/DetailsTab.jsx +31 -0
- package/ui/src/components/DetectedPathsList.jsx +171 -0
- package/ui/src/components/FileInput.jsx +144 -0
- package/ui/src/components/GroupHeader.jsx +76 -0
- package/ui/src/components/GroupedByMcpView.jsx +103 -0
- package/ui/src/components/GroupedByServerView.jsx +134 -0
- package/ui/src/components/GroupedBySessionView.jsx +127 -0
- package/ui/src/components/GroupedViews.jsx +2 -0
- package/ui/src/components/HexTab.jsx +188 -0
- package/ui/src/components/LogsDisplay.jsx +93 -0
- package/ui/src/components/LogsToolbar.jsx +193 -0
- package/ui/src/components/McpPlayground/LoadingModal.jsx +113 -0
- package/ui/src/components/McpPlayground/PromptsSection/PromptCallPanel.jsx +125 -0
- package/ui/src/components/McpPlayground/PromptsSection/PromptItem.jsx +48 -0
- package/ui/src/components/McpPlayground/PromptsSection/PromptsList.jsx +45 -0
- package/ui/src/components/McpPlayground/PromptsSection.jsx +106 -0
- package/ui/src/components/McpPlayground/ResourcesSection/ResourceCallPanel.jsx +89 -0
- package/ui/src/components/McpPlayground/ResourcesSection/ResourceItem.jsx +59 -0
- package/ui/src/components/McpPlayground/ResourcesSection/ResourcesList.jsx +45 -0
- package/ui/src/components/McpPlayground/ResourcesSection.jsx +91 -0
- package/ui/src/components/McpPlayground/ToolsSection/ToolCallPanel.jsx +125 -0
- package/ui/src/components/McpPlayground/ToolsSection/ToolItem.jsx +48 -0
- package/ui/src/components/McpPlayground/ToolsSection/ToolsList.jsx +45 -0
- package/ui/src/components/McpPlayground/ToolsSection.jsx +107 -0
- package/ui/src/components/McpPlayground/common/EmptyState.jsx +17 -0
- package/ui/src/components/McpPlayground/common/ErrorState.jsx +17 -0
- package/ui/src/components/McpPlayground/common/LoadingState.jsx +17 -0
- package/ui/src/components/McpPlayground/useMcpPlayground.js +280 -0
- package/ui/src/components/McpPlayground.jsx +171 -0
- package/ui/src/components/MessageDisplay.jsx +28 -0
- package/ui/src/components/PacketDetailHeader.jsx +88 -0
- package/ui/src/components/PacketFilters/ExportControls.jsx +126 -0
- package/ui/src/components/PacketFilters/FilterInput.jsx +59 -0
- package/ui/src/components/RawTab.jsx +142 -0
- package/ui/src/components/RequestRow/OrphanedResponseRow.jsx +155 -0
- package/ui/src/components/RequestRow/RequestRowMain.jsx +240 -0
- package/ui/src/components/RequestRow/ResponseRow.jsx +158 -0
- package/ui/src/components/RequestRow.jsx +70 -0
- package/ui/src/components/ServerControl.jsx +133 -0
- package/ui/src/components/ServiceSelector.jsx +209 -0
- package/ui/src/components/SetupHeader.jsx +30 -0
- package/ui/src/components/SharkLogo.jsx +21 -0
- package/ui/src/components/SmartScan/AnalysisResult.jsx +64 -0
- package/ui/src/components/SmartScan/BatchResultsDisplay/BatchResultItem.jsx +215 -0
- package/ui/src/components/SmartScan/BatchResultsDisplay/BatchResultsHeader.jsx +94 -0
- package/ui/src/components/SmartScan/BatchResultsDisplay.jsx +26 -0
- package/ui/src/components/SmartScan/DebugInfoSection.jsx +53 -0
- package/ui/src/components/SmartScan/EmptyState.jsx +57 -0
- package/ui/src/components/SmartScan/ErrorDisplay.jsx +48 -0
- package/ui/src/components/SmartScan/ExpandableSection.jsx +93 -0
- package/ui/src/components/SmartScan/FindingsTable.jsx +257 -0
- package/ui/src/components/SmartScan/ListViewContent.jsx +75 -0
- package/ui/src/components/SmartScan/NotablePatternsSection.jsx +75 -0
- package/ui/src/components/SmartScan/OverallSummarySection.jsx +72 -0
- package/ui/src/components/SmartScan/RawDataSection.jsx +52 -0
- package/ui/src/components/SmartScan/RecommendationsSection.jsx +78 -0
- package/ui/src/components/SmartScan/ScanDetailHeader.jsx +92 -0
- package/ui/src/components/SmartScan/ScanDetailView.jsx +141 -0
- package/ui/src/components/SmartScan/ScanListView/ScanListHeader.jsx +49 -0
- package/ui/src/components/SmartScan/ScanListView/ScanListItem.jsx +201 -0
- package/ui/src/components/SmartScan/ScanListView.jsx +73 -0
- package/ui/src/components/SmartScan/ScanOverviewSection.jsx +123 -0
- package/ui/src/components/SmartScan/ScanResultsDisplay.jsx +35 -0
- package/ui/src/components/SmartScan/ScanViewContent.jsx +68 -0
- package/ui/src/components/SmartScan/ScanningProgress.jsx +47 -0
- package/ui/src/components/SmartScan/ServerInfoSection.jsx +43 -0
- package/ui/src/components/SmartScan/ServerSelectionRow.jsx +207 -0
- package/ui/src/components/SmartScan/SingleResultDisplay.jsx +269 -0
- package/ui/src/components/SmartScan/SmartScanControls.jsx +290 -0
- package/ui/src/components/SmartScan/SmartScanHeader.jsx +77 -0
- package/ui/src/components/SmartScan/ViewModeTabs.jsx +57 -0
- package/ui/src/components/SmartScan/hooks/useCacheManagement.js +34 -0
- package/ui/src/components/SmartScan/hooks/useMcpDiscovery.js +121 -0
- package/ui/src/components/SmartScan/hooks/useScanList.js +193 -0
- package/ui/src/components/SmartScan/hooks/useScanOperations.js +87 -0
- package/ui/src/components/SmartScan/hooks/useServerStatus.js +26 -0
- package/ui/src/components/SmartScan/hooks/useTokenManagement.js +53 -0
- package/ui/src/components/SmartScan/scanDataUtils.js +98 -0
- package/ui/src/components/SmartScan/useSmartScan.js +72 -0
- package/ui/src/components/SmartScan/utils.js +19 -0
- package/ui/src/components/SmartScanIcons.jsx +58 -0
- package/ui/src/components/TabNavigation/DesktopTabs.jsx +111 -0
- package/ui/src/components/TabNavigation/MobileDropdown.jsx +140 -0
- package/ui/src/components/TabNavigation.jsx +97 -0
- package/ui/src/components/TabNavigationIcons.jsx +40 -0
- package/ui/src/components/TableHeader.jsx +164 -0
- package/ui/src/components/TourOverlay.jsx +117 -0
- package/ui/src/components/TourTooltip/TourTooltipButtons.jsx +117 -0
- package/ui/src/components/TourTooltip/TourTooltipHeader.jsx +70 -0
- package/ui/src/components/TourTooltip/TourTooltipIcons.jsx +45 -0
- package/ui/src/components/TourTooltip/useTooltipPosition.js +108 -0
- package/ui/src/components/TourTooltip.jsx +83 -0
- package/ui/src/components/ViewModeTabs.jsx +91 -0
- package/ui/src/components/WhatThisDoesSection.jsx +61 -0
- package/ui/src/config/tourSteps.jsx +141 -0
- package/ui/src/hooks/useAnimation.js +92 -0
- package/ui/src/hooks/useConfigManagement.js +124 -0
- package/ui/src/hooks/useServiceExtraction.js +51 -0
- package/ui/src/index.css +42 -0
- package/ui/src/main.jsx +10 -0
- package/ui/src/theme.js +65 -0
- package/ui/src/utils/animations.js +170 -0
- package/ui/src/utils/groupingUtils.js +93 -0
- package/ui/src/utils/hexUtils.js +24 -0
- package/ui/src/utils/mcpGroupingUtils.js +262 -0
- package/ui/src/utils/requestUtils.js +297 -0
- package/ui/vite.config.js +18 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { listAll } from '../../external/kv.js';
|
|
2
|
+
|
|
3
|
+
export function createPromptsListHandler(logger, mcpServers) {
|
|
4
|
+
return async _req => {
|
|
5
|
+
logger.debug('Prompts list');
|
|
6
|
+
|
|
7
|
+
const res = await listAll(mcpServers, 'prompts');
|
|
8
|
+
const result = Array.isArray(res) ? { prompts: res } : res;
|
|
9
|
+
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { listAll } from '../../external/kv.js';
|
|
2
|
+
|
|
3
|
+
export function createResourcesListHandler(logger, mcpServers) {
|
|
4
|
+
return async _req => {
|
|
5
|
+
logger.debug('Resources list');
|
|
6
|
+
|
|
7
|
+
const res = await listAll(mcpServers, 'resources');
|
|
8
|
+
const result = Array.isArray(res) ? { resources: res } : res;
|
|
9
|
+
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getBy } from '../../external/kv.js';
|
|
2
|
+
import { InternalServerError } from './error.js';
|
|
3
|
+
|
|
4
|
+
export function createResourcesReadHandler(logger, mcpServers) {
|
|
5
|
+
return async req => {
|
|
6
|
+
const uri = req.params.uri;
|
|
7
|
+
logger.debug('Resource read', uri);
|
|
8
|
+
|
|
9
|
+
const readResource = getBy(mcpServers, uri, 'readResource');
|
|
10
|
+
if (!readResource) {
|
|
11
|
+
throw new InternalServerError(`Resource not found: ${uri}`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const result = await readResource(uri);
|
|
15
|
+
logger.debug('Resource read result', result);
|
|
16
|
+
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { getBy, extractName } from '../../external/kv.js';
|
|
2
|
+
import { InternalServerError } from './error.js';
|
|
3
|
+
|
|
4
|
+
const isAsyncIterable = v => v && typeof v[Symbol.asyncIterator] === 'function';
|
|
5
|
+
|
|
6
|
+
export function createToolsCallHandler(logger, mcpServers) {
|
|
7
|
+
return async req => {
|
|
8
|
+
const { name, arguments: args } = req.params;
|
|
9
|
+
logger.debug('Tool call', name, args);
|
|
10
|
+
|
|
11
|
+
// Extract real server name from concatenated name
|
|
12
|
+
const { typeName } = extractName(name);
|
|
13
|
+
|
|
14
|
+
const callTool = getBy(mcpServers, name, 'callTool', args || {});
|
|
15
|
+
if (!callTool) {
|
|
16
|
+
throw new InternalServerError(`Tool not found: ${name}`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const result = await callTool({
|
|
20
|
+
...req.params,
|
|
21
|
+
name: typeName,
|
|
22
|
+
});
|
|
23
|
+
logger.debug('Tool call result', result);
|
|
24
|
+
|
|
25
|
+
if (isAsyncIterable(result)) {
|
|
26
|
+
async function* loggedStream() {
|
|
27
|
+
for await (const chunk of result) {
|
|
28
|
+
logger.debug('Tool call chunk forwarded', chunk);
|
|
29
|
+
yield chunk;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return loggedStream();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { listAll } from '../../external/kv.js';
|
|
2
|
+
|
|
3
|
+
export function createToolsListHandler(logger, mcpServers) {
|
|
4
|
+
return async req => {
|
|
5
|
+
logger.debug('Listing tools', req);
|
|
6
|
+
|
|
7
|
+
const res = await listAll(mcpServers, 'tools');
|
|
8
|
+
logger.debug('Tools list result', res);
|
|
9
|
+
|
|
10
|
+
const result = Array.isArray(res) ? { tools: res } : res;
|
|
11
|
+
|
|
12
|
+
return result;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import cors from 'cors';
|
|
3
|
+
|
|
4
|
+
import { withSession } from './session.js';
|
|
5
|
+
|
|
6
|
+
export function getInternalServer(
|
|
7
|
+
serverFactory,
|
|
8
|
+
auditLogger,
|
|
9
|
+
withAuditRequestResponseHandler
|
|
10
|
+
) {
|
|
11
|
+
const app = express();
|
|
12
|
+
|
|
13
|
+
// Parse JSON body for POST requests
|
|
14
|
+
app.use(express.json());
|
|
15
|
+
app.use(
|
|
16
|
+
'/mcp',
|
|
17
|
+
cors({
|
|
18
|
+
origin: ['*'],
|
|
19
|
+
methods: ['*'],
|
|
20
|
+
allowedHeaders: ['*'],
|
|
21
|
+
exposedHeaders: ['*'],
|
|
22
|
+
})
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
app.all('/mcp', async (req, res) => {
|
|
26
|
+
await withSession(
|
|
27
|
+
serverFactory,
|
|
28
|
+
withAuditRequestResponseHandler,
|
|
29
|
+
req,
|
|
30
|
+
res,
|
|
31
|
+
auditLogger
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Catch-all for other routes (404)
|
|
36
|
+
app.use((_req, res) => {
|
|
37
|
+
res.status(404).json({ error: 'Not found' });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return app;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function runInternalServer(logger, port, app) {
|
|
44
|
+
app.listen(port, '0.0.0.0', () => {
|
|
45
|
+
logger.info(
|
|
46
|
+
`MCP proxy HTTP server listening on http://localhost:${port}/mcp`
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ListToolsRequestSchema,
|
|
5
|
+
CallToolRequestSchema,
|
|
6
|
+
ListPromptsRequestSchema,
|
|
7
|
+
GetPromptRequestSchema,
|
|
8
|
+
ListResourcesRequestSchema,
|
|
9
|
+
ReadResourceRequestSchema,
|
|
10
|
+
} from '@modelcontextprotocol/sdk/types.js';
|
|
11
|
+
|
|
12
|
+
import { createToolsListHandler } from './handlers/tools-list.js';
|
|
13
|
+
import { createToolsCallHandler } from './handlers/tools-call.js';
|
|
14
|
+
import { createPromptsListHandler } from './handlers/prompts-list.js';
|
|
15
|
+
import { createPromptsGetHandler } from './handlers/prompts-get.js';
|
|
16
|
+
import { createResourcesListHandler } from './handlers/resources-list.js';
|
|
17
|
+
import { createResourcesReadHandler } from './handlers/resources-read.js';
|
|
18
|
+
import { SERVER_NAME } from './handlers/common.js';
|
|
19
|
+
|
|
20
|
+
export function createInternalServer(logger, mcpServers) {
|
|
21
|
+
// create MCP server
|
|
22
|
+
const server = new Server(
|
|
23
|
+
{ name: SERVER_NAME, version: '1.0.0' },
|
|
24
|
+
{ capabilities: { tools: {}, prompts: {}, resources: {} } }
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
// Register handlers
|
|
28
|
+
server.setRequestHandler(
|
|
29
|
+
ListToolsRequestSchema,
|
|
30
|
+
createToolsListHandler(logger, mcpServers)
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
server.setRequestHandler(
|
|
34
|
+
CallToolRequestSchema,
|
|
35
|
+
createToolsCallHandler(logger, mcpServers)
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
server.setRequestHandler(
|
|
39
|
+
ListPromptsRequestSchema,
|
|
40
|
+
createPromptsListHandler(logger, mcpServers)
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
server.setRequestHandler(
|
|
44
|
+
GetPromptRequestSchema,
|
|
45
|
+
createPromptsGetHandler(logger, mcpServers)
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
server.setRequestHandler(
|
|
49
|
+
ListResourcesRequestSchema,
|
|
50
|
+
createResourcesListHandler(logger, mcpServers)
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
server.setRequestHandler(
|
|
54
|
+
ReadResourceRequestSchema,
|
|
55
|
+
createResourcesReadHandler(logger, mcpServers)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return server;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function createInternalServerFactory(logger, mcpServers) {
|
|
62
|
+
return () => createInternalServer(logger, mcpServers);
|
|
63
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
3
|
+
|
|
4
|
+
import { getSessionFromRequest } from './handlers/common.js';
|
|
5
|
+
|
|
6
|
+
const sessions = new Map();
|
|
7
|
+
|
|
8
|
+
function storeTransportInSession(sessionId, transport) {
|
|
9
|
+
sessions.set(sessionId, transport);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function getTransportFromSession(sessionId) {
|
|
13
|
+
return sessions.get(sessionId);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function withSession(
|
|
17
|
+
serverFactory,
|
|
18
|
+
requestHandler,
|
|
19
|
+
req,
|
|
20
|
+
res,
|
|
21
|
+
auditLogger
|
|
22
|
+
) {
|
|
23
|
+
const sessionId = getSessionFromRequest(req);
|
|
24
|
+
if (!sessionId) {
|
|
25
|
+
const newSessionId = randomUUID();
|
|
26
|
+
const transport = new StreamableHTTPServerTransport({
|
|
27
|
+
sessionIdGenerator: () => newSessionId,
|
|
28
|
+
enableJsonResponse: true,
|
|
29
|
+
});
|
|
30
|
+
const server = serverFactory();
|
|
31
|
+
await server.connect(transport);
|
|
32
|
+
storeTransportInSession(newSessionId, transport);
|
|
33
|
+
// Session creation will be logged as part of the request packet in audit.js
|
|
34
|
+
return requestHandler(transport, req, res, auditLogger);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const transport = getTransportFromSession(sessionId);
|
|
38
|
+
return requestHandler(transport, req, res, auditLogger);
|
|
39
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { consola } from 'consola';
|
|
2
|
+
|
|
3
|
+
import { runAllExternalServers } from './lib/server/external/all.js';
|
|
4
|
+
import { isError } from './lib/common/error.js';
|
|
5
|
+
|
|
6
|
+
import { createInternalServerFactory } from './lib/server/internal/server.js';
|
|
7
|
+
import {
|
|
8
|
+
getInternalServer,
|
|
9
|
+
runInternalServer,
|
|
10
|
+
} from './lib/server/internal/run.js';
|
|
11
|
+
import { initDb } from 'mcp-shark-common/db/init.js';
|
|
12
|
+
import { getLogger } from 'mcp-shark-common/db/logger.js';
|
|
13
|
+
import {
|
|
14
|
+
getDatabaseFile,
|
|
15
|
+
getMcpConfigPath,
|
|
16
|
+
prepareAppDataSpaces,
|
|
17
|
+
} from 'mcp-shark-common/configs/index.js';
|
|
18
|
+
import { withAuditRequestResponseHandler } from './lib/auditor/audit.js';
|
|
19
|
+
|
|
20
|
+
function initAuditLogger(logger) {
|
|
21
|
+
logger.info('Initializing audit logger at', getDatabaseFile());
|
|
22
|
+
return getLogger(initDb(getDatabaseFile()));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function main() {
|
|
26
|
+
consola.level = 'info';
|
|
27
|
+
|
|
28
|
+
prepareAppDataSpaces();
|
|
29
|
+
|
|
30
|
+
const configPath = getMcpConfigPath();
|
|
31
|
+
console.log('[MCP-Shark] Starting MCP server...');
|
|
32
|
+
console.log(`[MCP-Shark] Config path: ${configPath}`);
|
|
33
|
+
console.log(`[MCP-Shark] Database path: ${getDatabaseFile()}`);
|
|
34
|
+
console.log(`[MCP-Shark] Working directory: ${process.cwd()}`);
|
|
35
|
+
console.log(`[MCP-Shark] PATH: ${process.env.PATH}`);
|
|
36
|
+
|
|
37
|
+
const auditLogger = initAuditLogger(consola);
|
|
38
|
+
const result = await runAllExternalServers(consola, configPath);
|
|
39
|
+
if (isError(result)) {
|
|
40
|
+
console.error(JSON.stringify(result));
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const internalServerFactory = createInternalServerFactory(consola, result);
|
|
45
|
+
const app = getInternalServer(
|
|
46
|
+
internalServerFactory,
|
|
47
|
+
auditLogger,
|
|
48
|
+
withAuditRequestResponseHandler
|
|
49
|
+
);
|
|
50
|
+
await runInternalServer(consola, 9851, app);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
main()
|
|
54
|
+
.then(() => {
|
|
55
|
+
const successMsg = 'MCP server started successfully';
|
|
56
|
+
console.log(`[MCP-Shark] ${successMsg}`);
|
|
57
|
+
consola.info(successMsg);
|
|
58
|
+
})
|
|
59
|
+
.catch(error => {
|
|
60
|
+
const errorMsg = 'Error starting MCP server';
|
|
61
|
+
console.error(`[MCP-Shark] ${errorMsg}:`, error);
|
|
62
|
+
console.error(`[MCP-Shark] Error message: ${error.message}`);
|
|
63
|
+
if (error.stack) {
|
|
64
|
+
console.error(`[MCP-Shark] Error stack:`, error.stack);
|
|
65
|
+
}
|
|
66
|
+
consola.error(errorMsg, error);
|
|
67
|
+
consola.error(`Error message: ${error.message}`);
|
|
68
|
+
if (error.stack) {
|
|
69
|
+
consola.error(`Error stack: ${error.stack}`);
|
|
70
|
+
}
|
|
71
|
+
process.exit(1);
|
|
72
|
+
});
|