@ecubelabs/mysql-mcp 1.0.0-next.7 → 1.0.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/README.md +296 -448
- package/dist/index.js +377 -239
- package/dist/src/catalog/collect.js +3 -2
- package/dist/src/catalog/docs.js +14 -12
- package/dist/src/catalog/index.js +59 -99
- package/dist/src/catalog/render.js +4 -10
- package/dist/src/catalog/store.js +193 -76
- package/dist/src/catalog/types.js +0 -12
- package/dist/src/config/index.js +175 -0
- package/dist/src/db/index.js +379 -379
- package/dist/src/db/permissions.js +32 -16
- package/dist/src/db/utils.js +3 -1
- package/dist/src/ssh/config.js +34 -27
- package/dist/src/ssh/tunnel.js +238 -155
- package/dist/src/utils/index.js +4 -35
- package/package.json +4 -4
- package/dist/src/catalog/context.js +0 -187
- package/dist/src/catalog/lock.js +0 -113
- package/dist/src/config/args.js +0 -30
- package/dist/src/config/load.js +0 -169
- package/dist/src/config/schema.js +0 -351
- package/dist/src/config/secrets.js +0 -128
- package/dist/src/config/ssl.js +0 -25
- package/dist/src/db/registry.js +0 -178
- package/dist/src/init/check.js +0 -31
- package/dist/src/init/cli.js +0 -26
- package/dist/src/init/config-edit.js +0 -95
- package/dist/src/init/prompt.js +0 -82
- package/dist/src/init/wizard.js +0 -223
- package/dist/src/server/catalog-tool.js +0 -91
- package/dist/src/server/connections.js +0 -117
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import { loadedCodeBranches } from './branch.js';
|
|
4
|
-
import { normalizeDocumentPath, normalizeGithubRepo } from './docs.js';
|
|
5
|
-
import { acquireCatalogLock, LOCK_WAIT_MS } from './lock.js';
|
|
6
|
-
import { loadedSchemaOwner } from './owner.js';
|
|
7
|
-
import { emptyInstanceContext, emptySchemaContext } from './types.js';
|
|
8
|
-
export const CONTEXT_VERSION = 1;
|
|
9
|
-
const EPOCH = new Date(0).toISOString();
|
|
10
|
-
function emptyFile() {
|
|
11
|
-
return { version: CONTEXT_VERSION, environments: {} };
|
|
12
|
-
}
|
|
13
|
-
function isObject(value) {
|
|
14
|
-
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
15
|
-
}
|
|
16
|
-
function strings(value) {
|
|
17
|
-
return Array.isArray(value) ? value.filter((item) => typeof item === 'string') : [];
|
|
18
|
-
}
|
|
19
|
-
function loadedDocumentDecision(curated) {
|
|
20
|
-
if (!Object.prototype.hasOwnProperty.call(curated, 'doc')) {
|
|
21
|
-
return {};
|
|
22
|
-
}
|
|
23
|
-
const doc = curated.doc;
|
|
24
|
-
if (!isObject(doc)) {
|
|
25
|
-
return { doc: null };
|
|
26
|
-
}
|
|
27
|
-
try {
|
|
28
|
-
return {
|
|
29
|
-
doc: {
|
|
30
|
-
path: normalizeDocumentPath(String(doc.path ?? '')),
|
|
31
|
-
repo: normalizeGithubRepo(doc.repo),
|
|
32
|
-
linkedBy: doc.linkedBy === 'user' ? 'user' : 'model',
|
|
33
|
-
linkedAt: typeof doc.linkedAt === 'string' ? doc.linkedAt : EPOCH,
|
|
34
|
-
},
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
return {};
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
function loadedInstance(raw) {
|
|
42
|
-
const instance = emptyInstanceContext();
|
|
43
|
-
if (!isObject(raw)) {
|
|
44
|
-
return instance;
|
|
45
|
-
}
|
|
46
|
-
instance.codeBranches = loadedCodeBranches(raw.codeBranches);
|
|
47
|
-
for (const [schemaName, schemaRaw] of Object.entries(isObject(raw.schemas) ? raw.schemas : {})) {
|
|
48
|
-
if (!isObject(schemaRaw)) {
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
const schema = emptySchemaContext();
|
|
52
|
-
if (isObject(schemaRaw.owner)) {
|
|
53
|
-
const owner = loadedSchemaOwner(schemaRaw.owner);
|
|
54
|
-
if ('repo' in owner && 'app' in owner) {
|
|
55
|
-
schema.owner = { repo: owner.repo, app: owner.app };
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
schema.notes = strings(schemaRaw.notes);
|
|
59
|
-
for (const [tableName, tableRaw] of Object.entries(isObject(schemaRaw.tables) ? schemaRaw.tables : {})) {
|
|
60
|
-
if (!isObject(tableRaw)) {
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
schema.tables[tableName] = {
|
|
64
|
-
notes: strings(tableRaw.notes),
|
|
65
|
-
aliases: strings(tableRaw.aliases),
|
|
66
|
-
...loadedDocumentDecision(tableRaw),
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
instance.schemas[schemaName] = schema;
|
|
70
|
-
}
|
|
71
|
-
return instance;
|
|
72
|
-
}
|
|
73
|
-
function parseContextFile(text, filePath) {
|
|
74
|
-
const parsed = JSON.parse(text);
|
|
75
|
-
if (!isObject(parsed) || parsed.version !== CONTEXT_VERSION) {
|
|
76
|
-
const version = isObject(parsed) ? JSON.stringify(parsed.version) : 'none';
|
|
77
|
-
throw new Error(`${filePath} has version ${version}; this build reads version ${CONTEXT_VERSION}. ` +
|
|
78
|
-
'Update @ecubelabs/mysql-mcp, or move the file away to start over.');
|
|
79
|
-
}
|
|
80
|
-
const file = emptyFile();
|
|
81
|
-
for (const [envName, envRaw] of Object.entries(isObject(parsed.environments) ? parsed.environments : {})) {
|
|
82
|
-
const instances = {};
|
|
83
|
-
const rawInstances = isObject(envRaw) && isObject(envRaw.instances) ? envRaw.instances : {};
|
|
84
|
-
for (const [instanceName, instanceRaw] of Object.entries(rawInstances)) {
|
|
85
|
-
instances[instanceName] = loadedInstance(instanceRaw);
|
|
86
|
-
}
|
|
87
|
-
file.environments[envName] = { instances };
|
|
88
|
-
}
|
|
89
|
-
return file;
|
|
90
|
-
}
|
|
91
|
-
export class ContextStore {
|
|
92
|
-
filePath;
|
|
93
|
-
data = emptyFile();
|
|
94
|
-
stamp = null;
|
|
95
|
-
warned = false;
|
|
96
|
-
constructor(filePath) {
|
|
97
|
-
this.filePath = filePath;
|
|
98
|
-
}
|
|
99
|
-
read(envName, instanceName) {
|
|
100
|
-
this.reloadIfChanged();
|
|
101
|
-
return this.data.environments[envName]?.instances[instanceName] ?? emptyInstanceContext();
|
|
102
|
-
}
|
|
103
|
-
async update(envName, instanceName, mutator) {
|
|
104
|
-
if (this.filePath === null) {
|
|
105
|
-
const result = mutator(this.ensureInstance(this.data, envName, instanceName));
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
108
|
-
const filePath = this.filePath;
|
|
109
|
-
await fs.promises.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
110
|
-
const release = await acquireCatalogLock(filePath, LOCK_WAIT_MS);
|
|
111
|
-
try {
|
|
112
|
-
const latest = this.readForWrite(filePath);
|
|
113
|
-
const result = mutator(this.ensureInstance(latest, envName, instanceName));
|
|
114
|
-
const tempPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
|
|
115
|
-
await fs.promises.writeFile(tempPath, `${JSON.stringify(latest, null, 2)}\n`, {
|
|
116
|
-
encoding: 'utf8',
|
|
117
|
-
mode: 0o600,
|
|
118
|
-
flag: 'wx',
|
|
119
|
-
});
|
|
120
|
-
await fs.promises.rename(tempPath, filePath);
|
|
121
|
-
await fs.promises.chmod(filePath, 0o600);
|
|
122
|
-
this.data = latest;
|
|
123
|
-
this.stamp = this.statFile(filePath);
|
|
124
|
-
return result;
|
|
125
|
-
}
|
|
126
|
-
finally {
|
|
127
|
-
await release();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
ensureInstance(file, envName, instanceName) {
|
|
131
|
-
const env = (file.environments[envName] ??= { instances: {} });
|
|
132
|
-
return (env.instances[instanceName] ??= emptyInstanceContext());
|
|
133
|
-
}
|
|
134
|
-
statFile(filePath) {
|
|
135
|
-
try {
|
|
136
|
-
const stat = fs.statSync(filePath);
|
|
137
|
-
return { mtimeMs: stat.mtimeMs, size: stat.size };
|
|
138
|
-
}
|
|
139
|
-
catch {
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
readForWrite(filePath) {
|
|
144
|
-
let text;
|
|
145
|
-
try {
|
|
146
|
-
text = fs.readFileSync(filePath, 'utf8');
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
if (error.code === 'ENOENT') {
|
|
150
|
-
return emptyFile();
|
|
151
|
-
}
|
|
152
|
-
throw error;
|
|
153
|
-
}
|
|
154
|
-
try {
|
|
155
|
-
return parseContextFile(text, filePath);
|
|
156
|
-
}
|
|
157
|
-
catch (error) {
|
|
158
|
-
throw new Error(`could not update ${filePath}: ${error instanceof Error ? error.message : String(error)}. ` +
|
|
159
|
-
'Nothing was recorded.', { cause: error });
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
reloadIfChanged() {
|
|
163
|
-
if (this.filePath === null) {
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
const stamp = this.statFile(this.filePath);
|
|
167
|
-
if (stamp?.mtimeMs === this.stamp?.mtimeMs && stamp?.size === this.stamp?.size) {
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
this.stamp = stamp;
|
|
171
|
-
if (!stamp) {
|
|
172
|
-
this.data = emptyFile();
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
try {
|
|
176
|
-
this.data = parseContextFile(fs.readFileSync(this.filePath, 'utf8'), this.filePath);
|
|
177
|
-
this.warned = false;
|
|
178
|
-
}
|
|
179
|
-
catch (error) {
|
|
180
|
-
this.data = emptyFile();
|
|
181
|
-
if (!this.warned) {
|
|
182
|
-
this.warned = true;
|
|
183
|
-
console.error(`[context] ignoring ${this.filePath} until it is fixed: ${error instanceof Error ? error.message : String(error)}`);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
package/dist/src/catalog/lock.js
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
const LOCK_RETRY_MS = 25;
|
|
3
|
-
export const LOCK_WAIT_MS = 15_000;
|
|
4
|
-
export const LOCK_CLOSE_WAIT_MS = 2_000;
|
|
5
|
-
const LOCK_STALE_MS = 30_000;
|
|
6
|
-
export class CatalogLockTimeoutError extends Error {
|
|
7
|
-
}
|
|
8
|
-
function errorCode(error) {
|
|
9
|
-
return error && typeof error === 'object' && 'code' in error ? String(error.code) : null;
|
|
10
|
-
}
|
|
11
|
-
function processIsAlive(pid) {
|
|
12
|
-
try {
|
|
13
|
-
process.kill(pid, 0);
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
return errorCode(error) !== 'ESRCH';
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
function delay(ms) {
|
|
21
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
22
|
-
}
|
|
23
|
-
export async function acquireCatalogLock(filePath, waitMs) {
|
|
24
|
-
const lockPath = `${filePath}.lock`;
|
|
25
|
-
const deadline = Date.now() + waitMs;
|
|
26
|
-
const record = {
|
|
27
|
-
pid: process.pid,
|
|
28
|
-
token: `${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
|
29
|
-
createdAt: Date.now(),
|
|
30
|
-
};
|
|
31
|
-
while (true) {
|
|
32
|
-
try {
|
|
33
|
-
const handle = await fs.promises.open(lockPath, 'wx', 0o600);
|
|
34
|
-
try {
|
|
35
|
-
await handle.writeFile(JSON.stringify(record), 'utf8');
|
|
36
|
-
await handle.sync();
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
await handle.close().catch(() => undefined);
|
|
40
|
-
await fs.promises.unlink(lockPath).catch(() => undefined);
|
|
41
|
-
throw error;
|
|
42
|
-
}
|
|
43
|
-
let released = false;
|
|
44
|
-
return async () => {
|
|
45
|
-
if (released) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
released = true;
|
|
49
|
-
await handle.close();
|
|
50
|
-
try {
|
|
51
|
-
const current = JSON.parse(await fs.promises.readFile(lockPath, 'utf8'));
|
|
52
|
-
if (current.token === record.token) {
|
|
53
|
-
await fs.promises.unlink(lockPath);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
if (errorCode(error) !== 'ENOENT') {
|
|
58
|
-
throw error;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
if (errorCode(error) !== 'EEXIST') {
|
|
65
|
-
throw error;
|
|
66
|
-
}
|
|
67
|
-
let lockText = null;
|
|
68
|
-
let stale = false;
|
|
69
|
-
try {
|
|
70
|
-
const [text, stat] = await Promise.all([
|
|
71
|
-
fs.promises.readFile(lockPath, 'utf8'),
|
|
72
|
-
fs.promises.stat(lockPath),
|
|
73
|
-
]);
|
|
74
|
-
lockText = text;
|
|
75
|
-
let owner = {};
|
|
76
|
-
try {
|
|
77
|
-
owner = JSON.parse(text);
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
}
|
|
81
|
-
stale =
|
|
82
|
-
typeof owner.pid === 'number'
|
|
83
|
-
? !processIsAlive(owner.pid)
|
|
84
|
-
: Date.now() - stat.mtimeMs >= LOCK_STALE_MS;
|
|
85
|
-
}
|
|
86
|
-
catch (readError) {
|
|
87
|
-
if (errorCode(readError) === 'ENOENT') {
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
throw readError;
|
|
91
|
-
}
|
|
92
|
-
if (stale && lockText !== null) {
|
|
93
|
-
try {
|
|
94
|
-
if ((await fs.promises.readFile(lockPath, 'utf8')) === lockText) {
|
|
95
|
-
await fs.promises.unlink(lockPath);
|
|
96
|
-
console.error(`[catalog] removed stale lock ${lockPath}`);
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
catch (cleanupError) {
|
|
101
|
-
if (errorCode(cleanupError) === 'ENOENT') {
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
throw cleanupError;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (Date.now() >= deadline) {
|
|
108
|
-
throw new CatalogLockTimeoutError(`timed out after ${waitMs}ms waiting for catalog lock ${lockPath}`);
|
|
109
|
-
}
|
|
110
|
-
await delay(LOCK_RETRY_MS + Math.floor(Math.random() * LOCK_RETRY_MS));
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
package/dist/src/config/args.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export class UsageError extends Error {
|
|
2
|
-
constructor(message) {
|
|
3
|
-
super(message);
|
|
4
|
-
this.name = 'UsageError';
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export const SERVER_USAGE = 'usage: mysql-mcp --env <name> [--config <path>]\n mysql-mcp init [--config <path>]';
|
|
8
|
-
export function parseFlags(argv, allowed) {
|
|
9
|
-
const flags = {};
|
|
10
|
-
for (let i = 0; i < argv.length; i++) {
|
|
11
|
-
const arg = argv[i];
|
|
12
|
-
const match = /^--([a-z-]+)(?:=(.*))?$/.exec(arg);
|
|
13
|
-
if (!match || !allowed.includes(match[1])) {
|
|
14
|
-
throw new UsageError(`unknown argument "${arg}"\n${SERVER_USAGE}`);
|
|
15
|
-
}
|
|
16
|
-
const value = match[2] ?? argv[++i];
|
|
17
|
-
if (value === undefined || value.trim() === '' || (match[2] === undefined && value.startsWith('--'))) {
|
|
18
|
-
throw new UsageError(`--${match[1]} needs a value\n${SERVER_USAGE}`);
|
|
19
|
-
}
|
|
20
|
-
flags[match[1]] = value.trim();
|
|
21
|
-
}
|
|
22
|
-
return flags;
|
|
23
|
-
}
|
|
24
|
-
export function parseServerArgs(argv) {
|
|
25
|
-
const flags = parseFlags(argv, ['env', 'config']);
|
|
26
|
-
if (!flags.env) {
|
|
27
|
-
throw new UsageError(`--env is required\n${SERVER_USAGE}`);
|
|
28
|
-
}
|
|
29
|
-
return { envName: flags.env, ...(flags.config ? { configPath: flags.config } : {}) };
|
|
30
|
-
}
|
package/dist/src/config/load.js
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import * as fs from 'node:fs';
|
|
2
|
-
import * as os from 'node:os';
|
|
3
|
-
import * as path from 'node:path';
|
|
4
|
-
import { expandHome } from '../ssh/config.js';
|
|
5
|
-
import { ConfigValidationError, WRITE_KINDS, validateConfig, } from './schema.js';
|
|
6
|
-
export const DEFAULT_STATE_DIR = path.join(os.homedir(), '.mysql-mcp');
|
|
7
|
-
export const DEFAULT_CONFIG_PATH = path.join(DEFAULT_STATE_DIR, 'config.json');
|
|
8
|
-
const WRITE_FORBIDDEN_ENVS = new Set(['prod', 'production']);
|
|
9
|
-
export function isWriteForbiddenEnv(name) {
|
|
10
|
-
return WRITE_FORBIDDEN_ENVS.has(name.trim().toLowerCase());
|
|
11
|
-
}
|
|
12
|
-
export class ConfigLoadError extends Error {
|
|
13
|
-
constructor(message) {
|
|
14
|
-
super(message);
|
|
15
|
-
this.name = 'ConfigLoadError';
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
export function permissionProblems(configPath, options = {}) {
|
|
19
|
-
const platform = options.platform ?? process.platform;
|
|
20
|
-
if (platform === 'win32') {
|
|
21
|
-
return [];
|
|
22
|
-
}
|
|
23
|
-
const uid = options.uid ?? process.getuid?.();
|
|
24
|
-
const stat = options.stat ?? ((target) => fs.statSync(target));
|
|
25
|
-
const problems = [];
|
|
26
|
-
const checks = [
|
|
27
|
-
{ target: path.dirname(configPath), wanted: 0o700 },
|
|
28
|
-
{ target: configPath, wanted: 0o600 },
|
|
29
|
-
];
|
|
30
|
-
for (const { target, wanted } of checks) {
|
|
31
|
-
const info = stat(target);
|
|
32
|
-
if (uid !== undefined && info.uid !== uid) {
|
|
33
|
-
problems.push(`${target} is owned by uid ${info.uid}, not the current user (uid ${uid})`);
|
|
34
|
-
}
|
|
35
|
-
if ((info.mode & 0o077) !== 0) {
|
|
36
|
-
const mode = (info.mode & 0o777).toString(8).padStart(3, '0');
|
|
37
|
-
problems.push(`${target} has mode ${mode}; group and other must have no access. Run: chmod ${wanted.toString(8)} ${target}`);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return problems;
|
|
41
|
-
}
|
|
42
|
-
function emptyPermissions() {
|
|
43
|
-
return {
|
|
44
|
-
insert: Object.create(null),
|
|
45
|
-
update: Object.create(null),
|
|
46
|
-
delete: Object.create(null),
|
|
47
|
-
ddl: Object.create(null),
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
function mergeWritePolicy(env, account) {
|
|
51
|
-
const allow = {};
|
|
52
|
-
const schemas = emptyPermissions();
|
|
53
|
-
for (const kind of WRITE_KINDS) {
|
|
54
|
-
allow[kind] = account?.[kind] ?? env?.[kind] ?? false;
|
|
55
|
-
for (const layer of [env, account]) {
|
|
56
|
-
for (const [schema, flags] of Object.entries(layer?.schemas ?? {})) {
|
|
57
|
-
const flag = flags[kind];
|
|
58
|
-
if (flag !== undefined) {
|
|
59
|
-
schemas[kind][schema] = flag;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return { allow, schemas };
|
|
65
|
-
}
|
|
66
|
-
function hasWriteSettings(policy) {
|
|
67
|
-
return policy !== undefined && Object.keys(policy).length > 0;
|
|
68
|
-
}
|
|
69
|
-
const READ_ONLY_POLICY = () => ({
|
|
70
|
-
allow: { insert: false, update: false, delete: false, ddl: false },
|
|
71
|
-
schemas: emptyPermissions(),
|
|
72
|
-
});
|
|
73
|
-
function resolveDefaults(config, stateDir) {
|
|
74
|
-
const d = config.defaults;
|
|
75
|
-
const maxTimeoutSeconds = d.maxTimeoutSeconds ?? 30;
|
|
76
|
-
return {
|
|
77
|
-
maxResponseRows: d.maxResponseRows ?? 5000,
|
|
78
|
-
maxTimeoutSeconds,
|
|
79
|
-
defaultTimeoutSeconds: Math.min(10, maxTimeoutSeconds),
|
|
80
|
-
catalogTimeoutSeconds: d.catalogTimeoutSeconds ?? 60,
|
|
81
|
-
poolSize: d.poolSize ?? 10,
|
|
82
|
-
queueLimit: d.queueLimit ?? 100,
|
|
83
|
-
connectTimeoutMs: d.connectTimeoutMs ?? 10_000,
|
|
84
|
-
logging: d.logging ?? false,
|
|
85
|
-
catalog: {
|
|
86
|
-
enabled: d.catalog?.enabled ?? true,
|
|
87
|
-
ttlHours: d.catalog?.ttlHours ?? 24,
|
|
88
|
-
dir: d.catalog?.path
|
|
89
|
-
? path.resolve(stateDir, expandHome(d.catalog.path))
|
|
90
|
-
: path.join(stateDir, 'cache', 'catalog'),
|
|
91
|
-
},
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
export function resolveEnvironment(config, envName, paths = { configPath: DEFAULT_CONFIG_PATH }) {
|
|
95
|
-
if (!Object.hasOwn(config.environments, envName)) {
|
|
96
|
-
const known = Object.keys(config.environments);
|
|
97
|
-
throw new ConfigLoadError(`environment "${envName}" is not defined in ${paths.configPath}. ` +
|
|
98
|
-
`Defined: ${known.length > 0 ? known.join(', ') : '(none)'}.`);
|
|
99
|
-
}
|
|
100
|
-
const env = config.environments[envName];
|
|
101
|
-
const writeForbidden = isWriteForbiddenEnv(envName);
|
|
102
|
-
const warnings = [];
|
|
103
|
-
if (writeForbidden) {
|
|
104
|
-
const requested = [
|
|
105
|
-
...(hasWriteSettings(env.write) ? [`environments.${envName}.write`] : []),
|
|
106
|
-
...Object.entries(env.accounts)
|
|
107
|
-
.filter(([, account]) => hasWriteSettings(account.write))
|
|
108
|
-
.map(([name]) => `environments.${envName}.accounts.${name}.write`),
|
|
109
|
-
];
|
|
110
|
-
if (requested.length > 0) {
|
|
111
|
-
warnings.push(`environment "${envName}" is read-only by policy; ignoring ${requested.join(', ')}.`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const bastion = env.bastion ? { name: env.bastion, ...config.bastions[env.bastion] } : null;
|
|
115
|
-
const connections = Object.entries(env.accounts).map(([name, account]) => ({
|
|
116
|
-
name,
|
|
117
|
-
env: envName,
|
|
118
|
-
instance: {
|
|
119
|
-
...env.instances[account.instance],
|
|
120
|
-
name: account.instance,
|
|
121
|
-
port: env.instances[account.instance].port ?? 3306,
|
|
122
|
-
},
|
|
123
|
-
bastion,
|
|
124
|
-
user: account.user,
|
|
125
|
-
password: account.password,
|
|
126
|
-
...(account.database ? { database: account.database } : {}),
|
|
127
|
-
write: writeForbidden ? READ_ONLY_POLICY() : mergeWritePolicy(env.write, account.write),
|
|
128
|
-
}));
|
|
129
|
-
const stateDir = paths.stateDir ?? path.dirname(paths.configPath);
|
|
130
|
-
return {
|
|
131
|
-
name: envName,
|
|
132
|
-
label: envName.toUpperCase(),
|
|
133
|
-
writeForbidden,
|
|
134
|
-
connections,
|
|
135
|
-
defaults: resolveDefaults(config, stateDir),
|
|
136
|
-
configPath: paths.configPath,
|
|
137
|
-
stateDir,
|
|
138
|
-
warnings,
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
export function loadEnvironment(options) {
|
|
142
|
-
const configPath = path.resolve(expandHome(options.configPath ?? DEFAULT_CONFIG_PATH));
|
|
143
|
-
if (!fs.existsSync(configPath)) {
|
|
144
|
-
throw new ConfigLoadError(`config not found at ${configPath}. Run \`npx @ecubelabs/mysql-mcp init\` to create it.`);
|
|
145
|
-
}
|
|
146
|
-
return resolveEnvironment(readConfigFile(configPath, options), options.envName, { configPath });
|
|
147
|
-
}
|
|
148
|
-
export function readConfigFile(configPath, options = {}) {
|
|
149
|
-
const problems = permissionProblems(configPath, options);
|
|
150
|
-
if (problems.length > 0) {
|
|
151
|
-
throw new ConfigLoadError(`refusing to read ${configPath}:\n${problems.map((p) => ` - ${p}`).join('\n')}`);
|
|
152
|
-
}
|
|
153
|
-
let raw;
|
|
154
|
-
try {
|
|
155
|
-
raw = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
156
|
-
}
|
|
157
|
-
catch (error) {
|
|
158
|
-
throw new ConfigLoadError(`could not parse ${configPath}: ${error.message}`);
|
|
159
|
-
}
|
|
160
|
-
try {
|
|
161
|
-
return validateConfig(raw);
|
|
162
|
-
}
|
|
163
|
-
catch (error) {
|
|
164
|
-
if (error instanceof ConfigValidationError) {
|
|
165
|
-
throw new ConfigLoadError(`${configPath} is ${error.message}`);
|
|
166
|
-
}
|
|
167
|
-
throw error;
|
|
168
|
-
}
|
|
169
|
-
}
|