@ctxprotocol/sdk 0.6.0 → 0.8.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 +136 -23
- package/dist/client/index.cjs +211 -4
- package/dist/client/index.cjs.map +1 -1
- package/dist/client/index.d.cts +228 -11
- package/dist/client/index.d.ts +228 -11
- package/dist/client/index.js +211 -5
- package/dist/client/index.js.map +1 -1
- package/dist/index.cjs +211 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +211 -5
- package/dist/index.js.map +1 -1
- package/package.json +5 -2
package/dist/client/index.d.cts
CHANGED
|
@@ -96,6 +96,11 @@ interface ExecuteOptions {
|
|
|
96
96
|
toolName: string;
|
|
97
97
|
/** Arguments to pass to the tool */
|
|
98
98
|
args?: Record<string, unknown>;
|
|
99
|
+
/**
|
|
100
|
+
* Optional idempotency key (UUID recommended).
|
|
101
|
+
* Reuse the same key when retrying the same logical request.
|
|
102
|
+
*/
|
|
103
|
+
idempotencyKey?: string;
|
|
99
104
|
}
|
|
100
105
|
/**
|
|
101
106
|
* Successful execution response from the API
|
|
@@ -141,10 +146,127 @@ interface ExecutionResult<T = unknown> {
|
|
|
141
146
|
/** Execution duration in milliseconds */
|
|
142
147
|
durationMs: number;
|
|
143
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Options for the agentic query endpoint (pay-per-response).
|
|
151
|
+
*
|
|
152
|
+
* Unlike `execute()` which calls a single tool once, `query()` sends a
|
|
153
|
+
* natural-language question and lets the server handle tool discovery,
|
|
154
|
+
* multi-tool orchestration, self-healing retries, and AI synthesis.
|
|
155
|
+
* One flat fee covers up to 100 MCP skill calls per tool.
|
|
156
|
+
*/
|
|
157
|
+
interface QueryOptions {
|
|
158
|
+
/** The natural-language question to answer */
|
|
159
|
+
query: string;
|
|
160
|
+
/**
|
|
161
|
+
* Optional tool IDs to use. When omitted the server discovers tools
|
|
162
|
+
* automatically (Auto Mode). When provided, only these tools are used
|
|
163
|
+
* (Manual Mode).
|
|
164
|
+
*/
|
|
165
|
+
tools?: string[];
|
|
166
|
+
/**
|
|
167
|
+
* Optional model ID for query orchestration/synthesis.
|
|
168
|
+
* Supported IDs are published by the Context API.
|
|
169
|
+
*/
|
|
170
|
+
modelId?: string;
|
|
171
|
+
/**
|
|
172
|
+
* Include execution data inline in the query response.
|
|
173
|
+
* Useful for headless agents that need raw structured outputs.
|
|
174
|
+
*/
|
|
175
|
+
includeData?: boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Persist execution data to Vercel Blob and return a download URL.
|
|
178
|
+
* Useful for large payload workflows where inline JSON is not ideal.
|
|
179
|
+
*/
|
|
180
|
+
includeDataUrl?: boolean;
|
|
181
|
+
/**
|
|
182
|
+
* Optional idempotency key (UUID recommended).
|
|
183
|
+
* Reuse the same key when retrying the same logical request.
|
|
184
|
+
*/
|
|
185
|
+
idempotencyKey?: string;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Information about a tool that was used during a query response
|
|
189
|
+
*/
|
|
190
|
+
interface QueryToolUsage {
|
|
191
|
+
/** Tool ID */
|
|
192
|
+
id: string;
|
|
193
|
+
/** Tool name */
|
|
194
|
+
name: string;
|
|
195
|
+
/** Number of MCP skill calls made for this tool */
|
|
196
|
+
skillCalls: number;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Cost breakdown for a query response.
|
|
200
|
+
* All values are strings representing USD amounts.
|
|
201
|
+
*/
|
|
202
|
+
interface QueryCost {
|
|
203
|
+
/** AI model inference cost */
|
|
204
|
+
modelCostUsd: string;
|
|
205
|
+
/** Sum of all tool fees */
|
|
206
|
+
toolCostUsd: string;
|
|
207
|
+
/** Total cost (model + tools) */
|
|
208
|
+
totalCostUsd: string;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The resolved result of a pay-per-response query
|
|
212
|
+
*/
|
|
213
|
+
interface QueryResult {
|
|
214
|
+
/** The AI-synthesized response text */
|
|
215
|
+
response: string;
|
|
216
|
+
/** Tools that were used to answer the query */
|
|
217
|
+
toolsUsed: QueryToolUsage[];
|
|
218
|
+
/** Cost breakdown */
|
|
219
|
+
cost: QueryCost;
|
|
220
|
+
/** Total duration in milliseconds */
|
|
221
|
+
durationMs: number;
|
|
222
|
+
/** Optional execution data from tools (when includeData=true) */
|
|
223
|
+
data?: unknown;
|
|
224
|
+
/** Optional blob URL for persisted execution data (when includeDataUrl=true) */
|
|
225
|
+
dataUrl?: string;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Successful response from the /api/v1/query endpoint
|
|
229
|
+
*/
|
|
230
|
+
interface QueryApiSuccessResponse {
|
|
231
|
+
success: true;
|
|
232
|
+
response: string;
|
|
233
|
+
toolsUsed: QueryToolUsage[];
|
|
234
|
+
cost: QueryCost;
|
|
235
|
+
durationMs: number;
|
|
236
|
+
data?: unknown;
|
|
237
|
+
dataUrl?: string;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Raw API response from the query endpoint
|
|
241
|
+
*/
|
|
242
|
+
type QueryApiResponse = QueryApiSuccessResponse | ExecuteApiErrorResponse;
|
|
243
|
+
/** Emitted when a tool starts or changes execution status */
|
|
244
|
+
interface QueryStreamToolStatusEvent {
|
|
245
|
+
type: "tool-status";
|
|
246
|
+
tool: {
|
|
247
|
+
id: string;
|
|
248
|
+
name: string;
|
|
249
|
+
};
|
|
250
|
+
status: string;
|
|
251
|
+
}
|
|
252
|
+
/** Emitted for each chunk of the AI response text */
|
|
253
|
+
interface QueryStreamTextDeltaEvent {
|
|
254
|
+
type: "text-delta";
|
|
255
|
+
delta: string;
|
|
256
|
+
}
|
|
257
|
+
/** Emitted when the full response is complete */
|
|
258
|
+
interface QueryStreamDoneEvent {
|
|
259
|
+
type: "done";
|
|
260
|
+
result: QueryResult;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Union of all events emitted during a streaming query
|
|
264
|
+
*/
|
|
265
|
+
type QueryStreamEvent = QueryStreamToolStatusEvent | QueryStreamTextDeltaEvent | QueryStreamDoneEvent;
|
|
144
266
|
/**
|
|
145
267
|
* Specific error codes returned by the Context Protocol API
|
|
146
268
|
*/
|
|
147
|
-
type ContextErrorCode = "unauthorized" | "no_wallet" | "insufficient_allowance" | "payment_failed" | "execution_failed";
|
|
269
|
+
type ContextErrorCode = "unauthorized" | "no_wallet" | "insufficient_allowance" | "payment_failed" | "execution_failed" | "query_failed";
|
|
148
270
|
/**
|
|
149
271
|
* Error thrown by the Context Protocol client
|
|
150
272
|
*/
|
|
@@ -206,8 +328,8 @@ declare class Tools {
|
|
|
206
328
|
* @returns The execution result with the tool's output data
|
|
207
329
|
*
|
|
208
330
|
* @throws {ContextError} With code `no_wallet` if wallet not set up
|
|
209
|
-
* @throws {ContextError} With code `insufficient_allowance` if
|
|
210
|
-
* @throws {ContextError} With code `payment_failed` if
|
|
331
|
+
* @throws {ContextError} With code `insufficient_allowance` if spending cap not set
|
|
332
|
+
* @throws {ContextError} With code `payment_failed` if payment settlement fails
|
|
211
333
|
* @throws {ContextError} With code `execution_failed` if tool execution fails
|
|
212
334
|
*
|
|
213
335
|
* @example
|
|
@@ -230,6 +352,85 @@ declare class Tools {
|
|
|
230
352
|
execute<T = unknown>(options: ExecuteOptions): Promise<ExecutionResult<T>>;
|
|
231
353
|
}
|
|
232
354
|
|
|
355
|
+
/**
|
|
356
|
+
* Query resource for pay-per-response agentic queries.
|
|
357
|
+
*
|
|
358
|
+
* Unlike `tools.execute()` which calls a single tool once (pay-per-request),
|
|
359
|
+
* the Query resource sends a natural-language question and lets the server
|
|
360
|
+
* handle tool discovery, multi-tool orchestration, self-healing retries,
|
|
361
|
+
* completeness checks, and AI synthesis — all for one flat fee.
|
|
362
|
+
*
|
|
363
|
+
* This is the "prepared meal" vs "raw ingredients" distinction:
|
|
364
|
+
* - `tools.execute()` = raw data, full control, predictable cost
|
|
365
|
+
* - `query.run()` / `query.stream()` = curated intelligence, one payment
|
|
366
|
+
*/
|
|
367
|
+
declare class Query {
|
|
368
|
+
private client;
|
|
369
|
+
constructor(client: ContextClient);
|
|
370
|
+
/**
|
|
371
|
+
* Run an agentic query and wait for the full response.
|
|
372
|
+
*
|
|
373
|
+
* The server discovers relevant tools (or uses the ones you specify),
|
|
374
|
+
* executes the full agentic pipeline (up to 100 MCP calls per tool),
|
|
375
|
+
* and returns an AI-synthesized answer. Payment is settled after
|
|
376
|
+
* successful execution via deferred settlement.
|
|
377
|
+
*
|
|
378
|
+
* @param options - Query options or a plain string question
|
|
379
|
+
* @returns The complete query result with response text, tools used, and cost
|
|
380
|
+
*
|
|
381
|
+
* @throws {ContextError} With code `no_wallet` if wallet not set up
|
|
382
|
+
* @throws {ContextError} With code `insufficient_allowance` if spending cap not set
|
|
383
|
+
* @throws {ContextError} With code `payment_failed` if payment settlement fails
|
|
384
|
+
* @throws {ContextError} With code `execution_failed` if the agentic pipeline fails
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```typescript
|
|
388
|
+
* // Simple question — server discovers tools automatically
|
|
389
|
+
* const answer = await client.query.run("What are the top whale movements on Base?");
|
|
390
|
+
* console.log(answer.response); // AI-synthesized answer
|
|
391
|
+
* console.log(answer.toolsUsed); // Which tools were used
|
|
392
|
+
* console.log(answer.cost); // Cost breakdown
|
|
393
|
+
*
|
|
394
|
+
* // With specific tools (Manual Mode)
|
|
395
|
+
* const answer = await client.query.run({
|
|
396
|
+
* query: "Analyze whale activity",
|
|
397
|
+
* tools: ["tool-uuid-1", "tool-uuid-2"],
|
|
398
|
+
* });
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
run(options: QueryOptions | string): Promise<QueryResult>;
|
|
402
|
+
/**
|
|
403
|
+
* Run an agentic query with streaming. Returns an async iterable that
|
|
404
|
+
* yields events as the server processes the query in real-time.
|
|
405
|
+
*
|
|
406
|
+
* Event types:
|
|
407
|
+
* - `tool-status` — A tool started executing or changed status
|
|
408
|
+
* - `text-delta` — A chunk of the AI response text
|
|
409
|
+
* - `done` — The full response is complete (includes final `QueryResult`)
|
|
410
|
+
*
|
|
411
|
+
* @param options - Query options or a plain string question
|
|
412
|
+
* @returns An async iterable of stream events
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```typescript
|
|
416
|
+
* for await (const event of client.query.stream("What are the top whale movements?")) {
|
|
417
|
+
* switch (event.type) {
|
|
418
|
+
* case "tool-status":
|
|
419
|
+
* console.log(`Tool ${event.tool.name}: ${event.status}`);
|
|
420
|
+
* break;
|
|
421
|
+
* case "text-delta":
|
|
422
|
+
* process.stdout.write(event.delta);
|
|
423
|
+
* break;
|
|
424
|
+
* case "done":
|
|
425
|
+
* console.log("\nCost:", event.result.cost.totalCostUsd);
|
|
426
|
+
* break;
|
|
427
|
+
* }
|
|
428
|
+
* }
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
stream(options: QueryOptions | string): AsyncGenerator<QueryStreamEvent>;
|
|
432
|
+
}
|
|
433
|
+
|
|
233
434
|
/**
|
|
234
435
|
* The official TypeScript client for the Context Protocol.
|
|
235
436
|
*
|
|
@@ -243,15 +444,16 @@ declare class Tools {
|
|
|
243
444
|
* apiKey: "sk_live_..."
|
|
244
445
|
* });
|
|
245
446
|
*
|
|
246
|
-
* //
|
|
247
|
-
* const tools = await client.discovery.search("gas prices");
|
|
248
|
-
*
|
|
249
|
-
* // Execute a tool method
|
|
447
|
+
* // Pay-per-request: Execute a specific tool
|
|
250
448
|
* const result = await client.tools.execute({
|
|
251
|
-
* toolId:
|
|
252
|
-
* toolName:
|
|
449
|
+
* toolId: "tool-uuid",
|
|
450
|
+
* toolName: "get_gas_prices",
|
|
253
451
|
* args: { chainId: 1 }
|
|
254
452
|
* });
|
|
453
|
+
*
|
|
454
|
+
* // Pay-per-response: Ask a question, get a curated answer
|
|
455
|
+
* const answer = await client.query.run("What are the top whale movements on Base?");
|
|
456
|
+
* console.log(answer.response);
|
|
255
457
|
* ```
|
|
256
458
|
*/
|
|
257
459
|
declare class ContextClient {
|
|
@@ -263,9 +465,17 @@ declare class ContextClient {
|
|
|
263
465
|
*/
|
|
264
466
|
readonly discovery: Discovery;
|
|
265
467
|
/**
|
|
266
|
-
* Tools resource for executing tools
|
|
468
|
+
* Tools resource for executing tools (pay-per-request)
|
|
267
469
|
*/
|
|
268
470
|
readonly tools: Tools;
|
|
471
|
+
/**
|
|
472
|
+
* Query resource for agentic queries (pay-per-response).
|
|
473
|
+
*
|
|
474
|
+
* Unlike `tools.execute()` which calls a single tool once, `query` sends
|
|
475
|
+
* a natural-language question and lets the server handle tool discovery,
|
|
476
|
+
* multi-tool orchestration, self-healing, and AI synthesis — one flat fee.
|
|
477
|
+
*/
|
|
478
|
+
readonly query: Query;
|
|
269
479
|
/**
|
|
270
480
|
* Creates a new Context Protocol client
|
|
271
481
|
*
|
|
@@ -286,6 +496,13 @@ declare class ContextClient {
|
|
|
286
496
|
* @internal
|
|
287
497
|
*/
|
|
288
498
|
_fetch<T>(endpoint: string, options?: RequestInit): Promise<T>;
|
|
499
|
+
/**
|
|
500
|
+
* Internal method for making authenticated HTTP requests that returns
|
|
501
|
+
* the raw Response object. Used for streaming endpoints (SSE).
|
|
502
|
+
*
|
|
503
|
+
* @internal
|
|
504
|
+
*/
|
|
505
|
+
_fetchRaw(endpoint: string, options?: RequestInit): Promise<Response>;
|
|
289
506
|
}
|
|
290
507
|
|
|
291
|
-
export { ContextClient, type ContextClientOptions, ContextError, type ContextErrorCode, Discovery, type ExecuteApiErrorResponse, type ExecuteApiResponse, type ExecuteApiSuccessResponse, type ExecuteOptions, type ExecutionResult, type McpTool, type SearchOptions, type SearchResponse, type Tool, Tools };
|
|
508
|
+
export { ContextClient, type ContextClientOptions, ContextError, type ContextErrorCode, Discovery, type ExecuteApiErrorResponse, type ExecuteApiResponse, type ExecuteApiSuccessResponse, type ExecuteOptions, type ExecutionResult, type McpTool, Query, type QueryApiResponse, type QueryApiSuccessResponse, type QueryCost, type QueryOptions, type QueryResult, type QueryStreamDoneEvent, type QueryStreamEvent, type QueryStreamTextDeltaEvent, type QueryStreamToolStatusEvent, type QueryToolUsage, type SearchOptions, type SearchResponse, type Tool, Tools };
|
package/dist/client/index.d.ts
CHANGED
|
@@ -96,6 +96,11 @@ interface ExecuteOptions {
|
|
|
96
96
|
toolName: string;
|
|
97
97
|
/** Arguments to pass to the tool */
|
|
98
98
|
args?: Record<string, unknown>;
|
|
99
|
+
/**
|
|
100
|
+
* Optional idempotency key (UUID recommended).
|
|
101
|
+
* Reuse the same key when retrying the same logical request.
|
|
102
|
+
*/
|
|
103
|
+
idempotencyKey?: string;
|
|
99
104
|
}
|
|
100
105
|
/**
|
|
101
106
|
* Successful execution response from the API
|
|
@@ -141,10 +146,127 @@ interface ExecutionResult<T = unknown> {
|
|
|
141
146
|
/** Execution duration in milliseconds */
|
|
142
147
|
durationMs: number;
|
|
143
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Options for the agentic query endpoint (pay-per-response).
|
|
151
|
+
*
|
|
152
|
+
* Unlike `execute()` which calls a single tool once, `query()` sends a
|
|
153
|
+
* natural-language question and lets the server handle tool discovery,
|
|
154
|
+
* multi-tool orchestration, self-healing retries, and AI synthesis.
|
|
155
|
+
* One flat fee covers up to 100 MCP skill calls per tool.
|
|
156
|
+
*/
|
|
157
|
+
interface QueryOptions {
|
|
158
|
+
/** The natural-language question to answer */
|
|
159
|
+
query: string;
|
|
160
|
+
/**
|
|
161
|
+
* Optional tool IDs to use. When omitted the server discovers tools
|
|
162
|
+
* automatically (Auto Mode). When provided, only these tools are used
|
|
163
|
+
* (Manual Mode).
|
|
164
|
+
*/
|
|
165
|
+
tools?: string[];
|
|
166
|
+
/**
|
|
167
|
+
* Optional model ID for query orchestration/synthesis.
|
|
168
|
+
* Supported IDs are published by the Context API.
|
|
169
|
+
*/
|
|
170
|
+
modelId?: string;
|
|
171
|
+
/**
|
|
172
|
+
* Include execution data inline in the query response.
|
|
173
|
+
* Useful for headless agents that need raw structured outputs.
|
|
174
|
+
*/
|
|
175
|
+
includeData?: boolean;
|
|
176
|
+
/**
|
|
177
|
+
* Persist execution data to Vercel Blob and return a download URL.
|
|
178
|
+
* Useful for large payload workflows where inline JSON is not ideal.
|
|
179
|
+
*/
|
|
180
|
+
includeDataUrl?: boolean;
|
|
181
|
+
/**
|
|
182
|
+
* Optional idempotency key (UUID recommended).
|
|
183
|
+
* Reuse the same key when retrying the same logical request.
|
|
184
|
+
*/
|
|
185
|
+
idempotencyKey?: string;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Information about a tool that was used during a query response
|
|
189
|
+
*/
|
|
190
|
+
interface QueryToolUsage {
|
|
191
|
+
/** Tool ID */
|
|
192
|
+
id: string;
|
|
193
|
+
/** Tool name */
|
|
194
|
+
name: string;
|
|
195
|
+
/** Number of MCP skill calls made for this tool */
|
|
196
|
+
skillCalls: number;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Cost breakdown for a query response.
|
|
200
|
+
* All values are strings representing USD amounts.
|
|
201
|
+
*/
|
|
202
|
+
interface QueryCost {
|
|
203
|
+
/** AI model inference cost */
|
|
204
|
+
modelCostUsd: string;
|
|
205
|
+
/** Sum of all tool fees */
|
|
206
|
+
toolCostUsd: string;
|
|
207
|
+
/** Total cost (model + tools) */
|
|
208
|
+
totalCostUsd: string;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The resolved result of a pay-per-response query
|
|
212
|
+
*/
|
|
213
|
+
interface QueryResult {
|
|
214
|
+
/** The AI-synthesized response text */
|
|
215
|
+
response: string;
|
|
216
|
+
/** Tools that were used to answer the query */
|
|
217
|
+
toolsUsed: QueryToolUsage[];
|
|
218
|
+
/** Cost breakdown */
|
|
219
|
+
cost: QueryCost;
|
|
220
|
+
/** Total duration in milliseconds */
|
|
221
|
+
durationMs: number;
|
|
222
|
+
/** Optional execution data from tools (when includeData=true) */
|
|
223
|
+
data?: unknown;
|
|
224
|
+
/** Optional blob URL for persisted execution data (when includeDataUrl=true) */
|
|
225
|
+
dataUrl?: string;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Successful response from the /api/v1/query endpoint
|
|
229
|
+
*/
|
|
230
|
+
interface QueryApiSuccessResponse {
|
|
231
|
+
success: true;
|
|
232
|
+
response: string;
|
|
233
|
+
toolsUsed: QueryToolUsage[];
|
|
234
|
+
cost: QueryCost;
|
|
235
|
+
durationMs: number;
|
|
236
|
+
data?: unknown;
|
|
237
|
+
dataUrl?: string;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Raw API response from the query endpoint
|
|
241
|
+
*/
|
|
242
|
+
type QueryApiResponse = QueryApiSuccessResponse | ExecuteApiErrorResponse;
|
|
243
|
+
/** Emitted when a tool starts or changes execution status */
|
|
244
|
+
interface QueryStreamToolStatusEvent {
|
|
245
|
+
type: "tool-status";
|
|
246
|
+
tool: {
|
|
247
|
+
id: string;
|
|
248
|
+
name: string;
|
|
249
|
+
};
|
|
250
|
+
status: string;
|
|
251
|
+
}
|
|
252
|
+
/** Emitted for each chunk of the AI response text */
|
|
253
|
+
interface QueryStreamTextDeltaEvent {
|
|
254
|
+
type: "text-delta";
|
|
255
|
+
delta: string;
|
|
256
|
+
}
|
|
257
|
+
/** Emitted when the full response is complete */
|
|
258
|
+
interface QueryStreamDoneEvent {
|
|
259
|
+
type: "done";
|
|
260
|
+
result: QueryResult;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Union of all events emitted during a streaming query
|
|
264
|
+
*/
|
|
265
|
+
type QueryStreamEvent = QueryStreamToolStatusEvent | QueryStreamTextDeltaEvent | QueryStreamDoneEvent;
|
|
144
266
|
/**
|
|
145
267
|
* Specific error codes returned by the Context Protocol API
|
|
146
268
|
*/
|
|
147
|
-
type ContextErrorCode = "unauthorized" | "no_wallet" | "insufficient_allowance" | "payment_failed" | "execution_failed";
|
|
269
|
+
type ContextErrorCode = "unauthorized" | "no_wallet" | "insufficient_allowance" | "payment_failed" | "execution_failed" | "query_failed";
|
|
148
270
|
/**
|
|
149
271
|
* Error thrown by the Context Protocol client
|
|
150
272
|
*/
|
|
@@ -206,8 +328,8 @@ declare class Tools {
|
|
|
206
328
|
* @returns The execution result with the tool's output data
|
|
207
329
|
*
|
|
208
330
|
* @throws {ContextError} With code `no_wallet` if wallet not set up
|
|
209
|
-
* @throws {ContextError} With code `insufficient_allowance` if
|
|
210
|
-
* @throws {ContextError} With code `payment_failed` if
|
|
331
|
+
* @throws {ContextError} With code `insufficient_allowance` if spending cap not set
|
|
332
|
+
* @throws {ContextError} With code `payment_failed` if payment settlement fails
|
|
211
333
|
* @throws {ContextError} With code `execution_failed` if tool execution fails
|
|
212
334
|
*
|
|
213
335
|
* @example
|
|
@@ -230,6 +352,85 @@ declare class Tools {
|
|
|
230
352
|
execute<T = unknown>(options: ExecuteOptions): Promise<ExecutionResult<T>>;
|
|
231
353
|
}
|
|
232
354
|
|
|
355
|
+
/**
|
|
356
|
+
* Query resource for pay-per-response agentic queries.
|
|
357
|
+
*
|
|
358
|
+
* Unlike `tools.execute()` which calls a single tool once (pay-per-request),
|
|
359
|
+
* the Query resource sends a natural-language question and lets the server
|
|
360
|
+
* handle tool discovery, multi-tool orchestration, self-healing retries,
|
|
361
|
+
* completeness checks, and AI synthesis — all for one flat fee.
|
|
362
|
+
*
|
|
363
|
+
* This is the "prepared meal" vs "raw ingredients" distinction:
|
|
364
|
+
* - `tools.execute()` = raw data, full control, predictable cost
|
|
365
|
+
* - `query.run()` / `query.stream()` = curated intelligence, one payment
|
|
366
|
+
*/
|
|
367
|
+
declare class Query {
|
|
368
|
+
private client;
|
|
369
|
+
constructor(client: ContextClient);
|
|
370
|
+
/**
|
|
371
|
+
* Run an agentic query and wait for the full response.
|
|
372
|
+
*
|
|
373
|
+
* The server discovers relevant tools (or uses the ones you specify),
|
|
374
|
+
* executes the full agentic pipeline (up to 100 MCP calls per tool),
|
|
375
|
+
* and returns an AI-synthesized answer. Payment is settled after
|
|
376
|
+
* successful execution via deferred settlement.
|
|
377
|
+
*
|
|
378
|
+
* @param options - Query options or a plain string question
|
|
379
|
+
* @returns The complete query result with response text, tools used, and cost
|
|
380
|
+
*
|
|
381
|
+
* @throws {ContextError} With code `no_wallet` if wallet not set up
|
|
382
|
+
* @throws {ContextError} With code `insufficient_allowance` if spending cap not set
|
|
383
|
+
* @throws {ContextError} With code `payment_failed` if payment settlement fails
|
|
384
|
+
* @throws {ContextError} With code `execution_failed` if the agentic pipeline fails
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* ```typescript
|
|
388
|
+
* // Simple question — server discovers tools automatically
|
|
389
|
+
* const answer = await client.query.run("What are the top whale movements on Base?");
|
|
390
|
+
* console.log(answer.response); // AI-synthesized answer
|
|
391
|
+
* console.log(answer.toolsUsed); // Which tools were used
|
|
392
|
+
* console.log(answer.cost); // Cost breakdown
|
|
393
|
+
*
|
|
394
|
+
* // With specific tools (Manual Mode)
|
|
395
|
+
* const answer = await client.query.run({
|
|
396
|
+
* query: "Analyze whale activity",
|
|
397
|
+
* tools: ["tool-uuid-1", "tool-uuid-2"],
|
|
398
|
+
* });
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
run(options: QueryOptions | string): Promise<QueryResult>;
|
|
402
|
+
/**
|
|
403
|
+
* Run an agentic query with streaming. Returns an async iterable that
|
|
404
|
+
* yields events as the server processes the query in real-time.
|
|
405
|
+
*
|
|
406
|
+
* Event types:
|
|
407
|
+
* - `tool-status` — A tool started executing or changed status
|
|
408
|
+
* - `text-delta` — A chunk of the AI response text
|
|
409
|
+
* - `done` — The full response is complete (includes final `QueryResult`)
|
|
410
|
+
*
|
|
411
|
+
* @param options - Query options or a plain string question
|
|
412
|
+
* @returns An async iterable of stream events
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```typescript
|
|
416
|
+
* for await (const event of client.query.stream("What are the top whale movements?")) {
|
|
417
|
+
* switch (event.type) {
|
|
418
|
+
* case "tool-status":
|
|
419
|
+
* console.log(`Tool ${event.tool.name}: ${event.status}`);
|
|
420
|
+
* break;
|
|
421
|
+
* case "text-delta":
|
|
422
|
+
* process.stdout.write(event.delta);
|
|
423
|
+
* break;
|
|
424
|
+
* case "done":
|
|
425
|
+
* console.log("\nCost:", event.result.cost.totalCostUsd);
|
|
426
|
+
* break;
|
|
427
|
+
* }
|
|
428
|
+
* }
|
|
429
|
+
* ```
|
|
430
|
+
*/
|
|
431
|
+
stream(options: QueryOptions | string): AsyncGenerator<QueryStreamEvent>;
|
|
432
|
+
}
|
|
433
|
+
|
|
233
434
|
/**
|
|
234
435
|
* The official TypeScript client for the Context Protocol.
|
|
235
436
|
*
|
|
@@ -243,15 +444,16 @@ declare class Tools {
|
|
|
243
444
|
* apiKey: "sk_live_..."
|
|
244
445
|
* });
|
|
245
446
|
*
|
|
246
|
-
* //
|
|
247
|
-
* const tools = await client.discovery.search("gas prices");
|
|
248
|
-
*
|
|
249
|
-
* // Execute a tool method
|
|
447
|
+
* // Pay-per-request: Execute a specific tool
|
|
250
448
|
* const result = await client.tools.execute({
|
|
251
|
-
* toolId:
|
|
252
|
-
* toolName:
|
|
449
|
+
* toolId: "tool-uuid",
|
|
450
|
+
* toolName: "get_gas_prices",
|
|
253
451
|
* args: { chainId: 1 }
|
|
254
452
|
* });
|
|
453
|
+
*
|
|
454
|
+
* // Pay-per-response: Ask a question, get a curated answer
|
|
455
|
+
* const answer = await client.query.run("What are the top whale movements on Base?");
|
|
456
|
+
* console.log(answer.response);
|
|
255
457
|
* ```
|
|
256
458
|
*/
|
|
257
459
|
declare class ContextClient {
|
|
@@ -263,9 +465,17 @@ declare class ContextClient {
|
|
|
263
465
|
*/
|
|
264
466
|
readonly discovery: Discovery;
|
|
265
467
|
/**
|
|
266
|
-
* Tools resource for executing tools
|
|
468
|
+
* Tools resource for executing tools (pay-per-request)
|
|
267
469
|
*/
|
|
268
470
|
readonly tools: Tools;
|
|
471
|
+
/**
|
|
472
|
+
* Query resource for agentic queries (pay-per-response).
|
|
473
|
+
*
|
|
474
|
+
* Unlike `tools.execute()` which calls a single tool once, `query` sends
|
|
475
|
+
* a natural-language question and lets the server handle tool discovery,
|
|
476
|
+
* multi-tool orchestration, self-healing, and AI synthesis — one flat fee.
|
|
477
|
+
*/
|
|
478
|
+
readonly query: Query;
|
|
269
479
|
/**
|
|
270
480
|
* Creates a new Context Protocol client
|
|
271
481
|
*
|
|
@@ -286,6 +496,13 @@ declare class ContextClient {
|
|
|
286
496
|
* @internal
|
|
287
497
|
*/
|
|
288
498
|
_fetch<T>(endpoint: string, options?: RequestInit): Promise<T>;
|
|
499
|
+
/**
|
|
500
|
+
* Internal method for making authenticated HTTP requests that returns
|
|
501
|
+
* the raw Response object. Used for streaming endpoints (SSE).
|
|
502
|
+
*
|
|
503
|
+
* @internal
|
|
504
|
+
*/
|
|
505
|
+
_fetchRaw(endpoint: string, options?: RequestInit): Promise<Response>;
|
|
289
506
|
}
|
|
290
507
|
|
|
291
|
-
export { ContextClient, type ContextClientOptions, ContextError, type ContextErrorCode, Discovery, type ExecuteApiErrorResponse, type ExecuteApiResponse, type ExecuteApiSuccessResponse, type ExecuteOptions, type ExecutionResult, type McpTool, type SearchOptions, type SearchResponse, type Tool, Tools };
|
|
508
|
+
export { ContextClient, type ContextClientOptions, ContextError, type ContextErrorCode, Discovery, type ExecuteApiErrorResponse, type ExecuteApiResponse, type ExecuteApiSuccessResponse, type ExecuteOptions, type ExecutionResult, type McpTool, Query, type QueryApiResponse, type QueryApiSuccessResponse, type QueryCost, type QueryOptions, type QueryResult, type QueryStreamDoneEvent, type QueryStreamEvent, type QueryStreamTextDeltaEvent, type QueryStreamToolStatusEvent, type QueryToolUsage, type SearchOptions, type SearchResponse, type Tool, Tools };
|