|
1 | 1 | // SPDX-License-Identifier: PMPL-1.0-or-later |
2 | 2 | // Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
3 | 3 | // |
4 | | -// typed-wasm-mcp/mod.js -- typed-wasm-mcp gateway. Delegates to the `typed-wasm` CLI binary. |
| 4 | +// typed-wasm-mcp/mod.js — MCP gateway. Delegates to the `affinescript` CLI, |
| 5 | +// which since 2026-05-03 carries the full typed-wasm Level 7/10 verifier |
| 6 | +// (intra-module ownership) and the Level 10 cross-module boundary verifier. |
| 7 | +// |
| 8 | +// Tools: |
| 9 | +// typed_wasm_validate_module → affinescript verify FILE.affine |
| 10 | +// typed_wasm_check_types → affinescript check FILE.affine |
| 11 | +// typed_wasm_compile_module → affinescript compile FILE.affine -o OUT.wasm |
| 12 | +// |
| 13 | +// All three accept .affine source paths. The verifier runs over the freshly |
| 14 | +// emitted Wasm so there is no separate .wasm-input path: shipping unchecked |
| 15 | +// .wasm and asking us to validate it would defeat the typed-wasm contract. |
| 16 | +// |
| 17 | +// Pass an explicit `output_path` to typed_wasm_compile_module; otherwise the |
| 18 | +// .wasm is written next to the source. |
| 19 | + |
| 20 | +const AFFINESCRIPT_BIN = Deno.env.get("AFFINESCRIPT_BIN") ?? "affinescript"; |
| 21 | + |
| 22 | +function dirOf(path) { |
| 23 | + const i = path.lastIndexOf("/"); |
| 24 | + return i > 0 ? path.slice(0, i) : "."; |
| 25 | +} |
| 26 | + |
| 27 | +// `cwd` is set to the source's directory because affinescript's Module_loader |
| 28 | +// resolves `use Foo.Bar` against the current working directory, not the source |
| 29 | +// file's location. |
| 30 | +async function runAffinescript(args, cwd) { |
| 31 | + try { |
| 32 | + const cmd = new Deno.Command(AFFINESCRIPT_BIN, { |
| 33 | + args, |
| 34 | + cwd, |
| 35 | + stdout: "piped", |
| 36 | + stderr: "piped", |
| 37 | + }); |
| 38 | + const out = await cmd.output(); |
| 39 | + const stdout = new TextDecoder().decode(out.stdout); |
| 40 | + const stderr = new TextDecoder().decode(out.stderr); |
| 41 | + return { code: out.code, stdout, stderr }; |
| 42 | + } catch (e) { |
| 43 | + return { code: -1, stdout: "", stderr: `failed to spawn ${AFFINESCRIPT_BIN}: ${e.message}` }; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function requireString(args, key) { |
| 48 | + const v = args?.[key]; |
| 49 | + if (typeof v !== "string" || v.length === 0) { |
| 50 | + return { error: `${key} (string) is required` }; |
| 51 | + } |
| 52 | + return { value: v }; |
| 53 | +} |
| 54 | + |
| 55 | +function deriveWasmPath(srcPath) { |
| 56 | + return srcPath.endsWith(".affine") |
| 57 | + ? srcPath.slice(0, -".affine".length) + ".wasm" |
| 58 | + : srcPath + ".wasm"; |
| 59 | +} |
5 | 60 |
|
6 | 61 | export async function handleTool(toolName, args) { |
7 | 62 | switch (toolName) { |
8 | 63 | case "typed_wasm_validate_module": { |
9 | | - const { module_path } = args ?? {}; |
10 | | - if (!module_path) return { status: 400, data: { error: "module_path is required" } }; |
11 | | - const cmd = new Deno.Command("typed-wasm", { args: ["validate-module", String(module_path)], stdout: "piped", stderr: "piped" }); |
12 | | - const out = await cmd.output(); |
13 | | - if (!out.success) return { status: 500, data: { success: false, error: new TextDecoder().decode(out.stderr) } }; |
14 | | - const stdout = new TextDecoder().decode(out.stdout); |
15 | | - try { return { status: 200, data: JSON.parse(stdout) }; } catch { return { status: 200, data: { success: true, output: stdout } }; } |
| 64 | + // Compiles + runs the intra-module Level 7/10 verifier (Tw_verify). |
| 65 | + // "Validation" = the emitted Wasm satisfies the typed-wasm ownership |
| 66 | + // contract declared in the source. |
| 67 | + const r = requireString(args, "module_path"); |
| 68 | + if (r.error) return { status: 400, data: { error: r.error } }; |
| 69 | + const out = await runAffinescript(["verify", r.value], dirOf(r.value)); |
| 70 | + const valid = out.code === 0; |
| 71 | + return { |
| 72 | + status: valid ? 200 : 200, |
| 73 | + data: { |
| 74 | + valid, |
| 75 | + report: out.stdout.trim(), |
| 76 | + diagnostics: out.stderr.trim() || undefined, |
| 77 | + }, |
| 78 | + }; |
16 | 79 | } |
17 | 80 |
|
18 | 81 | case "typed_wasm_check_types": { |
19 | | - const { module_path } = args ?? {}; |
20 | | - if (!module_path) return { status: 400, data: { error: "module_path is required" } }; |
21 | | - const cmd = new Deno.Command("typed-wasm", { args: ["check-types", String(module_path)], stdout: "piped", stderr: "piped" }); |
22 | | - const out = await cmd.output(); |
23 | | - if (!out.success) return { status: 500, data: { success: false, error: new TextDecoder().decode(out.stderr) } }; |
24 | | - const stdout = new TextDecoder().decode(out.stdout); |
25 | | - try { return { status: 200, data: JSON.parse(stdout) }; } catch { return { status: 200, data: { success: true, output: stdout } }; } |
| 82 | + // Runs only the type checker (no Wasm emission). |
| 83 | + const r = requireString(args, "module_path"); |
| 84 | + if (r.error) return { status: 400, data: { error: r.error } }; |
| 85 | + const out = await runAffinescript(["check", r.value], dirOf(r.value)); |
| 86 | + const ok = out.code === 0; |
| 87 | + return { |
| 88 | + status: 200, |
| 89 | + data: { |
| 90 | + ok, |
| 91 | + report: out.stdout.trim(), |
| 92 | + errors: ok ? [] : (out.stderr.trim() ? [out.stderr.trim()] : []), |
| 93 | + }, |
| 94 | + }; |
26 | 95 | } |
27 | 96 |
|
28 | 97 | case "typed_wasm_compile_module": { |
29 | | - const { module_path, target } = args ?? {}; |
30 | | - if (!module_path) return { status: 400, data: { error: "module_path is required" } }; |
31 | | - const cmd = new Deno.Command("typed-wasm", { args: ["compile-module", String(module_path)], stdout: "piped", stderr: "piped" }); |
32 | | - const out = await cmd.output(); |
33 | | - if (!out.success) return { status: 500, data: { success: false, error: new TextDecoder().decode(out.stderr) } }; |
34 | | - const stdout = new TextDecoder().decode(out.stdout); |
35 | | - try { return { status: 200, data: JSON.parse(stdout) }; } catch { return { status: 200, data: { success: true, output: stdout } }; } |
| 98 | + // Compiles .affine → .wasm. The emitted module carries the |
| 99 | + // [affinescript.ownership] custom section consumed by typed-wasm |
| 100 | + // intra- and cross-module verifiers. |
| 101 | + const r = requireString(args, "module_path"); |
| 102 | + if (r.error) return { status: 400, data: { error: r.error } }; |
| 103 | + const outputPath = (typeof args?.output_path === "string" && args.output_path) |
| 104 | + ? args.output_path |
| 105 | + : deriveWasmPath(r.value); |
| 106 | + const out = await runAffinescript(["compile", r.value, "-o", outputPath], dirOf(r.value)); |
| 107 | + const success = out.code === 0; |
| 108 | + return { |
| 109 | + status: success ? 200 : 200, |
| 110 | + data: { |
| 111 | + success, |
| 112 | + output_path: success ? outputPath : undefined, |
| 113 | + report: out.stdout.trim(), |
| 114 | + diagnostics: out.stderr.trim() || undefined, |
| 115 | + }, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + case "typed_wasm_verify_boundary": { |
| 120 | + // Cross-module Level 10 boundary check. Compiles both files, then |
| 121 | + // verifies that the caller's local funcs respect the ownership |
| 122 | + // contract declared by the callee's Wasm exports. |
| 123 | + const callee = requireString(args, "callee_path"); |
| 124 | + if (callee.error) return { status: 400, data: { error: callee.error } }; |
| 125 | + const caller = requireString(args, "caller_path"); |
| 126 | + if (caller.error) return { status: 400, data: { error: caller.error } }; |
| 127 | + // Verify-boundary needs to find both files' imports — run from the |
| 128 | + // caller's directory (most likely the place where Callee lives too). |
| 129 | + const out = await runAffinescript( |
| 130 | + ["verify-boundary", callee.value, caller.value], |
| 131 | + dirOf(caller.value), |
| 132 | + ); |
| 133 | + const clean = out.code === 0; |
| 134 | + return { |
| 135 | + status: 200, |
| 136 | + data: { |
| 137 | + clean, |
| 138 | + report: out.stdout.trim(), |
| 139 | + diagnostics: out.stderr.trim() || undefined, |
| 140 | + }, |
| 141 | + }; |
36 | 142 | } |
| 143 | + |
37 | 144 | default: |
38 | 145 | return { status: 404, data: { error: `Unknown tool: ${toolName}` } }; |
39 | 146 | } |
|
0 commit comments