@ctxprotocol/sdk 0.9.0 → 0.11.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/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { ContextClient, ContextClientOptions, ContextError, ContextErrorCode, Discovery, ExecuteApiErrorResponse, ExecuteApiResponse, ExecuteApiSuccessResponse, ExecuteOptions, ExecuteSessionApiResponse, ExecuteSessionApiSuccessResponse, ExecuteSessionResult, ExecuteSessionSpend, ExecuteSessionStartOptions, ExecuteSessionStatus, ExecutionResult, McpTool, McpToolMeta, McpToolRateLimitHints, Query, QueryApiResponse, QueryApiSuccessResponse, QueryCost, QueryDeepMode, QueryDeveloperTrace, QueryDeveloperTraceLoopInfo, QueryDeveloperTraceStep, QueryDeveloperTraceSummary, QueryDeveloperTraceToolRef, QueryOptions, QueryResult, QueryStreamDeveloperTraceEvent, QueryStreamDoneEvent, QueryStreamErrorEvent, QueryStreamEvent, QueryStreamTextDeltaEvent, QueryStreamToolStatusEvent, QueryToolUsage, SearchOptions, SearchResponse, Tool, Tools } from './client/index.js';
1
+ export { ContextClient, Developer, Discovery, Query, Tools } from './client/index.js';
2
+ export { D as ContextClientOptions, B as ContextError, al as ContextErrorCode, U as ExecuteApiErrorResponse, V as ExecuteApiResponse, P as ExecuteApiSuccessResponse, I as ExecuteOptions, X as ExecuteSessionApiResponse, W as ExecuteSessionApiSuccessResponse, N as ExecuteSessionResult, L as ExecuteSessionSpend, J as ExecuteSessionStartOptions, K as ExecuteSessionStatus, O as ExecutionResult, M as McpTool, E as McpToolMeta, F as McpToolRateLimitHints, ac as QueryApiResponse, ab as QueryApiSuccessResponse, Z as QueryAttemptForkReason, _ as QueryAttemptReference, a5 as QueryCompletenessRepairEvent, a4 as QueryCost, Y as QueryDeepMode, Q as QueryDeveloperTrace, a6 as QueryDeveloperTraceDiagnostics, aa as QueryDeveloperTraceLoopInfo, a8 as QueryDeveloperTraceStep, a7 as QueryDeveloperTraceSummary, a9 as QueryDeveloperTraceToolRef, $ as QueryForkReference, a0 as QueryOptions, a1 as QueryResult, a2 as QuerySessionState, ag as QueryStreamDeveloperTraceEvent, ah as QueryStreamDoneEvent, ai as QueryStreamErrorEvent, ad as QueryStreamEvent, af as QueryStreamTextDeltaEvent, ae as QueryStreamToolStatusEvent, a3 as QueryToolUsage, H as SearchOptions, G as SearchResponse, T as Tool, aj as UpdateToolOptions, ak as UpdateToolResult } from './types-DRbq-FA6.js';
2
3
  import { JWTPayload } from 'jose';
3
4
 
