m2m-market 2.3.0 → 2.4.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.
- package/README.md +45 -1
- package/index.d.ts +14 -1
- package/index.js +18 -3
- package/package.json +1 -1
- package/protocols.js +1 -1
package/README.md
CHANGED
|
@@ -115,7 +115,51 @@ console.log(result.purchase.status, result.purchase.response);
|
|
|
115
115
|
|
|
116
116
|
Keep `result.idempotency_key`. If the result is `pending`, call
|
|
117
117
|
`purchaseExternal(candidateId, { idempotencyKey })` again with the same key
|
|
118
|
-
to get the outcome. The same key is never paid twice.
|
|
118
|
+
to get the outcome. The same key is never paid twice. Reusing a key with a
|
|
119
|
+
different method, body or query is refused with `idempotency_key_conflict` (409).
|
|
120
|
+
|
|
121
|
+
### GET and POST listings
|
|
122
|
+
|
|
123
|
+
Each listing's `consumption` block has the `method` (GET or POST today) and an
|
|
124
|
+
`input` summary: `body_type`, `body_required`, `has_body_schema`,
|
|
125
|
+
`has_query_schema`, `schema_coverage`. M2M always uses the listing's method.
|
|
126
|
+
`method` is optional and must match it.
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
// GET with query parameters
|
|
130
|
+
await client.purchaseExternal(listing.candidate_id, { query: { chain: "base" } });
|
|
131
|
+
|
|
132
|
+
// POST with a JSON body
|
|
133
|
+
await client.purchaseExternal(listing.candidate_id, {
|
|
134
|
+
body: { text: "hello", lang: "en" },
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Put sensitive values in `body`, not `query`. Query values are stored with the
|
|
139
|
+
purchase record; only a hash of the body is stored. Calls with no `method`,
|
|
140
|
+
`body` or `query` send the same request as before.
|
|
141
|
+
|
|
142
|
+
### Refusals and rejected requests
|
|
143
|
+
|
|
144
|
+
Input is checked against the listing's schema before anything is signed or
|
|
145
|
+
charged. A refusal rejects the promise and nothing is charged. The reason code
|
|
146
|
+
is in the error payload: `candidate_not_consumable`, `method_unknown`,
|
|
147
|
+
`method_unsupported`, `method_mismatch`, `body_not_accepted`,
|
|
148
|
+
`body_type_unsupported`, `input_required`, `input_invalid` (with a `fields` list
|
|
149
|
+
of `path` and `code`), `input_too_large` (413) and `query_conflict`.
|
|
150
|
+
|
|
151
|
+
If the seller refuses the paid request, `purchase.status` is
|
|
152
|
+
`paid_request_rejected`, with `http_status`, `method` and
|
|
153
|
+
`rejection: { http_status, reason }`. The reason is one of `bad_request`,
|
|
154
|
+
`unauthorized`, `payment_required`, `forbidden`, `not_found`,
|
|
155
|
+
`method_not_allowed`, `payload_too_large`, `unsupported_media_type`,
|
|
156
|
+
`unprocessable_input`, `rate_limited`, `seller_error` or `other_non_2xx`. The
|
|
157
|
+
request is not retried. The purchase stays pending
|
|
158
|
+
(`charge_policy: "retained_pending_review"`) until M2M checks the payment on
|
|
159
|
+
the chain. It is then released and the charge is returned, and the status
|
|
160
|
+
becomes `reconciled_released`. If the payment did settle, the status becomes
|
|
161
|
+
`reconciled_settled` and the charge stands. The same applies to
|
|
162
|
+
`seller_error_refund_pending_review`.
|
|
119
163
|
|
|
120
164
|
## OpenAI-compatible tool
|
|
121
165
|
|
package/index.d.ts
CHANGED
|
@@ -46,6 +46,19 @@ export interface ExecuteTaskOptions extends Omit<SubmitOptions, "attemptNumber"
|
|
|
46
46
|
queryTimeoutMs?: number;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };
|
|
50
|
+
|
|
51
|
+
export interface PurchaseExternalOptions {
|
|
52
|
+
/** Reuse the same key to fetch a pending result. A key reused with a different request is refused (409). */
|
|
53
|
+
idempotencyKey?: string;
|
|
54
|
+
/** Must match the listing's method (`consumption.method`). Optional. */
|
|
55
|
+
method?: string;
|
|
56
|
+
/** JSON request body for POST listings. Omit (undefined) to send no body. Put sensitive values here, not in `query`. */
|
|
57
|
+
body?: JsonValue;
|
|
58
|
+
/** Query parameters merged into the listing URL. */
|
|
59
|
+
query?: Record<string, string | number | boolean | Array<string | number | boolean>>;
|
|
60
|
+
}
|
|
61
|
+
|
|
49
62
|
export interface DiscoverOptions {
|
|
50
63
|
category?: string;
|
|
51
64
|
intent?: string;
|
|
@@ -73,7 +86,7 @@ export class M2MConnectionClient {
|
|
|
73
86
|
/** Buy one external x402 Bazaar resource for this Agent. Reuse idempotencyKey to fetch a pending result. */
|
|
74
87
|
/** Find x402 Bazaar resources M2M can buy for this Agent (read-only). */
|
|
75
88
|
searchExternal(intent: string, options?: { limit?: number; cursor?: string }): Promise<JsonObject>;
|
|
76
|
-
purchaseExternal(candidateId: string, options?:
|
|
89
|
+
purchaseExternal(candidateId: string, options?: PurchaseExternalOptions): Promise<JsonObject>;
|
|
77
90
|
/** Evidence only; does not change settlement, reputation or ranking. */
|
|
78
91
|
submitAcceptance(queryId: string, options: { acceptanceId: string; accepted: boolean }): Promise<JsonObject>;
|
|
79
92
|
getQueryStatus(queryId: string): Promise<JsonObject>;
|
package/index.js
CHANGED
|
@@ -185,14 +185,29 @@ export class M2MConnectionClient {
|
|
|
185
185
|
return this.request("POST", "/connect/x402/search", body);
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
/**
|
|
189
|
+
* Optional request fields, sent only when supplied (a call without them sends the same JSON as before):
|
|
190
|
+
* method (must match the listing), body (any JSON value, undefined means no body) and query (object).
|
|
191
|
+
* Put sensitive values in the body, not the query. The server validates all of them before anything is paid.
|
|
192
|
+
*/
|
|
193
|
+
async purchaseExternal(candidateId, { idempotencyKey, method, body, query } = {}) {
|
|
189
194
|
const key = String(idempotencyKey || randomUUID());
|
|
190
195
|
if (key.length < 8 || key.length > 128) throw new Error("idempotencyKey must be 8-128 characters");
|
|
191
196
|
if (!String(candidateId || "").trim()) throw new Error("candidateId is required");
|
|
192
|
-
|
|
197
|
+
if (method !== undefined && method !== null && (typeof method !== "string" || !method.trim())) {
|
|
198
|
+
throw new TypeError("method must be a non-empty string");
|
|
199
|
+
}
|
|
200
|
+
if (query !== undefined && query !== null && (typeof query !== "object" || Array.isArray(query))) {
|
|
201
|
+
throw new TypeError("query must be an object");
|
|
202
|
+
}
|
|
203
|
+
const payload = {
|
|
193
204
|
candidate_id: String(candidateId).trim(),
|
|
194
205
|
idempotency_key: key,
|
|
195
|
-
}
|
|
206
|
+
};
|
|
207
|
+
if (method !== undefined && method !== null) payload.method = method.trim().toUpperCase();
|
|
208
|
+
if (body !== undefined) payload.body = body;
|
|
209
|
+
if (query !== undefined && query !== null) payload.query = query;
|
|
210
|
+
const result = await this.request("POST", "/connect/x402/purchase", payload);
|
|
196
211
|
return { idempotency_key: key, ...result };
|
|
197
212
|
}
|
|
198
213
|
|
package/package.json
CHANGED
package/protocols.js
CHANGED
|
@@ -32,7 +32,7 @@ export async function connectMcp({ accessToken, timeoutMs = 30000, fetchImpl = g
|
|
|
32
32
|
const transport = new sdk.StreamableHTTPClientTransport(new URL(MCP_RESOURCE), {
|
|
33
33
|
fetch: guardedFetch, reconnectionOptions: { maxRetries: 0, initialReconnectionDelay: 1000, maxReconnectionDelay: 1000, reconnectionDelayGrowFactor: 1 }
|
|
34
34
|
});
|
|
35
|
-
const client = new sdk.Client({ name: 'm2m-market', version: '2.
|
|
35
|
+
const client = new sdk.Client({ name: 'm2m-market', version: '2.4.1' });
|
|
36
36
|
try { await client.connect(transport); }
|
|
37
37
|
catch { await client.close().catch(() => {}); throw new M2MAPIError('mcp_connection_failed'); }
|
|
38
38
|
return client; // caller closes it in finally; no automatic paid call or replay
|