apex-psi-mcp 1.1.1 → 1.2.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 +45 -0
- package/dist/gate.d.ts +101 -0
- package/dist/gate.js +214 -0
- package/dist/index.js +58 -14
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
<p align="center"><img src="https://raw.githubusercontent.com/kawal393/apex-psi-mcp-server/main/assets/apex-psi-icon.png" alt="APEX PSI mark: a check inside a hexagonal seal" width="96" height="96"></p>
|
|
2
|
+
|
|
1
3
|
# APEX PSI MCP server
|
|
2
4
|
|
|
5
|
+
[](https://mcprush.com/to/apex-psi-mcp?ref=badge)
|
|
6
|
+
|
|
3
7
|
> **Apex PSI is not a tool and not a product. It is the verification substrate — the layer beneath proof. It does not judge. It does not certify. It only remembers.** This server is one application built on that layer.
|
|
4
8
|
|
|
9
|
+
Machine-readable setup for agents and directory scrapers: [llms-install.md](https://github.com/kawal393/apex-psi-mcp-server/blob/main/llms-install.md).
|
|
10
|
+
|
|
5
11
|
One config line gives any MCP-capable agent (Claude, Cursor, Qoder, Cline, Windsurf, and the rest) five evidence tools: `seal`, `verify`, `anchor`, `cite`, `verify_chain`.
|
|
6
12
|
|
|
7
13
|
A seal records the SHA-256 hash of a decision, document or artefact in a public append-only ledger and returns a receipt. Anyone can look that hash up again — free, forever, with no account, no key and no cooperation from us.
|
|
@@ -54,6 +60,45 @@ running the verifier locally costs nothing at all —
|
|
|
54
60
|
[`apex-psi-verifier`](https://www.npmjs.com/package/apex-psi-verifier) on npm,
|
|
55
61
|
[`psi-verifier`](https://pypi.org/project/psi-verifier/) on PyPI.
|
|
56
62
|
|
|
63
|
+
## Make the seal unavoidable: the execution gate
|
|
64
|
+
|
|
65
|
+
The five tools above are available to an agent, but nothing in MCP makes an agent call them. An
|
|
66
|
+
agent that never calls `seal` leaves no gap in the ledger — the ledger stays complete and says
|
|
67
|
+
nothing about that action. If your agent can move money, send a request or write to a system, that
|
|
68
|
+
gap is the whole problem.
|
|
69
|
+
|
|
70
|
+
So this package also ships a **gate**: a small module that seals first and acts second, inside the
|
|
71
|
+
process that performs the side effect.
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
import { createGate, createNotarySealer } from "apex-psi-mcp/gate";
|
|
75
|
+
|
|
76
|
+
const gate = createGate({ sealer: createNotarySealer(), label: "payments-bot" });
|
|
77
|
+
|
|
78
|
+
const { value, receipt } = await gate.run(
|
|
79
|
+
() => payments.transfer({ amount: 500, to: "acct-42" }),
|
|
80
|
+
{ tool: "transfer", amount: 500, to: "acct-42" },
|
|
81
|
+
);
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
If the seal fails, `payments.transfer` is never called — the gate throws `GateClosedError`. A gate
|
|
85
|
+
with no sealer configured refuses every action, because a gate nobody configured must not look like
|
|
86
|
+
a gate that is working.
|
|
87
|
+
|
|
88
|
+
What the gate does and does not do, stated plainly:
|
|
89
|
+
|
|
90
|
+
- It enforces the **order**: the receipt exists before the action runs.
|
|
91
|
+
- It cannot reach a process that never routes its side effect through it.
|
|
92
|
+
- It cannot attest what a model did inside a GPU; the receipt covers the action as you declared it
|
|
93
|
+
and the order in which the two happened.
|
|
94
|
+
- `createLocalSealer()` seals into an in-process hash chain with no network and no account — a local
|
|
95
|
+
permit, not a ledger receipt. `createNotarySealer()` returns a ledger receipt.
|
|
96
|
+
|
|
97
|
+
The same gate exists in the Python runtime as `apex_psi.ExecutionGate`. The two implementations
|
|
98
|
+
fingerprint the same bytes: `fingerprint({"b":1,"a":[1,2],"c":"x"})` is
|
|
99
|
+
`95c12ab4ca0e8f3c485ea1b1b1a2b24976d07180baf5ecad0d81b5bbaf07532b` in both, and a CI check on each
|
|
100
|
+
side pins that number.
|
|
101
|
+
|
|
57
102
|
## Configuration
|
|
58
103
|
|
|
59
104
|
Environment variables (both optional):
|
package/dist/gate.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* APEX PSI execution gate — seal before you act.
|
|
3
|
+
*
|
|
4
|
+
* Why this exists: the five tools in this server are available to an agent, but
|
|
5
|
+
* nothing in the protocol makes an agent call them. An agent that never calls
|
|
6
|
+
* `seal` leaves no gap in the ledger — the ledger is complete and says nothing.
|
|
7
|
+
* That is the "voluntary confession" problem: the record is only as complete as
|
|
8
|
+
* the caller's discipline.
|
|
9
|
+
*
|
|
10
|
+
* The gate is the mechanical answer, and it lives in one place only: inside the
|
|
11
|
+
* process that performs the side effect. `gate.run()` seals first and calls the
|
|
12
|
+
* action second, so the action cannot happen without a receipt that already
|
|
13
|
+
* exists. If the seal fails, the action is never called.
|
|
14
|
+
*
|
|
15
|
+
* What it does NOT do, stated plainly:
|
|
16
|
+
* - it cannot reach a process that never routes its side effect through it;
|
|
17
|
+
* - it cannot attest what a model did inside a GPU;
|
|
18
|
+
* - the receipt covers the action as the caller declared it, plus the order in
|
|
19
|
+
* which the receipt and the action happened.
|
|
20
|
+
*
|
|
21
|
+
* Two sealers:
|
|
22
|
+
* createLocalSealer() — in-process SHA-256 chain, no network, no account.
|
|
23
|
+
* createNotarySealer() — the hosted APEX PSI notary; returns a ledger receipt.
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* import { createGate, createNotarySealer } from "apex-psi-mcp/gate";
|
|
27
|
+
*
|
|
28
|
+
* const gate = createGate({ sealer: createNotarySealer(), label: "payments-bot" });
|
|
29
|
+
* const { value, receipt } = await gate.run(
|
|
30
|
+
* () => payments.transfer({ amount: 500, to: "acct-42" }),
|
|
31
|
+
* { tool: "transfer", amount: 500, to: "acct-42" },
|
|
32
|
+
* );
|
|
33
|
+
*/
|
|
34
|
+
export declare const GENESIS: string;
|
|
35
|
+
export interface GateReceipt {
|
|
36
|
+
receipt_id?: string;
|
|
37
|
+
decision_hash: string;
|
|
38
|
+
algorithm?: string;
|
|
39
|
+
scope?: "local" | "ledger";
|
|
40
|
+
entry?: Record<string, unknown>;
|
|
41
|
+
[key: string]: unknown;
|
|
42
|
+
}
|
|
43
|
+
export type Sealer = (action: unknown) => Promise<GateReceipt>;
|
|
44
|
+
export declare class GateClosedError extends Error {
|
|
45
|
+
readonly reason: string;
|
|
46
|
+
readonly action?: unknown;
|
|
47
|
+
constructor(reason: string, action?: unknown);
|
|
48
|
+
}
|
|
49
|
+
export interface GateResult<T> {
|
|
50
|
+
value: T;
|
|
51
|
+
receipt: GateReceipt;
|
|
52
|
+
action: unknown;
|
|
53
|
+
}
|
|
54
|
+
export interface Gate {
|
|
55
|
+
/** Seal an action and return the receipt. Throws GateClosedError if it cannot. */
|
|
56
|
+
permit(action: unknown): Promise<GateReceipt>;
|
|
57
|
+
/** Seal first, then call `fn`. `fn` is never called if the seal fails. */
|
|
58
|
+
run<T>(fn: () => T | Promise<T>, action: unknown): Promise<GateResult<T>>;
|
|
59
|
+
/** Throw unless the receipt carries a hash. Use before a payout. */
|
|
60
|
+
requireReceipt(receipt: unknown): GateReceipt;
|
|
61
|
+
/** The value to put on an outbound request so the other side can look the receipt up. */
|
|
62
|
+
permitHeader(receipt: unknown): string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Stable JSON: sorted keys, no whitespace. Matches Python's
|
|
66
|
+
* json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False) for
|
|
67
|
+
* every value a receipt is built from, so a fingerprint computed here and one
|
|
68
|
+
* computed by the Python runtime agree byte for byte. One divergence remains:
|
|
69
|
+
* a whole number written as a float (1.0) prints "1" here and "1.0" there.
|
|
70
|
+
*/
|
|
71
|
+
export declare function canonicalize(value: unknown): string;
|
|
72
|
+
/** The SHA-256 of the canonical form, hex. */
|
|
73
|
+
export declare function fingerprint(value: unknown): string;
|
|
74
|
+
/**
|
|
75
|
+
* Seals into an in-process hash chain. No network, no account, no clock anyone
|
|
76
|
+
* else can check. A local permit: it proves order and arguments, and it is not
|
|
77
|
+
* a ledger receipt.
|
|
78
|
+
*/
|
|
79
|
+
export declare function createLocalSealer(options?: {
|
|
80
|
+
label?: string;
|
|
81
|
+
}): Sealer;
|
|
82
|
+
/**
|
|
83
|
+
* Seals the fingerprint of the action in the hosted APEX PSI ledger. Only the
|
|
84
|
+
* SHA-256 fingerprint is sent, so the notary never sees the contents.
|
|
85
|
+
*/
|
|
86
|
+
export declare function createNotarySealer(options?: {
|
|
87
|
+
endpoint?: string;
|
|
88
|
+
apiKey?: string;
|
|
89
|
+
label?: string;
|
|
90
|
+
modelId?: string;
|
|
91
|
+
}): Sealer;
|
|
92
|
+
/**
|
|
93
|
+
* The gate. Without a sealer it is closed: every action is refused. A gate
|
|
94
|
+
* nobody configured must not look like a gate that is working.
|
|
95
|
+
*/
|
|
96
|
+
export declare function createGate(options?: {
|
|
97
|
+
sealer?: Sealer;
|
|
98
|
+
label?: string;
|
|
99
|
+
}): Gate;
|
|
100
|
+
/** Module-level form of `gate.requireReceipt`. */
|
|
101
|
+
export declare function requireReceipt(receipt: unknown): GateReceipt;
|
package/dist/gate.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* APEX PSI execution gate — seal before you act.
|
|
3
|
+
*
|
|
4
|
+
* Why this exists: the five tools in this server are available to an agent, but
|
|
5
|
+
* nothing in the protocol makes an agent call them. An agent that never calls
|
|
6
|
+
* `seal` leaves no gap in the ledger — the ledger is complete and says nothing.
|
|
7
|
+
* That is the "voluntary confession" problem: the record is only as complete as
|
|
8
|
+
* the caller's discipline.
|
|
9
|
+
*
|
|
10
|
+
* The gate is the mechanical answer, and it lives in one place only: inside the
|
|
11
|
+
* process that performs the side effect. `gate.run()` seals first and calls the
|
|
12
|
+
* action second, so the action cannot happen without a receipt that already
|
|
13
|
+
* exists. If the seal fails, the action is never called.
|
|
14
|
+
*
|
|
15
|
+
* What it does NOT do, stated plainly:
|
|
16
|
+
* - it cannot reach a process that never routes its side effect through it;
|
|
17
|
+
* - it cannot attest what a model did inside a GPU;
|
|
18
|
+
* - the receipt covers the action as the caller declared it, plus the order in
|
|
19
|
+
* which the receipt and the action happened.
|
|
20
|
+
*
|
|
21
|
+
* Two sealers:
|
|
22
|
+
* createLocalSealer() — in-process SHA-256 chain, no network, no account.
|
|
23
|
+
* createNotarySealer() — the hosted APEX PSI notary; returns a ledger receipt.
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* import { createGate, createNotarySealer } from "apex-psi-mcp/gate";
|
|
27
|
+
*
|
|
28
|
+
* const gate = createGate({ sealer: createNotarySealer(), label: "payments-bot" });
|
|
29
|
+
* const { value, receipt } = await gate.run(
|
|
30
|
+
* () => payments.transfer({ amount: 500, to: "acct-42" }),
|
|
31
|
+
* { tool: "transfer", amount: 500, to: "acct-42" },
|
|
32
|
+
* );
|
|
33
|
+
*/
|
|
34
|
+
import { createHash } from "node:crypto";
|
|
35
|
+
const APEX_API_BASE = (process.env.APEX_API_BASE || "https://qhtntebpcribjiwrdtdd.supabase.co/functions/v1").replace(/\/$/, "");
|
|
36
|
+
const APEX_API_KEY = process.env.APEX_API_KEY || "";
|
|
37
|
+
export const GENESIS = "0".repeat(64);
|
|
38
|
+
export class GateClosedError extends Error {
|
|
39
|
+
reason;
|
|
40
|
+
action;
|
|
41
|
+
constructor(reason, action) {
|
|
42
|
+
super(reason);
|
|
43
|
+
this.name = "GateClosedError";
|
|
44
|
+
this.reason = reason;
|
|
45
|
+
this.action = action;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Stable JSON: sorted keys, no whitespace. Matches Python's
|
|
50
|
+
* json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False) for
|
|
51
|
+
* every value a receipt is built from, so a fingerprint computed here and one
|
|
52
|
+
* computed by the Python runtime agree byte for byte. One divergence remains:
|
|
53
|
+
* a whole number written as a float (1.0) prints "1" here and "1.0" there.
|
|
54
|
+
*/
|
|
55
|
+
export function canonicalize(value) {
|
|
56
|
+
if (value === null)
|
|
57
|
+
return "null";
|
|
58
|
+
if (value === undefined)
|
|
59
|
+
return "null";
|
|
60
|
+
if (typeof value === "number")
|
|
61
|
+
return Number.isFinite(value) ? JSON.stringify(value) : "null";
|
|
62
|
+
if (typeof value === "boolean")
|
|
63
|
+
return value ? "true" : "false";
|
|
64
|
+
if (typeof value === "string")
|
|
65
|
+
return JSON.stringify(value);
|
|
66
|
+
if (Array.isArray(value))
|
|
67
|
+
return `[${value.map(canonicalize).join(",")}]`;
|
|
68
|
+
if (typeof value === "object") {
|
|
69
|
+
const obj = value;
|
|
70
|
+
const keys = Object.keys(obj)
|
|
71
|
+
.filter((k) => obj[k] !== undefined)
|
|
72
|
+
.sort();
|
|
73
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalize(obj[k])}`).join(",")}}`;
|
|
74
|
+
}
|
|
75
|
+
return JSON.stringify(String(value));
|
|
76
|
+
}
|
|
77
|
+
/** The SHA-256 of the canonical form, hex. */
|
|
78
|
+
export function fingerprint(value) {
|
|
79
|
+
return createHash("sha256").update(canonicalize(value), "utf8").digest("hex");
|
|
80
|
+
}
|
|
81
|
+
function entryHash(entry) {
|
|
82
|
+
const canonical = canonicalize({
|
|
83
|
+
seq: entry.seq,
|
|
84
|
+
ts: entry.ts,
|
|
85
|
+
kind: entry.kind,
|
|
86
|
+
payload_sha256: entry.payload_sha256,
|
|
87
|
+
prev: entry.prev,
|
|
88
|
+
});
|
|
89
|
+
return createHash("sha256").update(canonical, "utf8").digest("hex");
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Seals into an in-process hash chain. No network, no account, no clock anyone
|
|
93
|
+
* else can check. A local permit: it proves order and arguments, and it is not
|
|
94
|
+
* a ledger receipt.
|
|
95
|
+
*/
|
|
96
|
+
export function createLocalSealer(options = {}) {
|
|
97
|
+
const entries = [];
|
|
98
|
+
return async (action) => {
|
|
99
|
+
const seq = entries.length;
|
|
100
|
+
const ts = new Date().toISOString().replace(/\.\d{3}Z$/, ".000Z");
|
|
101
|
+
const prev = entries.length ? String(entries[entries.length - 1].entry_hash) : GENESIS;
|
|
102
|
+
const payload = fingerprint(action);
|
|
103
|
+
const entry = {
|
|
104
|
+
seq,
|
|
105
|
+
ts,
|
|
106
|
+
kind: "gate",
|
|
107
|
+
payload_sha256: payload,
|
|
108
|
+
prev,
|
|
109
|
+
};
|
|
110
|
+
entry.entry_hash = entryHash(entry);
|
|
111
|
+
entries.push(entry);
|
|
112
|
+
return {
|
|
113
|
+
receipt_id: `LOCAL-${String(entry.entry_hash).slice(0, 16)}`,
|
|
114
|
+
decision_hash: `sha256:${payload}`,
|
|
115
|
+
algorithm: "sha256-local-chain",
|
|
116
|
+
scope: "local",
|
|
117
|
+
entry,
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Seals the fingerprint of the action in the hosted APEX PSI ledger. Only the
|
|
123
|
+
* SHA-256 fingerprint is sent, so the notary never sees the contents.
|
|
124
|
+
*/
|
|
125
|
+
export function createNotarySealer(options = {}) {
|
|
126
|
+
const endpoint = options.endpoint || APEX_API_BASE;
|
|
127
|
+
const apiKey = options.apiKey ?? APEX_API_KEY;
|
|
128
|
+
const label = options.label || "gate";
|
|
129
|
+
const modelId = options.modelId || "apex-psi-mcp/gate";
|
|
130
|
+
return async (action) => {
|
|
131
|
+
const digest = fingerprint(action);
|
|
132
|
+
const headers = { "Content-Type": "application/json" };
|
|
133
|
+
if (apiKey)
|
|
134
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
135
|
+
const response = await fetch(`${endpoint}/notarize`, {
|
|
136
|
+
method: "POST",
|
|
137
|
+
headers,
|
|
138
|
+
body: JSON.stringify({
|
|
139
|
+
decision: `sha256:${digest}`,
|
|
140
|
+
context: label,
|
|
141
|
+
predicate: "sealed-before-execution",
|
|
142
|
+
model_id: modelId,
|
|
143
|
+
}),
|
|
144
|
+
});
|
|
145
|
+
if (!response.ok) {
|
|
146
|
+
let detail = "";
|
|
147
|
+
try {
|
|
148
|
+
detail = (await response.text()).slice(0, 300);
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
/* ignore */
|
|
152
|
+
}
|
|
153
|
+
throw new Error(`notary refused the seal: ${response.status} ${response.statusText}${detail ? ` — ${detail}` : ""}`);
|
|
154
|
+
}
|
|
155
|
+
const receipt = (await response.json());
|
|
156
|
+
if (!receipt.decision_hash)
|
|
157
|
+
receipt.decision_hash = `sha256:${digest}`;
|
|
158
|
+
receipt.scope = "ledger";
|
|
159
|
+
return receipt;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The gate. Without a sealer it is closed: every action is refused. A gate
|
|
164
|
+
* nobody configured must not look like a gate that is working.
|
|
165
|
+
*/
|
|
166
|
+
export function createGate(options = {}) {
|
|
167
|
+
const sealer = options.sealer;
|
|
168
|
+
const label = options.label;
|
|
169
|
+
const permit = async (action) => {
|
|
170
|
+
if (!sealer) {
|
|
171
|
+
throw new GateClosedError("no sealer is configured, so no receipt can exist, so the action is refused", action);
|
|
172
|
+
}
|
|
173
|
+
let receipt;
|
|
174
|
+
try {
|
|
175
|
+
receipt = await sealer(action);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
179
|
+
throw new GateClosedError(`sealing failed (${why}), so the action is refused`, action);
|
|
180
|
+
}
|
|
181
|
+
if (!receipt || typeof receipt !== "object" || !receipt.decision_hash) {
|
|
182
|
+
throw new GateClosedError("the sealer returned no receipt carrying a hash, so the action is refused", action);
|
|
183
|
+
}
|
|
184
|
+
return receipt;
|
|
185
|
+
};
|
|
186
|
+
const requireReceipt = (receipt) => {
|
|
187
|
+
if (!receipt || typeof receipt !== "object" || !receipt.decision_hash) {
|
|
188
|
+
throw new GateClosedError("no receipt with a hash was supplied, so the action is refused");
|
|
189
|
+
}
|
|
190
|
+
return receipt;
|
|
191
|
+
};
|
|
192
|
+
return {
|
|
193
|
+
permit,
|
|
194
|
+
requireReceipt,
|
|
195
|
+
async run(fn, action) {
|
|
196
|
+
const described = action && typeof action === "object" && !Array.isArray(action)
|
|
197
|
+
? { label, ...action }
|
|
198
|
+
: { label, action };
|
|
199
|
+
const receipt = await permit(described);
|
|
200
|
+
const value = await fn();
|
|
201
|
+
return { value, receipt, action: described };
|
|
202
|
+
},
|
|
203
|
+
permitHeader(receipt) {
|
|
204
|
+
return requireReceipt(receipt).decision_hash;
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/** Module-level form of `gate.requireReceipt`. */
|
|
209
|
+
export function requireReceipt(receipt) {
|
|
210
|
+
if (!receipt || typeof receipt !== "object" || !receipt.decision_hash) {
|
|
211
|
+
throw new GateClosedError("no receipt with a hash was supplied, so the action is refused");
|
|
212
|
+
}
|
|
213
|
+
return receipt;
|
|
214
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -19,7 +19,10 @@ import { z } from "zod";
|
|
|
19
19
|
// Configuration
|
|
20
20
|
const APEX_API_BASE = (process.env.APEX_API_BASE || "https://qhtntebpcribjiwrdtdd.supabase.co/functions/v1").replace(/\/$/, "");
|
|
21
21
|
const APEX_API_KEY = process.env.APEX_API_KEY || "";
|
|
22
|
-
|
|
22
|
+
// Human-facing site. The API host is a Supabase function and is not a page a
|
|
23
|
+
// person can open, so every link handed back to a user points here instead.
|
|
24
|
+
const APEX_SITE_BASE = (process.env.APEX_SITE_BASE || "https://www.ai-governance-standard.com").replace(/\/$/, "");
|
|
25
|
+
const VERSION = "1.1.2";
|
|
23
26
|
// Helper: make an API call to the Apex PSI backend
|
|
24
27
|
async function apexApiCall(endpoint, method = "GET", body) {
|
|
25
28
|
const url = `${APEX_API_BASE}${endpoint}`;
|
|
@@ -47,6 +50,36 @@ async function apexApiCall(endpoint, method = "GET", body) {
|
|
|
47
50
|
function stripHashPrefix(value) {
|
|
48
51
|
return typeof value === "string" ? value.replace(/^sha256:/i, "") : "";
|
|
49
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Accept either a bare receipt or the whole envelope `seal` returns
|
|
55
|
+
* (`{ success, receipt, verify_hash, ... }`). The readers below once assumed a
|
|
56
|
+
* flat receipt, which meant a genuine seal piped straight back into verify,
|
|
57
|
+
* cite or verify_chain reported "chain broken" with an empty hash.
|
|
58
|
+
*/
|
|
59
|
+
function asReceipt(value) {
|
|
60
|
+
if (!value || typeof value !== "object")
|
|
61
|
+
return {};
|
|
62
|
+
const obj = value;
|
|
63
|
+
const nested = obj.receipt;
|
|
64
|
+
if (obj.decision_hash === undefined && nested && typeof nested === "object" && !Array.isArray(nested)) {
|
|
65
|
+
return nested;
|
|
66
|
+
}
|
|
67
|
+
return obj;
|
|
68
|
+
}
|
|
69
|
+
// Helper: the hash a receipt stands for, from any of the fields the ledger uses
|
|
70
|
+
function receiptHash(value) {
|
|
71
|
+
const r = asReceipt(value);
|
|
72
|
+
const raw = r.decision_hash ?? r.merkle_leaf ?? r.merkle_root ?? r.verify_hash ?? r.hash;
|
|
73
|
+
const hash = stripHashPrefix(raw);
|
|
74
|
+
if (!hash) {
|
|
75
|
+
throw new Error("This receipt carries no hash (looked for decision_hash, merkle_leaf, merkle_root) — pass the object exactly as the seal tool returned it");
|
|
76
|
+
}
|
|
77
|
+
return hash;
|
|
78
|
+
}
|
|
79
|
+
// Helper: a link a person can open in a browser
|
|
80
|
+
function verifyPageUrl(hash) {
|
|
81
|
+
return `${APEX_SITE_BASE}/verify?hash=${encodeURIComponent(hash)}`;
|
|
82
|
+
}
|
|
50
83
|
// Helper: wrap a result (or error) into an MCP text response
|
|
51
84
|
function mcpResult(result) {
|
|
52
85
|
return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] };
|
|
@@ -93,7 +126,8 @@ server.registerTool("seal", {
|
|
|
93
126
|
receipt,
|
|
94
127
|
receipt_id: receipt.receipt_id,
|
|
95
128
|
verify_hash: decisionHash,
|
|
96
|
-
verify_url:
|
|
129
|
+
verify_url: verifyPageUrl(decisionHash),
|
|
130
|
+
verify_api_url: `${APEX_API_BASE}/verify-hash?hash=${decisionHash}`,
|
|
97
131
|
message: "Content sealed in the APEX PSI append-only ledger",
|
|
98
132
|
});
|
|
99
133
|
}
|
|
@@ -114,17 +148,19 @@ server.registerTool("verify", {
|
|
|
114
148
|
},
|
|
115
149
|
}, async ({ hash, receipt }) => {
|
|
116
150
|
try {
|
|
117
|
-
let lookup = hash
|
|
118
|
-
if (!lookup && receipt) {
|
|
119
|
-
lookup =
|
|
151
|
+
let lookup = stripHashPrefix(hash);
|
|
152
|
+
if (!lookup && receipt !== undefined) {
|
|
153
|
+
lookup = receiptHash(receipt);
|
|
120
154
|
}
|
|
121
155
|
if (!lookup) {
|
|
122
156
|
throw new Error("Provide either a hash or a receipt object");
|
|
123
157
|
}
|
|
124
|
-
const result = await apexApiCall(`/verify-hash?hash=${encodeURIComponent(
|
|
158
|
+
const result = await apexApiCall(`/verify-hash?hash=${encodeURIComponent(lookup)}`);
|
|
125
159
|
return mcpResult({
|
|
126
160
|
success: true,
|
|
161
|
+
hash: lookup,
|
|
127
162
|
verification: result,
|
|
163
|
+
verify_url: verifyPageUrl(lookup),
|
|
128
164
|
message: result.verified
|
|
129
165
|
? "Seal verified — hash is present in the APEX PSI append-only ledger"
|
|
130
166
|
: "Hash not found in the ledger",
|
|
@@ -184,13 +220,18 @@ server.registerTool("cite", {
|
|
|
184
220
|
}, async ({ receipt, format }) => {
|
|
185
221
|
try {
|
|
186
222
|
const fmt = format || "apa";
|
|
187
|
-
const
|
|
188
|
-
const
|
|
223
|
+
const r = asReceipt(receipt);
|
|
224
|
+
const receiptId = String(r.receipt_id || "APEX-PSI-RECEIPT");
|
|
225
|
+
const timestamp = String(r.timestamp || new Date().toISOString());
|
|
189
226
|
const year = timestamp.slice(0, 4);
|
|
190
227
|
const date = timestamp.slice(0, 10);
|
|
191
|
-
const decisionHash =
|
|
192
|
-
const verifyUrl =
|
|
193
|
-
|
|
228
|
+
const decisionHash = receiptHash(receipt);
|
|
229
|
+
const verifyUrl = verifyPageUrl(decisionHash);
|
|
230
|
+
// Only name algorithms the receipt itself names. The ledger attaches
|
|
231
|
+
// LMS-W4-SHA256 only when a slot is available, so a hard-coded default
|
|
232
|
+
// would cite signatures this particular receipt does not carry.
|
|
233
|
+
const stated = typeof r.algorithm === "string" ? r.algorithm.trim() : "";
|
|
234
|
+
const algorithm = stated || "algorithms not stated in this receipt";
|
|
194
235
|
let citation;
|
|
195
236
|
if (fmt === "bibtex") {
|
|
196
237
|
citation = [
|
|
@@ -235,9 +276,12 @@ server.registerTool("verify_chain", {
|
|
|
235
276
|
let previousRoot = "";
|
|
236
277
|
let chainIntact = true;
|
|
237
278
|
for (let i = 0; i < receipts.length; i++) {
|
|
238
|
-
const r = receipts[i];
|
|
239
|
-
|
|
240
|
-
|
|
279
|
+
const r = asReceipt(receipts[i]);
|
|
280
|
+
let hash = "";
|
|
281
|
+
try {
|
|
282
|
+
hash = receiptHash(r);
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
241
285
|
results.push({ index: i, receipt_id: String(r.receipt_id || "?"), verified: false, found: false, hash: "" });
|
|
242
286
|
chainIntact = false;
|
|
243
287
|
continue;
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apex-psi-mcp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"mcpName": "io.github.kawal393/apex-psi-mcp",
|
|
5
5
|
"description": "APEX PSI MCP server - append-only evidence receipts for AI decisions and documents: seal a record, verify a hash against the public ledger, check a chain, cite a receipt, read Bitcoin anchoring status. Verification is free and needs no account.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.js",
|
|
10
|
+
"./gate": "./dist/gate.js"
|
|
11
|
+
},
|
|
8
12
|
"files": [
|
|
9
13
|
"dist",
|
|
10
14
|
"README.md"
|
|
@@ -15,7 +19,11 @@
|
|
|
15
19
|
"scripts": {
|
|
16
20
|
"build": "tsc",
|
|
17
21
|
"start": "node dist/index.js",
|
|
18
|
-
"dev": "tsx src/index.ts"
|
|
22
|
+
"dev": "tsx src/index.ts",
|
|
23
|
+
"check:roundtrip": "node scripts/roundtrip-check.mjs",
|
|
24
|
+
"check:all": "npm run build && node scripts/roundtrip-check.mjs && node scripts/anchor-tool-check.mjs && node scripts/card-copy-check.mjs && node scripts/registry-card-check.mjs && node scripts/gate-check.mjs",
|
|
25
|
+
"check:registry-card": "node scripts/registry-card-check.mjs",
|
|
26
|
+
"check:gate": "node scripts/gate-check.mjs"
|
|
19
27
|
},
|
|
20
28
|
"keywords": [
|
|
21
29
|
"mcp",
|