@ctxprotocol/sdk 0.1.1 → 0.1.3

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.
@@ -0,0 +1,277 @@
1
+ /**
2
+ * Configuration options for initializing the ContextClient
3
+ */
4
+ interface ContextClientOptions {
5
+ /**
6
+ * Your Context Protocol API key
7
+ * @example "sk_live_abc123..."
8
+ */
9
+ apiKey: string;
10
+ /**
11
+ * Base URL for the Context Protocol API
12
+ * @default "https://ctxprotocol.com"
13
+ */
14
+ baseUrl?: string;
15
+ }
16
+ /**
17
+ * An individual MCP tool exposed by a tool listing
18
+ */
19
+ interface McpTool {
20
+ /** Name of the MCP tool method */
21
+ name: string;
22
+ /** Description of what this method does */
23
+ description: string;
24
+ /**
25
+ * JSON Schema for the input arguments this tool accepts.
26
+ * Used by LLMs to generate correct arguments.
27
+ */
28
+ inputSchema?: Record<string, unknown>;
29
+ /**
30
+ * JSON Schema for the output this tool returns.
31
+ * Used by LLMs to understand the response structure.
32
+ */
33
+ outputSchema?: Record<string, unknown>;
34
+ }
35
+ /**
36
+ * Represents a tool available on the Context Protocol marketplace
37
+ */
38
+ interface Tool {
39
+ /** Unique identifier for the tool (UUID) */
40
+ id: string;
41
+ /** Human-readable name of the tool */
42
+ name: string;
43
+ /** Description of what the tool does */
44
+ description: string;
45
+ /** Price per execution in USDC */
46
+ price: string;
47
+ /** Tool category (e.g., "defi", "nft") */
48
+ category?: string;
49
+ /** Whether the tool is verified by Context Protocol */
50
+ isVerified?: boolean;
51
+ /**
52
+ * Available MCP tool methods
53
+ * Use items from this array as `toolName` when executing
54
+ */
55
+ mcpTools?: McpTool[];
56
+ /** Creation timestamp */
57
+ createdAt?: string;
58
+ /** Last update timestamp */
59
+ updatedAt?: string;
60
+ }
61
+ /**
62
+ * Response from the tools search endpoint
63
+ */
64
+ interface SearchResponse {
65
+ /** Array of matching tools */
66
+ tools: Tool[];
67
+ /** The search query that was used */
68
+ query: string;
69
+ /** Total number of results */
70
+ count: number;
71
+ }
72
+ /**
73
+ * Options for searching tools
74
+ */
75
+ interface SearchOptions {
76
+ /** Search query (semantic search) */
77
+ query?: string;
78
+ /** Maximum number of results (1-50, default 10) */
79
+ limit?: number;
80
+ }
81
+ /**
82
+ * Options for executing a tool
83
+ */
84
+ interface ExecuteOptions {
85
+ /** The UUID of the tool to execute (from search results) */
86
+ toolId: string;
87
+ /** The specific MCP tool name to call (from tool's mcpTools array) */
88
+ toolName: string;
89
+ /** Arguments to pass to the tool */
90
+ args?: Record<string, unknown>;
91
+ }
92
+ /**
93
+ * Successful execution response from the API
94
+ */
95
+ interface ExecuteApiSuccessResponse {
96
+ success: true;
97
+ /** The result data from the tool execution */
98
+ result: unknown;
99
+ /** Information about the executed tool */
100
+ tool: {
101
+ id: string;
102
+ name: string;
103
+ };
104
+ /** Execution duration in milliseconds */
105
+ durationMs: number;
106
+ }
107
+ /**
108
+ * Error response from the API
109
+ */
110
+ interface ExecuteApiErrorResponse {
111
+ /** Human-readable error message */
112
+ error: string;
113
+ /** Error code for programmatic handling */
114
+ code?: ContextErrorCode;
115
+ /** URL to help resolve the issue */
116
+ helpUrl?: string;
117
+ }
118
+ /**
119
+ * Raw API response from the execute endpoint
120
+ */
121
+ type ExecuteApiResponse = ExecuteApiSuccessResponse | ExecuteApiErrorResponse;
122
+ /**
123
+ * The resolved result returned to the user after SDK processing
124
+ */
125
+ interface ExecutionResult<T = unknown> {
126
+ /** The data returned by the tool */
127
+ result: T;
128
+ /** Information about the executed tool */
129
+ tool: {
130
+ id: string;
131
+ name: string;
132
+ };
133
+ /** Execution duration in milliseconds */
134
+ durationMs: number;
135
+ }
136
+ /**
137
+ * Specific error codes returned by the Context Protocol API
138
+ */
139
+ type ContextErrorCode = "unauthorized" | "no_wallet" | "insufficient_allowance" | "payment_failed" | "execution_failed";
140
+ /**
141
+ * Error thrown by the Context Protocol client
142
+ */
143
+ declare class ContextError extends Error {
144
+ readonly code?: (ContextErrorCode | string) | undefined;
145
+ readonly statusCode?: number | undefined;
146
+ readonly helpUrl?: string | undefined;
147
+ constructor(message: string, code?: (ContextErrorCode | string) | undefined, statusCode?: number | undefined, helpUrl?: string | undefined);
148
+ }
149
+
150
+ /**
151
+ * Discovery resource for searching and finding tools on the Context Protocol marketplace
152
+ */
153
+ declare class Discovery {
154
+ private client;
155
+ constructor(client: ContextClient);
156
+ /**
157
+ * Search for tools matching a query string
158
+ *
159
+ * @param query - The search query (e.g., "gas prices", "nft metadata")
160
+ * @param limit - Maximum number of results (1-50, default 10)
161
+ * @returns Array of matching tools
162
+ *
163
+ * @example
164
+ * ```typescript
165
+ * const tools = await client.discovery.search("gas prices");
166
+ * console.log(tools[0].name); // "Gas Price Oracle"
167
+ * console.log(tools[0].mcpTools); // Available methods
168
+ * ```
169
+ */
170
+ search(query: string, limit?: number): Promise<Tool[]>;
171
+ /**
172
+ * Get featured/popular tools (empty query search)
173
+ *
174
+ * @param limit - Maximum number of results (1-50, default 10)
175
+ * @returns Array of featured tools
176
+ *
177
+ * @example
178
+ * ```typescript
179
+ * const featured = await client.discovery.getFeatured(5);
180
+ * ```
181
+ */
182
+ getFeatured(limit?: number): Promise<Tool[]>;
183
+ }
184
+
185
+ /**
186
+ * Tools resource for executing tools on the Context Protocol marketplace
187
+ */
188
+ declare class Tools {
189
+ private client;
190
+ constructor(client: ContextClient);
191
+ /**
192
+ * Execute a tool with the provided arguments
193
+ *
194
+ * @param options - Execution options
195
+ * @param options.toolId - The UUID of the tool (from search results)
196
+ * @param options.toolName - The specific MCP tool method to call (from tool's mcpTools array)
197
+ * @param options.args - Arguments to pass to the tool
198
+ * @returns The execution result with the tool's output data
199
+ *
200
+ * @throws {ContextError} With code `no_wallet` if wallet not set up
201
+ * @throws {ContextError} With code `insufficient_allowance` if Auto Pay not enabled
202
+ * @throws {ContextError} With code `payment_failed` if on-chain payment fails
203
+ * @throws {ContextError} With code `execution_failed` if tool execution fails
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * // First, search for a tool
208
+ * const tools = await client.discovery.search("gas prices");
209
+ * const tool = tools[0];
210
+ *
211
+ * // Execute a specific method from the tool's mcpTools
212
+ * const result = await client.tools.execute({
213
+ * toolId: tool.id,
214
+ * toolName: tool.mcpTools[0].name, // e.g., "get_gas_prices"
215
+ * args: { chainId: 1 }
216
+ * });
217
+ *
218
+ * console.log(result.result); // The tool's output
219
+ * console.log(result.durationMs); // Execution time
220
+ * ```
221
+ */
222
+ execute<T = unknown>(options: ExecuteOptions): Promise<ExecutionResult<T>>;
223
+ }
224
+
225
+ /**
226
+ * The official TypeScript client for the Context Protocol.
227
+ *
228
+ * Use this client to discover and execute AI tools programmatically.
229
+ *
230
+ * @example
231
+ * ```typescript
232
+ * import { ContextClient } from "@contextprotocol/client";
233
+ *
234
+ * const client = new ContextClient({
235
+ * apiKey: "sk_live_..."
236
+ * });
237
+ *
238
+ * // Discover tools
239
+ * const tools = await client.discovery.search("gas prices");
240
+ *
241
+ * // Execute a tool method
242
+ * const result = await client.tools.execute({
243
+ * toolId: tools[0].id,
244
+ * toolName: tools[0].mcpTools[0].name,
245
+ * args: { chainId: 1 }
246
+ * });
247
+ * ```
248
+ */
249
+ declare class ContextClient {
250
+ private readonly apiKey;
251
+ private readonly baseUrl;
252
+ /**
253
+ * Discovery resource for searching tools
254
+ */
255
+ readonly discovery: Discovery;
256
+ /**
257
+ * Tools resource for executing tools
258
+ */
259
+ readonly tools: Tools;
260
+ /**
261
+ * Creates a new Context Protocol client
262
+ *
263
+ * @param options - Client configuration options
264
+ * @param options.apiKey - Your Context Protocol API key (format: sk_live_...)
265
+ * @param options.baseUrl - Optional base URL override (defaults to https://ctxprotocol.com)
266
+ */
267
+ constructor(options: ContextClientOptions);
268
+ /**
269
+ * Internal method for making authenticated HTTP requests
270
+ * All requests include the Authorization header with the API key
271
+ *
272
+ * @internal
273
+ */
274
+ fetch<T>(endpoint: string, options?: RequestInit): Promise<T>;
275
+ }
276
+
277
+ 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 };
package/dist/index.d.ts CHANGED
@@ -1,40 +1,277 @@
1
- import { z } from 'zod';
2
-
3
- type ToolContext = {
1
+ /**
2
+ * Configuration options for initializing the ContextClient
3
+ */
4
+ interface ContextClientOptions {
5
+ /**
6
+ * Your Context Protocol API key
7
+ * @example "sk_live_abc123..."
8
+ */
9
+ apiKey: string;
4
10
  /**
5
- * Raw headers from the incoming request. Useful if contributors need to
6
- * inspect authentication or tracing metadata.
11
+ * Base URL for the Context Protocol API
12
+ * @default "https://ctxprotocol.com"
7
13
  */
8
- headers?: Record<string, string | string[] | undefined>;
9
- };
10
- type DefineHttpToolOptions<I extends z.ZodTypeAny, O extends z.ZodTypeAny> = {
14
+ baseUrl?: string;
15
+ }
16
+ /**
17
+ * An individual MCP tool exposed by a tool listing
18
+ */
19
+ interface McpTool {
20
+ /** Name of the MCP tool method */
11
21
  name: string;
12
- version?: string;
13
- description?: string;
14
- inputSchema: I;
15
- outputSchema: O;
16
- handler: (input: z.infer<I>, context: ToolContext) => Promise<z.infer<O>>;
17
- };
18
- type HttpToolDefinition<I extends z.ZodTypeAny, O extends z.ZodTypeAny> = {
22
+ /** Description of what this method does */
23
+ description: string;
24
+ /**
25
+ * JSON Schema for the input arguments this tool accepts.
26
+ * Used by LLMs to generate correct arguments.
27
+ */
28
+ inputSchema?: Record<string, unknown>;
29
+ /**
30
+ * JSON Schema for the output this tool returns.
31
+ * Used by LLMs to understand the response structure.
32
+ */
33
+ outputSchema?: Record<string, unknown>;
34
+ }
35
+ /**
36
+ * Represents a tool available on the Context Protocol marketplace
37
+ */
38
+ interface Tool {
39
+ /** Unique identifier for the tool (UUID) */
40
+ id: string;
41
+ /** Human-readable name of the tool */
19
42
  name: string;
20
- version?: string;
21
- description?: string;
22
- inputSchema: I;
23
- outputSchema: O;
24
- handler: (input: z.infer<I>, context: ToolContext) => Promise<z.infer<O>>;
25
- };
26
- type ExecuteHttpToolOptions = {
27
- headers?: Record<string, string | string[] | undefined>;
28
- };
29
- type ContextResponse<T> = {
30
- data: T;
31
- meta: {
32
- tool: string;
33
- version?: string;
34
- generatedAt: string;
43
+ /** Description of what the tool does */
44
+ description: string;
45
+ /** Price per execution in USDC */
46
+ price: string;
47
+ /** Tool category (e.g., "defi", "nft") */
48
+ category?: string;
49
+ /** Whether the tool is verified by Context Protocol */
50
+ isVerified?: boolean;
51
+ /**
52
+ * Available MCP tool methods
53
+ * Use items from this array as `toolName` when executing
54
+ */
55
+ mcpTools?: McpTool[];
56
+ /** Creation timestamp */
57
+ createdAt?: string;
58
+ /** Last update timestamp */
59
+ updatedAt?: string;
60
+ }
61
+ /**
62
+ * Response from the tools search endpoint
63
+ */
64
+ interface SearchResponse {
65
+ /** Array of matching tools */
66
+ tools: Tool[];
67
+ /** The search query that was used */
68
+ query: string;
69
+ /** Total number of results */
70
+ count: number;
71
+ }
72
+ /**
73
+ * Options for searching tools
74
+ */
75
+ interface SearchOptions {
76
+ /** Search query (semantic search) */
77
+ query?: string;
78
+ /** Maximum number of results (1-50, default 10) */
79
+ limit?: number;
80
+ }
81
+ /**
82
+ * Options for executing a tool
83
+ */
84
+ interface ExecuteOptions {
85
+ /** The UUID of the tool to execute (from search results) */
86
+ toolId: string;
87
+ /** The specific MCP tool name to call (from tool's mcpTools array) */
88
+ toolName: string;
89
+ /** Arguments to pass to the tool */
90
+ args?: Record<string, unknown>;
91
+ }
92
+ /**
93
+ * Successful execution response from the API
94
+ */
95
+ interface ExecuteApiSuccessResponse {
96
+ success: true;
97
+ /** The result data from the tool execution */
98
+ result: unknown;
99
+ /** Information about the executed tool */
100
+ tool: {
101
+ id: string;
102
+ name: string;
35
103
  };
36
- };
37
- declare function defineHttpTool<I extends z.ZodTypeAny, O extends z.ZodTypeAny>(options: DefineHttpToolOptions<I, O>): HttpToolDefinition<I, O>;
38
- declare function executeHttpTool<I extends z.ZodTypeAny, O extends z.ZodTypeAny>(tool: HttpToolDefinition<I, O>, input: unknown, options?: ExecuteHttpToolOptions): Promise<ContextResponse<z.infer<O>>>;
104
+ /** Execution duration in milliseconds */
105
+ durationMs: number;
106
+ }
107
+ /**
108
+ * Error response from the API
109
+ */
110
+ interface ExecuteApiErrorResponse {
111
+ /** Human-readable error message */
112
+ error: string;
113
+ /** Error code for programmatic handling */
114
+ code?: ContextErrorCode;
115
+ /** URL to help resolve the issue */
116
+ helpUrl?: string;
117
+ }
118
+ /**
119
+ * Raw API response from the execute endpoint
120
+ */
121
+ type ExecuteApiResponse = ExecuteApiSuccessResponse | ExecuteApiErrorResponse;
122
+ /**
123
+ * The resolved result returned to the user after SDK processing
124
+ */
125
+ interface ExecutionResult<T = unknown> {
126
+ /** The data returned by the tool */
127
+ result: T;
128
+ /** Information about the executed tool */
129
+ tool: {
130
+ id: string;
131
+ name: string;
132
+ };
133
+ /** Execution duration in milliseconds */
134
+ durationMs: number;
135
+ }
136
+ /**
137
+ * Specific error codes returned by the Context Protocol API
138
+ */
139
+ type ContextErrorCode = "unauthorized" | "no_wallet" | "insufficient_allowance" | "payment_failed" | "execution_failed";
140
+ /**
141
+ * Error thrown by the Context Protocol client
142
+ */
143
+ declare class ContextError extends Error {
144
+ readonly code?: (ContextErrorCode | string) | undefined;
145
+ readonly statusCode?: number | undefined;
146
+ readonly helpUrl?: string | undefined;
147
+ constructor(message: string, code?: (ContextErrorCode | string) | undefined, statusCode?: number | undefined, helpUrl?: string | undefined);
148
+ }
149
+
150
+ /**
151
+ * Discovery resource for searching and finding tools on the Context Protocol marketplace
152
+ */
153
+ declare class Discovery {
154
+ private client;
155
+ constructor(client: ContextClient);
156
+ /**
157
+ * Search for tools matching a query string
158
+ *
159
+ * @param query - The search query (e.g., "gas prices", "nft metadata")
160
+ * @param limit - Maximum number of results (1-50, default 10)
161
+ * @returns Array of matching tools
162
+ *
163
+ * @example
164
+ * ```typescript
165
+ * const tools = await client.discovery.search("gas prices");
166
+ * console.log(tools[0].name); // "Gas Price Oracle"
167
+ * console.log(tools[0].mcpTools); // Available methods
168
+ * ```
169
+ */
170
+ search(query: string, limit?: number): Promise<Tool[]>;
171
+ /**
172
+ * Get featured/popular tools (empty query search)
173
+ *
174
+ * @param limit - Maximum number of results (1-50, default 10)
175
+ * @returns Array of featured tools
176
+ *
177
+ * @example
178
+ * ```typescript
179
+ * const featured = await client.discovery.getFeatured(5);
180
+ * ```
181
+ */
182
+ getFeatured(limit?: number): Promise<Tool[]>;
183
+ }
184
+
185
+ /**
186
+ * Tools resource for executing tools on the Context Protocol marketplace
187
+ */
188
+ declare class Tools {
189
+ private client;
190
+ constructor(client: ContextClient);
191
+ /**
192
+ * Execute a tool with the provided arguments
193
+ *
194
+ * @param options - Execution options
195
+ * @param options.toolId - The UUID of the tool (from search results)
196
+ * @param options.toolName - The specific MCP tool method to call (from tool's mcpTools array)
197
+ * @param options.args - Arguments to pass to the tool
198
+ * @returns The execution result with the tool's output data
199
+ *
200
+ * @throws {ContextError} With code `no_wallet` if wallet not set up
201
+ * @throws {ContextError} With code `insufficient_allowance` if Auto Pay not enabled
202
+ * @throws {ContextError} With code `payment_failed` if on-chain payment fails
203
+ * @throws {ContextError} With code `execution_failed` if tool execution fails
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * // First, search for a tool
208
+ * const tools = await client.discovery.search("gas prices");
209
+ * const tool = tools[0];
210
+ *
211
+ * // Execute a specific method from the tool's mcpTools
212
+ * const result = await client.tools.execute({
213
+ * toolId: tool.id,
214
+ * toolName: tool.mcpTools[0].name, // e.g., "get_gas_prices"
215
+ * args: { chainId: 1 }
216
+ * });
217
+ *
218
+ * console.log(result.result); // The tool's output
219
+ * console.log(result.durationMs); // Execution time
220
+ * ```
221
+ */
222
+ execute<T = unknown>(options: ExecuteOptions): Promise<ExecutionResult<T>>;
223
+ }
224
+
225
+ /**
226
+ * The official TypeScript client for the Context Protocol.
227
+ *
228
+ * Use this client to discover and execute AI tools programmatically.
229
+ *
230
+ * @example
231
+ * ```typescript
232
+ * import { ContextClient } from "@contextprotocol/client";
233
+ *
234
+ * const client = new ContextClient({
235
+ * apiKey: "sk_live_..."
236
+ * });
237
+ *
238
+ * // Discover tools
239
+ * const tools = await client.discovery.search("gas prices");
240
+ *
241
+ * // Execute a tool method
242
+ * const result = await client.tools.execute({
243
+ * toolId: tools[0].id,
244
+ * toolName: tools[0].mcpTools[0].name,
245
+ * args: { chainId: 1 }
246
+ * });
247
+ * ```
248
+ */
249
+ declare class ContextClient {
250
+ private readonly apiKey;
251
+ private readonly baseUrl;
252
+ /**
253
+ * Discovery resource for searching tools
254
+ */
255
+ readonly discovery: Discovery;
256
+ /**
257
+ * Tools resource for executing tools
258
+ */
259
+ readonly tools: Tools;
260
+ /**
261
+ * Creates a new Context Protocol client
262
+ *
263
+ * @param options - Client configuration options
264
+ * @param options.apiKey - Your Context Protocol API key (format: sk_live_...)
265
+ * @param options.baseUrl - Optional base URL override (defaults to https://ctxprotocol.com)
266
+ */
267
+ constructor(options: ContextClientOptions);
268
+ /**
269
+ * Internal method for making authenticated HTTP requests
270
+ * All requests include the Authorization header with the API key
271
+ *
272
+ * @internal
273
+ */
274
+ fetch<T>(endpoint: string, options?: RequestInit): Promise<T>;
275
+ }
39
276
 
40
- export { type ContextResponse, type DefineHttpToolOptions, type ExecuteHttpToolOptions, type HttpToolDefinition, type ToolContext, defineHttpTool, executeHttpTool };
277
+ 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 };