@ctxprotocol/sdk 0.1.1
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 +103 -0
- package/dist/index.d.mts +40 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +49 -0
- package/dist/index.mjs +23 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Context SDK
|
|
2
|
+
> Server-side helpers for publishing HTTP tools to the Context marketplace.
|
|
3
|
+
|
|
4
|
+
The Context SDK is a tiny server-side helper for contributors who want to
|
|
5
|
+
expose HTTP-based tools to the Context marketplace in a safe, typed, and
|
|
6
|
+
consistent way.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
Using **pnpm**:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @ctxprotocol/sdk zod
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Using **npm**:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @ctxprotocol/sdk zod
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Quick start
|
|
27
|
+
|
|
28
|
+
Define a tool with input/output schemas and a handler, then expose it via a
|
|
29
|
+
single HTTP endpoint:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { z } from "zod";
|
|
33
|
+
import { defineHttpTool, executeHttpTool } from "@ctxprotocol/sdk";
|
|
34
|
+
|
|
35
|
+
const tool = defineHttpTool({
|
|
36
|
+
name: "blocknative_gas_base",
|
|
37
|
+
inputSchema: z.object({ chainId: z.number().default(8453) }),
|
|
38
|
+
outputSchema: z.object({
|
|
39
|
+
endpoint: z.string(),
|
|
40
|
+
data: z.unknown(),
|
|
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
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export async function handler(req: Request) {
|
|
56
|
+
const body = await req.json();
|
|
57
|
+
const result = await executeHttpTool(tool, body.input, {
|
|
58
|
+
headers: Object.fromEntries(req.headers.entries()),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return Response.json(result);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
See `examples/blocknative-contributor/` for a full Express server that wraps
|
|
66
|
+
the Blocknative Gas Platform.
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Scripts
|
|
71
|
+
|
|
72
|
+
From the repo root:
|
|
73
|
+
|
|
74
|
+
- **Build** – bundle to `dist/`:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pnpm run build
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- **Lint** – Biome checks:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
pnpm run lint
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- **Test** – Vitest (add tests as needed):
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pnpm run test
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Publishing (for maintainers)
|
|
95
|
+
|
|
96
|
+
Install dependencies, build once to ensure everything compiles, then publish:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
pnpm install
|
|
100
|
+
pnpm run build
|
|
101
|
+
pnpm publish --access public
|
|
102
|
+
```
|
|
103
|
+
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
defineHttpTool: () => defineHttpTool,
|
|
24
|
+
executeHttpTool: () => executeHttpTool
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
function defineHttpTool(options) {
|
|
28
|
+
return options;
|
|
29
|
+
}
|
|
30
|
+
async function executeHttpTool(tool, input, options = {}) {
|
|
31
|
+
const parsedInput = tool.inputSchema.parse(input);
|
|
32
|
+
const data = await tool.handler(parsedInput, {
|
|
33
|
+
headers: options.headers
|
|
34
|
+
});
|
|
35
|
+
const parsedOutput = tool.outputSchema.parse(data);
|
|
36
|
+
return {
|
|
37
|
+
data: parsedOutput,
|
|
38
|
+
meta: {
|
|
39
|
+
tool: tool.name,
|
|
40
|
+
version: tool.version,
|
|
41
|
+
generatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
46
|
+
0 && (module.exports = {
|
|
47
|
+
defineHttpTool,
|
|
48
|
+
executeHttpTool
|
|
49
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ctxprotocol/sdk",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Server-side helpers for publishing Context HTTP tools.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/ctxprotocol/sdk.git"
|
|
13
|
+
},
|
|
14
|
+
"author": "Context Protocol",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"zod": "^3.25.76"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"tsup": "^8.3.0",
|
|
21
|
+
"typescript": "^5.6.3",
|
|
22
|
+
"vitest": "^2.1.4",
|
|
23
|
+
"@biomejs/biome": "^2.2.2"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
30
|
+
"lint": "biome check src",
|
|
31
|
+
"test": "vitest"
|
|
32
|
+
}
|
|
33
|
+
}
|