@scriptmasterlabs/mcp-x402 2.0.2 → 2.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 (93) hide show
  1. package/.well-known/agentcard.json +34 -34
  2. package/.well-known/ai.txt +32 -0
  3. package/CONTRIBUTING.md +76 -76
  4. package/LICENSE +21 -21
  5. package/README.md +304 -304
  6. package/agents.json +81 -67
  7. package/ai/faq.json +74 -0
  8. package/ai/summary.json +157 -0
  9. package/dist/lib/chains/base.d.ts.map +1 -1
  10. package/dist/lib/chains/base.js +2 -0
  11. package/dist/lib/chains/base.js.map +1 -1
  12. package/dist/lib/credit/bureau.d.ts +7 -1
  13. package/dist/lib/credit/bureau.d.ts.map +1 -1
  14. package/dist/lib/credit/bureau.js +40 -10
  15. package/dist/lib/credit/bureau.js.map +1 -1
  16. package/dist/server/index.js +128 -5
  17. package/dist/server/index.js.map +1 -1
  18. package/llms.txt +170 -70
  19. package/package.json +78 -78
  20. package/server.json +52 -48
  21. package/.env.example +0 -35
  22. package/.github/workflows/ci.yml +0 -59
  23. package/.github/workflows/keepalive.yml +0 -31
  24. package/Dockerfile +0 -19
  25. package/docker-compose.yml +0 -50
  26. package/mcp-publisher.exe +0 -0
  27. package/render.yaml +0 -39
  28. package/sdk/mcp-x402-sdk/package.json +0 -18
  29. package/sdk/mcp-x402-sdk/src/index.ts +0 -118
  30. package/sdk/mcp-x402-sdk/tsconfig.json +0 -14
  31. package/services/backtest_service.py +0 -176
  32. package/src/lib/chains/base.ts +0 -77
  33. package/src/lib/chains/solana.ts +0 -59
  34. package/src/lib/chains/xrpl.ts +0 -63
  35. package/src/lib/credit/bureau.ts +0 -65
  36. package/src/lib/sml-api/agentcard.ts +0 -40
  37. package/src/lib/sml-api/backtest.ts +0 -47
  38. package/src/lib/sml-api/brokers.ts +0 -160
  39. package/src/lib/sml-api/copytrader.ts +0 -33
  40. package/src/lib/sml-api/crawl.ts +0 -44
  41. package/src/lib/sml-api/echo.ts +0 -28
  42. package/src/lib/sml-api/forge.ts +0 -33
  43. package/src/lib/sml-api/ftd.ts +0 -53
  44. package/src/lib/sml-api/ghost.ts +0 -35
  45. package/src/lib/sml-api/launchpad.ts +0 -43
  46. package/src/lib/sml-api/leviathan.ts +0 -49
  47. package/src/lib/sml-api/nexus.ts +0 -50
  48. package/src/lib/sml-api/proof402.ts +0 -27
  49. package/src/lib/sml-api/rails.ts +0 -34
  50. package/src/lib/sml-api/shadow.ts +0 -35
  51. package/src/lib/sml-api/squeezeos.ts +0 -95
  52. package/src/lib/sml-api/xdeo.ts +0 -40
  53. package/src/lib/sml-api/xmit.ts +0 -40
  54. package/src/server/health.ts +0 -52
  55. package/src/server/index.ts +0 -213
  56. package/src/server/payments/ap2.ts +0 -101
  57. package/src/server/payments/receipt.ts +0 -85
  58. package/src/server/payments/router.ts +0 -110
  59. package/src/server/payments/wallet.ts +0 -123
  60. package/src/server/payments/x402.ts +0 -177
  61. package/src/server/registry/catalog.ts +0 -61
  62. package/src/server/registry/discovery.ts +0 -39
  63. package/src/server/registry/pricing.ts +0 -133
  64. package/src/server/security/acl.ts +0 -42
  65. package/src/server/security/audit.ts +0 -94
  66. package/src/server/security/rate-limit.ts +0 -84
  67. package/src/server/security/sandbox.ts +0 -40
  68. package/src/server/tools/agentcard.ts +0 -134
  69. package/src/server/tools/backtest.ts +0 -119
  70. package/src/server/tools/brokers.ts +0 -250
  71. package/src/server/tools/copytrader.ts +0 -104
  72. package/src/server/tools/crawl.ts +0 -70
  73. package/src/server/tools/discovery.ts +0 -202
  74. package/src/server/tools/echo.ts +0 -58
  75. package/src/server/tools/forge.ts +0 -87
  76. package/src/server/tools/ftd.ts +0 -88
  77. package/src/server/tools/ghost.ts +0 -93
  78. package/src/server/tools/index.ts +0 -42
  79. package/src/server/tools/launchpad.ts +0 -173
  80. package/src/server/tools/leviathan.ts +0 -81
  81. package/src/server/tools/nexus.ts +0 -76
  82. package/src/server/tools/proof402.ts +0 -87
  83. package/src/server/tools/rails.ts +0 -92
  84. package/src/server/tools/shadow.ts +0 -128
  85. package/src/server/tools/squeezeos.ts +0 -312
  86. package/src/server/tools/xdeo.ts +0 -67
  87. package/src/server/tools/xmit.ts +0 -68
  88. package/tests/integration/e2e.test.ts +0 -51
  89. package/tests/unit/payments.test.ts +0 -49
  90. package/tests/unit/security.test.ts +0 -92
  91. package/tests/unit/tools.test.ts +0 -42
  92. package/tsconfig.json +0 -21
  93. package/vitest.config.ts +0 -20