4
5
  /**
@@ -461,6 +462,10 @@ declare function createContextMiddleware(options?: CreateContextMiddlewareOption
461
462
  * of their MCP response. The Context platform intercepts these and
462
463
  * presents the appropriate UI to the user.
463
464
  *
465
+ * These helpers define the contributor-side MCP response contract.
466
+ * They do not create a headless Query API approval/resume flow; completing
467
+ * a handshake currently requires the Context chat app UI.
468
+ *
464
469
  * ## Action Types
465
470
  *
466
471
  * - `signature_request`: For EIP-712 signatures (Hyperliquid, Polymarket, etc.)
@@ -661,6 +666,9 @@ declare function createAuthRequired(params: Omit<AuthRequired, "_action">): Auth
661
666
  *
662
667
  * MCP tools should return handshake actions in `_meta.handshakeAction` to prevent
663
668
  * the MCP SDK from stripping unknown fields.
669
+ * Headless Query clients may observe raw internal handshake markers in
670
+ * execution data, but they cannot submit approval results through the
671
+ * Query API today.
664
672
  *
665
673
  * @example
666
674
  * ```typescript
package/dist/index.js CHANGED
@@ -1,6 +1,18 @@
1
1
  import { jwtVerify, importSPKI } from 'jose';
2
2
 
3
3
  // src/client/types.ts
4
+ var ALLOWED_TOOL_CATEGORIES = [
5
+ "Crypto & DeFi",
6
+ "Financial Markets",
7
+ "Business & Sales",
8
+ "Marketing & SEO",
9
+ "Legal & Regulatory",
10
+ "Real World",
11
+ "Developer Tools",
12
+ "Research & Academia",
13
+ "Utility",
14
+ "Other"
15
+ ];
4
16
  var ContextError = class _ContextError extends Error {
5
17
  constructor(message, code, statusCode, helpUrl) {
6
18
  super(message);
@@ -12,6 +24,57 @@ var ContextError = class _ContextError extends Error {
12
24
  }
13
25
  };
14
26
 
27
+ // src/client/resources/developer.ts
28
+ var Developer = class {
29
+ constructor(client) {
30
+ this.client = client;
31
+ }
32
+ /**
33
+ * Update a tool listing's metadata (name, description, category).
34
+ *
35
+ * Requires an API key belonging to the tool's owner.
36
+ *
37
+ * @param toolId - The UUID of the tool to update
38
+ * @param updates - Fields to update (at least one required)
39
+ * @returns The updated tool metadata
40
+ *
41
+ * @throws {ContextError} If authentication fails or the caller does not own the tool
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const updated = await client.developer.updateTool("tool-uuid", {
46
+ * description: "Updated description with better showcase prompts",
47
+ * category: "crypto",
48
+ * });
49
+ * console.log(updated.updatedAt);
50
+ * ```
51
+ */
52
+ async updateTool(toolId, updates) {
53
+ if (!toolId) {
54
+ throw new ContextError("toolId is required");
55
+ }
56
+ if (updates.name === void 0 && updates.description === void 0 && updates.category === void 0) {
57
+ throw new ContextError(
58
+ "At least one field required: name, description, or category"
59
+ );
60
+ }
61
+ if (updates.category !== void 0 && updates.category !== null && !ALLOWED_TOOL_CATEGORIES.includes(updates.category)) {
62
+ throw new ContextError(
63
+ `category must be one of: ${ALLOWED_TOOL_CATEGORIES.join(", ")}`
64
+ );
65
+ }
66
+ const encodedToolId = encodeURIComponent(toolId);
67
+ return this.client._fetch(
68
+ `/api/v1/tools/${encodedToolId}`,
69
+ {
70
+ method: "PATCH",
71
+ body: JSON.stringify(updates)
72
+ },
73
+ { retry: false }
74
+ );
75
+ }
76
+ };
77
+
15
78
  // src/client/resources/discovery.ts
