magector 2.12.0 → 2.13.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/package.json +5 -5
- package/src/mcp-server.js +229 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "magector",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"description": "Semantic code search for Magento 2 — index, search, MCP server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/mcp-server.js",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
"ruvector": "^0.1.96"
|
|
34
34
|
},
|
|
35
35
|
"optionalDependencies": {
|
|
36
|
-
"@magector/cli-darwin-arm64": "2.
|
|
37
|
-
"@magector/cli-linux-x64": "2.
|
|
38
|
-
"@magector/cli-linux-arm64": "2.
|
|
39
|
-
"@magector/cli-win32-x64": "2.
|
|
36
|
+
"@magector/cli-darwin-arm64": "2.13.0",
|
|
37
|
+
"@magector/cli-linux-x64": "2.13.0",
|
|
38
|
+
"@magector/cli-linux-arm64": "2.13.0",
|
|
39
|
+
"@magector/cli-win32-x64": "2.13.0"
|
|
40
40
|
},
|
|
41
41
|
"keywords": [
|
|
42
42
|
"magento",
|
package/src/mcp-server.js
CHANGED
|
@@ -3425,6 +3425,146 @@ async function traceCallChain(startClass, startMethod, maxDepth = 3) {
|
|
|
3425
3425
|
return result;
|
|
3426
3426
|
}
|
|
3427
3427
|
|
|
3428
|
+
// ─── Method Chain Enrichment ────────────────────────────────────
|
|
3429
|
+
// Scans PHP files for two-step method chains (->first()->second()) and detects
|
|
3430
|
+
// null guards in surrounding code. Results stored in SQLite enrichment.db for
|
|
3431
|
+
// instant O(1) queries — eliminates 20+ grep calls for null-risk analyses.
|
|
3432
|
+
|
|
3433
|
+
const ENRICHMENT_DB_PATH = (root) => path.join(root, '.magector', 'enrichment.db');
|
|
3434
|
+
|
|
3435
|
+
/**
|
|
3436
|
+
* Detect null guard for a chained call in surrounding lines.
|
|
3437
|
+
* Checks ±guardRadius lines for: null checks, ?->, ??, isset()
|
|
3438
|
+
*/
|
|
3439
|
+
function hasNullGuard(lines, matchLineIdx, receiverExpr, guardRadius = 6) {
|
|
3440
|
+
const start = Math.max(0, matchLineIdx - guardRadius);
|
|
3441
|
+
const end = Math.min(lines.length - 1, matchLineIdx + guardRadius);
|
|
3442
|
+
const window = lines.slice(start, end + 1).join('\n');
|
|
3443
|
+
|
|
3444
|
+
if (window.includes('?->')) return true;
|
|
3445
|
+
if (/\?\?|\?:/.test(window)) return true;
|
|
3446
|
+
|
|
3447
|
+
if (receiverExpr) {
|
|
3448
|
+
const esc = receiverExpr.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
3449
|
+
if (new RegExp(`(?:is_null\\s*\\(\\s*${esc}|${esc}\\s*(?:===|!==)\\s*null|!\\s*${esc}\\s*[,)]|isset\\s*\\(\\s*${esc})`, 'i').test(window)) return true;
|
|
3450
|
+
}
|
|
3451
|
+
return false;
|
|
3452
|
+
}
|
|
3453
|
+
|
|
3454
|
+
/**
|
|
3455
|
+
* Scan vendor/ PHP files for ->first()->second() chains and store null-safety
|
|
3456
|
+
* analysis in enrichment.db. Called by magento_enrich and after magento_index.
|
|
3457
|
+
*/
|
|
3458
|
+
async function enrichMethodChains(root, options = {}) {
|
|
3459
|
+
const dbPath = ENRICHMENT_DB_PATH(root);
|
|
3460
|
+
|
|
3461
|
+
// Use node:sqlite (built-in, no deps)
|
|
3462
|
+
let DatabaseSync;
|
|
3463
|
+
try {
|
|
3464
|
+
({ DatabaseSync } = await import('node:sqlite'));
|
|
3465
|
+
} catch {
|
|
3466
|
+
throw new Error('node:sqlite not available — requires Node.js 22.5+');
|
|
3467
|
+
}
|
|
3468
|
+
|
|
3469
|
+
const db = new DatabaseSync(dbPath);
|
|
3470
|
+
db.exec('PRAGMA journal_mode = WAL');
|
|
3471
|
+
db.exec(`
|
|
3472
|
+
CREATE TABLE IF NOT EXISTS method_chains (
|
|
3473
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
3474
|
+
file TEXT NOT NULL,
|
|
3475
|
+
line INTEGER NOT NULL,
|
|
3476
|
+
chain TEXT NOT NULL,
|
|
3477
|
+
first_method TEXT NOT NULL,
|
|
3478
|
+
second_method TEXT NOT NULL,
|
|
3479
|
+
has_null_guard INTEGER NOT NULL DEFAULT 0,
|
|
3480
|
+
updated_at INTEGER NOT NULL
|
|
3481
|
+
);
|
|
3482
|
+
CREATE INDEX IF NOT EXISTS idx_first_method ON method_chains (first_method);
|
|
3483
|
+
CREATE INDEX IF NOT EXISTS idx_null_guard ON method_chains (has_null_guard, first_method);
|
|
3484
|
+
`);
|
|
3485
|
+
|
|
3486
|
+
// Two-step chain: $var->firstMethod(...)->secondMethod(
|
|
3487
|
+
// Captures: receiver ($var), firstMethod, secondMethod
|
|
3488
|
+
const chainRegex = /(\$\w+)\s*->\s*(\w+)\s*\([^)]{0,60}\)\s*->\s*(\w+)\s*\(/g;
|
|
3489
|
+
const now = Date.now();
|
|
3490
|
+
|
|
3491
|
+
const phpFiles = await glob('vendor/**/*.php', { cwd: root, absolute: true, nodir: true });
|
|
3492
|
+
let scanned = 0;
|
|
3493
|
+
let chains = 0;
|
|
3494
|
+
|
|
3495
|
+
const insertStmt = db.prepare(
|
|
3496
|
+
'INSERT INTO method_chains (file, line, chain, first_method, second_method, has_null_guard, updated_at) VALUES (?,?,?,?,?,?,?)'
|
|
3497
|
+
);
|
|
3498
|
+
const deleteFile = db.prepare('DELETE FROM method_chains WHERE file = ?');
|
|
3499
|
+
|
|
3500
|
+
// Process files in batches for memory efficiency
|
|
3501
|
+
for (const phpFile of phpFiles) {
|
|
3502
|
+
let content;
|
|
3503
|
+
try { content = readFileSync(phpFile, 'utf-8'); } catch { continue; }
|
|
3504
|
+
if (!content.includes('->')) continue;
|
|
3505
|
+
|
|
3506
|
+
const relPath = phpFile.replace(root + '/', '');
|
|
3507
|
+
const lines = content.split('\n');
|
|
3508
|
+
const rows = [];
|
|
3509
|
+
|
|
3510
|
+
chainRegex.lastIndex = 0;
|
|
3511
|
+
let m;
|
|
3512
|
+
while ((m = chainRegex.exec(content)) !== null) {
|
|
3513
|
+
const lineNum = content.slice(0, m.index).split('\n').length;
|
|
3514
|
+
rows.push({
|
|
3515
|
+
file: relPath, line: lineNum,
|
|
3516
|
+
chain: `->${m[2]}()->${m[3]}()`,
|
|
3517
|
+
firstMethod: m[2], secondMethod: m[3],
|
|
3518
|
+
hasNullGuard: hasNullGuard(lines, lineNum - 1, m[1]) ? 1 : 0
|
|
3519
|
+
});
|
|
3520
|
+
chains++;
|
|
3521
|
+
}
|
|
3522
|
+
|
|
3523
|
+
if (rows.length > 0) {
|
|
3524
|
+
deleteFile.run(relPath);
|
|
3525
|
+
for (const r of rows) {
|
|
3526
|
+
insertStmt.run(r.file, r.line, r.chain, r.firstMethod, r.secondMethod, r.hasNullGuard, now);
|
|
3527
|
+
}
|
|
3528
|
+
}
|
|
3529
|
+
scanned++;
|
|
3530
|
+
}
|
|
3531
|
+
|
|
3532
|
+
db.close();
|
|
3533
|
+
return { scanned, chains };
|
|
3534
|
+
}
|
|
3535
|
+
|
|
3536
|
+
/**
|
|
3537
|
+
* Query enrichment.db for unsafe method chains (no null guard).
|
|
3538
|
+
*/
|
|
3539
|
+
async function queryNullRisks(root, firstMethod, limit = 100) {
|
|
3540
|
+
const dbPath = ENRICHMENT_DB_PATH(root);
|
|
3541
|
+
if (!existsSync(dbPath)) return null;
|
|
3542
|
+
|
|
3543
|
+
let DatabaseSync;
|
|
3544
|
+
try {
|
|
3545
|
+
({ DatabaseSync } = await import('node:sqlite'));
|
|
3546
|
+
} catch {
|
|
3547
|
+
return null;
|
|
3548
|
+
}
|
|
3549
|
+
|
|
3550
|
+
const db = new DatabaseSync(dbPath, { open: true });
|
|
3551
|
+
let rows;
|
|
3552
|
+
try {
|
|
3553
|
+
if (firstMethod) {
|
|
3554
|
+
rows = db.prepare(
|
|
3555
|
+
'SELECT file, line, chain, second_method FROM method_chains WHERE has_null_guard = 0 AND first_method = ? ORDER BY file, line LIMIT ?'
|
|
3556
|
+
).all(firstMethod, limit);
|
|
3557
|
+
} else {
|
|
3558
|
+
rows = db.prepare(
|
|
3559
|
+
'SELECT file, line, chain, first_method, second_method FROM method_chains WHERE has_null_guard = 0 ORDER BY first_method, file, line LIMIT ?'
|
|
3560
|
+
).all(limit);
|
|
3561
|
+
}
|
|
3562
|
+
} finally {
|
|
3563
|
+
db.close();
|
|
3564
|
+
}
|
|
3565
|
+
return rows;
|
|
3566
|
+
}
|
|
3567
|
+
|
|
3428
3568
|
// ─── AST Search (semgrep) ───────────────────────────────────────
|
|
3429
3569
|
|
|
3430
3570
|
async function astSearch(pattern, searchPath, lang, maxResults) {
|
|
@@ -4243,6 +4383,29 @@ server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
|
4243
4383
|
required: ['pattern']
|
|
4244
4384
|
}
|
|
4245
4385
|
},
|
|
4386
|
+
{
|
|
4387
|
+
name: 'magento_enrich',
|
|
4388
|
+
description: 'Build the method-chain enrichment index. Scans all vendor/ PHP files for two-step method chains (->firstMethod()->secondMethod()) and analyses whether each call has a null guard in surrounding code. Results stored in .magector/enrichment.db. Run this once after magento_index, then use magento_find_null_risks for instant O(1) null-safety queries instead of 20+ grep calls. Also runs automatically after magento_index completes.',
|
|
4389
|
+
inputSchema: { type: 'object', properties: {} }
|
|
4390
|
+
},
|
|
4391
|
+
{
|
|
4392
|
+
name: 'magento_find_null_risks',
|
|
4393
|
+
description: 'Find method chains without null guards using the pre-built enrichment index. Returns all ->firstMethod()->secondMethod() calls where no null check (=== null, !== null, ?->, ??, isset, is_null) was detected in surrounding code. Requires magento_enrich to have been run first. 100× faster than grep — O(1) SQLite query vs O(n) file scan. Use firstMethod to filter (e.g., "getPayment" finds all ->getPayment()->anything() without null guard). ⚡ For multi-query workflows use magento_batch.',
|
|
4394
|
+
inputSchema: {
|
|
4395
|
+
type: 'object',
|
|
4396
|
+
properties: {
|
|
4397
|
+
firstMethod: {
|
|
4398
|
+
type: 'string',
|
|
4399
|
+
description: 'Filter by first method name. Example: "getPayment" returns all ->getPayment()->$X() without null guard. Omit to get all unsafe chains.'
|
|
4400
|
+
},
|
|
4401
|
+
limit: {
|
|
4402
|
+
type: 'number',
|
|
4403
|
+
description: 'Maximum results (default: 100, max: 500)',
|
|
4404
|
+
default: 100
|
|
4405
|
+
}
|
|
4406
|
+
}
|
|
4407
|
+
}
|
|
4408
|
+
},
|
|
4246
4409
|
{
|
|
4247
4410
|
name: 'magento_trace_api',
|
|
4248
4411
|
description: 'Trace a REST or GraphQL API endpoint from URL to implementation. Parses webapi.xml to find the service interface, resolves the DI preference to the concrete class, reads the execute/method body, and checks di.xml for constructor arguments. Returns the complete chain in one call.',
|
|
@@ -4309,7 +4472,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
4309
4472
|
// These tools have filesystem/di.xml fallbacks — work without serve process
|
|
4310
4473
|
'magento_find_class', 'magento_find_method', 'magento_find_plugin',
|
|
4311
4474
|
'magento_find_observer', 'magento_find_di_wiring', 'magento_module_structure',
|
|
4312
|
-
'magento_batch', 'magento_find_config', 'magento_find_callers', 'magento_grep', 'magento_read', 'magento_trace_api', 'magento_ast_search'];
|
|
4475
|
+
'magento_batch', 'magento_find_config', 'magento_find_callers', 'magento_grep', 'magento_read', 'magento_trace_api', 'magento_ast_search', 'magento_find_null_risks'];
|
|
4313
4476
|
if (warmupInProgress && !indexFreeTools.includes(name)) {
|
|
4314
4477
|
logToFile('REQ', `${name} → blocked (warmup: loading index)`);
|
|
4315
4478
|
return {
|
|
@@ -4572,10 +4735,16 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
4572
4735
|
case 'magento_index': {
|
|
4573
4736
|
const root = args.path || config.magentoRoot;
|
|
4574
4737
|
const output = rustIndex(root);
|
|
4738
|
+
// Auto-enrich after indexing: runs in background, doesn't block response
|
|
4739
|
+
enrichMethodChains(root, { verbose: true }).then(({ scanned, chains }) => {
|
|
4740
|
+
logToFile('INFO', `Auto-enrich complete: ${scanned} files, ${chains} chains`);
|
|
4741
|
+
}).catch(err => {
|
|
4742
|
+
logToFile('WARN', `Auto-enrich failed: ${err.message}`);
|
|
4743
|
+
});
|
|
4575
4744
|
return {
|
|
4576
4745
|
content: [{
|
|
4577
4746
|
type: 'text',
|
|
4578
|
-
text: `Indexing complete (Rust core).\n\n${output}`
|
|
4747
|
+
text: `Indexing complete (Rust core).\n\n${output}\n\n_Method-chain enrichment index is being built in the background. Use \`magento_enrich\` to run it manually or wait ~30-60s._`
|
|
4579
4748
|
}]
|
|
4580
4749
|
};
|
|
4581
4750
|
}
|
|
@@ -6203,6 +6372,19 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
6203
6372
|
}
|
|
6204
6373
|
break;
|
|
6205
6374
|
}
|
|
6375
|
+
case 'magento_find_null_risks': {
|
|
6376
|
+
const bRoot = config.magentoRoot;
|
|
6377
|
+
const bLimit = Math.min(a.limit || 100, 500);
|
|
6378
|
+
const bRows = bRoot ? await queryNullRisks(bRoot, a.firstMethod || null, bLimit) : null;
|
|
6379
|
+
if (!bRows) { text = '⚠️ Run magento_enrich first.'; break; }
|
|
6380
|
+
if (bRows.length === 0) { text = 'No unsafe chains found.'; break; }
|
|
6381
|
+
text = `Found ${bRows.length} unsafe chain(s):\n`;
|
|
6382
|
+
for (const r of bRows.slice(0, 50)) {
|
|
6383
|
+
const chain = r.chain || `->${r.first_method}()->${r.second_method}()`;
|
|
6384
|
+
text += `${r.file}:${r.line}: ${chain}\n`;
|
|
6385
|
+
}
|
|
6386
|
+
break;
|
|
6387
|
+
}
|
|
6206
6388
|
default:
|
|
6207
6389
|
text = `Unsupported batch tool: ${q.tool}`;
|
|
6208
6390
|
}
|
|
@@ -6460,6 +6642,51 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
6460
6642
|
return { content: [{ type: 'text', text }] };
|
|
6461
6643
|
}
|
|
6462
6644
|
|
|
6645
|
+
case 'magento_enrich': {
|
|
6646
|
+
const root = config.magentoRoot;
|
|
6647
|
+
if (!root) return { content: [{ type: 'text', text: 'MAGENTO_ROOT not set.' }], isError: true };
|
|
6648
|
+
let text = `## magento_enrich\n\nScanning vendor/ PHP files for method chains...\n`;
|
|
6649
|
+
try {
|
|
6650
|
+
const { scanned, chains } = await enrichMethodChains(root, { verbose: true });
|
|
6651
|
+
text += `\n✅ **Done**\n- Files scanned: ${scanned}\n- Method chains indexed: ${chains}\n- Null-risk index saved to: \`.magector/enrichment.db\`\n\nUse \`magento_find_null_risks\` to query unsafe chains.`;
|
|
6652
|
+
} catch (err) {
|
|
6653
|
+
text += `\n❌ Error: ${err.message}`;
|
|
6654
|
+
}
|
|
6655
|
+
return { content: [{ type: 'text', text }] };
|
|
6656
|
+
}
|
|
6657
|
+
|
|
6658
|
+
case 'magento_find_null_risks': {
|
|
6659
|
+
const root = config.magentoRoot;
|
|
6660
|
+
if (!root) return { content: [{ type: 'text', text: 'MAGENTO_ROOT not set.' }], isError: true };
|
|
6661
|
+
const limit = Math.min(args.limit || 100, 500);
|
|
6662
|
+
const rows = await queryNullRisks(root, args.firstMethod || null, limit);
|
|
6663
|
+
if (rows === null) {
|
|
6664
|
+
return { content: [{ type: 'text', text: `## magento_find_null_risks\n\n⚠️ Enrichment index not found. Run \`magento_enrich\` first to build the method-chain index.` }] };
|
|
6665
|
+
}
|
|
6666
|
+
if (rows.length === 0) {
|
|
6667
|
+
const filter = args.firstMethod ? ` for \`->${args.firstMethod}()\`` : '';
|
|
6668
|
+
return { content: [{ type: 'text', text: `## magento_find_null_risks${filter}\n\nNo unsafe chains found. All detected chains have null guards.` }] };
|
|
6669
|
+
}
|
|
6670
|
+
const filter = args.firstMethod ? ` for \`->${args.firstMethod}()\`` : '';
|
|
6671
|
+
let text = `## magento_find_null_risks${filter}\n\nFound **${rows.length}** chain(s) without null guard:\n\n`;
|
|
6672
|
+
// Group by chain type for readability
|
|
6673
|
+
const byChain = {};
|
|
6674
|
+
for (const r of rows) {
|
|
6675
|
+
const key = r.chain || `->${r.first_method}()->${r.second_method}()`;
|
|
6676
|
+
if (!byChain[key]) byChain[key] = [];
|
|
6677
|
+
byChain[key].push(r);
|
|
6678
|
+
}
|
|
6679
|
+
for (const [chain, sites] of Object.entries(byChain)) {
|
|
6680
|
+
text += `### \`${chain}\` (${sites.length} site${sites.length > 1 ? 's' : ''})\n`;
|
|
6681
|
+
for (const s of sites.slice(0, 20)) {
|
|
6682
|
+
text += `- \`${s.file}:${s.line}\`\n`;
|
|
6683
|
+
}
|
|
6684
|
+
if (sites.length > 20) text += `- ... and ${sites.length - 20} more\n`;
|
|
6685
|
+
text += '\n';
|
|
6686
|
+
}
|
|
6687
|
+
return { content: [{ type: 'text', text }] };
|
|
6688
|
+
}
|
|
6689
|
+
|
|
6463
6690
|
default:
|
|
6464
6691
|
return {
|
|
6465
6692
|
content: [{
|