feat(mcp): implement MCP infrastructure

- Add `mcp_settings` table to database schema
- Enable internal MCP API endpoint via middleware header authentication
- Introduce runtime log directory and capture utilities for diagnostics
- Update build pipeline to capture build logs
This commit is contained in:
Team Dark Devil committed 2026-11-21 23:00:29 +06:00
1 parent ae9b554189
commit b2669e2ad5
19 files changed
+3280 -20

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
#!/usr/bin/env node
/**
* Captures Next.js production build output & metadata into `.next/standalone/build.log`
* and `.next/standalone/build-meta.json` (plus runtime logs dir when writable) so
* build diagnostics are inspectable via MCP `read_build_logs` inside running containers.
*/
const fs = require('node:fs');
const path = require('node:path');
const { spawn } = require('node:child_process');
const { resolveRuntimeLogsDir, scrubSensitiveLogText } = require('../server/runtimeLogCapture');
const MAX_BUILD_LOG_BYTES = 512 * 1024;
const nextBin = require.resolve('next/dist/bin/next');
const pkg = require(path.resolve(__dirname, '..', 'package.json'));
const startedAt = new Date().toISOString();
const startMs = Date.now();
const capturedChunks = [];
let capturedBytes = 0;
function recordChunk(text) {
const clean = scrubSensitiveLogText(text);
capturedChunks.push(clean);
capturedBytes += Buffer.byteLength(clean, 'utf8');
while (capturedBytes > MAX_BUILD_LOG_BYTES && capturedChunks.length > 1) {
const dropped = capturedChunks.shift();
capturedBytes -= Buffer.byteLength(dropped, 'utf8');
}
}
const childEnv = {
...process.env,
NODE_ENV: 'production',
};
const child = spawn(process.execPath, [nextBin, 'build'], {
stdio: ['inherit', 'pipe', 'pipe'],
shell: false,
env: childEnv,
});
child.stdout.on('data', (buf) => {
process.stdout.write(buf);
recordChunk(buf.toString('utf8'));
});
child.stderr.on('data', (buf) => {
process.stderr.write(buf);
recordChunk(buf.toString('utf8'));
});
child.on('error', (err) => {
console.error('[DataStack Build] Failed to spawn Next.js build:', err.message);
process.exit(1);
});
child.on('close', (code) => {
const exitCode = typeof code === 'number' ? code : 1;
const finishedAt = new Date().toISOString();
const durationMs = Date.now() - startMs;
const meta = {
appName: pkg.name || 'datastack',
appVersion: pkg.version || '0.1.0',
nodeVersion: process.version,
nextVersion: (pkg.dependencies && pkg.dependencies.next) || 'unknown',
startedAt,
finishedAt,
durationMs,
exitCode,
status: exitCode === 0 ? 'succeeded' : 'failed',
};
const fullLog = capturedChunks.join('');
const writeArtifactDir = (dir, logName, metaName) => {
try {
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, logName), fullLog, 'utf8');
fs.writeFileSync(path.join(dir, metaName), JSON.stringify(meta, null, 2) + '\n', 'utf8');
} catch {
// Non-fatal if target dir is read-only or unavailable
}
};
const standaloneDir = path.resolve(process.cwd(), '.next', 'standalone');
if (exitCode === 0 && fs.existsSync(standaloneDir)) {
writeArtifactDir(standaloneDir, 'build.log', 'build-meta.json');
}
const runtimeLogsDir = resolveRuntimeLogsDir(process.env);
writeArtifactDir(runtimeLogsDir, 'last-build.log', 'last-build-meta.json');
process.exit(exitCode);
});
+28 -4
View File
@@ -7,6 +7,7 @@
const path = require('node:path');
const { spawn } = require('node:child_process');
const { appendRuntimeLogLine } = require('../server/runtimeLogCapture');
const webPort = String(process.env.PORT || '3000');
const nextBin = require.resolve('next/dist/bin/next');
@@ -19,18 +20,41 @@ console.log('\x1b[36m%s\x1b[0m', ` - Port ${webPort}: Next.js 15 Web & Admin Co
console.log('\x1b[36m%s\x1b[0m', ' - Port 4000: Ingestion & Socket.IO Streaming Engine');
console.log('\x1b[36m%s\x1b[0m', '=======================================================\n');
const childEnv = {
...process.env,
DATASTACK_LOG_CAPTURED_BY_RUNNER: '1',
};
const nextProc = spawn(process.execPath, [nextBin, 'dev', '-p', webPort, '-H', '0.0.0.0'], {
stdio: 'inherit',
stdio: ['inherit', 'pipe', 'pipe'],
shell: false,
env: { ...process.env },
env: childEnv,
});
const serverProc = spawn(process.execPath, [serverEntry], {
stdio: 'inherit',
stdio: ['inherit', 'pipe', 'pipe'],
shell: false,
env: { ...process.env },
env: childEnv,
});
function wireStreamCapture(proc, serviceName) {
if (proc.stdout) {
proc.stdout.on('data', (chunk) => {
process.stdout.write(chunk);
appendRuntimeLogLine(serviceName, 'stdout', chunk.toString('utf8'));
});
}
if (proc.stderr) {
proc.stderr.on('data', (chunk) => {
process.stderr.write(chunk);
appendRuntimeLogLine(serviceName, 'stderr', chunk.toString('utf8'));
});
}
}
wireStreamCapture(nextProc, 'web');
wireStreamCapture(serverProc, 'ingestion');
let shuttingDown = false;
let exitedChildren = 0;
let nextExited = false;
+28 -4
View File
@@ -7,28 +7,52 @@
const path = require('node:path');
const { spawn } = require('node:child_process');
const { appendRuntimeLogLine } = require('../server/runtimeLogCapture');
const webPort = String(process.env.PORT || '3000');
const nextBin = require.resolve('next/dist/bin/next');
const serverEntry = path.resolve(__dirname, '..', 'server', 'index.js');
const GRACEFUL_SHUTDOWN_TIMEOUT_MS = 7000;
const childEnv = {
...process.env,
DATASTACK_LOG_CAPTURED_BY_RUNNER: '1',
};
const webProc = spawn(
process.execPath,
[nextBin, 'start', '-p', webPort, '-H', '0.0.0.0'],
{
stdio: 'inherit',
stdio: ['inherit', 'pipe', 'pipe'],
shell: false,
env: { ...process.env },
env: childEnv,
},
);
const serverProc = spawn(process.execPath, [serverEntry], {
stdio: 'inherit',
stdio: ['inherit', 'pipe', 'pipe'],
shell: false,
env: { ...process.env },
env: childEnv,
});
function wireStreamCapture(proc, serviceName) {
if (proc.stdout) {
proc.stdout.on('data', (chunk) => {
process.stdout.write(chunk);
appendRuntimeLogLine(serviceName, 'stdout', chunk.toString('utf8'));
});
}
if (proc.stderr) {
proc.stderr.on('data', (chunk) => {
process.stderr.write(chunk);
appendRuntimeLogLine(serviceName, 'stderr', chunk.toString('utf8'));
});
}
}
wireStreamCapture(webProc, 'web');
wireStreamCapture(serverProc, 'ingestion');
let shuttingDown = false;
let exitedChildren = 0;
let webExited = false;