m2m-market 2.1.0 → 2.3.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 +33 -0
- package/adapters.js +5 -5
- package/index.d.ts +8 -1
- package/index.js +44 -1
- package/package.json +1 -1
- package/protocols.js +1 -1
package/README.md
CHANGED
|
@@ -52,6 +52,21 @@ const result = await client.executeTask({
|
|
|
52
52
|
console.log(result.response);
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
+
## Ask in plain language
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
const result = await client.ask("I need weather for Nairobi today");
|
|
59
|
+
console.log(result.response);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
M2M works out the capability, reads the inputs from the text (here a location
|
|
63
|
+
and a date), maps them onto each Provider's own field names, picks a validated
|
|
64
|
+
Provider and applies the dashboard spending policy. No payload or policy is
|
|
65
|
+
needed. If a required input is missing from the text, the request fails with
|
|
66
|
+
`intent_input_missing` before anything is reserved; add it to the text or pass
|
|
67
|
+
`payload`. `client.discover({ intent })` previews what would be sent
|
|
68
|
+
(`resolved.input` and each candidate's `provider_input`) without spending.
|
|
69
|
+
|
|
55
70
|
## Ask for an outcome
|
|
56
71
|
|
|
57
72
|
```js
|
|
@@ -84,6 +99,24 @@ try {
|
|
|
84
99
|
console.log(await M2MConnectionClient.fromEnvironment().testConnection());
|
|
85
100
|
```
|
|
86
101
|
|
|
102
|
+
## Buy an x402 resource
|
|
103
|
+
|
|
104
|
+
M2M can buy resources listed in the x402 Bazaar for your Agent. M2M pays the
|
|
105
|
+
seller in USDC on Base and charges your Agent's M2M balance, within the limits
|
|
106
|
+
M2M sets for your account. Search matches whole words.
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
const found = await client.searchExternal("supported chains rpcs explorers", { limit: 10 });
|
|
110
|
+
const listing = found.listings[0]; // candidate_id, name, price_usdc_base_units, resource_url
|
|
111
|
+
|
|
112
|
+
const result = await client.purchaseExternal(listing.candidate_id);
|
|
113
|
+
console.log(result.purchase.status, result.purchase.response);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Keep `result.idempotency_key`. If the result is `pending`, call
|
|
117
|
+
`purchaseExternal(candidateId, { idempotencyKey })` again with the same key
|
|
118
|
+
to get the outcome. The same key is never paid twice.
|
|
119
|
+
|
|
87
120
|
## OpenAI-compatible tool
|
|
88
121
|
|
|
89
122
|
```bash
|
package/adapters.js
CHANGED
|
@@ -19,7 +19,7 @@ export function openAIToolDefinition() {
|
|
|
19
19
|
},
|
|
20
20
|
intent: {
|
|
21
21
|
type: "string",
|
|
22
|
-
description: "Plain-language outcome needed, such as '
|
|
22
|
+
description: "Plain-language outcome needed, such as 'I need weather for Nairobi today'. M2M reads the inputs from it.",
|
|
23
23
|
},
|
|
24
24
|
required_capabilities: {
|
|
25
25
|
type: "array",
|
|
@@ -39,7 +39,7 @@ export function openAIToolDefinition() {
|
|
|
39
39
|
},
|
|
40
40
|
payload: {
|
|
41
41
|
type: "object",
|
|
42
|
-
description: "JSON input
|
|
42
|
+
description: "Optional JSON input. Omit it to let M2M read the inputs from the intent.",
|
|
43
43
|
additionalProperties: true,
|
|
44
44
|
},
|
|
45
45
|
maximum_price_m2m: {
|
|
@@ -53,7 +53,7 @@ export function openAIToolDefinition() {
|
|
|
53
53
|
description: "Optional stricter minimum Provider reputation.",
|
|
54
54
|
},
|
|
55
55
|
},
|
|
56
|
-
required: ["
|
|
56
|
+
required: ["intent"],
|
|
57
57
|
additionalProperties: false,
|
|
58
58
|
},
|
|
59
59
|
},
|
|
@@ -134,8 +134,8 @@ export class M2MToolAdapter {
|
|
|
134
134
|
description: M2M_TOOL_DESCRIPTION,
|
|
135
135
|
schema: z.object({
|
|
136
136
|
category: z.string().min(1).optional(),
|
|
137
|
-
intent: z.string().min(1)
|
|
138
|
-
payload: z.object({}).catchall(z.unknown()),
|
|
137
|
+
intent: z.string().min(1),
|
|
138
|
+
payload: z.object({}).catchall(z.unknown()).optional(),
|
|
139
139
|
required_capabilities: z.array(z.string()).optional(),
|
|
140
140
|
expect: z.array(z.string()).optional(),
|
|
141
141
|
maximum_attempts: z.number().int().min(1).max(5).optional(),
|
package/index.d.ts
CHANGED
|
@@ -27,7 +27,8 @@ export interface SubmitOptions {
|
|
|
27
27
|
/** Provide category, intent, or both. */
|
|
28
28
|
category?: string;
|
|
29
29
|
intent?: string;
|
|
30
|
-
|
|
30
|
+
/** Optional when an intent is given: M2M reads the inputs from the text. */
|
|
31
|
+
payload?: JsonObject;
|
|
31
32
|
requiredCapabilities?: string[];
|
|
32
33
|
integrationTypes?: string[];
|
|
33
34
|
/** Output fields the result must contain. */
|
|
@@ -69,6 +70,10 @@ export class M2MConnectionClient {
|
|
|
69
70
|
submit(options: SubmitOptions): Promise<JsonObject>;
|
|
70
71
|
/** Preview matching services without reserving funds or invoking a Provider. */
|
|
71
72
|
discover(options?: DiscoverOptions): Promise<JsonObject>;
|
|
73
|
+
/** Buy one external x402 Bazaar resource for this Agent. Reuse idempotencyKey to fetch a pending result. */
|
|
74
|
+
/** Find x402 Bazaar resources M2M can buy for this Agent (read-only). */
|
|
75
|
+
searchExternal(intent: string, options?: { limit?: number; cursor?: string }): Promise<JsonObject>;
|
|
76
|
+
purchaseExternal(candidateId: string, options?: { idempotencyKey?: string }): Promise<JsonObject>;
|
|
72
77
|
/** Evidence only; does not change settlement, reputation or ranking. */
|
|
73
78
|
submitAcceptance(queryId: string, options: { acceptanceId: string; accepted: boolean }): Promise<JsonObject>;
|
|
74
79
|
getQueryStatus(queryId: string): Promise<JsonObject>;
|
|
@@ -80,6 +85,8 @@ export class M2MConnectionClient {
|
|
|
80
85
|
purchase(options: ExecuteTaskOptions): Promise<JsonObject>;
|
|
81
86
|
/** @deprecated Use purchase. */
|
|
82
87
|
executeTask(options: ExecuteTaskOptions): Promise<JsonObject>;
|
|
88
|
+
/** Buy an outcome from a plain-language request, e.g. "I need weather for Nairobi today". */
|
|
89
|
+
ask(intent: string, options?: Omit<ExecuteTaskOptions, "intent">): Promise<JsonObject>;
|
|
83
90
|
}
|
|
84
91
|
|
|
85
92
|
export * from "./procurement.js";
|
package/index.js
CHANGED
|
@@ -107,7 +107,15 @@ export class M2MConnectionClient {
|
|
|
107
107
|
if (!String(category || "").trim() && !String(intent || "").trim()) {
|
|
108
108
|
throw new TypeError("A service category or an intent is required");
|
|
109
109
|
}
|
|
110
|
-
|
|
110
|
+
// Payload is optional with an intent: M2M reads standard inputs from the
|
|
111
|
+
// text and maps them onto each Provider's own field names.
|
|
112
|
+
if (payload === undefined || payload === null) {
|
|
113
|
+
if (!String(intent || "").trim()) {
|
|
114
|
+
throw new TypeError("A payload is required when no intent is given");
|
|
115
|
+
}
|
|
116
|
+
payload = {};
|
|
117
|
+
}
|
|
118
|
+
if (typeof payload !== "object" || Array.isArray(payload)) {
|
|
111
119
|
throw new TypeError("Service payload must be an object");
|
|
112
120
|
}
|
|
113
121
|
const body = {
|
|
@@ -163,6 +171,31 @@ export class M2MConnectionClient {
|
|
|
163
171
|
return result.discovery;
|
|
164
172
|
}
|
|
165
173
|
|
|
174
|
+
/**
|
|
175
|
+
* Buy one external x402 resource (an x402 Bazaar discovery candidate) for this Agent.
|
|
176
|
+
* M2M pays the seller in USDC on Base and charges the Agent's M2M balance within M2M's limits.
|
|
177
|
+
* Keep the returned idempotencyKey: if the result is pending, call again with the same key.
|
|
178
|
+
* The same key is never paid twice.
|
|
179
|
+
*/
|
|
180
|
+
/** Find x402 Bazaar resources M2M can buy for this Agent (read-only). Search matches whole words. */
|
|
181
|
+
searchExternal(intent, { limit = 10, cursor } = {}) {
|
|
182
|
+
if (!String(intent || "").trim()) throw new Error("intent is required");
|
|
183
|
+
const body = { intent: String(intent).trim(), limit };
|
|
184
|
+
if (cursor) body.cursor = cursor;
|
|
185
|
+
return this.request("POST", "/connect/x402/search", body);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async purchaseExternal(candidateId, { idempotencyKey } = {}) {
|
|
189
|
+
const key = String(idempotencyKey || randomUUID());
|
|
190
|
+
if (key.length < 8 || key.length > 128) throw new Error("idempotencyKey must be 8-128 characters");
|
|
191
|
+
if (!String(candidateId || "").trim()) throw new Error("candidateId is required");
|
|
192
|
+
const result = await this.request("POST", "/connect/x402/purchase", {
|
|
193
|
+
candidate_id: String(candidateId).trim(),
|
|
194
|
+
idempotency_key: key,
|
|
195
|
+
});
|
|
196
|
+
return { idempotency_key: key, ...result };
|
|
197
|
+
}
|
|
198
|
+
|
|
166
199
|
/** Evidence only: does not change settlement, reputation or ranking. */
|
|
167
200
|
submitAcceptance(queryId, { acceptanceId, accepted } = {}) {
|
|
168
201
|
if (typeof accepted !== "boolean") throw new TypeError("accepted must be a boolean");
|
|
@@ -200,6 +233,16 @@ export class M2MConnectionClient {
|
|
|
200
233
|
/** @deprecated Use purchase; Connect failover semantics are unchanged. */
|
|
201
234
|
executeTask(options) { return this.purchase(options); }
|
|
202
235
|
|
|
236
|
+
/**
|
|
237
|
+
* Buy an outcome from a plain-language request, for example
|
|
238
|
+
* `ask("I need weather for Nairobi today")`. M2M resolves the capability,
|
|
239
|
+
* reads the inputs, picks a validated Provider and applies the dashboard policy.
|
|
240
|
+
*/
|
|
241
|
+
ask(intent, options = {}) {
|
|
242
|
+
if (!String(intent || "").trim()) throw new TypeError("An intent is required");
|
|
243
|
+
return this.purchase({ ...options, intent: String(intent).trim() });
|
|
244
|
+
}
|
|
245
|
+
|
|
203
246
|
async purchase({
|
|
204
247
|
category,
|
|
205
248
|
intent,
|
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.3.0' });
|
|
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
|