@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/dist/index.js
CHANGED
|
@@ -1,49 +1,189 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var ContextError = class extends Error {
|
|
3
|
+
constructor(message, code, statusCode, helpUrl) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.statusCode = statusCode;
|
|
7
|
+
this.helpUrl = helpUrl;
|
|
8
|
+
this.name = "ContextError";
|
|
9
|
+
}
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
|
|
12
|
+
// src/resources/discovery.ts
|
|
13
|
+
var Discovery = class {
|
|
14
|
+
constructor(client) {
|
|
15
|
+
this.client = client;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Search for tools matching a query string
|
|
19
|
+
*
|
|
20
|
+
* @param query - The search query (e.g., "gas prices", "nft metadata")
|
|
21
|
+
* @param limit - Maximum number of results (1-50, default 10)
|
|
22
|
+
* @returns Array of matching tools
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const tools = await client.discovery.search("gas prices");
|
|
27
|
+
* console.log(tools[0].name); // "Gas Price Oracle"
|
|
28
|
+
* console.log(tools[0].mcpTools); // Available methods
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
async search(query, limit) {
|
|
32
|
+
const params = new URLSearchParams();
|
|
33
|
+
if (query) {
|
|
34
|
+
params.set("q", query);
|
|
35
|
+
}
|
|
36
|
+
if (limit !== void 0) {
|
|
37
|
+
params.set("limit", String(limit));
|
|
38
|
+
}
|
|
39
|
+
const queryString = params.toString();
|
|
40
|
+
const endpoint = `/api/v1/tools/search${queryString ? `?${queryString}` : ""}`;
|
|
41
|
+
const response = await this.client.fetch(endpoint);
|
|
42
|
+
return response.tools;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get featured/popular tools (empty query search)
|
|
46
|
+
*
|
|
47
|
+
* @param limit - Maximum number of results (1-50, default 10)
|
|
48
|
+
* @returns Array of featured tools
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const featured = await client.discovery.getFeatured(5);
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
async getFeatured(limit) {
|
|
56
|
+
return this.search("", limit);
|
|
15
57
|
}
|
|
16
|
-
return to;
|
|
17
58
|
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
59
|
|
|
20
|
-
// src/
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
60
|
+
// src/resources/tools.ts
|
|
61
|
+
var Tools = class {
|
|
62
|
+
constructor(client) {
|
|
63
|
+
this.client = client;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Execute a tool with the provided arguments
|
|
67
|
+
*
|
|
68
|
+
* @param options - Execution options
|
|
69
|
+
* @param options.toolId - The UUID of the tool (from search results)
|
|
70
|
+
* @param options.toolName - The specific MCP tool method to call (from tool's mcpTools array)
|
|
71
|
+
* @param options.args - Arguments to pass to the tool
|
|
72
|
+
* @returns The execution result with the tool's output data
|
|
73
|
+
*
|
|
74
|
+
* @throws {ContextError} With code `no_wallet` if wallet not set up
|
|
75
|
+
* @throws {ContextError} With code `insufficient_allowance` if Auto Pay not enabled
|
|
76
|
+
* @throws {ContextError} With code `payment_failed` if on-chain payment fails
|
|
77
|
+
* @throws {ContextError} With code `execution_failed` if tool execution fails
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* // First, search for a tool
|
|
82
|
+
* const tools = await client.discovery.search("gas prices");
|
|
83
|
+
* const tool = tools[0];
|
|
84
|
+
*
|
|
85
|
+
* // Execute a specific method from the tool's mcpTools
|
|
86
|
+
* const result = await client.tools.execute({
|
|
87
|
+
* toolId: tool.id,
|
|
88
|
+
* toolName: tool.mcpTools[0].name, // e.g., "get_gas_prices"
|
|
89
|
+
* args: { chainId: 1 }
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* console.log(result.result); // The tool's output
|
|
93
|
+
* console.log(result.durationMs); // Execution time
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
async execute(options) {
|
|
97
|
+
const { toolId, toolName, args } = options;
|
|
98
|
+
const response = await this.client.fetch(
|
|
99
|
+
"/api/v1/tools/execute",
|
|
100
|
+
{
|
|
101
|
+
method: "POST",
|
|
102
|
+
body: JSON.stringify({ toolId, toolName, args })
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
if ("error" in response) {
|
|
106
|
+
throw new ContextError(
|
|
107
|
+
response.error,
|
|
108
|
+
response.code,
|
|
109
|
+
400,
|
|
110
|
+
response.helpUrl
|
|
111
|
+
);
|
|
42
112
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
113
|
+
if (response.success) {
|
|
114
|
+
return {
|
|
115
|
+
result: response.result,
|
|
116
|
+
tool: response.tool,
|
|
117
|
+
durationMs: response.durationMs
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
throw new ContextError("Unexpected response format from API");
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/client.ts
|
|
125
|
+
var ContextClient = class {
|
|
126
|
+
apiKey;
|
|
127
|
+
baseUrl;
|
|
128
|
+
/**
|
|
129
|
+
* Discovery resource for searching tools
|
|
130
|
+
*/
|
|
131
|
+
discovery;
|
|
132
|
+
/**
|
|
133
|
+
* Tools resource for executing tools
|
|
134
|
+
*/
|
|
135
|
+
tools;
|
|
136
|
+
/**
|
|
137
|
+
* Creates a new Context Protocol client
|
|
138
|
+
*
|
|
139
|
+
* @param options - Client configuration options
|
|
140
|
+
* @param options.apiKey - Your Context Protocol API key (format: sk_live_...)
|
|
141
|
+
* @param options.baseUrl - Optional base URL override (defaults to https://ctxprotocol.com)
|
|
142
|
+
*/
|
|
143
|
+
constructor(options) {
|
|
144
|
+
if (!options.apiKey) {
|
|
145
|
+
throw new ContextError("API key is required");
|
|
146
|
+
}
|
|
147
|
+
this.apiKey = options.apiKey;
|
|
148
|
+
this.baseUrl = (options.baseUrl ?? "https://ctxprotocol.com").replace(/\/$/, "");
|
|
149
|
+
this.discovery = new Discovery(this);
|
|
150
|
+
this.tools = new Tools(this);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Internal method for making authenticated HTTP requests
|
|
154
|
+
* All requests include the Authorization header with the API key
|
|
155
|
+
*
|
|
156
|
+
* @internal
|
|
157
|
+
*/
|
|
158
|
+
async fetch(endpoint, options = {}) {
|
|
159
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
160
|
+
const response = await fetch(url, {
|
|
161
|
+
...options,
|
|
162
|
+
headers: {
|
|
163
|
+
"Content-Type": "application/json",
|
|
164
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
165
|
+
...options.headers
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
if (!response.ok) {
|
|
169
|
+
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
|
170
|
+
let errorCode;
|
|
171
|
+
let helpUrl;
|
|
172
|
+
try {
|
|
173
|
+
const errorBody = await response.json();
|
|
174
|
+
if (errorBody.error) {
|
|
175
|
+
errorMessage = errorBody.error;
|
|
176
|
+
errorCode = errorBody.code;
|
|
177
|
+
helpUrl = errorBody.helpUrl;
|
|
178
|
+
}
|
|
179
|
+
} catch {
|
|
180
|
+
}
|
|
181
|
+
throw new ContextError(errorMessage, errorCode, response.status, helpUrl);
|
|
182
|
+
}
|
|
183
|
+
return response.json();
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export { ContextClient, ContextError, Discovery, Tools };
|
|
188
|
+
//# sourceMappingURL=index.js.map
|
|
189
|
+
//# sourceMappingURL=index.js.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;AAA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AAI,KAAA,CAAM,IAAI,CAAA;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,KAAA,CAAS,QAAA,EAAkB,OAAA,GAAuB,EAAC,EAAe;AACtE,IAAA,MAAM,GAAA,GAAM,CAAA,EAAG,IAAA,CAAK,OAAO,GAAG,QAAQ,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.js","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"]}
|
package/package.json
CHANGED
|
@@ -1,33 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ctxprotocol/sdk",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
"
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Official TypeScript SDK for the Context Protocol - Discover and execute AI tools programmatically",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
7
21
|
"files": [
|
|
8
22
|
"dist"
|
|
9
23
|
],
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"dev": "tsup --watch",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"prepublishOnly": "npm run build"
|
|
13
29
|
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"context-protocol",
|
|
32
|
+
"ctxprotocol",
|
|
33
|
+
"mcp",
|
|
34
|
+
"ai",
|
|
35
|
+
"tools",
|
|
36
|
+
"sdk",
|
|
37
|
+
"api-client",
|
|
38
|
+
"typescript"
|
|
39
|
+
],
|
|
14
40
|
"author": "Context Protocol",
|
|
15
41
|
"license": "MIT",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "https://github.com/ctxprotocol/sdk"
|
|
18
45
|
},
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"vitest": "^2.1.4",
|
|
23
|
-
"@biomejs/biome": "^2.2.2"
|
|
46
|
+
"homepage": "https://ctxprotocol.com",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/ctxprotocol/sdk/issues"
|
|
24
49
|
},
|
|
25
|
-
"
|
|
26
|
-
"
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"tsup": "^8.3.5",
|
|
52
|
+
"typescript": "^5.7.2"
|
|
27
53
|
},
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"lint": "biome check src",
|
|
31
|
-
"test": "vitest"
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=18.0.0"
|
|
32
56
|
}
|
|
33
|
-
}
|
|
57
|
+
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
type ToolContext = {
|
|
4
|
-
/**
|
|
5
|
-
* Raw headers from the incoming request. Useful if contributors need to
|
|
6
|
-
* inspect authentication or tracing metadata.
|
|
7
|
-
*/
|
|
8
|
-
headers?: Record<string, string | string[] | undefined>;
|
|
9
|
-
};
|
|
10
|
-
type DefineHttpToolOptions<I extends z.ZodTypeAny, O extends z.ZodTypeAny> = {
|
|
11
|
-
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> = {
|
|
19
|
-
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;
|
|
35
|
-
};
|
|
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>>>;
|
|
39
|
-
|
|
40
|
-
export { type ContextResponse, type DefineHttpToolOptions, type ExecuteHttpToolOptions, type HttpToolDefinition, type ToolContext, defineHttpTool, executeHttpTool };
|
package/dist/index.mjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
function defineHttpTool(options) {
|
|
3
|
-
return options;
|
|
4
|
-
}
|
|
5
|
-
async function executeHttpTool(tool, input, options = {}) {
|
|
6
|
-
const parsedInput = tool.inputSchema.parse(input);
|
|
7
|
-
const data = await tool.handler(parsedInput, {
|
|
8
|
-
headers: options.headers
|
|
9
|
-
});
|
|
10
|
-
const parsedOutput = tool.outputSchema.parse(data);
|
|
11
|
-
return {
|
|
12
|
-
data: parsedOutput,
|
|
13
|
-
meta: {
|
|
14
|
-
tool: tool.name,
|
|
15
|
-
version: tool.version,
|
|
16
|
-
generatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
export {
|
|
21
|
-
defineHttpTool,
|
|
22
|
-
executeHttpTool
|
|
23
|
-
};
|