lugh-lang 0.7.2 → 0.7.3
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 +5 -1
- package/bin/lugh-lang.mjs +5 -0
- package/lib/guard.mjs +3 -3
- package/lib/protect.mjs +173 -0
- package/lugh.wasm +0 -0
- package/package.json +2 -2
- package/policies/default.lugh +12 -0
package/README.md
CHANGED
|
@@ -16,6 +16,7 @@ npx lugh-lang test program.lugh # run test blocks
|
|
|
16
16
|
npx lugh-lang check program.lugh # compile only
|
|
17
17
|
npx lugh-lang sandbox # what the simulated world contains
|
|
18
18
|
npx lugh-lang guard --policy <file|pack> -- <mcp server command> # guard a real MCP server
|
|
19
|
+
npx lugh-lang protect [--yes] [--undo] # guard every MCP server in your client configs
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
Commands exit with status 1 when a goal, test or compile step fails.
|
|
@@ -46,7 +47,10 @@ npx -y lugh-lang guard --policy filesystem -- npx -y @modelcontextprotocol/serve
|
|
|
46
47
|
Each tool call gets an effect (from the server's annotations or your policy),
|
|
47
48
|
then passes the gate: budgets, deny, allow, otherwise your client asks you.
|
|
48
49
|
Refused calls never reach the server, and every decision goes to an audit log.
|
|
49
|
-
Bundled policies: `filesystem`, `github`, `supabase`, `stripe`.
|
|
50
|
+
Bundled policies: `default`, `filesystem`, `github`, `supabase`, `stripe`.
|
|
51
|
+
`npx lugh-lang protect` does this for every server in your Claude Code, Claude
|
|
52
|
+
Desktop, Cursor, VS Code and Windsurf configs at once (it asks first; `--undo`
|
|
53
|
+
reverts). Details:
|
|
50
54
|
[docs/GUARD.md](https://github.com/Ashutosh3142857/lugh-lang/blob/main/docs/GUARD.md).
|
|
51
55
|
|
|
52
56
|
## Running for real
|
package/bin/lugh-lang.mjs
CHANGED
|
@@ -12,6 +12,8 @@ const HELP = `lugh-lang: Lugh, the programming language for AI agents
|
|
|
12
12
|
npx lugh-lang mcp MCP server (stdio) for AI agents
|
|
13
13
|
npx lugh-lang guard --policy <file.lugh> -- <mcp server command>
|
|
14
14
|
put a Lugh policy in front of any MCP server
|
|
15
|
+
npx lugh-lang protect [--yes] [--undo] guard every MCP server in your Claude, Cursor,
|
|
16
|
+
VS Code and Windsurf configs (asks first)
|
|
15
17
|
|
|
16
18
|
The sandbox simulates files, an HTTP API and MCP servers, and every call
|
|
17
19
|
passes the real policy gate. To act on real systems, install Lugh Core:
|
|
@@ -26,6 +28,9 @@ if (cmd === "--version" || cmd === "-v") {
|
|
|
26
28
|
if (cmd === "guard") {
|
|
27
29
|
const { main } = await import("../lib/guard.mjs");
|
|
28
30
|
main(process.argv.slice(3));
|
|
31
|
+
} else if (cmd === "protect") {
|
|
32
|
+
const { main } = await import("../lib/protect.mjs");
|
|
33
|
+
await main(process.argv.slice(3));
|
|
29
34
|
} else {
|
|
30
35
|
const run = await load();
|
|
31
36
|
if (cmd === "mcp") {
|
package/lib/guard.mjs
CHANGED
|
@@ -288,8 +288,8 @@ function lines(stream, onMessage, onBad) {
|
|
|
288
288
|
});
|
|
289
289
|
}
|
|
290
290
|
|
|
291
|
-
// A policy is a file path, or the name of a bundled pack (filesystem, github, supabase, stripe).
|
|
292
|
-
export const PACKS = ["filesystem", "github", "supabase", "stripe"];
|
|
291
|
+
// A policy is a file path, or the name of a bundled pack (default, filesystem, github, supabase, stripe).
|
|
292
|
+
export const PACKS = ["default", "filesystem", "github", "supabase", "stripe"];
|
|
293
293
|
export function resolvePolicy(name) {
|
|
294
294
|
if (existsSync(name)) return name;
|
|
295
295
|
if (PACKS.includes(name)) return fileURLToPath(new URL(`../policies/${name}.lugh`, import.meta.url));
|
|
@@ -304,7 +304,7 @@ export function main(argv) {
|
|
|
304
304
|
const get = (flag) => { const i = opts.indexOf(flag); return i >= 0 ? opts[i + 1] : undefined; };
|
|
305
305
|
const file = get("--policy");
|
|
306
306
|
if (!file || !cmd.length) {
|
|
307
|
-
process.stderr.write("usage: npx lugh-lang guard --policy <file.lugh | filesystem | github | supabase | stripe> [--audit <log.jsonl>] [--hide-denied] -- <server command> [args...]\n");
|
|
307
|
+
process.stderr.write("usage: npx lugh-lang guard --policy <file.lugh | default | filesystem | github | supabase | stripe> [--audit <log.jsonl>] [--hide-denied] -- <server command> [args...]\n");
|
|
308
308
|
process.exit(2);
|
|
309
309
|
}
|
|
310
310
|
let policy;
|
package/lib/protect.mjs
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// lugh-lang protect: put every local MCP server you already use behind Lugh guard.
|
|
2
|
+
//
|
|
3
|
+
// npx lugh-lang protect show the plan, then ask before changing anything
|
|
4
|
+
// npx lugh-lang protect --yes apply without asking
|
|
5
|
+
// npx lugh-lang protect --undo put every config back the way it was
|
|
6
|
+
//
|
|
7
|
+
// It finds MCP configs for Claude Code, Claude Desktop, Cursor, VS Code and
|
|
8
|
+
// Windsurf, and rewrites each local (stdio) server entry from
|
|
9
|
+
// "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "~/docs"]
|
|
10
|
+
// to
|
|
11
|
+
// "command": "npx", "args": ["-y", "lugh-lang", "guard", "--policy", "filesystem", "--",
|
|
12
|
+
// "npx", "-y", "@modelcontextprotocol/server-filesystem", "~/docs"]
|
|
13
|
+
// Nothing else in the file changes. A copy of each file is saved next to it
|
|
14
|
+
// first (<file>.lugh-backup), and --undo unwraps the entries in place, so later
|
|
15
|
+
// edits you made to the file are kept.
|
|
16
|
+
|
|
17
|
+
import { copyFileSync, existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { homedir } from "node:os";
|
|
19
|
+
import { join, resolve } from "node:path";
|
|
20
|
+
import { createInterface } from "node:readline";
|
|
21
|
+
|
|
22
|
+
// Where each client keeps its MCP servers. `key` is the object holding the
|
|
23
|
+
// servers; `projects` marks Claude Code's per-project ("local") servers.
|
|
24
|
+
export function configLocations({ home = homedir(), cwd = process.cwd(), platform = process.platform, env = process.env } = {}) {
|
|
25
|
+
const appData = env.APPDATA || join(home, "AppData", "Roaming");
|
|
26
|
+
const appSupport = platform === "darwin" ? join(home, "Library", "Application Support")
|
|
27
|
+
: platform === "win32" ? appData : join(home, ".config");
|
|
28
|
+
const list = [
|
|
29
|
+
{ client: "Claude Code (user and local)", file: join(home, ".claude.json"), key: "mcpServers", projects: true },
|
|
30
|
+
{ client: "Claude Code (this project)", file: join(cwd, ".mcp.json"), key: "mcpServers" },
|
|
31
|
+
{ client: "Claude Desktop", file: join(appSupport, "Claude", "claude_desktop_config.json"), key: "mcpServers" },
|
|
32
|
+
{ client: "Cursor", file: join(home, ".cursor", "mcp.json"), key: "mcpServers" },
|
|
33
|
+
{ client: "Cursor (this project)", file: join(cwd, ".cursor", "mcp.json"), key: "mcpServers" },
|
|
34
|
+
{ client: "VS Code", file: join(appSupport, "Code", "User", "mcp.json"), key: "servers" },
|
|
35
|
+
{ client: "VS Code (this project)", file: join(cwd, ".vscode", "mcp.json"), key: "servers" },
|
|
36
|
+
{ client: "Windsurf", file: join(home, ".codeium", "windsurf", "mcp_config.json"), key: "mcpServers" },
|
|
37
|
+
];
|
|
38
|
+
const seen = new Set();
|
|
39
|
+
return list.filter((l) => { const f = resolve(l.file); if (seen.has(f)) return false; seen.add(f); return true; });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Which bundled policy fits a server, from its command line.
|
|
43
|
+
export function packFor(entry) {
|
|
44
|
+
const s = [entry.command, ...(entry.args || [])].join(" ").toLowerCase();
|
|
45
|
+
if (s.includes("server-filesystem")) return "filesystem";
|
|
46
|
+
if (s.includes("github-mcp-server") || s.includes("server-github") || s.includes("githubcopilot.com/mcp")) return "github";
|
|
47
|
+
if (s.includes("supabase")) return "supabase";
|
|
48
|
+
if (s.includes("mcp.stripe.com") || s.includes("@stripe/mcp")) return "stripe";
|
|
49
|
+
return "default";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const isLugh = (e) => [e.command, ...(e.args || [])].some((a) => typeof a === "string" && /(^|[/@])lugh-lang(@[\w.]+)?$/.test(a));
|
|
53
|
+
|
|
54
|
+
// The guard's own arguments inside a wrapped entry: [... "guard", "--policy", P, (flags), "--", cmd, ...args]
|
|
55
|
+
function unwrapArgs(entry) {
|
|
56
|
+
const args = entry.args || [];
|
|
57
|
+
const g = args.indexOf("guard");
|
|
58
|
+
const sep = args.indexOf("--", g);
|
|
59
|
+
if (g < 0 || sep < 0 || !args.slice(0, g).some((a) => /lugh-lang/.test(a))) return null;
|
|
60
|
+
const inner = args.slice(sep + 1);
|
|
61
|
+
if (!inner.length) return null;
|
|
62
|
+
return { command: inner[0], args: inner.slice(1) };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function wrapEntry(entry, pack, platform = process.platform) {
|
|
66
|
+
const inner = [entry.command, ...(entry.args || [])];
|
|
67
|
+
const guard = ["-y", "lugh-lang", "guard", "--policy", pack, "--", ...inner];
|
|
68
|
+
return platform === "win32"
|
|
69
|
+
? { ...entry, command: "cmd", args: ["/c", "npx", ...guard] }
|
|
70
|
+
: { ...entry, command: "npx", args: guard };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function unwrapEntry(entry) {
|
|
74
|
+
const inner = unwrapArgs(entry);
|
|
75
|
+
return inner ? { ...entry, ...inner } : null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Every server block in one parsed config, with a path for messages.
|
|
79
|
+
function blocks(json, loc) {
|
|
80
|
+
const out = [];
|
|
81
|
+
if (json && typeof json[loc.key] === "object" && json[loc.key]) out.push({ where: "", servers: json[loc.key] });
|
|
82
|
+
if (loc.projects && json && typeof json.projects === "object") {
|
|
83
|
+
for (const [path, p] of Object.entries(json.projects)) {
|
|
84
|
+
if (p && typeof p.mcpServers === "object" && p.mcpServers && Object.keys(p.mcpServers).length) out.push({ where: ` [${path}]`, servers: p.mcpServers });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Work out what would change. Returns one item per config file that has servers.
|
|
91
|
+
export function plan({ undo = false, platform = process.platform, ...where } = {}) {
|
|
92
|
+
const files = [];
|
|
93
|
+
for (const loc of configLocations({ platform, ...where })) {
|
|
94
|
+
if (!existsSync(loc.file)) continue;
|
|
95
|
+
let json;
|
|
96
|
+
try { json = JSON.parse(readFileSync(loc.file, "utf8")); } catch (e) {
|
|
97
|
+
files.push({ ...loc, error: `not valid JSON (${e.message}); left alone` });
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const changes = [];
|
|
101
|
+
for (const b of blocks(json, loc)) {
|
|
102
|
+
for (const [name, entry] of Object.entries(b.servers)) {
|
|
103
|
+
if (!entry || typeof entry !== "object") continue;
|
|
104
|
+
if (undo) {
|
|
105
|
+
const un = unwrapEntry(entry);
|
|
106
|
+
if (un) changes.push({ name: name + b.where, servers: b.servers, key: name, to: un, note: "guard removed" });
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (entry.url || (entry.type && entry.type !== "stdio")) { changes.push({ name: name + b.where, skip: "remote server: guard runs local (stdio) servers; see docs/GUARD.md for mcp-remote" }); continue; }
|
|
110
|
+
if (!entry.command) continue;
|
|
111
|
+
if (unwrapArgs(entry)) { changes.push({ name: name + b.where, skip: "already guarded" }); continue; }
|
|
112
|
+
if (isLugh(entry)) { changes.push({ name: name + b.where, skip: "Lugh's own server" }); continue; }
|
|
113
|
+
const pack = packFor(entry);
|
|
114
|
+
changes.push({ name: name + b.where, servers: b.servers, key: name, to: wrapEntry(entry, pack, platform), note: `guard, policy ${pack}` });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (changes.length) files.push({ ...loc, json, changes });
|
|
118
|
+
}
|
|
119
|
+
return files;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Write the planned changes: backup first, then an atomic replace.
|
|
123
|
+
export function apply(files) {
|
|
124
|
+
let changed = 0;
|
|
125
|
+
for (const f of files) {
|
|
126
|
+
const todo = (f.changes || []).filter((c) => c.to);
|
|
127
|
+
if (!todo.length) continue;
|
|
128
|
+
for (const c of todo) c.servers[c.key] = c.to;
|
|
129
|
+
const backup = f.file + ".lugh-backup";
|
|
130
|
+
copyFileSync(f.file, backup);
|
|
131
|
+
const tmp = f.file + ".lugh-tmp";
|
|
132
|
+
writeFileSync(tmp, JSON.stringify(f.json, null, 2) + "\n");
|
|
133
|
+
renameSync(tmp, f.file);
|
|
134
|
+
changed += todo.length;
|
|
135
|
+
}
|
|
136
|
+
return changed;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function describe(files, undo) {
|
|
140
|
+
if (!files.length) return "No MCP servers found in Claude Code, Claude Desktop, Cursor, VS Code or Windsurf configs.";
|
|
141
|
+
const lines = [];
|
|
142
|
+
for (const f of files) {
|
|
143
|
+
lines.push(`${f.client}: ${f.file}`);
|
|
144
|
+
if (f.error) { lines.push(` ! ${f.error}`); continue; }
|
|
145
|
+
for (const c of f.changes) lines.push(c.to ? ` ✓ ${c.name}: ${c.note}` : ` · ${c.name}: ${c.skip}`);
|
|
146
|
+
}
|
|
147
|
+
return lines.join("\n");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export async function main(argv) {
|
|
151
|
+
const undo = argv.includes("--undo");
|
|
152
|
+
const yes = argv.includes("--yes") || argv.includes("-y");
|
|
153
|
+
const files = plan({ undo });
|
|
154
|
+
const count = files.flatMap((f) => f.changes || []).filter((c) => c.to).length;
|
|
155
|
+
console.log(describe(files, undo));
|
|
156
|
+
if (!count) {
|
|
157
|
+
console.log(undo ? "\nNothing to undo." : "\nNothing to change.");
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
console.log(undo
|
|
161
|
+
? `\n${count} server(s) will go back to running without guard.`
|
|
162
|
+
: `\n${count} server(s) will run behind Lugh guard. Reads run, risky actions ask you, destructive ones are refused.\nA copy of each file is saved as <file>.lugh-backup; undo any time with: npx lugh-lang protect --undo`);
|
|
163
|
+
if (!yes) {
|
|
164
|
+
if (!process.stdin.isTTY) { console.log("\nRun again with --yes to apply."); return; }
|
|
165
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
166
|
+
const answer = await new Promise((r) => rl.question("\nApply these changes? [y/N] ", r));
|
|
167
|
+
rl.close();
|
|
168
|
+
if (!/^y(es)?$/i.test(answer.trim())) { console.log("No changes made."); return; }
|
|
169
|
+
}
|
|
170
|
+
const n = apply(files);
|
|
171
|
+
console.log(`\nDone: ${n} server(s) ${undo ? "unguarded" : "guarded"}. Restart your MCP clients to pick up the change.`);
|
|
172
|
+
if (!undo) console.log("Every decision is logged to ~/.lugh/guard-audit.jsonl. Policies: https://github.com/Ashutosh3142857/lugh-lang/blob/main/docs/GUARD.md");
|
|
173
|
+
}
|
package/lugh.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lugh-lang",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Lugh, the programming language for AI agents: check and dry-run programs where policy, budgets and human approval are part of the code. Includes an MCP server.",
|
|
5
5
|
"mcpName": "io.github.Ashutosh3142857/lugh",
|
|
6
6
|
"type": "module",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"exports": { ".": "./lib/runtime.mjs", "./mcp": "./lib/mcp.mjs" },
|
|
9
9
|
"files": ["bin/", "lib/", "policies/", "lugh.wasm", "REFERENCE.md", "README.md"],
|
|
10
10
|
"engines": { "node": ">=18" },
|
|
11
|
-
"scripts": { "test": "node test/mcp.test.mjs && node --test --test-timeout=15000 test/guard.test.mjs" },
|
|
11
|
+
"scripts": { "test": "node test/mcp.test.mjs && node --test --test-timeout=15000 test/guard.test.mjs test/protect.test.mjs" },
|
|
12
12
|
"keywords": ["ai-agents", "mcp-proxy", "mcp-guard", "mcp-security", "agents", "mcp", "model-context-protocol", "mcp-server", "programming-language", "guardrails", "human-in-the-loop", "agent-safety", "policy", "webassembly"],
|
|
13
13
|
"homepage": "https://ashutosh3142857.github.io/lugh-lang/",
|
|
14
14
|
"repository": { "type": "git", "url": "git+https://github.com/Ashutosh3142857/lugh-lang.git", "directory": "npm" },
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Lugh guard policy for any MCP server without a pack of its own.
|
|
2
|
+
# npx -y lugh-lang guard --policy default -- <server command>
|
|
3
|
+
#
|
|
4
|
+
# Reads run freely. Anything that writes, sends, submits, spends money or runs
|
|
5
|
+
# commands asks first. Anything the server marks destructive is refused. Tools
|
|
6
|
+
# that carry no annotations count as submit, so they ask.
|
|
7
|
+
agent DefaultGuard
|
|
8
|
+
budget calls 500
|
|
9
|
+
policy
|
|
10
|
+
allow read
|
|
11
|
+
ask human before write, send, submit, spend, execute
|
|
12
|
+
deny delete
|