16
79
  var Discovery = class {
17
80
  constructor(client) {
@@ -236,6 +299,47 @@ var Query = class {
236
299
  constructor(client) {
237
300
  this.client = client;
238
301
  }
302
+ normalizeResult(result) {
303
+ const candidate = result;
304
+ if (candidate.outcomeType === "clarification_required" && "clarification" in candidate && candidate.clarification) {
305
+ return candidate;
306
+ }
307
+ if (candidate.outcomeType === "capability_miss" && "capabilityMiss" in candidate && candidate.capabilityMiss) {
308
+ return candidate;
309
+ }
310
+ return {
311
+ ...candidate,
312
+ outcomeType: "answer"
313
+ };
314
+ }
315
+ buildPolicyErrorEvent(params) {
316
+ if (params.clarificationPolicy !== "error") {
317
+ return;
318
+ }
319
+ if (params.result.outcomeType === "clarification_required") {
320
+ return {
321
+ type: "error",
322
+ error: params.result.response,
323
+ code: "clarification_required",
324
+ reasonCode: "clarification_required",
325
+ outcomeType: "clarification_required",
326
+ clarification: params.result.clarification,
327
+ querySession: params.result.querySession
328
+ };
329
+ }
330
+ if (params.result.outcomeType === "capability_miss") {
331
+ return {
332
+ type: "error",
333
+ error: params.result.response,
334
+ code: "capability_miss",
335
+ reasonCode: "capability_miss",
336
+ outcomeType: "capability_miss",
337
+ capabilityMiss: params.result.capabilityMiss,
338
+ querySession: params.result.querySession
339
+ };
340
+ }
341
+ return void 0;
342
+ }
239
343
  buildSyntheticTraceFromRunResult(params) {
240
344
  const timeline = params.toolsUsed.map((tool, index) => ({
241
345
  stepType: "tool-call",
@@ -378,6 +482,7 @@ var Query = class {
378
482
  async run(options) {
379
483
  const opts = typeof options === "string" ? { query: options } : options;
380
484
  let terminalError;
485
+ let finalResult;
381
486
  for await (const event of this.stream(opts)) {
382
487
  if (event.type === "error") {
383
488
  terminalError = {
@@ -389,9 +494,12 @@ var Query = class {
389
494
  continue;
390
495
  }
391
496
  if (event.type === "done") {
392
- return event.result;
497
+ finalResult = event.result;
393
498
  }
394
499
  }
500
+ if (finalResult) {
501
+ return finalResult;
502
+ }
395
503
  if (terminalError) {
396
504
  throw new ContextError(terminalError.error, terminalError.code);
397
505
  }
@@ -443,7 +551,11 @@ var Query = class {
443
551
  body: JSON.stringify({
444
552
  query: opts.query,
445
553
  tools: opts.tools,
446
- modelId: opts.modelId,
554
+ resumeFrom: opts.resumeFrom,
555
+ forkFrom: opts.forkFrom,
556
+ clarificationPolicy: opts.clarificationPolicy,
557
+ answerModelId: opts.answerModelId,
558
+ responseShape: opts.responseShape,
447
559
  includeData: opts.includeData,
448
560
  includeDataUrl: opts.includeDataUrl,
449
561
  includeDeveloperTrace: opts.includeDeveloperTrace,
@@ -481,22 +593,31 @@ var Query = class {
481
593
  return event;
482
594
  }
483
595
  if (event.type === "done") {
596
+ const normalizedResult = this.normalizeResult(event.result);
484
597
  let mergedTrace = this.mergeDeveloperTrace(
485
598
  aggregatedTrace,
486
- event.result.developerTrace
599
+ normalizedResult.developerTrace
487
600
  );
488
601
  if (!mergedTrace && opts.includeDeveloperTrace) {
489
602
  mergedTrace = statusTimeline.length > 0 ? this.buildSyntheticTraceFromStreamStatus({
490
603
  statusTimeline,
491
- toolsUsed: event.result.toolsUsed,
492
- durationMs: event.result.durationMs
604
+ toolsUsed: normalizedResult.toolsUsed,
605
+ durationMs: normalizedResult.durationMs
493
606
  }) : this.buildSyntheticTraceFromRunResult({
494
- toolsUsed: event.result.toolsUsed,
495
- durationMs: event.result.durationMs
607
+ toolsUsed: normalizedResult.toolsUsed,
608
+ durationMs: normalizedResult.durationMs
496
609
  });
497
610
  }
498
611
  if (mergedTrace) {
499
- event.result.developerTrace = mergedTrace;
612
+ normalizedResult.developerTrace = mergedTrace;
613
+ }
614
+ event.result = normalizedResult;
615
+ const policyErrorEvent = this.buildPolicyErrorEvent({
616
+ result: normalizedResult,
617
+ clarificationPolicy: opts.clarificationPolicy
618
+ });
619
+ if (policyErrorEvent) {
620
+ return policyErrorEvent;
500
621
  }
501
622
  }
502
623
  return event;
@@ -551,6 +672,10 @@ var ContextClient = class {
551
672
  requestTimeoutMs;
552
673
  streamTimeoutMs;
553
674
  _closed = false;
675
+ /**
676
+ * Developer resource for managing tool listings (contributor/developer concerns).
677
+ */
678
+ developer;
554
679
  /**
555
680
  * Discovery resource for searching tools
556
681
  */
@@ -592,6 +717,7 @@ var ContextClient = class {
592
717
  this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
593
718
  this.requestTimeoutMs = requestTimeoutMs;
594
719
  this.streamTimeoutMs = streamTimeoutMs;
720
+ this.developer = new Developer(this);
595
721
  this.discovery = new Discovery(this);
596
722
  this.tools = new Tools(this);
597
723
  this.query = new Query(this);
@@ -912,6 +1038,6 @@ function wrapHandshakeResponse(action) {
912
1038
  };
913
1039
  }
914
1040
 
915
- export { CONTEXT_REQUIREMENTS_KEY, ContextClient, ContextError, Discovery, META_CONTEXT_REQUIREMENTS_KEY, Query, Tools, createAuthRequired, createContextMiddleware, createSignatureRequest, createTransactionProposal, isAuthRequired, isHandshakeAction, isOpenMcpMethod, isProtectedMcpMethod, isSignatureRequest, isTransactionProposal, verifyContextRequest, wrapHandshakeResponse };
1041
+ export { CONTEXT_REQUIREMENTS_KEY, ContextClient, ContextError, Developer, Discovery, META_CONTEXT_REQUIREMENTS_KEY, Query, Tools, createAuthRequired, createContextMiddleware, createSignatureRequest, createTransactionProposal, isAuthRequired, isHandshakeAction, isOpenMcpMethod, isProtectedMcpMethod, isSignatureRequest, isTransactionProposal, verifyContextRequest, wrapHandshakeResponse };
916
1042
  //# sourceMappingURL=index.js.map
917
1043
  //# sourceMappingURL=index.js.map