mrmainspring 0.1.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.
Files changed (79) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +47 -0
  3. package/dist/audit/service.d.ts +7 -0
  4. package/dist/audit/service.js +98 -0
  5. package/dist/audit/store.d.ts +7 -0
  6. package/dist/audit/store.js +37 -0
  7. package/dist/audit/supabase-store.d.ts +9 -0
  8. package/dist/audit/supabase-store.js +22 -0
  9. package/dist/audit/types.d.ts +31 -0
  10. package/dist/audit/types.js +1 -0
  11. package/dist/casper/anchorClient.d.ts +99 -0
  12. package/dist/casper/anchorClient.js +412 -0
  13. package/dist/config.d.ts +51 -0
  14. package/dist/config.js +215 -0
  15. package/dist/env-file.d.ts +1 -0
  16. package/dist/env-file.js +51 -0
  17. package/dist/grimoire/service.d.ts +13 -0
  18. package/dist/grimoire/service.js +199 -0
  19. package/dist/grimoire/store.d.ts +10 -0
  20. package/dist/grimoire/store.js +64 -0
  21. package/dist/grimoire/supabase-store.d.ts +13 -0
  22. package/dist/grimoire/supabase-store.js +50 -0
  23. package/dist/grimoire/types.d.ts +60 -0
  24. package/dist/grimoire/types.js +1 -0
  25. package/dist/index.d.ts +2 -0
  26. package/dist/index.js +17 -0
  27. package/dist/mcp/auditTools.d.ts +3 -0
  28. package/dist/mcp/auditTools.js +13 -0
  29. package/dist/mcp/grimoireTools.d.ts +3 -0
  30. package/dist/mcp/grimoireTools.js +91 -0
  31. package/dist/mcp/jsonResult.d.ts +2 -0
  32. package/dist/mcp/jsonResult.js +10 -0
  33. package/dist/mcp/memoryTools.d.ts +3 -0
  34. package/dist/mcp/memoryTools.js +73 -0
  35. package/dist/mcp/paymentTools.d.ts +3 -0
  36. package/dist/mcp/paymentTools.js +33 -0
  37. package/dist/memory/canonical.d.ts +4 -0
  38. package/dist/memory/canonical.js +49 -0
  39. package/dist/memory/hash.d.ts +1 -0
  40. package/dist/memory/hash.js +4 -0
  41. package/dist/memory/service.d.ts +37 -0
  42. package/dist/memory/service.js +175 -0
  43. package/dist/memory/store.d.ts +8 -0
  44. package/dist/memory/store.js +49 -0
  45. package/dist/memory/supabase-store.d.ts +10 -0
  46. package/dist/memory/supabase-store.js +30 -0
  47. package/dist/memory/types.d.ts +56 -0
  48. package/dist/memory/types.js +7 -0
  49. package/dist/payments/service.d.ts +26 -0
  50. package/dist/payments/service.js +613 -0
  51. package/dist/payments/store.d.ts +10 -0
  52. package/dist/payments/store.js +64 -0
  53. package/dist/payments/supabase-store.d.ts +13 -0
  54. package/dist/payments/supabase-store.js +51 -0
  55. package/dist/payments/types.d.ts +101 -0
  56. package/dist/payments/types.js +1 -0
  57. package/dist/server.d.ts +5 -0
  58. package/dist/server.js +68 -0
  59. package/dist/storage/json-file-store.d.ts +17 -0
  60. package/dist/storage/json-file-store.js +87 -0
  61. package/dist/storage/store-factory.d.ts +12 -0
  62. package/dist/storage/store-factory.js +26 -0
  63. package/dist/storage/supabase-rest.d.ts +26 -0
  64. package/dist/storage/supabase-rest.js +85 -0
  65. package/dist/x402/client.d.ts +44 -0
  66. package/dist/x402/client.js +95 -0
  67. package/dist/x402/facilitator.d.ts +84 -0
  68. package/dist/x402/facilitator.js +800 -0
  69. package/dist/x402/readiness.d.ts +55 -0
  70. package/dist/x402/readiness.js +433 -0
  71. package/dist/x402/redaction.d.ts +1 -0
  72. package/dist/x402/redaction.js +30 -0
  73. package/dist/x402/resource.d.ts +69 -0
  74. package/dist/x402/resource.js +325 -0
  75. package/dist/x402/settlement.d.ts +176 -0
  76. package/dist/x402/settlement.js +1210 -0
  77. package/dist/x402/signer.d.ts +71 -0
  78. package/dist/x402/signer.js +616 -0
  79. package/package.json +61 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mr Mainspring contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Mr Mainspring