@@ -1,177 +0,0 @@
1
- import { z } from 'zod';
2
- import { AuditLogger } from '../security/audit.js';
3
- import { AP2Client } from './ap2.js';
4
- import { WalletManager } from './wallet.js';
5
- import { ChainRouter } from './router.js';
6
- import { ReceiptStore } from './receipt.js';
7
- import { CreditBureau } from '../../lib/credit/bureau.js';
8
- import { PriceRegistry } from '../registry/pricing.js';
9
-
10
- export const PaymentConfigSchema = z.object({
11
- price: z.string().regex(/^\d+(\.\d+)?$/),
12
- currency: z.enum(['USDC', 'RLUSD']),
13
- toolName: z.string(),
14
- walletAddress: z.string().optional(),
15
- });
16
-
17
- export type PaymentConfig = z.infer<typeof PaymentConfigSchema>;
18
-
19
- export interface PaymentResult {
20
- receiptId: string;
21
- txHash: string;
22
- chain: string;
23
- amountPaid: string;
24
- currency: string;
25
- timestamp: number;
26
- walletAddress: string;
27
- }
28
-
29
- const AUTO_APPROVE_THRESHOLD = parseFloat(
30
- process.env['AUTO_APPROVE_THRESHOLD_USD'] ?? '1.0',
31
- );
32
-
33
- const DAILY_SPEND_CAP = parseFloat(
34
- process.env['DAILY_SPEND_CAP_USD'] ?? '50.0',
35
- );
36
-
37
- const dailySpend = new Map<string, { amount: number; date: string }>();
38
-
39
- function getTodayKey(): string {
40
- return new Date().toISOString().slice(0, 10);
41
- }
42
-
43
- function getDailySpend(wallet: string): number {
44
- const today = getTodayKey();
45
- const entry = dailySpend.get(wallet);
46
- if (!entry || entry.date !== today) return 0;
47
- return entry.amount;
48
- }
49
-
50
- function addDailySpend(wallet: string, amount: number): void {
51
- const today = getTodayKey();
52
- const current = getDailySpend(wallet);
53
- dailySpend.set(wallet, { amount: current + amount, date: today });
54
- }
55
-
56
- export async function executeX402Payment(
57
- config: PaymentConfig,
58
- ): Promise<PaymentResult> {
59
- const audit = AuditLogger.getInstance();
60
- const wallet = await WalletManager.getInstance().getOrCreateWallet();
61
- const walletAddress = wallet.address;
62
-
63
- // Enforce daily spend cap (N9)
64
- const priceNum = parseFloat(config.price);
65
- const currentSpend = getDailySpend(walletAddress);
66
- if (currentSpend + priceNum > DAILY_SPEND_CAP) {
67
- audit.warn('spend_cap_exceeded', {
68
- wallet: walletAddress,
69
- current: currentSpend,
70
- requested: priceNum,
71
- cap: DAILY_SPEND_CAP,
72
- });
73
- throw new Error(
74
- `Daily spend cap of $${DAILY_SPEND_CAP} exceeded. Current: $${currentSpend.toFixed(4)}`,
75
- );
76
- }
77
-
78
- // Verify price cache freshness (N12)
79
- const cachedPrice = await PriceRegistry.getInstance().getPrice(config.toolName);
80
- if (!cachedPrice) {
81
- throw new Error('Price data stale or unavailable. Rejecting payment.');
82
- }
83
- if (cachedPrice !== config.price) {
84
- throw new Error(
85
- `Price mismatch: expected ${cachedPrice}, got ${config.price}. Cache may be stale.`,
86
- );
87
- }
88
-
89
- // Credit Bureau check (N8)
90
- const score = await CreditBureau.getInstance().getScore(walletAddress);
91
- const autoApprove = priceNum <= AUTO_APPROVE_THRESHOLD && score >= 300;
92
-
93
- audit.info('payment_attempt', {
94
- tool: config.toolName,
95
- price: config.price,
96
- currency: config.currency,
97
- wallet: walletAddress,
98
- bureauScore: score,
99
- autoApprove,
100
- });
101
-
102
- if (!autoApprove && score < 300) {
103
- throw new Error(
104
- `Credit Bureau score ${score} below minimum 300. Payment requires manual approval.`,
105
- );
106
- }
107
-
108
- // AP2 mandate verification (N6)
109
- const ap2 = AP2Client.getInstance();
110
- const mandateValid = await ap2.verifyMandate(walletAddress, {
111
- maxAmount: config.price,
112
- currency: config.currency,
113
- toolName: config.toolName,
114
- });
115
-
116
- if (!mandateValid) {
117
- audit.warn('ap2_mandate_rejected', { wallet: walletAddress, tool: config.toolName });
118
- throw new Error(
119
- 'AP2 mandate verification failed. Agent not authorized for this payment.',
120
- );
121
- }
122
-
123
- // Route payment to cheapest/fastest chain (N13)
124
- // If SML_PAYMENT_RECEIVER is not configured, log the intended payment and continue
125
- const receiver = process.env['SML_PAYMENT_RECEIVER'] ?? '';
126
- let txResult: { txHash: string; chain: string; latencyMs: number };
127
-
128
- if (!receiver) {
129
- audit.warn('payment_receiver_unset', { tool: config.toolName, amount: config.price, note: 'SML_PAYMENT_RECEIVER not configured — logging only' });
130
- txResult = { txHash: `pending-${Date.now()}`, chain: 'none', latencyMs: 0 };
131
- } else {
132
- try {
133
- const router = ChainRouter.getInstance();
134
- txResult = await router.route({
135
- amount: config.price,
136
- currency: config.currency,
137
- from: walletAddress,
138
- to: receiver,
139
- timeoutMs: 500,
140
- });
141
- } catch (err) {
142
- // Log payment failure but don't block tool execution
143
- audit.warn('payment_tx_failed', { tool: config.toolName, amount: config.price, error: String(err), note: 'Server wallet may be unfunded — tool served anyway' });
144
- txResult = { txHash: `failed-${Date.now()}`, chain: 'none', latencyMs: 0 };
145
- }
146
- }
147
-
148
- addDailySpend(walletAddress, priceNum);
149
-
150
- // Generate 402Proof receipt (N7)
151
- const receipt = await ReceiptStore.getInstance().create({
152
- txHash: txResult.txHash,
153
- chain: txResult.chain,
154
- amount: config.price,
155
- currency: config.currency,
156
- tool: config.toolName,
157
- wallet: walletAddress,
158
- });
159
-
160
- audit.info('payment_success', {
161
- receiptId: receipt.id,
162
- txHash: txResult.txHash,
163
- chain: txResult.chain,
164
- tool: config.toolName,
165
- wallet: walletAddress,
166
- });
167
-
168
- return {
169
- receiptId: receipt.id,
170
- txHash: txResult.txHash,
171
- chain: txResult.chain,
172
- amountPaid: config.price,
173
- currency: config.currency,
174
- timestamp: Date.now(),
175
- walletAddress,
176
- };
177
- }
@@ -1,61 +0,0 @@
1
- export interface ToolMeta {
2
- name: string;
3
- description: string;
4
- price: string;
5
- currency: 'USDC' | 'RLUSD';
6
- freeTier?: string;
7
- ap2Required: boolean;
8
- cacheTtl?: number;
9
- }
10
-
11
- export const CATALOG: ToolMeta[] = [
12
- {
13
- name: 'leviathan_signal',
14
- description: 'Institutional-grade squeeze signals. Multi-engine verdict for any ticker.',
15
- price: '0.05',
16
- currency: 'USDC',
17
- ap2Required: true,
18
- },
19
- {
20
- name: 'xmit_edgar_decode',
21
- description: 'Parse SEC DEF 14A / 13F / 13D filings. Raw text never leaves SML servers.',
22
- price: '0.02',
23
- currency: 'USDC',
24
- ap2Required: true,
25
- },
26
- {
27
- name: 'xdeo_earnings_estimate',
28
- description: 'Decentralized earnings oracle. +2 bureau_score on success.',
29
- price: '0.02',
30
- currency: 'USDC',
31
- ap2Required: true,
32
- },
33
- {
34
- name: 'ftd_threshold_scan',
35
- description: 'SEC Reg SHO FTD data. Alerts free; full data 0.05 USDC. 15-min cache.',
36
- price: '0.05',
37
- currency: 'USDC',
38
- ap2Required: false,
39
- freeTier: 'alerts_only',
40
- cacheTtl: 900,
41
- },
42
- {
43
- name: 'nexus_agent_hire',
44
- description: 'Agent marketplace. Query free; hire charges 5% commission.',
45
- price: '0.00',
46
- currency: 'USDC',
47
- ap2Required: false,
48
- freeTier: 'query_only',
49
- },
50
- {
51
- name: 'crawl_paid_fetch',
52
- description: 'Pay-per-fetch scraping. Humans bypass free.',
53
- price: '0.005',
54
- currency: 'USDC',
55
- ap2Required: false,
56
- },
57
- ];
58
-
59
- export function getToolMeta(name: string): ToolMeta | undefined {
60
- return CATALOG.find((t) => t.name === name);
61
- }
@@ -1,39 +0,0 @@
1
- import { readFileSync } from 'fs';
2
- import { join } from 'path';
3
-
4
- interface AgentsJson {
5
- schema_version: string;
6
- name: string;
7
- tools: unknown[];
8
- }
9
-
10
- export class Discovery {
11
- private static instance: Discovery;
12
- private agentsJson: AgentsJson | null = null;
13
- private llmsTxt: string | null = null;
14
-
15
- private constructor() {}
16
-
17
- static getInstance(): Discovery {
18
- if (!Discovery.instance) {
19
- Discovery.instance = new Discovery();
20
- }
21
- return Discovery.instance;
22
- }
23
-
24
- getAgentsJson(): AgentsJson {
25
- if (!this.agentsJson) {
26
- const path = join(process.cwd(), 'agents.json');
27
- this.agentsJson = JSON.parse(readFileSync(path, 'utf8')) as AgentsJson;
28
- }
29
- return this.agentsJson;
30
- }
31
-
32
- getLlmsTxt(): string {
33
- if (!this.llmsTxt) {
34
- const path = join(process.cwd(), 'llms.txt');
35
- this.llmsTxt = readFileSync(path, 'utf8');
36
- }
37
- return this.llmsTxt;
38
- }
39
- }
@@ -1,133 +0,0 @@
1
- const PRICE_CACHE_TTL = parseInt(process.env['PRICE_CACHE_TTL_MS'] ?? '60000', 10);
2
-
3
- const BASE_PRICES: Record<string, string> = {
4
- // Discovery (free)
5
- sml_discover: '0.00',
6
- sml_status: '0.00',
7
- // SqueezeOS signals
8
- leviathan_signal: '0.05',
9
- squeezeos_council: '0.10',
10
- squeezeos_scan: '0.05',
11
- squeezeos_options: '0.05',
12
- squeezeos_iwm: '0.03',
13
- squeezeos_preview: '0.00',
14
- squeezeos_status: '0.00',
15
- // SEC / Earnings
16
- xmit_edgar_decode: '0.02',
17
- xdeo_earnings_estimate: '0.02',
18
- // FTD
19
- ftd_threshold_scan: '0.05',
20
- // Crawl
21
- crawl_paid_fetch: '0.005',
22
- // Agent marketplace
23
- nexus_agent_hire: '0.00',
24
- nexus_agent_list: '0.00',
25
- nexus_agent_status: '0.00',
26
- // Ghost Layer (cross-chain)
27
- ghost_transfer: '0.02',
28
- ghost_status: '0.00',
29
- ghost_routes: '0.00',
30
- // RLUSD Rails
31
- rails_send: '0.01',
32
- rails_status: '0.00',
33
- // Launchpad
34
- launchpad_create_token: '0.10',
35
- launchpad_buy_token: '0.02',
36
- launchpad_list: '0.00',
37
- launchpad_status: '0.00',
38
- // Copy-Trader
39
- copytrader_subscribe: '0.05',
40
- copytrader_status: '0.00',
41
- // Backtest
42
- backtest_run: '0.05',
43
- backtest_validate: '0.05',
44
- backtest_status: '0.00',
45
- // Brokers (Tradier)
46
- brokers_quote: '0.00',
47
- brokers_options_chain: '0.01',
48
- brokers_place_order: '0.05',
49
- brokers_account: '0.01',
50
- // Shadow Desk
51
- shadow_query: '0.05',
52
- shadow_ingest: '0.02',
53
- shadow_status: '0.00',
54
- // Forge (LLM gateway)
55
- forge_complete: '0.02',
56
- forge_status: '0.00',
57
- // Proof402
58
- proof402_get_invoice: '0.00',
59
- proof402_verify: '0.00',
60
- proof402_credit_score: '0.00',
61
- // Echo (pattern matching)
62
- echo_analogs: '0.05',
63
- // Agent card / identity
64
- agentcard_verify: '0.00',
65
- agentcard_register: '0.01',
66
- agentcard_lookup: '0.00',
67
- };
68
-
69
- interface CachedPrice {
70
- price: string;
71
- fetchedAt: number;
72
- }
73
-
74
- export class PriceRegistry {
75
- private static instance: PriceRegistry;
76
- private readonly cache = new Map<string, CachedPrice>();
77
- private readonly baseUrl: string;
78
-
79
- private constructor() {
80
- this.baseUrl = process.env['SML_API_BASE'] ?? 'https://squeezeos-api.onrender.com';
81
- }
82
-
83
- static getInstance(): PriceRegistry {
84
- if (!PriceRegistry.instance) {
85
- PriceRegistry.instance = new PriceRegistry();
86
- }
87
- return PriceRegistry.instance;
88
- }
89
-
90
- async getPrice(toolName: string): Promise<string | null> {
91
- const cached = this.cache.get(toolName);
92
- const now = Date.now();
93
-
94
- if (cached && now - cached.fetchedAt < PRICE_CACHE_TTL) {
95
- return cached.price;
96
- }
97
-
98
- // Fetch live price from SML pricing API
99
- try {
100
- const res = await fetch(`${this.baseUrl}/pricing/v1/tool/${toolName}`, {
101
- signal: AbortSignal.timeout(3000),
102
- });
103
-
104
- if (res.ok) {
105
- const body = (await res.json()) as { price: string };
106
- this.cache.set(toolName, { price: body.price, fetchedAt: now });
107
- return body.price;
108
- }
109
- } catch {
110
- // Fall through to hardcoded baseline
111
- }
112
-
113
- // Use hardcoded baseline if API unavailable
114
- const fallback = BASE_PRICES[toolName];
115
- if (fallback !== undefined) {
116
- this.cache.set(toolName, { price: fallback, fetchedAt: now - PRICE_CACHE_TTL / 2 });
117
- return fallback;
118
- }
119
-
120
- // Unknown tool — default to free rather than reject
121
- this.cache.set(toolName, { price: '0.00', fetchedAt: now });
122
- return '0.00';
123
- }
124
-
125
- seedDefaults(): void {
126
- const now = Date.now();
127
- for (const [tool, price] of Object.entries(BASE_PRICES)) {
128
- if (!this.cache.has(tool)) {
129
- this.cache.set(tool, { price, fetchedAt: now });
130
- }
131
- }
132
- }
133
- }
@@ -1,42 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- export const ToolACLSchema = z.object({
4
- toolName: z.string(),
5
- walletAddress: z.string().optional(),
6
- creditScore: z.number().optional(),
7
- paidTier: z.boolean().default(false),
8
- });
9
-
10
- export type ToolACL = z.infer<typeof ToolACLSchema>;
11
-
12
- const FREE_TOOLS = new Set(['ftd_threshold_scan_alerts', 'nexus_agent_hire_query']);
13
-
14
- export class ACL {
15
- private static instance: ACL;
16
-
17
- private constructor() {}
18
-
19
- static getInstance(): ACL {
20
- if (!ACL.instance) {
21
- ACL.instance = new ACL();
22
- }
23
- return ACL.instance;
24
- }
25
-
26
- isFree(toolName: string): boolean {
27
- return FREE_TOOLS.has(toolName);
28
- }
29
-
30
- requiresPayment(toolName: string): boolean {
31
- return !this.isFree(toolName);
32
- }
33
-
34
- requiresAP2(toolName: string): boolean {
35
- // leviathan, xmit, xdeo require AP2 per spec
36
- return ['leviathan_signal', 'xmit_edgar_decode', 'xdeo_earnings_estimate'].includes(toolName);
37
- }
38
-
39
- minCreditScore(_toolName: string): number {
40
- return 300;
41
- }
42
- }
@@ -1,94 +0,0 @@
1
- import { createHash, createHmac } from 'crypto';
2
- import { appendFileSync } from 'fs';
3
-
4
- type LogLevel = 'info' | 'warn' | 'error';
5
-
6
- interface LogEntry {
7
- seq: number;
8
- ts: number;
9
- level: LogLevel;
10
- event: string;
11
- data: Record<string, unknown>;
12
- prev_hash: string;
13
- hash: string;
14
- }
15
-
16
- // Append-only SHA-256 chained audit log (N5)
17
- // Each entry includes the hash of the previous entry — tampering breaks the chain.
18
- export class AuditLogger {
19
- private static instance: AuditLogger;
20
- private seq = 0;
21
- private prevHash = '0000000000000000000000000000000000000000000000000000000000000000';
22
- private readonly logPath: string;
23
- private readonly hmacSecret: string;
24
-
25
- private constructor() {
26
- this.logPath = process.env['AUDIT_LOG_PATH'] ?? './audit.log';
27
- this.hmacSecret = process.env['AUDIT_HMAC_SECRET'] ?? 'mcp-x402-audit-secret';
28
- }
29
-
30
- static getInstance(): AuditLogger {
31
- if (!AuditLogger.instance) {
32
- AuditLogger.instance = new AuditLogger();
33
- }
34
- return AuditLogger.instance;
35
- }
36
-
37
- private log(level: LogLevel, event: string, data: Record<string, unknown>): void {
38
- const seq = ++this.seq;
39
- const ts = Date.now();
40
-
41
- // Redact PII (N3): hash wallet addresses, never log raw filing content
42
- const safeData = this.redact(data);
43
-
44
- const payload = JSON.stringify({ seq, ts, level, event, data: safeData, prev_hash: this.prevHash });
45
- const hash = createHmac('sha256', this.hmacSecret).update(payload).digest('hex');
46
-
47
- const entry: LogEntry = {
48
- seq,
49
- ts,
50
- level,
51
- event,
52
- data: safeData,
53
- prev_hash: this.prevHash,
54
- hash,
55
- };
56
-
57
- this.prevHash = hash;
58
-
59
- try {
60
- appendFileSync(this.logPath, JSON.stringify(entry) + '\n', 'utf8');
61
- } catch {
62
- // If log write fails, emit to stderr but don't crash
63
- process.stderr.write(`[audit-fail] ${JSON.stringify(entry)}\n`);
64
- }
65
- }
66
-
67
- private redact(data: Record<string, unknown>): Record<string, unknown> {
68
- const out: Record<string, unknown> = {};
69
- for (const [k, v] of Object.entries(data)) {
70
- if (k === 'wallet' || k === 'address') {
71
- // Hash wallet addresses (N3)
72
- out[k] = createHash('sha256').update(String(v)).digest('hex').slice(0, 16) + '...';
73
- } else if (k === 'content' || k === 'raw_text' || k === 'filing') {
74
- // Never log raw filing data (N3)
75
- out[k] = '[REDACTED]';
76
- } else {
77
- out[k] = v;
78
- }
79
- }
80
- return out;
81
- }
82
-
83
- info(event: string, data: Record<string, unknown> = {}): void {
84
- this.log('info', event, data);
85
- }
86
-
87
- warn(event: string, data: Record<string, unknown> = {}): void {
88
- this.log('warn', event, data);
89
- }
90
-
91
- error(event: string, data: Record<string, unknown> = {}): void {
92
- this.log('error', event, data);
93
- }
94
- }
@@ -1,84 +0,0 @@
1
- interface BucketState {
2
- minute: { count: number; resetAt: number };
3
- day: { count: number; resetAt: number };
4
- }
5
-
6
- const PER_TOOL_MINUTE_LIMIT = 100;
7
- const PER_WALLET_DAY_LIMIT = 1000;
8
- const IP_MINUTE_LIMIT = 200;
9
-
10
- function nowMs(): number {
11
- return Date.now();
12
- }
13
-
14
- export class RateLimiter {
15
- private static instance: RateLimiter;
16
- private readonly toolBuckets = new Map<string, BucketState>();
17
- private readonly walletBuckets = new Map<string, BucketState>();
18
- private readonly ipBuckets = new Map<string, { count: number; resetAt: number }>();
19
-
20
- private constructor() {}
21
-
22
- static getInstance(): RateLimiter {
23
- if (!RateLimiter.instance) {
24
- RateLimiter.instance = new RateLimiter();
25
- }
26
- return RateLimiter.instance;
27
- }
28
-
29
- checkTool(toolName: string): boolean {
30
- const now = nowMs();
31
- let bucket = this.toolBuckets.get(toolName);
32
-
33
- if (!bucket) {
34
- bucket = {
35
- minute: { count: 0, resetAt: now + 60_000 },
36
- day: { count: 0, resetAt: now + 86_400_000 },
37
- };
38
- this.toolBuckets.set(toolName, bucket);
39
- }
40
-
41
- if (now > bucket.minute.resetAt) {
42
- bucket.minute = { count: 0, resetAt: now + 60_000 };
43
- }
44
-
45
- if (bucket.minute.count >= PER_TOOL_MINUTE_LIMIT) return false;
46
- bucket.minute.count++;
47
- return true;
48
- }
49
-
50
- checkWallet(wallet: string): boolean {
51
- const now = nowMs();
52
- let bucket = this.walletBuckets.get(wallet);
53
-
54
- if (!bucket) {
55
- bucket = {
56
- minute: { count: 0, resetAt: now + 60_000 },
57
- day: { count: 0, resetAt: now + 86_400_000 },
58
- };
59
- this.walletBuckets.set(wallet, bucket);
60
- }
61
-
62
- if (now > bucket.day.resetAt) {
63
- bucket.day = { count: 0, resetAt: now + 86_400_000 };
64
- }
65
-
66
- if (bucket.day.count >= PER_WALLET_DAY_LIMIT) return false;
67
- bucket.day.count++;
68
- return true;
69
- }
70
-
71
- checkIp(ip: string): boolean {
72
- const now = nowMs();
73
- let entry = this.ipBuckets.get(ip);
74
-
75
- if (!entry || now > entry.resetAt) {
76
- entry = { count: 0, resetAt: now + 60_000 };
77
- this.ipBuckets.set(ip, entry);
78
- }
79
-
80
- if (entry.count >= IP_MINUTE_LIMIT) return false;
81
- entry.count++;
82
- return true;
83
- }
84
- }
@@ -1,40 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- // Sandboxed input validation layer — all tool inputs pass through here before execution.
4
- // No eval(), no dynamic require(), no raw SQL (N4 enforcement at schema layer).
5
- export class Sandbox {
6
- static validate<T>(schema: z.ZodType<T>, input: unknown): T {
7
- const result = schema.safeParse(input);
8
- if (!result.success) {
9
- const issues = result.error.issues
10
- .map((i) => `${i.path.join('.')}: ${i.message}`)
11
- .join('; ');
12
- throw new Error(`Input validation failed: ${issues}`);
13
- }
14
- return result.data;
15
- }
16
-
17
- // Ensure URL is http/https only — no file://, data://, javascript:
18
- static validateUrl(raw: string): URL {
19
- let url: URL;
20
- try {
21
- url = new URL(raw);
22
- } catch {
23
- throw new Error(`Invalid URL: ${raw}`);
24
- }
25
- if (url.protocol !== 'http:' && url.protocol !== 'https:') {
26
- throw new Error(`Disallowed URL protocol: ${url.protocol}`);
27
- }
28
- return url;
29
- }
30
-
31
- // Strip any response content that looks like a prompt injection attempt
32
- static sanitizeApiResponse(text: string): string {
33
- // Remove common injection markers
34
- return text
35
- .replace(/<\/?system>/gi, '')
36
- .replace(/\[INST\]/gi, '')
37
- .replace(/\[\/?INST\]/gi, '')
38
- .slice(0, 50_000); // Hard cap on returned content size
39
- }
40
- }