@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.
- package/README.md +251 -73
- package/dist/index.cjs +194 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +269 -0
- package/dist/index.d.ts +263 -34
- package/dist/index.js +184 -44
- package/dist/index.js.map +1 -0
- package/package.json +45 -21
- package/dist/index.d.mts +0 -40
- package/dist/index.mjs +0 -23
package/README.md
CHANGED
|
@@ -1,103 +1,281 @@
|
|
|
1
|
-
#
|
|
2
|
-
> Server-side helpers for publishing HTTP tools to the Context marketplace.
|
|
1
|
+
# @ctxprotocol/sdk
|
|
3
2
|
|
|
4
|
-
The
|
|
5
|
-
expose HTTP-based tools to the Context marketplace in a safe, typed, and
|
|
6
|
-
consistent way.
|
|
3
|
+
The official TypeScript SDK for the [Context Protocol](https://ctxprotocol.com) — the monetization layer for MCP. Discover and execute AI tools programmatically.
|
|
7
4
|
|
|
8
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/@ctxprotocol/sdk)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
7
|
|
|
10
|
-
##
|
|
11
|
-
|
|
12
|
-
Using **pnpm**:
|
|
8
|
+
## Installation
|
|
13
9
|
|
|
14
10
|
```bash
|
|
15
|
-
|
|
11
|
+
npm install @ctxprotocol/sdk
|
|
16
12
|
```
|
|
17
13
|
|
|
18
|
-
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @ctxprotocol/sdk
|
|
16
|
+
```
|
|
19
17
|
|
|
20
18
|
```bash
|
|
21
|
-
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}),
|
|
42
|
-
async handler(input) {
|
|
43
|
-
const response = await fetch("https://your-endpoint", {
|
|
44
|
-
method: "POST",
|
|
45
|
-
body: JSON.stringify(input),
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
endpoint: "gas_price",
|
|
50
|
-
data: await response.json(),
|
|
51
|
-
};
|
|
52
|
-
},
|
|
19
|
+
yarn add @ctxprotocol/sdk
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Prerequisites
|
|
23
|
+
|
|
24
|
+
Before using the API, you must complete setup via the web dashboard:
|
|
25
|
+
|
|
26
|
+
1. **Sign in** at [ctxprotocol.com](https://ctxprotocol.com) — Creates your embedded wallet
|
|
27
|
+
2. **Enable Auto Pay** — Approve USDC spending for tool payments
|
|
28
|
+
3. **Fund wallet** — Add USDC for tool execution fees
|
|
29
|
+
4. **Generate API key** — In Settings page
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { ContextClient } from "@ctxprotocol/sdk";
|
|
35
|
+
|
|
36
|
+
// Initialize the client with your API key
|
|
37
|
+
const client = new ContextClient({
|
|
38
|
+
apiKey: "sk_live_...",
|
|
53
39
|
});
|
|
54
40
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
41
|
+
// 1. Discover tools
|
|
42
|
+
const tools = await client.discovery.search("gas prices");
|
|
43
|
+
console.log(tools[0].name); // "Gas Price Oracle"
|
|
44
|
+
console.log(tools[0].mcpTools); // Available methods
|
|
45
|
+
|
|
46
|
+
// 2. Execute a tool method
|
|
47
|
+
const result = await client.tools.execute({
|
|
48
|
+
toolId: tools[0].id,
|
|
49
|
+
toolName: tools[0].mcpTools[0].name, // e.g., "get_gas_prices"
|
|
50
|
+
args: { chainId: 1 },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
console.log(result.result); // Tool output data
|
|
54
|
+
console.log(result.durationMs); // Execution time in ms
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Configuration
|
|
58
|
+
|
|
59
|
+
### Client Options
|
|
60
|
+
|
|
61
|
+
| Option | Type | Required | Default | Description |
|
|
62
|
+
| --------- | -------- | -------- | ------------------------ | ------------------------------ |
|
|
63
|
+
| `apiKey` | `string` | Yes | — | Your Context Protocol API key |
|
|
64
|
+
| `baseUrl` | `string` | No | `https://ctxprotocol.com`| API base URL (for development) |
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
// Production usage
|
|
68
|
+
const client = new ContextClient({
|
|
69
|
+
apiKey: process.env.CONTEXT_API_KEY!,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Development/testing with local server
|
|
73
|
+
const devClient = new ContextClient({
|
|
74
|
+
apiKey: "sk_test_...",
|
|
75
|
+
baseUrl: "http://localhost:3000",
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## API Reference
|
|
80
|
+
|
|
81
|
+
### Discovery
|
|
82
|
+
|
|
83
|
+
#### `client.discovery.search(query, limit?)`
|
|
84
|
+
|
|
85
|
+
Search for tools matching a query string.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const tools = await client.discovery.search("gas prices", 10);
|
|
89
|
+
|
|
90
|
+
// Returns: Tool[]
|
|
91
|
+
// [
|
|
92
|
+
// {
|
|
93
|
+
// id: "uuid-string",
|
|
94
|
+
// name: "Gas Price Oracle",
|
|
95
|
+
// description: "Get current gas prices",
|
|
96
|
+
// price: "0.001",
|
|
97
|
+
// category: "defi",
|
|
98
|
+
// isVerified: true,
|
|
99
|
+
// kind: "mcp",
|
|
100
|
+
// mcpTools: [
|
|
101
|
+
// { name: "get_gas_prices", description: "Get gas prices for a chain" },
|
|
102
|
+
// { name: "get_supported_chains", description: "List supported chains" }
|
|
103
|
+
// ]
|
|
104
|
+
// }
|
|
105
|
+
// ]
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### `client.discovery.getFeatured(limit?)`
|
|
109
|
+
|
|
110
|
+
Get featured/popular tools.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const featured = await client.discovery.getFeatured(5);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Tools
|
|
60
117
|
|
|
61
|
-
|
|
118
|
+
#### `client.tools.execute(options)`
|
|
119
|
+
|
|
120
|
+
Execute a tool method with the provided arguments.
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const result = await client.tools.execute({
|
|
124
|
+
toolId: "uuid-of-tool", // From search results
|
|
125
|
+
toolName: "get_gas_prices", // From tool's mcpTools array
|
|
126
|
+
args: { chainId: 1 }, // Tool-specific arguments
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Returns: ExecutionResult<T>
|
|
130
|
+
// {
|
|
131
|
+
// result: { gasPrice: "25.5", unit: "gwei", ... },
|
|
132
|
+
// tool: { id: "uuid", name: "Gas Price Oracle" },
|
|
133
|
+
// durationMs: 245
|
|
134
|
+
// }
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Types
|
|
138
|
+
|
|
139
|
+
All types are exported for full TypeScript autocomplete support:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import type {
|
|
143
|
+
ContextClientOptions,
|
|
144
|
+
Tool,
|
|
145
|
+
McpTool,
|
|
146
|
+
ExecuteOptions,
|
|
147
|
+
ExecutionResult,
|
|
148
|
+
ContextErrorCode,
|
|
149
|
+
} from "@ctxprotocol/sdk";
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Tool
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
interface Tool {
|
|
156
|
+
id: string;
|
|
157
|
+
name: string;
|
|
158
|
+
description: string;
|
|
159
|
+
price: string;
|
|
160
|
+
category?: string;
|
|
161
|
+
isVerified?: boolean;
|
|
162
|
+
kind?: string;
|
|
163
|
+
mcpTools?: McpTool[];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
interface McpTool {
|
|
167
|
+
name: string;
|
|
168
|
+
description: string;
|
|
62
169
|
}
|
|
63
170
|
```
|
|
64
171
|
|
|
65
|
-
|
|
66
|
-
the Blocknative Gas Platform.
|
|
172
|
+
### ExecuteOptions
|
|
67
173
|
|
|
68
|
-
|
|
174
|
+
```typescript
|
|
175
|
+
interface ExecuteOptions {
|
|
176
|
+
toolId: string; // UUID of the tool
|
|
177
|
+
toolName: string; // MCP method name from mcpTools array
|
|
178
|
+
args?: Record<string, unknown>;
|
|
179
|
+
}
|
|
180
|
+
```
|
|
69
181
|
|
|
70
|
-
|
|
182
|
+
### ExecutionResult
|
|
71
183
|
|
|
72
|
-
|
|
184
|
+
```typescript
|
|
185
|
+
interface ExecutionResult<T = unknown> {
|
|
186
|
+
result: T;
|
|
187
|
+
tool: { id: string; name: string };
|
|
188
|
+
durationMs: number;
|
|
189
|
+
}
|
|
190
|
+
```
|
|
73
191
|
|
|
74
|
-
|
|
192
|
+
## Error Handling
|
|
75
193
|
|
|
76
|
-
|
|
77
|
-
pnpm run build
|
|
78
|
-
```
|
|
194
|
+
The SDK throws `ContextError` for all API errors with specific error codes:
|
|
79
195
|
|
|
80
|
-
|
|
196
|
+
```typescript
|
|
197
|
+
import { ContextClient, ContextError } from "@ctxprotocol/sdk";
|
|
81
198
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
199
|
+
try {
|
|
200
|
+
const result = await client.tools.execute({
|
|
201
|
+
toolId: "...",
|
|
202
|
+
toolName: "...",
|
|
203
|
+
args: {},
|
|
204
|
+
});
|
|
205
|
+
} catch (error) {
|
|
206
|
+
if (error instanceof ContextError) {
|
|
207
|
+
console.error("Error:", error.message);
|
|
208
|
+
console.error("Code:", error.code);
|
|
209
|
+
console.error("HTTP Status:", error.statusCode);
|
|
85
210
|
|
|
86
|
-
|
|
211
|
+
// Handle specific error cases
|
|
212
|
+
switch (error.code) {
|
|
213
|
+
case "no_wallet":
|
|
214
|
+
console.log("Please set up your wallet at", error.helpUrl);
|
|
215
|
+
break;
|
|
216
|
+
case "insufficient_allowance":
|
|
217
|
+
console.log("Please enable Auto Pay at", error.helpUrl);
|
|
218
|
+
break;
|
|
219
|
+
case "payment_failed":
|
|
220
|
+
console.log("Payment transaction failed");
|
|
221
|
+
break;
|
|
222
|
+
case "execution_failed":
|
|
223
|
+
console.log("Tool execution failed");
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
```
|
|
87
229
|
|
|
88
|
-
|
|
89
|
-
pnpm run test
|
|
90
|
-
```
|
|
230
|
+
### Error Codes
|
|
91
231
|
|
|
92
|
-
|
|
232
|
+
| Code | Description |
|
|
233
|
+
| ------------------------ | ---------------------------------------- |
|
|
234
|
+
| `unauthorized` | Missing or invalid API key |
|
|
235
|
+
| `no_wallet` | User hasn't set up wallet via dashboard |
|
|
236
|
+
| `insufficient_allowance` | Auto Pay not enabled or allowance too low|
|
|
237
|
+
| `payment_failed` | On-chain payment transaction failed |
|
|
238
|
+
| `execution_failed` | MCP tool execution error |
|
|
93
239
|
|
|
94
|
-
##
|
|
240
|
+
## Authentication
|
|
95
241
|
|
|
96
|
-
|
|
242
|
+
All requests are automatically authenticated using the Bearer token scheme:
|
|
97
243
|
|
|
98
|
-
```bash
|
|
99
|
-
pnpm install
|
|
100
|
-
pnpm run build
|
|
101
|
-
pnpm publish --access public
|
|
102
244
|
```
|
|
245
|
+
Authorization: Bearer sk_live_...
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Your API key should be kept secret. Use environment variables in production:
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
const client = new ContextClient({
|
|
252
|
+
apiKey: process.env.CONTEXT_API_KEY!,
|
|
253
|
+
});
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Payment Flow
|
|
257
|
+
|
|
258
|
+
When you execute a tool:
|
|
259
|
+
|
|
260
|
+
1. Your pre-approved USDC allowance is used for payment
|
|
261
|
+
2. **90%** goes to the tool developer
|
|
262
|
+
3. **10%** goes to the protocol
|
|
263
|
+
4. Tool executes and returns results
|
|
264
|
+
|
|
265
|
+
Ensure your wallet has sufficient USDC balance before executing paid tools.
|
|
266
|
+
|
|
267
|
+
## Links
|
|
268
|
+
|
|
269
|
+
- [Context Protocol](https://ctxprotocol.com) — Main website
|
|
270
|
+
- [GitHub](https://github.com/ctxprotocol/context) — Main project repository
|
|
271
|
+
- [SDK Repository](https://github.com/ctxprotocol/sdk) — This SDK
|
|
272
|
+
- [NPM Package](https://www.npmjs.com/package/@ctxprotocol/sdk)
|
|
273
|
+
|
|
274
|
+
## Requirements
|
|
275
|
+
|
|
276
|
+
- Node.js 18.0.0 or later (for native `fetch` support)
|
|
277
|
+
- TypeScript 5.0+ (recommended)
|
|
278
|
+
|
|
279
|
+
## License
|
|
103
280
|
|
|
281
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/types.ts
|
|
4
|
+
var ContextError = class extends Error {
|
|
5
|
+
constructor(message, code, statusCode, helpUrl) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.statusCode = statusCode;
|
|
9
|
+
this.helpUrl = helpUrl;
|
|
10
|
+
this.name = "ContextError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// src/resources/discovery.ts
|
|
15
|
+
var Discovery = class {
|
|
16
|
+
constructor(client) {
|
|
17
|
+
this.client = client;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Search for tools matching a query string
|
|
21
|
+
*
|
|
22
|
+
* @param query - The search query (e.g., "gas prices", "nft metadata")
|
|
23
|
+
* @param limit - Maximum number of results (1-50, default 10)
|
|
24
|
+
* @returns Array of matching tools
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const tools = await client.discovery.search("gas prices");
|
|
29
|
+
* console.log(tools[0].name); // "Gas Price Oracle"
|
|
30
|
+
* console.log(tools[0].mcpTools); // Available methods
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
async search(query, limit) {
|
|
34
|
+
const params = new URLSearchParams();
|
|
35
|
+
if (query) {
|
|
36
|
+
params.set("q", query);
|
|
37
|
+
}
|
|
38
|
+
if (limit !== void 0) {
|
|
39
|
+
params.set("limit", String(limit));
|
|
40
|
+
}
|
|
41
|
+
const queryString = params.toString();
|
|
42
|
+
const endpoint = `/api/v1/tools/search${queryString ? `?${queryString}` : ""}`;
|
|
43
|
+
const response = await this.client.fetch(endpoint);
|
|
44
|
+
return response.tools;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get featured/popular tools (empty query search)
|
|
48
|
+
*
|
|
49
|
+
* @param limit - Maximum number of results (1-50, default 10)
|
|
50
|
+
* @returns Array of featured tools
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* const featured = await client.discovery.getFeatured(5);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
async getFeatured(limit) {
|
|
58
|
+
return this.search("", limit);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// src/resources/tools.ts
|
|
63
|
+
var Tools = class {
|
|
64
|
+
constructor(client) {
|
|
65
|
+
this.client = client;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Execute a tool with the provided arguments
|
|
69
|
+
*
|
|
70
|
+
* @param options - Execution options
|
|
71
|
+
* @param options.toolId - The UUID of the tool (from search results)
|
|
72
|
+
* @param options.toolName - The specific MCP tool method to call (from tool's mcpTools array)
|
|
73
|
+
* @param options.args - Arguments to pass to the tool
|
|
74
|
+
* @returns The execution result with the tool's output data
|
|
75
|
+
*
|
|
76
|
+
* @throws {ContextError} With code `no_wallet` if wallet not set up
|
|
77
|
+
* @throws {ContextError} With code `insufficient_allowance` if Auto Pay not enabled
|
|
78
|
+
* @throws {ContextError} With code `payment_failed` if on-chain payment fails
|
|
79
|
+
* @throws {ContextError} With code `execution_failed` if tool execution fails
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // First, search for a tool
|
|
84
|
+
* const tools = await client.discovery.search("gas prices");
|
|
85
|
+
* const tool = tools[0];
|
|
86
|
+
*
|
|
87
|
+
* // Execute a specific method from the tool's mcpTools
|
|
88
|
+
* const result = await client.tools.execute({
|
|
89
|
+
* toolId: tool.id,
|
|
90
|
+
* toolName: tool.mcpTools[0].name, // e.g., "get_gas_prices"
|
|
91
|
+
* args: { chainId: 1 }
|
|
92
|
+
* });
|
|
93
|
+
*
|
|
94
|
+
* console.log(result.result); // The tool's output
|
|
95
|
+
* console.log(result.durationMs); // Execution time
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
async execute(options) {
|
|
99
|
+
const { toolId, toolName, args } = options;
|
|
100
|
+
const response = await this.client.fetch(
|
|
101
|
+
"/api/v1/tools/execute",
|
|
102
|
+
{
|
|
103
|
+
method: "POST",
|
|
104
|
+
body: JSON.stringify({ toolId, toolName, args })
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
if ("error" in response) {
|
|
108
|
+
throw new ContextError(
|
|
109
|
+
response.error,
|
|
110
|
+
response.code,
|
|
111
|
+
400,
|
|
112
|
+
response.helpUrl
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (response.success) {
|
|
116
|
+
return {
|
|
117
|
+
result: response.result,
|
|
118
|
+
tool: response.tool,
|
|
119
|
+
durationMs: response.durationMs
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
throw new ContextError("Unexpected response format from API");
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// src/client.ts
|
|
127
|
+
var ContextClient = class {
|
|
128
|
+
apiKey;
|
|
129
|
+
baseUrl;
|
|
130
|
+
/**
|
|
131
|
+
* Discovery resource for searching tools
|
|
132
|
+
*/
|
|
133
|
+
discovery;
|
|
134
|
+
/**
|
|
135
|
+
* Tools resource for executing tools
|
|
136
|
+
*/
|
|
137
|
+
tools;
|
|
138
|
+
/**
|
|
139
|
+
* Creates a new Context Protocol client
|
|
140
|
+
*
|
|
141
|
+
* @param options - Client configuration options
|
|
142
|
+
* @param options.apiKey - Your Context Protocol API key (format: sk_live_...)
|
|
143
|
+
* @param options.baseUrl - Optional base URL override (defaults to https://ctxprotocol.com)
|
|
144
|
+
*/
|
|
145
|
+
constructor(options) {
|
|
146
|
+
if (!options.apiKey) {
|
|
147
|
+
throw new ContextError("API key is required");
|
|
148
|
+
}
|
|
149
|
+
this.apiKey = options.apiKey;
|
|
150
|
+
this.baseUrl = (options.baseUrl ?? "https://ctxprotocol.com").replace(/\/$/, "");
|
|
151
|
+
this.discovery = new Discovery(this);
|
|
152
|
+
this.tools = new Tools(this);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Internal method for making authenticated HTTP requests
|
|
156
|
+
* All requests include the Authorization header with the API key
|
|
157
|
+
*
|
|
158
|
+
* @internal
|
|
159
|
+
*/
|
|
160
|
+
async fetch(endpoint, options = {}) {
|
|
161
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
162
|
+
const response = await fetch(url, {
|
|
163
|
+
...options,
|
|
164
|
+
headers: {
|
|
165
|
+
"Content-Type": "application/json",
|
|
166
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
167
|
+
...options.headers
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
if (!response.ok) {
|
|
171
|
+
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
|
172
|
+
let errorCode;
|
|
173
|
+
let helpUrl;
|
|
174
|
+
try {
|
|
175
|
+
const errorBody = await response.json();
|
|
176
|
+
if (errorBody.error) {
|
|
177
|
+
errorMessage = errorBody.error;
|
|
178
|
+
errorCode = errorBody.code;
|
|
179
|
+
helpUrl = errorBody.helpUrl;
|
|
180
|
+
}
|
|
181
|
+
} catch {
|
|
182
|
+
}
|
|
183
|
+
throw new ContextError(errorMessage, errorCode, response.status, helpUrl);
|
|
184
|
+
}
|
|
185
|
+
return response.json();
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
exports.ContextClient = ContextClient;
|
|
190
|
+
exports.ContextError = ContextError;
|
|
191
|
+
exports.Discovery = Discovery;
|
|
192
|
+
exports.Tools = Tools;
|
|
193
|
+
//# sourceMappingURL=index.cjs.map
|
|
194
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts","../src/resources/discovery.ts","../src/resources/tools.ts","../src/client.ts"],"names":[],"mappings":";;;AA6KO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA,EACtC,WAAA,CACE,OAAA,EACgB,IAAA,EACA,UAAA,EACA,OAAA,EAChB;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AAJG,IAAA,IAAA,CAAA,IAAA,GAAA,IAAA;AACA,IAAA,IAAA,CAAA,UAAA,GAAA,UAAA;AACA,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AAGhB,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AAAA,EACd;AACF;;;ACjLO,IAAM,YAAN,MAAgB;AAAA,EACrB,YAAoB,MAAA,EAAuB;AAAvB,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAAA,EAAwB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgB5C,MAAM,MAAA,CAAO,KAAA,EAAe,KAAA,EAAiC;AAC3D,IAAA,MAAM,MAAA,GAAS,IAAI,eAAA,EAAgB;AAEnC,IAAA,IAAI,KAAA,EAAO;AACT,MAAA,MAAA,CAAO,GAAA,CAAI,KAAK,KAAK,CA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AQ,CAAA,CAAA;AAEtC,IAAA,MAAM,QAAA,GAAW,MAAM,KAAA,CAAM,GAAA,EAAK;AAAA,MAChC,GAAG,OAAA;AAAA,MACH,OAAA,EAAS;AAAA,QACP,cAAA,EAAgB,kBAAA;AAAA,QAChB,aAAA,EAAe,CAAA,OAAA,EAAU,IAAA,CAAK,MAAM,CAAA,CAAA;AAAA,QACpC,GAAG,OAAA,CAAQ;AAAA;AACb,KACD,CAAA;AAED,IAAA,IAAI,CAAC,SAAS,EAAA,EAAI;AAChB,MAAA,IAAI,eAAe,CAAA,KAAA,EAAQ,QAAA,CAAS,MAAM,CAAA,EAAA,EAAK,SAAS,UAAU,CAAA,CAAA;AAClE,MAAA,IAAI,SAAA;AACJ,MAAA,IAAI,OAAA;AAEJ,MAAA,IAAI;AACF,QAAA,MAAM,SAAA,GAAY,MAAM,QAAA,CAAS,IAAA,EAAK;AACtC,QAAA,IAAI,UAAU,KAAA,EAAO;AACnB,UAAA,YAAA,GAAe,SAAA,CAAU,KAAA;AACzB,UAAA,SAAA,GAAY,SAAA,CAAU,IAAA;AACtB,UAAA,OAAA,GAAU,SAAA,CAAU,OAAA;AAAA,QACtB;AAAA,MACF,CAAA,CAAA,MAAQ;AAAA,MAER;AAEA,MAAA,MAAM,IAAI,YAAA,CAAa,YAAA,EAAc,SAAA,EAAW,QAAA,CAAS,QAAQ,OAAO,CAAA;AAAA,IAC1E;AAEA,IAAA,OAAO,SAAS,IAAA,EAAK;AAAA,EACvB;AACF","file":"index.cjs","sourcesContent":["/**\n * Configuration options for initializing the ContextClient\n */\nexport interface ContextClientOptions {\n /**\n * Your Context Protocol API key\n * @example \"sk_live_abc123...\"\n */\n apiKey: string;\n\n /**\n * Base URL for the Context Protocol API\n * @default \"https://ctxprotocol.com\"\n */\n baseUrl?: string;\n}\n\n/**\n * An individual MCP tool exposed by a tool listing\n */\nexport interface McpTool {\n /** Name of the MCP tool method */\n name: string;\n\n /** Description of what this method does */\n description: string;\n}\n\n/**\n * Represents a tool available on the Context Protocol marketplace\n */\nexport interface Tool {\n /** Unique identifier for the tool (UUID) */\n id: string;\n\n /** Human-readable name of the tool */\n name: string;\n\n /** Description of what the tool does */\n description: string;\n\n /** Price per execution in USDC */\n price: string;\n\n /** Tool category (e.g., \"defi\", \"nft\") */\n category?: string;\n\n /** Whether the tool is verified by Context Protocol */\n isVerified?: boolean;\n\n /** Type of tool (e.g., \"mcp\") */\n kind?: string;\n\n /**\n * Available MCP tool methods\n * Use items from this array as `toolName` when executing\n */\n mcpTools?: McpTool[];\n\n /** Creation timestamp */\n createdAt?: string;\n\n /** Last update timestamp */\n updatedAt?: string;\n}\n\n/**\n * Response from the tools search endpoint\n */\nexport interface SearchResponse {\n /** Array of matching tools */\n tools: Tool[];\n\n /** The search query that was used */\n query: string;\n\n /** Total number of results */\n count: number;\n}\n\n/**\n * Options for searching tools\n */\nexport interface SearchOptions {\n /** Search query (semantic search) */\n query?: string;\n\n /** Maximum number of results (1-50, default 10) */\n limit?: number;\n}\n\n/**\n * Options for executing a tool\n */\nexport interface ExecuteOptions {\n /** The UUID of the tool to execute (from search results) */\n toolId: string;\n\n /** The specific MCP tool name to call (from tool's mcpTools array) */\n toolName: string;\n\n /** Arguments to pass to the tool */\n args?: Record<string, unknown>;\n}\n\n/**\n * Successful execution response from the API\n */\nexport interface ExecuteApiSuccessResponse {\n success: true;\n\n /** The result data from the tool execution */\n result: unknown;\n\n /** Information about the executed tool */\n tool: {\n id: string;\n name: string;\n };\n\n /** Execution duration in milliseconds */\n durationMs: number;\n}\n\n/**\n * Error response from the API\n */\nexport interface ExecuteApiErrorResponse {\n /** Human-readable error message */\n error: string;\n\n /** Error code for programmatic handling */\n code?: ContextErrorCode;\n\n /** URL to help resolve the issue */\n helpUrl?: string;\n}\n\n/**\n * Raw API response from the execute endpoint\n */\nexport type ExecuteApiResponse = ExecuteApiSuccessResponse | ExecuteApiErrorResponse;\n\n/**\n * The resolved result returned to the user after SDK processing\n */\nexport interface ExecutionResult<T = unknown> {\n /** The data returned by the tool */\n result: T;\n\n /** Information about the executed tool */\n tool: {\n id: string;\n name: string;\n };\n\n /** Execution duration in milliseconds */\n durationMs: number;\n}\n\n/**\n * Specific error codes returned by the Context Protocol API\n */\nexport type ContextErrorCode =\n | \"unauthorized\"\n | \"no_wallet\"\n | \"insufficient_allowance\"\n | \"payment_failed\"\n | \"execution_failed\";\n\n/**\n * Error thrown by the Context Protocol client\n */\nexport class ContextError extends Error {\n constructor(\n message: string,\n public readonly code?: ContextErrorCode | string,\n public readonly statusCode?: number,\n public readonly helpUrl?: string\n ) {\n super(message);\n this.name = \"ContextError\";\n }\n}\n","import type { Tool, SearchResponse } from \"../types.js\";\nimport type { ContextClient } from \"../client.js\";\n\n/**\n * Discovery resource for searching and finding tools on the Context Protocol marketplace\n */\nexport class Discovery {\n constructor(private client: ContextClient) {}\n\n /**\n * Search for tools matching a query string\n *\n * @param query - The search query (e.g., \"gas prices\", \"nft metadata\")\n * @param limit - Maximum number of results (1-50, default 10)\n * @returns Array of matching tools\n *\n * @example\n * ```typescript\n * const tools = await client.discovery.search(\"gas prices\");\n * console.log(tools[0].name); // \"Gas Price Oracle\"\n * console.log(tools[0].mcpTools); // Available methods\n * ```\n */\n async search(query: string, limit?: number): Promise<Tool[]> {\n const params = new URLSearchParams();\n\n if (query) {\n params.set(\"q\", query);\n }\n\n if (limit !== undefined) {\n params.set(\"limit\", String(limit));\n }\n\n const queryString = params.toString();\n const endpoint = `/api/v1/tools/search${queryString ? `?${queryString}` : \"\"}`;\n\n const response = await this.client.fetch<SearchResponse>(endpoint);\n\n return response.tools;\n }\n\n /**\n * Get featured/popular tools (empty query search)\n *\n * @param limit - Maximum number of results (1-50, default 10)\n * @returns Array of featured tools\n *\n * @example\n * ```typescript\n * const featured = await client.discovery.getFeatured(5);\n * ```\n */\n async getFeatured(limit?: number): Promise<Tool[]> {\n return this.search(\"\", limit);\n }\n}\n","import type {\n ExecuteOptions,\n ExecuteApiResponse,\n ExecutionResult,\n} from \"../types.js\";\nimport { ContextError } from \"../types.js\";\nimport type { ContextClient } from \"../client.js\";\n\n/**\n * Tools resource for executing tools on the Context Protocol marketplace\n */\nexport class Tools {\n constructor(private client: ContextClient) {}\n\n /**\n * Execute a tool with the provided arguments\n *\n * @param options - Execution options\n * @param options.toolId - The UUID of the tool (from search results)\n * @param options.toolName - The specific MCP tool method to call (from tool's mcpTools array)\n * @param options.args - Arguments to pass to the tool\n * @returns The execution result with the tool's output data\n *\n * @throws {ContextError} With code `no_wallet` if wallet not set up\n * @throws {ContextError} With code `insufficient_allowance` if Auto Pay not enabled\n * @throws {ContextError} With code `payment_failed` if on-chain payment fails\n * @throws {ContextError} With code `execution_failed` if tool execution fails\n *\n * @example\n * ```typescript\n * // First, search for a tool\n * const tools = await client.discovery.search(\"gas prices\");\n * const tool = tools[0];\n *\n * // Execute a specific method from the tool's mcpTools\n * const result = await client.tools.execute({\n * toolId: tool.id,\n * toolName: tool.mcpTools[0].name, // e.g., \"get_gas_prices\"\n * args: { chainId: 1 }\n * });\n *\n * console.log(result.result); // The tool's output\n * console.log(result.durationMs); // Execution time\n * ```\n */\n async execute<T = unknown>(options: ExecuteOptions): Promise<ExecutionResult<T>> {\n const { toolId, toolName, args } = options;\n\n const response = await this.client.fetch<ExecuteApiResponse>(\n \"/api/v1/tools/execute\",\n {\n method: \"POST\",\n body: JSON.stringify({ toolId, toolName, args }),\n }\n );\n\n // Handle error response\n if (\"error\" in response) {\n throw new ContextError(\n response.error,\n response.code,\n 400,\n response.helpUrl\n );\n }\n\n // Handle success response\n if (response.success) {\n return {\n result: response.result as T,\n tool: response.tool,\n durationMs: response.durationMs,\n };\n }\n\n // Fallback - shouldn't reach here with valid API responses\n throw new ContextError(\"Unexpected response format from API\");\n }\n}\n","import type { ContextClientOptions } from \"./types.js\";\nimport { ContextError } from \"./types.js\";\nimport { Discovery } from \"./resources/discovery.js\";\nimport { Tools } from \"./resources/tools.js\";\n\n/**\n * The official TypeScript client for the Context Protocol.\n *\n * Use this client to discover and execute AI tools programmatically.\n *\n * @example\n * ```typescript\n * import { ContextClient } from \"@contextprotocol/client\";\n *\n * const client = new ContextClient({\n * apiKey: \"sk_live_...\"\n * });\n *\n * // Discover tools\n * const tools = await client.discovery.search(\"gas prices\");\n *\n * // Execute a tool method\n * const result = await client.tools.execute({\n * toolId: tools[0].id,\n * toolName: tools[0].mcpTools[0].name,\n * args: { chainId: 1 }\n * });\n * ```\n */\nexport class ContextClient {\n private readonly apiKey: string;\n private readonly baseUrl: string;\n\n /**\n * Discovery resource for searching tools\n */\n public readonly discovery: Discovery;\n\n /**\n * Tools resource for executing tools\n */\n public readonly tools: Tools;\n\n /**\n * Creates a new Context Protocol client\n *\n * @param options - Client configuration options\n * @param options.apiKey - Your Context Protocol API key (format: sk_live_...)\n * @param options.baseUrl - Optional base URL override (defaults to https://ctxprotocol.com)\n */\n constructor(options: ContextClientOptions) {\n if (!options.apiKey) {\n throw new ContextError(\"API key is required\");\n }\n\n this.apiKey = options.apiKey;\n this.baseUrl = (options.baseUrl ?? \"https://ctxprotocol.com\").replace(/\\/$/, \"\");\n\n // Initialize resources\n this.discovery = new Discovery(this);\n this.tools = new Tools(this);\n }\n\n /**\n * Internal method for making authenticated HTTP requests\n * All requests include the Authorization header with the API key\n *\n * @internal\n */\n async fetch<T>(endpoint: string, options: RequestInit = {}): Promise<T> {\n const url = `${this.baseUrl}${endpoint}`;\n\n const response = await fetch(url, {\n ...options,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.apiKey}`,\n ...options.headers,\n },\n });\n\n if (!response.ok) {\n let errorMessage = `HTTP ${response.status}: ${response.statusText}`;\n let errorCode: string | undefined;\n let helpUrl: string | undefined;\n\n try {\n const errorBody = await response.json();\n if (errorBody.error) {\n errorMessage = errorBody.error;\n errorCode = errorBody.code;\n helpUrl = errorBody.helpUrl;\n }\n } catch {\n // Use default error message if JSON parsing fails\n }\n\n throw new ContextError(errorMessage, errorCode, response.status, helpUrl);\n }\n\n return response.json() as Promise<T>;\n }\n}\n"]}
|