2
+
3
+ Installable MCP backend for local agent demos. It exposes memory, Grimoire
4
+ policy/secret storage, payment intent tools, audit trail tools, Casper anchoring
5
+ boundaries, and x402 settlement-provider wiring.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install -g mrmainspring
11
+ ```
12
+
13
+ Run the stdio MCP server:
14
+
15
+ ```bash
16
+ mainspring
17
+ ```
18
+
19
+ For local development from this repository:
20
+
21
+ ```bash
22
+ npm run build
23
+ npm run mcp:stdio
24
+ ```
25
+
26
+ ## Environment
27
+
28
+ The server loads `.env` from the current directory, backend directory, or repo
29
+ root. Use `SIGIL_ENV_FILE` to point at a specific env file.
30
+
31
+ Important package boundaries:
32
+
33
+ - Keep `.env`, `.sigil/`, local keys, and generated demo data outside the npm
34
+ package.
35
+ - Real Casper submission remains gated by `CASPER_ENABLE_REAL_SUBMISSION=true`.
36
+ - Real x402 settlement remains gated by `X402_ENABLE_REAL_SETTLEMENT=true` and
37
+ a configured `X402_SIGNER_URL`.
38
+ - The signer private key must live outside the repository workspace.
39
+
40
+ ## Library Entry
41
+
42
+ ```ts
43
+ import { createSigilServer } from "mrmainspring";
44
+ ```
45
+
46
+ The CLI entry is also exported as `mrmainspring/mcp`, but importing it starts
47
+ the stdio server; use the package bin for normal MCP client configuration.
@@ -0,0 +1,7 @@
1
+ import type { AuditEvent, AuditEventInput, AuditStore, AuditTailInput, AuditTailResult } from "./types.js";
2
+ export declare class AuditService {
3
+ private readonly store;
4
+ constructor(store: AuditStore);
5
+ record(input: AuditEventInput): Promise<AuditEvent>;
6
+ tail(input?: AuditTailInput): Promise<AuditTailResult>;
7
+ }
@@ -0,0 +1,98 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { toJsonObject } from "../memory/canonical.js";
3
+ const DEFAULT_TAIL_LIMIT = 50;
4
+ const MAX_TAIL_LIMIT = 200;
5
+ const MAX_METADATA_BYTES = 16 * 1024;
6
+ const MAX_METADATA_DEPTH = 6;
7
+ const MAX_METADATA_KEYS = 50;
8
+ const MAX_METADATA_ARRAY_ITEMS = 50;
9
+ const MAX_METADATA_STRING_LENGTH = 2048;
10
+ const TRUNCATED = "[truncated]";
11
+ const REDACTED = "[redacted]";
12
+ export class AuditService {
13
+ store;
14
+ constructor(store) {
15
+ this.store = store;
16
+ }
17
+ async record(input) {
18
+ const event = {
19
+ id: createAuditId(),
20
+ agent_id: input.agent_id ?? null,
21
+ event_type: input.event_type,
22
+ subject_type: input.subject_type,
23
+ subject_id: input.subject_id ?? null,
24
+ severity: input.severity ?? "info",
25
+ metadata: sanitizeMetadata(input.metadata ?? {}),
26
+ created_at: new Date().toISOString()
27
+ };
28
+ await this.store.append(event);
29
+ return event;
30
+ }
31
+ async tail(input = {}) {
32
+ const limit = Math.max(1, Math.min(input.limit ?? DEFAULT_TAIL_LIMIT, MAX_TAIL_LIMIT));
33
+ const events = (await this.store.list())
34
+ .filter((event) => !input.agent_id || event.agent_id === input.agent_id)
35
+ .filter((event) => !input.event_type || event.event_type === input.event_type)
36
+ .sort((left, right) => right.created_at.localeCompare(left.created_at))
37
+ .slice(0, limit);
38
+ return {
39
+ count: events.length,
40
+ events
41
+ };
42
+ }
43
+ }
44
+ function sanitizeMetadata(metadata) {
45
+ const object = toJsonObject(metadata, "metadata");
46
+ const bounded = redactAndBound(object, 0);
47
+ const size = Buffer.byteLength(JSON.stringify(bounded), "utf8");
48
+ if (size > MAX_METADATA_BYTES) {
49
+ return {
50
+ truncated: true,
51
+ original_size_bytes: size
52
+ };
53
+ }
54
+ return bounded;
55
+ }
56
+ function redactAndBound(value, depth) {
57
+ if (depth >= MAX_METADATA_DEPTH) {
58
+ return TRUNCATED;
59
+ }
60
+ if (Array.isArray(value)) {
61
+ const boundedItems = value
62
+ .slice(0, MAX_METADATA_ARRAY_ITEMS)
63
+ .map((item) => redactAndBound(item, depth + 1));
64
+ if (value.length > MAX_METADATA_ARRAY_ITEMS) {
65
+ boundedItems.push(TRUNCATED);
66
+ }
67
+ return boundedItems;
68
+ }
69
+ if (value && typeof value === "object") {
70
+ const redacted = {};
71
+ const entries = Object.entries(value).slice(0, MAX_METADATA_KEYS);
72
+ for (const [key, nestedValue] of entries) {
73
+ if (isSensitiveKey(key)) {
74
+ redacted[key] = REDACTED;
75
+ }
76
+ else {
77
+ redacted[key] = redactAndBound(nestedValue, depth + 1);
78
+ }
79
+ }
80
+ if (Object.keys(value).length > MAX_METADATA_KEYS) {
81
+ redacted.truncated_keys = true;
82
+ }
83
+ return redacted;
84
+ }
85
+ if (typeof value === "string" && value.length > MAX_METADATA_STRING_LENGTH) {
86
+ return `${value.slice(0, MAX_METADATA_STRING_LENGTH)}${TRUNCATED}`;
87
+ }
88
+ return value;
89
+ }
90
+ function isSensitiveKey(key) {
91
+ if (/(_hash|Hash|hash)$/.test(key)) {
92
+ return false;
93
+ }
94
+ return /secret|token|key|password|private|payload|credential|authorization|signature|value/i.test(key);
95
+ }
96
+ function createAuditId() {
97
+ return `aud_${randomUUID().replaceAll("-", "")}`;
98
+ }
@@ -0,0 +1,7 @@
1
+ import type { AuditEvent, AuditStore } from "./types.js";
2
+ export declare class FileAuditStore implements AuditStore {
3
+ private readonly store;
4
+ constructor(dataDir: string);
5
+ append(event: AuditEvent): Promise<void>;
6
+ list(): Promise<AuditEvent[]>;
7
+ }
@@ -0,0 +1,37 @@
1
+ import { join } from "node:path";
2
+ import { JsonFileStore } from "../storage/json-file-store.js";
3
+ export class FileAuditStore {
4
+ store;
5
+ constructor(dataDir) {
6
+ this.store = new JsonFileStore({
7
+ filePath: join(dataDir, "audit.json"),
8
+ empty: emptyStore,
9
+ normalize: normalizeStore
10
+ });
11
+ }
12
+ async append(event) {
13
+ await this.store.update((data) => {
14
+ data.events.push(event);
15
+ });
16
+ }
17
+ async list() {
18
+ const data = await this.store.read();
19
+ return [...data.events];
20
+ }
21
+ }
22
+ function emptyStore() {
23
+ return {
24
+ schema_version: "sigil.audit-store.v1",
25
+ events: []
26
+ };
27
+ }
28
+ function normalizeStore(parsed) {
29
+ const data = asStoreObject(parsed);
30
+ return {
31
+ schema_version: "sigil.audit-store.v1",
32
+ events: Array.isArray(data.events) ? data.events : []
33
+ };
34
+ }
35
+ function asStoreObject(parsed) {
36
+ return parsed && typeof parsed === "object" ? parsed : {};
37
+ }
@@ -0,0 +1,9 @@
1
+ import { SupabaseRestClient } from "../storage/supabase-rest.js";
2
+ import type { AuditEvent, AuditStore } from "./types.js";
3
+ export declare class SupabaseAuditStore implements AuditStore {
4
+ private readonly client;
5
+ private readonly table;
6
+ constructor(client: SupabaseRestClient);
7
+ append(event: AuditEvent): Promise<void>;
8
+ list(): Promise<AuditEvent[]>;
9
+ }
@@ -0,0 +1,22 @@
1
+ export class SupabaseAuditStore {
2
+ client;
3
+ table;
4
+ constructor(client) {
5
+ this.client = client;
6
+ this.table = client.table("audit_events");
7
+ }
8
+ async append(event) {
9
+ await this.client.insert(this.table, {
10
+ id: event.id,
11
+ agent_id: event.agent_id,
12
+ event_type: event.event_type,
13
+ created_at: event.created_at,
14
+ record: event
15
+ });
16
+ }
17
+ async list() {
18
+ return this.client.selectRecords(this.table, {
19
+ order: { column: "created_at", ascending: true }
20
+ });
21
+ }
22
+ }
@@ -0,0 +1,31 @@
1
+ import type { JsonObject } from "../memory/types.js";
2
+ export type AuditSeverity = "debug" | "info" | "warn" | "error";
3
+ export type AuditEventInput = {
4
+ agent_id?: string | null;
5
+ event_type: string;
6
+ subject_type: string;
7
+ subject_id?: string | null;
8
+ severity?: AuditSeverity;
9
+ metadata?: JsonObject;
10
+ };
11
+ export type AuditEvent = Required<Omit<AuditEventInput, "severity" | "metadata" | "agent_id" | "subject_id">> & {
12
+ id: string;
13
+ agent_id: string | null;
14
+ subject_id: string | null;
15
+ severity: AuditSeverity;
16
+ metadata: JsonObject;
17
+ created_at: string;
18
+ };
19
+ export type AuditTailInput = {
20
+ agent_id?: string;
21
+ event_type?: string;
22
+ limit?: number;
23
+ };
24
+ export type AuditTailResult = {
25
+ count: number;
26
+ events: AuditEvent[];
27
+ };
28
+ export type AuditStore = {
29
+ append(event: AuditEvent): Promise<void>;
30
+ list(): Promise<AuditEvent[]>;
31
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,99 @@
1
+ export declare const REAL_CASPER_ANCHOR_ENV_VARS: readonly ["CASPER_RPC_URL", "CASPER_NETWORK_NAME", "MEMORY_ANCHOR_CONTRACT_HASH", "MEMORY_ANCHOR_PACKAGE_HASH", "CASPER_ACCOUNT_KEY_PATH", "CASPER_ENABLE_REAL_SUBMISSION"];
2
+ export type AnchorMemoryRequest = {
3
+ agent_id: string;
4
+ memory_id: string;
5
+ content_hash: string;
6
+ metadata_hash: string;
7
+ prev_anchor_hash: string | null;
8
+ };
9
+ export type AnchorSubmission = {
10
+ anchor_id: string;
11
+ agent_id_hash: string;
12
+ memory_id_hash: string;
13
+ content_hash: string;
14
+ metadata_hash: string;
15
+ prev_anchor_hash: string | null;
16
+ };
17
+ export type AnchorSubmissionReason = "casper_contract_not_configured" | "casper_client_not_configured" | "casper_transaction_submission_disabled" | "casper_transaction_submission_failed" | "casper_transaction_hash_missing";
18
+ export type AnchorSubmissionResult = {
19
+ status: "pending" | "anchored" | "failed";
20
+ anchor_id: string;
21
+ casper_transaction_hash: string | null;
22
+ onchain_content_hash: string | null;
23
+ reason?: AnchorSubmissionReason;
24
+ };
25
+ export type CasperAnchorClient = {
26
+ readonly mode: "unconfigured" | "configured";
27
+ anchorMemory(submission: AnchorSubmission): Promise<AnchorSubmissionResult>;
28
+ };
29
+ export type CasperAnchorClientConfig = {
30
+ networkName: string;
31
+ caip2ChainId: string;
32
+ rpcUrl: string | null;
33
+ accountKeyPath: string | null;
34
+ memoryAnchorContractHash: string | null;
35
+ memoryAnchorPackageHash: string | null;
36
+ submissionEnabled?: boolean;
37
+ clientBin?: string;
38
+ clientWslDistro?: string | null;
39
+ anchorSubmissionMode?: "transaction-package" | "deploy-contract-hash";
40
+ gasPriceTolerance?: string;
41
+ pricingMode?: string;
42
+ anchorPaymentAmountMotes?: string;
43
+ };
44
+ export type ValidatedCasperAnchorConfig = {
45
+ networkName: string;
46
+ caip2ChainId: string;
47
+ rpcUrl: string;
48
+ accountKeyPath: string;
49
+ memoryAnchorContractHash: string;
50
+ memoryAnchorPackageHash: string;
51
+ submissionEnabled: boolean;
52
+ clientBin: string;
53
+ clientWslDistro: string | null;
54
+ anchorSubmissionMode: "transaction-package" | "deploy-contract-hash";
55
+ gasPriceTolerance: string;
56
+ pricingMode: string;
57
+ anchorPaymentAmountMotes: string;
58
+ };
59
+ export type CasperCommandResult = {
60
+ exitCode: number;
61
+ stdout: string;
62
+ stderr: string;
63
+ };
64
+ export type CasperCommandRunner = (command: string, args: readonly string[]) => Promise<CasperCommandResult>;
65
+ export type CasperAnchorClientOptions = {
66
+ commandRunner?: CasperCommandRunner;
67
+ };
68
+ export type CasperCommandInvocation = {
69
+ command: string;
70
+ args: string[];
71
+ };
72
+ export declare class UnconfiguredCasperAnchorClient implements CasperAnchorClient {
73
+ private readonly reason;
74
+ readonly mode: "unconfigured";
75
+ readonly submissions: AnchorSubmission[];
76
+ constructor(reason?: AnchorSubmissionReason);
77
+ anchorMemory(submission: AnchorSubmission): Promise<AnchorSubmissionResult>;
78
+ }
79
+ export declare class ConfiguredCasperAnchorClient implements CasperAnchorClient {
80
+ readonly config: ValidatedCasperAnchorConfig;
81
+ private readonly commandRunner;
82
+ readonly mode: "configured";
83
+ readonly submissions: AnchorSubmission[];
84
+ constructor(config: ValidatedCasperAnchorConfig, commandRunner?: CasperCommandRunner);
85
+ anchorMemory(submission: AnchorSubmission): Promise<AnchorSubmissionResult>;
86
+ }
87
+ export declare class MockCasperAnchorClient extends UnconfiguredCasperAnchorClient {
88
+ constructor();
89
+ }
90
+ export declare function createCasperAnchorClient(config: CasperAnchorClientConfig, options?: CasperAnchorClientOptions): CasperAnchorClient;
91
+ export declare function validateCasperAnchorConfig(config: CasperAnchorClientConfig): ValidatedCasperAnchorConfig | null;
92
+ export declare function createAnchorSubmission(input: AnchorMemoryRequest): AnchorSubmission;
93
+ export declare function createPendingAnchorResult(submission: AnchorSubmission, reason: AnchorSubmissionReason): AnchorSubmissionResult;
94
+ export declare function createFailedAnchorResult(submission: AnchorSubmission, reason: AnchorSubmissionReason): AnchorSubmissionResult;
95
+ export declare function createAnchoredAnchorResult(submission: AnchorSubmission, casperTransactionHash: string): AnchorSubmissionResult;
96
+ export declare function createSubmittedAnchorResult(submission: AnchorSubmission, casperTransactionHash: string): AnchorSubmissionResult;
97
+ export declare function computeAnchorId(input: AnchorMemoryRequest): string;
98
+ export declare function buildCasperAnchorCommand(config: ValidatedCasperAnchorConfig, submission: AnchorSubmission): CasperCommandInvocation;
99
+ export declare function extractCasperTransactionHash(result: CasperCommandResult): string | null;