agentsla 0.0.0-stage → 2.0.1

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 (4) hide show
  1. package/README.md +49 -2
  2. package/index.js +192 -0
  3. package/package.json +25 -4
  4. package/test.js +32 -0
package/README.md CHANGED
@@ -1,3 +1,50 @@
1
- # Temporary Holding Version
1
+ # AgentSLA SDK (v2.0.0)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Official M2M JavaScript/TypeScript SDK for **AgentSLA** (https://agentsla.io) — the autonomous API telemetry directory, EIP-712 cryptographic SLA oracle, Streamable HTTP MCP server, and Circle USDC escrow protocol on **Base Mainnet (Chain ID 8453)**.
4
+
5
+ ## Production Infrastructure (Base Mainnet)
6
+
7
+ - **Verified Escrow Contract:** [0x6ca6cDDd6EC59D75Ea0a36882AC57fc199Ba79CD](https://basescan.org/address/0x6ca6cDDd6EC59D75Ea0a36882AC57fc199Ba79CD)
8
+ - **Settlement Token (Circle USDC):** [0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913](https://basescan.org/token/0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913) (6 decimals)
9
+ - **Streamable HTTP MCP Endpoint:** https://agentsla.io/mcp
10
+ - **LLM Discovery Manifests:** https://agentsla.io/llms.txt | https://agentsla.io/llms-full.txt
11
+ - **OpenAPI 3.1.0 Spec:** https://agentsla.io/openapi.json
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install agentsla ethers
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ```javascript
22
+ import { ethers } from "ethers";
23
+ import { AgentSLA } from "agentsla";
24
+
25
+ const provider = new ethers.JsonRpcProvider("https://mainnet.base.org");
26
+ const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
27
+ const client = new AgentSLA({ signer: wallet });
28
+
29
+ // 1. Discover top-ranked APIs by category
30
+ const directory = await client.discover("ai-models");
31
+
32
+ // 2. Probe an endpoint and receive a signed EIP-712 SLA attestation
33
+ const proof = await client.verify("https://mainnet.base.org");
34
+
35
+ // 3. Lock Circle USDC in Base Mainnet Escrow for an M2M API task
36
+ const receipt = await client.lockEscrowUsdc(wallet.address, 1, "0.01");
37
+ ```
38
+
39
+ ## Streamable HTTP MCP Configuration
40
+
41
+ ```json
42
+ {
43
+ "mcpServers": {
44
+ "agentsla": {
45
+ "type": "streamable-http",
46
+ "url": "https://agentsla.io/mcp"
47
+ }
48
+ }
49
+ }
50
+ ```
package/index.js ADDED
@@ -0,0 +1,192 @@
1
+ import { ethers } from "ethers";
2
+
3
+ export const DEFAULT_ORACLE_URL = "https://agentsla-oracle.agentsla-dev.workers.dev";
4
+ export const DEFAULT_MCP_URL = "https://agentsla.io/mcp";
5
+ export const ESCROW_ADDRESS = "0x6ca6cDDd6EC59D75Ea0a36882AC57fc199Ba79CD";
6
+ export const USDC_ADDRESS = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
7
+ export const BASE_CHAIN_ID = 8453;
8
+
9
+ export const ESCROW_ABI = [
10
+ "function owner() view returns (address)",
11
+ "function payoutWallet() view returns (address)",
12
+ "function usdcToken() view returns (address)",
13
+ "function nextJobId() view returns (uint256)",
14
+ "function accumulatedFees() view returns (uint256)",
15
+ "function isSubscribed(address) view returns (bool)",
16
+ "function jobs(uint256) view returns (address creator, address agent, uint256 apiId, uint256 lockedAmount, bool isActive)",
17
+ "function paySubscription() external",
18
+ "function createJob(address _agent, uint256 _apiId, uint256 _lockedAmount) external returns (uint256)",
19
+ "function resolveJob(uint256 _jobId, bool _slaMet) external"
20
+ ];
21
+
22
+ export const USDC_ABI = [
23
+ "function balanceOf(address account) view returns (uint256)",
24
+ "function allowance(address owner, address spender) view returns (uint256)",
25
+ "function approve(address spender, uint256 amount) external returns (bool)"
26
+ ];
27
+
28
+ export class AgentSLA {
29
+ /**
30
+ * @param {Object} config
31
+ * @param {string} [config.oracleUrl] - Custom AgentSLA Oracle Worker URL
32
+ * @param {string} [config.mcpUrl] - Streamable HTTP MCP URL (https://agentsla.io/mcp)
33
+ * @param {string} [config.escrowAddress] - Base Mainnet Escrow Contract Address
34
+ * @param {string} [config.usdcAddress] - Base Mainnet Circle USDC Address
35
+ * @param {string} [config.walletAddress] - Subscribed Base Mainnet wallet address (0x...)
36
+ * @param {ethers.Signer|ethers.Provider} [config.signer] - Ethers v6 Signer or Provider
37
+ */
38
+ constructor(config = {}) {
39
+ this.oracleUrl = config.oracleUrl || DEFAULT_ORACLE_URL;
40
+ this.mcpUrl = config.mcpUrl || DEFAULT_MCP_URL;
41
+ this.escrowAddress = config.escrowAddress || ESCROW_ADDRESS;
42
+ this.usdcAddress = config.usdcAddress || USDC_ADDRESS;
43
+ this.walletAddress = config.walletAddress || null;
44
+ this.signer = config.signer || null;
45
+
46
+ if (this.signer) {
47
+ this.contract = new ethers.Contract(this.escrowAddress, ESCROW_ABI, this.signer);
48
+ this.usdc = new ethers.Contract(this.usdcAddress, USDC_ABI, this.signer);
49
+ }
50
+ }
51
+
52
+ async _resolveWallet(explicitWallet) {
53
+ if (explicitWallet) return explicitWallet;
54
+ if (this.walletAddress) return this.walletAddress;
55
+ if (this.signer) {
56
+ if (this.signer.address) return this.signer.address;
57
+ if (typeof this.signer.getAddress === "function") {
58
+ try { return await this.signer.getAddress(); } catch (e) {}
59
+ }
60
+ }
61
+ return undefined;
62
+ }
63
+
64
+ /**
65
+ * Probe an API endpoint via the AgentSLA Oracle, generate a signed EIP-712 SLA proof,
66
+ * and optionally resolve an active on-chain Base Mainnet USDC escrow job.
67
+ * @param {string} targetApiUrl - Target API endpoint to probe
68
+ * @param {number} [jobId=0] - Active Base Mainnet escrow jobId (0 for probe-only)
69
+ * @param {Object} [targetApiPayload] - Optional POST payload for the target API
70
+ * @param {string} [walletAddress] - Optional subscribed Base Mainnet wallet address
71
+ */
72
+ async verify(targetApiUrl, jobId = 0, targetApiPayload = undefined, walletAddress = undefined) {
73
+ if (!targetApiUrl) throw new Error("targetApiUrl is required.");
74
+
75
+ const resolvedWallet = await this._resolveWallet(walletAddress);
76
+
77
+ const response = await fetch(`${this.oracleUrl}/v1/verify`, {
78
+ method: "POST",
79
+ headers: { "Content-Type": "application/json" },
80
+ body: JSON.stringify({
81
+ jobId,
82
+ targetApiUrl,
83
+ targetApiPayload,
84
+ walletAddress: resolvedWallet
85
+ })
86
+ });
87
+
88
+ const data = await response.json();
89
+ if (!response.ok) {
90
+ const err = new Error(data.error || "Failed to verify endpoint via AgentSLA Oracle.");
91
+ err.status = response.status;
92
+ err.paymentDetails = data;
93
+ throw err;
94
+ }
95
+ return data;
96
+ }
97
+
98
+ /**
99
+ * Approve Circle USDC (if needed) and lock USDC in Base Mainnet Escrow for an M2M API task.
100
+ * @param {string} agentWallet - Provider agent wallet address
101
+ * @param {number|bigint} apiId - Monitored API ID from the AgentSLA directory
102
+ * @param {string|number} amountUsdc - Amount in USDC (e.g., "0.01" or "5.00")
103
+ */
104
+ async lockEscrowUsdc(agentWallet, apiId, amountUsdc) {
105
+ if (!this.contract || !this.usdc || typeof this.signer.getAddress !== "function") {
106
+ throw new Error("A connected ethers.js Signer is required to lock USDC in escrow.");
107
+ }
108
+
109
+ const amountUnits = ethers.parseUnits(amountUsdc.toString(), 6);
110
+ const ownerAddress = await this.signer.getAddress();
111
+ const currentAllowance = await this.usdc.allowance(ownerAddress, this.escrowAddress);
112
+
113
+ if (currentAllowance < amountUnits) {
114
+ const approveTx = await this.usdc.approve(this.escrowAddress, amountUnits);
115
+ await approveTx.wait(1);
116
+ }
117
+
118
+ const createTx = await this.contract.createJob(agentWallet, BigInt(apiId), amountUnits);
119
+ return await createTx.wait(1);
120
+ }
121
+
122
+ /**
123
+ * Pay the 5 USDC 7-day autonomous oracle subscription on Base Mainnet.
124
+ */
125
+ async paySubscription() {
126
+ if (!this.contract || !this.usdc || typeof this.signer.getAddress !== "function") {
127
+ throw new Error("A connected ethers.js Signer is required to pay subscription.");
128
+ }
129
+
130
+ const feeUnits = ethers.parseUnits("5.0", 6);
131
+ const ownerAddress = await this.signer.getAddress();
132
+ const currentAllowance = await this.usdc.allowance(ownerAddress, this.escrowAddress);
133
+
134
+ if (currentAllowance < feeUnits) {
135
+ const approveTx = await this.usdc.approve(this.escrowAddress, feeUnits);
136
+ await approveTx.wait(1);
137
+ }
138
+
139
+ const tx = await this.contract.paySubscription();
140
+ return await tx.wait(1);
141
+ }
142
+
143
+ /**
144
+ * Discover ranked APIs in the AgentSLA directory.
145
+ * @param {string} [category="all"]
146
+ */
147
+ async discover(category = "all") {
148
+ const query = category && category !== "all" ? `?category=${encodeURIComponent(category)}` : "";
149
+ const response = await fetch(`${this.oracleUrl}/v1/directory${query}`);
150
+ return await response.json();
151
+ }
152
+
153
+ /**
154
+ * Register a new API endpoint in the AgentSLA directory.
155
+ */
156
+ async register({ service_name, endpoint_url, category = "general", sla_threshold = 99.9, capabilities = [] }) {
157
+ const response = await fetch(`${this.oracleUrl}/v1/directory`, {
158
+ method: "POST",
159
+ headers: { "Content-Type": "application/json" },
160
+ body: JSON.stringify({ service_name, endpoint_url, category, sla_threshold, capabilities })
161
+ });
162
+ return await response.json();
163
+ }
164
+
165
+ /**
166
+ * Fetch recent EIP-712 signed oracle attestations.
167
+ */
168
+ async getAttestations() {
169
+ const response = await fetch(`${this.oracleUrl}/api/attestations`);
170
+ return await response.json();
171
+ }
172
+
173
+ /**
174
+ * Call a tool directly on the hosted Streamable HTTP MCP server (https://agentsla.io/mcp).
175
+ */
176
+ async callMcpTool(name, args = {}) {
177
+ const resolvedWallet = await this._resolveWallet(args.walletAddress);
178
+ const finalArgs = resolvedWallet && !args.walletAddress ? { ...args, walletAddress: resolvedWallet } : args;
179
+
180
+ const response = await fetch(this.mcpUrl, {
181
+ method: "POST",
182
+ headers: { "Content-Type": "application/json" },
183
+ body: JSON.stringify({
184
+ jsonrpc: "2.0",
185
+ id: Date.now(),
186
+ method: "tools/call",
187
+ params: { name, arguments: finalArgs }
188
+ })
189
+ });
190
+ return await response.json();
191
+ }
192
+ }
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "agentsla",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.0.1",
4
+ "description": "Official M2M SDK for the AgentSLA API Telemetry Directory, EIP-712 SLA Oracle, Streamable HTTP MCP Server, and Circle USDC Escrow Contract on Base Mainnet (8453).",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "node test.js"
9
+ },
10
+ "keywords": [
11
+ "ai",
12
+ "agents",
13
+ "m2m",
14
+ "mcp",
15
+ "web3",
16
+ "sla",
17
+ "oracle",
18
+ "usdc",
19
+ "eip-712",
20
+ "base-mainnet"
21
+ ],
22
+ "author": "AgentSLA (https://agentsla.io)",
23
+ "license": "MIT",
24
+ "dependencies": {
25
+ "ethers": "^6.7.0"
26
+ }
27
+ }
package/test.js ADDED
@@ -0,0 +1,32 @@
1
+ import { AgentSLA } from "./index.js";
2
+
3
+ async function main() {
4
+ const unpaidClient = new AgentSLA();
5
+
6
+ console.log("=== 1. Testing Free AgentSLA Directory Discovery ===");
7
+ const dir = await unpaidClient.discover("ai-models");
8
+ console.log(`Discovered ${dir.count} AI Model APIs (Escrow Contract: ${dir.escrowContract})`);
9
+
10
+ console.log("\n=== 2. Testing Unpaid Call to /v1/verify (Verifying 402 Paywall) ===");
11
+ try {
12
+ await unpaidClient.verify("https://mainnet.base.org");
13
+ } catch (err) {
14
+ console.log(`Blocked with HTTP ${err.status}: ${err.message}`);
15
+ }
16
+
17
+ console.log("\n=== 3. Testing Authenticated Wallet Call to /v1/verify ===");
18
+ const paidClient = new AgentSLA({
19
+ walletAddress: "0x9A0d2dD2402991eC8095fE2Be0d96A59d9DCF13C"
20
+ });
21
+ const verification = await paidClient.verify("https://mainnet.base.org");
22
+ console.log("Latency (ms) :", verification.latencyMs);
23
+ console.log("SLA Met :", verification.slaMet);
24
+ console.log("EIP-712 Signature:", verification.eip712Signature);
25
+ console.log("Oracle Signer :", verification.oracleSigner);
26
+
27
+ console.log("\n=== 4. Testing Hosted Streamable HTTP MCP Server (https://agentsla.io/mcp) ===");
28
+ const mcpRes = await paidClient.callMcpTool("get_usdc_escrow_contract", {});
29
+ console.log("MCP Tool Output :", mcpRes.result.content[0].text);
30
+ }
31
+
32
+ main().catch(console.error);