Examples
Examples
Each example is a complete script you can save to ~/.mentu/scripts/ and run with mentu script run <name>.
1. CIR Audit
Query all signals, check for contradictions, and report aggregate statistics.
Save as ~/.mentu/scripts/cir-audit.ts:
const stats = mentu.cir.stats();
const contradictions = mentu.cir.contradictions();
const recent = mentu.cir.query({ since: '24h', limit: 100 });
console.log('=== CIR Audit Report ===');
console.log(`Total signals: ${stats.signals}`);
console.log(`Relations: ${stats.relations}`);
console.log(`Embeddings: ${stats.embeddings}`);
console.log(`Patterns: ${stats.patterns}`);
console.log(`Contradictions: ${stats.contradictions}`);
console.log(`Database size: ${(stats.database_size_bytes / 1024 / 1024).toFixed(1)} MB`);
console.log(`Signals (last 24h): ${recent.length}`);
if (contradictions.length > 0) {
console.log('\n--- Contradictions ---');
for (const c of contradictions) {
console.log(` [${c.id}] ${c.body}`);
if (c.domain) console.log(` domain: ${c.domain}`);
}
}
return {
healthy: contradictions.length === 0,
stats,
recent_count: recent.length,
contradiction_count: contradictions.length,
};Run it:
mentu script run cir-audit2. Daily Health Check
Run a system health check, gather CIR stats, and send a notification if anything looks wrong.
Save as ~/.mentu/scripts/daily-health.ts:
const health = mentu.health.check();
const stats = mentu.cir.stats();
const contradictions = mentu.cir.contradictions();
const ledger = mentu.ledger.verify();
const issues: string[] = [];
if (contradictions.length > 3) {
issues.push(`${contradictions.length} contradictions in CIR`);
}
if (!ledger.valid) {
issues.push('Ledger integrity check failed');
}
if (stats.signals === 0) {
issues.push('CIR has zero signals — possible data loss');
}
if (issues.length > 0) {
console.log(`Health check: ${issues.length} issue(s) found`);
for (const issue of issues) {
console.log(` - ${issue}`);
}
mentu.notify.send('Mentu Health Alert', issues.join('; '));
} else {
console.log('Health check: all clear');
console.log(` CIR: ${stats.signals} signals, ${stats.contradictions} contradictions`);
console.log(` Ledger: ${ledger.entries} entries, valid`);
}
return { healthy: issues.length === 0, issues, stats, ledger };Run it:
mentu script run daily-health3. Vault-Driven API Call
Read an API key from the vault and use it in script logic. Demonstrates the pattern of keeping secrets out of code.
Save as ~/.mentu/scripts/vault-check.ts:
const keyName = mentu.vars.KEY ?? 'API_TOKEN';
const scope = mentu.vars.SCOPE;
const value = mentu.vault.get(keyName, scope ? { scope } : undefined);
if (!value) {
console.log(`No secret found for key: ${keyName}`);
console.log('Store one with: mentu vault set <key> <value>');
return { found: false, key: keyName };
}
const prefix = value.slice(0, 8);
const keys = mentu.vault.list(scope ? { scope } : undefined);
console.log(`Secret "${keyName}" found: ${prefix}...`);
console.log(`Vault contains ${keys.length} total keys`);
// Capture an observation that the key was accessed
mentu.cir.capture(`Vault key "${keyName}" accessed by script`, {
type: 'observation',
domain: 'security',
source: 'vault-check',
});
return { found: true, key: keyName, prefix, total_keys: keys.length };Run it:
mentu script run vault-check --var KEY=OPENAI_KEY
mentu script run vault-check --var KEY=DEPLOY_TOKEN --var SCOPE=ci4. Conditional Sequence Trigger
Check CIR health, and trigger a remediation sequence only if contradictions exceed a threshold.
Save as ~/.mentu/scripts/conditional-trigger.ts:
const threshold = parseInt(mentu.vars.THRESHOLD ?? '3', 10);
const sequenceName = mentu.vars.SEQUENCE ?? 'cir-audit';
const contradictions = mentu.cir.contradictions();
const stats = mentu.cir.stats();
console.log(`Contradictions: ${contradictions.length} (threshold: ${threshold})`);
if (contradictions.length > threshold) {
console.log(`Threshold exceeded — triggering sequence: ${sequenceName}`);
mentu.cir.capture(`Auto-triggered ${sequenceName}: ${contradictions.length} contradictions`, {
type: 'observation',
domain: 'automation',
source: 'conditional-trigger',
confidence: 1.0,
});
const output = mentu.sequence.run(sequenceName, {
vars: {
CONTRADICTION_COUNT: String(contradictions.length),
THRESHOLD: String(threshold),
},
});
mentu.notify.send('Sequence Triggered', `${sequenceName} completed`);
console.log('Sequence output:', output);
return { triggered: true, sequence: sequenceName, contradictions: contradictions.length };
} else {
console.log('Within threshold — no action needed');
return { triggered: false, contradictions: contradictions.length, threshold };
}Run it:
mentu script run conditional-trigger --var THRESHOLD=5 --var SEQUENCE=cir-audit5. Temporal Manager
List all temporals, identify circuit-broken ones, and optionally re-enable them.
Save as ~/.mentu/scripts/temporal-manager.ts:
const action = mentu.vars.ACTION ?? 'report';
const temporals = mentu.temporal.list();
const active = temporals.filter(t => t.enabled && !t.circuit_broken);
const disabled = temporals.filter(t => !t.enabled);
const broken = temporals.filter(t => t.circuit_broken);
console.log(`=== Temporal Status ===`);
console.log(`Active: ${active.length}`);
console.log(`Disabled: ${disabled.length}`);
console.log(`Circuit-broken: ${broken.length}`);
if (broken.length > 0) {
console.log('\n--- Circuit-Broken Temporals ---');
for (const t of broken) {
console.log(` ${t.name}: ${t.consecutive_failures} consecutive failures`);
console.log(` schedule: ${t.schedule}`);
console.log(` last run: ${t.last_run ?? 'never'}`);
}
}
if (action === 'recover' && broken.length > 0) {
console.log('\n--- Recovering ---');
for (const t of broken) {
console.log(` Re-enabling: ${t.name}`);
mentu.temporal.enable(t.name);
}
mentu.cir.capture(`Re-enabled ${broken.length} circuit-broken temporals`, {
type: 'observation',
domain: 'temporals',
source: 'temporal-manager',
});
mentu.notify.send('Temporals Recovered', `${broken.length} temporal(s) re-enabled`);
}
return {
total: temporals.length,
active: active.length,
disabled: disabled.length,
broken: broken.map(t => t.name),
action,
};Run it:
# Report only
mentu script run temporal-manager
# Re-enable circuit-broken temporals
mentu script run temporal-manager --var ACTION=recover6. MCP Tool Composition
Use the servers.* proxy to call external MCP tools from within a script. This example calls a search tool and captures the results in CIR.
Save as ~/.mentu/scripts/mcp-search.ts:
const query = mentu.vars.QUERY;
if (!query) {
console.log('Usage: mentu script run mcp-search --var QUERY=<search-term>');
return { error: 'QUERY variable required' };
}
const server = mentu.vars.SERVER ?? 'crawlio';
const tool = mentu.vars.TOOL ?? 'search_api';
console.log(`Searching via ${server}.${tool}: "${query}"`);
const result = await servers[server].call(tool, { query });
console.log('Results:', JSON.stringify(result, null, 2));
// Capture the search in CIR for provenance
mentu.cir.capture(`MCP search via ${server}.${tool}: "${query}"`, {
type: 'observation',
domain: 'mcp',
source: 'mcp-search',
tags: [server, tool],
});
return { server, tool, query, result };Run it:
mentu script run mcp-search --var QUERY="authentication endpoints"
mentu script run mcp-search --var QUERY="login" --var SERVER=crawlio --var TOOL=search_apiThe servers.* proxy requires a .mcp.json file in your workspace with the target server configured. See SDK Overview for details on the security model.