@ctxprotocol/sdk 0.1.1 → 0.1.2

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