@jalpp/mcp-adapter 0.2.0 → 0.3.0
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 +180 -102
- package/dist/index.d.ts +157 -30
- package/dist/index.js +38 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @jalpp/mcp-adapter
|
|
2
2
|
|
|
3
|
-
Lightweight adapter utilities for registering tools on an [MCP](https://modelcontextprotocol.io) server with full TypeScript type safety. Supports manual tool registration and automatic HTTP endpoint-to-tool bridging with built-in auth.
|
|
3
|
+
Lightweight adapter utilities for registering tools on an [MCP](https://modelcontextprotocol.io) server with full TypeScript type safety. Supports manual tool registration and automatic HTTP endpoint-to-tool bridging with built-in auth, path variables, and method-specific adapters.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -16,26 +16,170 @@ npm install @modelcontextprotocol/sdk zod axios
|
|
|
16
16
|
|
|
17
17
|
## Adapters
|
|
18
18
|
|
|
19
|
-
| Adapter | Description |
|
|
20
|
-
|
|
21
|
-
| `toolAdapter` | Register a tool with a typed callback |
|
|
22
|
-
| `toolContentAdapter` | Normalize a result into a `CallToolResult` |
|
|
23
|
-
| `httpToolAdapter` | Register
|
|
19
|
+
| Adapter | Method | Description |
|
|
20
|
+
|---------|--------|-------------|
|
|
21
|
+
| `toolAdapter` | any | Register a tool with a typed callback |
|
|
22
|
+
| `toolContentAdapter` | — | Normalize a result into a `CallToolResult` |
|
|
23
|
+
| `httpToolAdapter` | any | Register any HTTP endpoint as a tool |
|
|
24
|
+
| `getToolAdapter` | GET | Register a GET endpoint — args → query params |
|
|
25
|
+
| `postToolAdapter` | POST | Register a POST endpoint — args → request body |
|
|
26
|
+
| `putToolAdapter` | PUT | Register a PUT endpoint — args → request body |
|
|
27
|
+
| `patchToolAdapter` | PATCH | Register a PATCH endpoint — args → request body |
|
|
28
|
+
| `deleteToolAdapter` | DELETE | Register a DELETE endpoint — args → query params |
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Path Variables
|
|
33
|
+
|
|
34
|
+
All HTTP adapters support `:paramName` path variable syntax. Any input arg whose name matches a path variable is interpolated into the URL and removed from query params / request body.
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
getToolAdapter(server, {
|
|
38
|
+
name: "get-game-details",
|
|
39
|
+
description: "Fetch a game by ID",
|
|
40
|
+
endpoint: "https://api.example.com/games/:gameId",
|
|
41
|
+
inputSchema: {
|
|
42
|
+
gameId: z.string().describe("Game ID"),
|
|
43
|
+
expand: z.string().optional().describe("Optional fields to expand"),
|
|
44
|
+
},
|
|
45
|
+
auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
46
|
+
});
|
|
47
|
+
// GET https://api.example.com/games/abc123?expand=moves
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## `getToolAdapter`
|
|
53
|
+
|
|
54
|
+
Registers a GET endpoint as an MCP tool. Remaining args (after path variable interpolation) are sent as **query parameters**.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { getToolAdapter } from "@jalpp/mcp-adapter";
|
|
58
|
+
import z from "zod";
|
|
59
|
+
|
|
60
|
+
getToolAdapter(server, {
|
|
61
|
+
name: "get-user",
|
|
62
|
+
description: "Fetch a user by ID",
|
|
63
|
+
endpoint: "https://api.example.com/users/:userId",
|
|
64
|
+
inputSchema: {
|
|
65
|
+
userId: z.string().describe("User ID"),
|
|
66
|
+
expand: z.string().optional().describe("Comma-separated fields to expand"),
|
|
67
|
+
},
|
|
68
|
+
auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
69
|
+
});
|
|
70
|
+
// → GET https://api.example.com/users/abc123?expand=profile
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## `postToolAdapter`
|
|
76
|
+
|
|
77
|
+
Registers a POST endpoint as an MCP tool. Remaining args (after path variable interpolation) are sent as the **JSON request body**.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { postToolAdapter } from "@jalpp/mcp-adapter";
|
|
81
|
+
|
|
82
|
+
postToolAdapter(server, {
|
|
83
|
+
name: "create-post",
|
|
84
|
+
description: "Create a new post for a user",
|
|
85
|
+
endpoint: "https://api.example.com/users/:userId/posts",
|
|
86
|
+
inputSchema: {
|
|
87
|
+
userId: z.string().describe("User ID"),
|
|
88
|
+
title: z.string().describe("Post title"),
|
|
89
|
+
body: z.string().describe("Post body"),
|
|
90
|
+
},
|
|
91
|
+
auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
92
|
+
});
|
|
93
|
+
// → POST https://api.example.com/users/abc123/posts { title, body }
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## `putToolAdapter`
|
|
99
|
+
|
|
100
|
+
Registers a PUT endpoint as an MCP tool. Remaining args are sent as the **JSON request body**.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
putToolAdapter(server, {
|
|
104
|
+
name: "update-user",
|
|
105
|
+
description: "Replace a user record",
|
|
106
|
+
endpoint: "https://api.example.com/users/:userId",
|
|
107
|
+
inputSchema: {
|
|
108
|
+
userId: z.string(),
|
|
109
|
+
name: z.string(),
|
|
110
|
+
email: z.string(),
|
|
111
|
+
},
|
|
112
|
+
auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
113
|
+
});
|
|
114
|
+
// → PUT https://api.example.com/users/abc123 { name, email }
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## `patchToolAdapter`
|
|
120
|
+
|
|
121
|
+
Registers a PATCH endpoint as an MCP tool. Remaining args are sent as the **JSON request body**.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
patchToolAdapter(server, {
|
|
125
|
+
name: "update-post-title",
|
|
126
|
+
description: "Partially update a post",
|
|
127
|
+
endpoint: "https://api.example.com/posts/:postId",
|
|
128
|
+
inputSchema: {
|
|
129
|
+
postId: z.string(),
|
|
130
|
+
title: z.string(),
|
|
131
|
+
},
|
|
132
|
+
auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
133
|
+
});
|
|
134
|
+
// → PATCH https://api.example.com/posts/xyz789 { title }
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## `deleteToolAdapter`
|
|
140
|
+
|
|
141
|
+
Registers a DELETE endpoint as an MCP tool. Remaining args (after path variable interpolation) are sent as **query parameters**.
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
deleteToolAdapter(server, {
|
|
145
|
+
name: "delete-post",
|
|
146
|
+
description: "Delete a post by ID",
|
|
147
|
+
endpoint: "https://api.example.com/posts/:postId",
|
|
148
|
+
inputSchema: { postId: z.string() },
|
|
149
|
+
auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
150
|
+
});
|
|
151
|
+
// → DELETE https://api.example.com/posts/xyz789
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## `httpToolAdapter`
|
|
157
|
+
|
|
158
|
+
Lower-level adapter that accepts an explicit `method` field. Use this when the method needs to be dynamic or you prefer a single unified call style.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import { httpToolAdapter } from "@jalpp/mcp-adapter";
|
|
162
|
+
|
|
163
|
+
httpToolAdapter(server, {
|
|
164
|
+
name: "get-analysis",
|
|
165
|
+
description: "Fetch position analysis",
|
|
166
|
+
endpoint: "https://api.example.com/analyze",
|
|
167
|
+
method: "POST",
|
|
168
|
+
inputSchema: { fen: z.string(), depth: z.number() },
|
|
169
|
+
auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
170
|
+
});
|
|
171
|
+
```
|
|
24
172
|
|
|
25
173
|
---
|
|
26
174
|
|
|
27
175
|
## `toolAdapter`
|
|
28
176
|
|
|
29
|
-
Registers a typed
|
|
177
|
+
Registers a tool with a fully custom typed callback. Use when you need data transformation, custom error handling, or logic that goes beyond a single HTTP call.
|
|
30
178
|
|
|
31
179
|
**With input schema:**
|
|
32
180
|
|
|
33
181
|
```ts
|
|
34
182
|
import { toolAdapter, toolContentAdapter } from "@jalpp/mcp-adapter";
|
|
35
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
36
|
-
import z from "zod";
|
|
37
|
-
|
|
38
|
-
const server = new McpServer({ name: "my-server", version: "1.0.0" });
|
|
39
183
|
|
|
40
184
|
toolAdapter(server, {
|
|
41
185
|
name: "get-analysis",
|
|
@@ -59,9 +203,7 @@ toolAdapter(server, {
|
|
|
59
203
|
```ts
|
|
60
204
|
toolAdapter(server, {
|
|
61
205
|
name: "get-status",
|
|
62
|
-
config: {
|
|
63
|
-
description: "Returns current server status",
|
|
64
|
-
},
|
|
206
|
+
config: { description: "Returns current server status" },
|
|
65
207
|
cb: async () => {
|
|
66
208
|
const { data, error } = await myService.getStatus();
|
|
67
209
|
return toolContentAdapter(data ?? {}, error);
|
|
@@ -73,108 +215,50 @@ toolAdapter(server, {
|
|
|
73
215
|
|
|
74
216
|
## `toolContentAdapter`
|
|
75
217
|
|
|
76
|
-
Normalizes a
|
|
218
|
+
Normalizes a result into a `CallToolResult` text block. If `error` is defined it takes priority; otherwise `data` is serialized as pretty-printed JSON.
|
|
77
219
|
|
|
78
220
|
```ts
|
|
79
|
-
import { toolContentAdapter } from "@jalpp/mcp-adapter";
|
|
80
|
-
|
|
81
221
|
return toolContentAdapter(data ?? {}, error);
|
|
82
222
|
```
|
|
83
223
|
|
|
84
224
|
| Argument | Type | Description |
|
|
85
225
|
|----------|------|-------------|
|
|
86
226
|
| `data` | `object` | Successful result payload |
|
|
87
|
-
| `error` | `string \| undefined` | Error message — takes priority over `data`
|
|
227
|
+
| `error` | `string \| undefined` | Error message — takes priority over `data` |
|
|
88
228
|
|
|
89
229
|
---
|
|
90
230
|
|
|
91
|
-
##
|
|
92
|
-
|
|
93
|
-
Registers an HTTP endpoint directly as an MCP tool. Handles the full request lifecycle — auth headers, query params vs request body, and error mapping — so you only need to declare the tool metadata and schema.
|
|
94
|
-
|
|
95
|
-
- **GET** requests map input args to **query parameters**
|
|
96
|
-
- **POST / PUT / PATCH / DELETE** map input args to the **JSON request body**
|
|
97
|
-
|
|
98
|
-
**With input schema:**
|
|
99
|
-
|
|
100
|
-
```ts
|
|
101
|
-
import { httpToolAdapter } from "@jalpp/mcp-adapter";
|
|
102
|
-
import z from "zod";
|
|
231
|
+
## Authentication
|
|
103
232
|
|
|
104
|
-
|
|
105
|
-
name: "get-analysis",
|
|
106
|
-
description: "Fetch position analysis from external API",
|
|
107
|
-
endpoint: "https://api.example.com/analyze",
|
|
108
|
-
method: "POST",
|
|
109
|
-
inputSchema: {
|
|
110
|
-
fen: z.string().describe("FEN string"),
|
|
111
|
-
depth: z.number().min(1).max(20),
|
|
112
|
-
},
|
|
113
|
-
auth: {
|
|
114
|
-
type: "bearer",
|
|
115
|
-
token: process.env.API_TOKEN!,
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
**Without input schema:**
|
|
233
|
+
All HTTP adapters accept an optional `auth` field. Three strategies are supported:
|
|
121
234
|
|
|
235
|
+
**Bearer token** — `Authorization: Bearer <token>`:
|
|
122
236
|
```ts
|
|
123
|
-
|
|
124
|
-
name: "get-server-status",
|
|
125
|
-
description: "Fetch API health status",
|
|
126
|
-
endpoint: "https://api.example.com/status",
|
|
127
|
-
method: "GET",
|
|
128
|
-
});
|
|
237
|
+
auth: { type: "bearer", token: process.env.API_TOKEN! }
|
|
129
238
|
```
|
|
130
239
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
Three auth strategies are supported, all passed via the `auth` field:
|
|
134
|
-
|
|
135
|
-
**Bearer token** — sends `Authorization: Bearer <token>`:
|
|
136
|
-
|
|
240
|
+
**API key** — custom header:
|
|
137
241
|
```ts
|
|
138
|
-
auth: {
|
|
139
|
-
type: "bearer",
|
|
140
|
-
token: process.env.API_TOKEN!,
|
|
141
|
-
}
|
|
242
|
+
auth: { type: "apikey", header: "X-API-Key", key: process.env.API_KEY! }
|
|
142
243
|
```
|
|
143
244
|
|
|
144
|
-
**
|
|
145
|
-
|
|
245
|
+
**Basic auth** — `Authorization: Basic <base64>`:
|
|
146
246
|
```ts
|
|
147
|
-
auth: {
|
|
148
|
-
type: "apikey",
|
|
149
|
-
header: "X-API-Key",
|
|
150
|
-
key: process.env.API_KEY!,
|
|
151
|
-
}
|
|
247
|
+
auth: { type: "basic", username: "user", password: process.env.PASSWORD! }
|
|
152
248
|
```
|
|
153
249
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
```ts
|
|
157
|
-
auth: {
|
|
158
|
-
type: "basic",
|
|
159
|
-
username: "myuser",
|
|
160
|
-
password: process.env.PASSWORD!,
|
|
161
|
-
}
|
|
162
|
-
```
|
|
250
|
+
---
|
|
163
251
|
|
|
164
|
-
|
|
252
|
+
## Extra axios config
|
|
165
253
|
|
|
166
|
-
Pass any
|
|
254
|
+
Pass any [axios request config](https://axios-http.com/docs/req_config) via `axiosConfig`:
|
|
167
255
|
|
|
168
256
|
```ts
|
|
169
|
-
|
|
257
|
+
getToolAdapter(server, {
|
|
170
258
|
name: "get-data",
|
|
171
259
|
description: "Fetch with custom timeout",
|
|
172
260
|
endpoint: "https://api.example.com/data",
|
|
173
|
-
|
|
174
|
-
axiosConfig: {
|
|
175
|
-
timeout: 5000,
|
|
176
|
-
headers: { "Accept-Language": "en" },
|
|
177
|
-
},
|
|
261
|
+
axiosConfig: { timeout: 5000 },
|
|
178
262
|
});
|
|
179
263
|
```
|
|
180
264
|
|
|
@@ -182,23 +266,17 @@ httpToolAdapter(server, {
|
|
|
182
266
|
|
|
183
267
|
## API Reference
|
|
184
268
|
|
|
185
|
-
###
|
|
186
|
-
|
|
187
|
-
| Overload | When to use |
|
|
188
|
-
|----------|-------------|
|
|
189
|
-
| `toolAdapter<T>(server, ToolInputAdapterWithSchema<T>)` | Tool that receives typed input args |
|
|
190
|
-
| `toolAdapter(server, ToolInputAdapterWithoutSchema)` | Tool that takes no input |
|
|
191
|
-
|
|
192
|
-
### `toolContentAdapter(data, error)`
|
|
193
|
-
|
|
194
|
-
Returns a `CallToolResult` with a single `text` content block.
|
|
269
|
+
### Method-specific adapters
|
|
195
270
|
|
|
196
|
-
|
|
271
|
+
| Function | Method | Args mapping |
|
|
272
|
+
|----------|--------|--------------|
|
|
273
|
+
| `getToolAdapter` | GET | remaining args → query params |
|
|
274
|
+
| `postToolAdapter` | POST | remaining args → request body |
|
|
275
|
+
| `putToolAdapter` | PUT | remaining args → request body |
|
|
276
|
+
| `patchToolAdapter` | PATCH | remaining args → request body |
|
|
277
|
+
| `deleteToolAdapter` | DELETE | remaining args → query params |
|
|
197
278
|
|
|
198
|
-
|
|
199
|
-
|----------|-------------|
|
|
200
|
-
| `httpToolAdapter<T>(server, HttpToolAdapterWithSchema<T>)` | Endpoint that receives typed input args |
|
|
201
|
-
| `httpToolAdapter(server, HttpToolAdapterWithoutSchema)` | Endpoint that takes no input |
|
|
279
|
+
All support optional `inputSchema` and `:paramName` path variables.
|
|
202
280
|
|
|
203
281
|
### Auth types
|
|
204
282
|
|
package/dist/index.d.ts
CHANGED
|
@@ -124,23 +124,23 @@ interface BasicAuth {
|
|
|
124
124
|
username: string;
|
|
125
125
|
password: string;
|
|
126
126
|
}
|
|
127
|
-
/**
|
|
128
|
-
* Union of all supported authentication strategies.
|
|
129
|
-
*/
|
|
127
|
+
/** Union of all supported authentication strategies. */
|
|
130
128
|
type HttpAuth = ApiKeyAuth | BearerAuth | BasicAuth;
|
|
131
129
|
/**
|
|
132
130
|
* Configuration for registering an HTTP endpoint as an MCP tool with an input schema.
|
|
133
|
-
*
|
|
134
|
-
* @template T - Zod raw shape inferred from `inputSchema`, typed through to the request.
|
|
131
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
135
132
|
*/
|
|
136
133
|
interface HttpToolAdapterWithSchema<T extends ZodRawShapeCompat> {
|
|
137
134
|
/** Unique tool name in the MCP registry. */
|
|
138
135
|
name: string;
|
|
139
136
|
/** Description of the tool shown to the model. */
|
|
140
137
|
description: string;
|
|
141
|
-
/**
|
|
138
|
+
/**
|
|
139
|
+
* Full URL of the API endpoint. Supports path variable templates using `:paramName` syntax.
|
|
140
|
+
* @example "https://api.example.com/users/:userId/posts"
|
|
141
|
+
*/
|
|
142
142
|
endpoint: string;
|
|
143
|
-
/** HTTP method to use.
|
|
143
|
+
/** HTTP method to use. */
|
|
144
144
|
method: HttpMethod;
|
|
145
145
|
/** Zod shape defining the tool's input arguments. */
|
|
146
146
|
inputSchema: T;
|
|
@@ -157,7 +157,10 @@ interface HttpToolAdapterWithoutSchema {
|
|
|
157
157
|
name: string;
|
|
158
158
|
/** Description of the tool shown to the model. */
|
|
159
159
|
description: string;
|
|
160
|
-
/**
|
|
160
|
+
/**
|
|
161
|
+
* Full URL of the API endpoint. Supports path variable templates using `:paramName` syntax.
|
|
162
|
+
* @example "https://api.example.com/status"
|
|
163
|
+
*/
|
|
161
164
|
endpoint: string;
|
|
162
165
|
/** HTTP method to use. */
|
|
163
166
|
method: HttpMethod;
|
|
@@ -168,41 +171,165 @@ interface HttpToolAdapterWithoutSchema {
|
|
|
168
171
|
axiosConfig?: AxiosRequestConfig;
|
|
169
172
|
}
|
|
170
173
|
/**
|
|
171
|
-
*
|
|
174
|
+
* Configuration specific to GET tool adapters.
|
|
175
|
+
* Input args are mapped to query parameters unless consumed by path variables.
|
|
176
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
177
|
+
*/
|
|
178
|
+
type GetToolAdapterConfig<T extends ZodRawShapeCompat> = Omit<HttpToolAdapterWithSchema<T>, "method">;
|
|
179
|
+
/**
|
|
180
|
+
* Configuration specific to GET tool adapters without input schema.
|
|
181
|
+
*/
|
|
182
|
+
type GetToolAdapterConfigNoSchema = Omit<HttpToolAdapterWithoutSchema, "method">;
|
|
183
|
+
/**
|
|
184
|
+
* Configuration specific to POST tool adapters.
|
|
185
|
+
* Input args are sent as the JSON request body unless consumed by path variables.
|
|
186
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
187
|
+
*/
|
|
188
|
+
type PostToolAdapterConfig<T extends ZodRawShapeCompat> = Omit<HttpToolAdapterWithSchema<T>, "method">;
|
|
189
|
+
/**
|
|
190
|
+
* Configuration specific to POST tool adapters without input schema.
|
|
191
|
+
*/
|
|
192
|
+
type PostToolAdapterConfigNoSchema = Omit<HttpToolAdapterWithoutSchema, "method">;
|
|
193
|
+
/**
|
|
194
|
+
* Configuration specific to PUT tool adapters.
|
|
195
|
+
* Input args are sent as the JSON request body unless consumed by path variables.
|
|
196
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
197
|
+
*/
|
|
198
|
+
type PutToolAdapterConfig<T extends ZodRawShapeCompat> = Omit<HttpToolAdapterWithSchema<T>, "method">;
|
|
199
|
+
/**
|
|
200
|
+
* Configuration specific to PATCH tool adapters.
|
|
201
|
+
* Input args are sent as the JSON request body unless consumed by path variables.
|
|
202
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
203
|
+
*/
|
|
204
|
+
type PatchToolAdapterConfig<T extends ZodRawShapeCompat> = Omit<HttpToolAdapterWithSchema<T>, "method">;
|
|
205
|
+
/**
|
|
206
|
+
* Configuration specific to DELETE tool adapters.
|
|
207
|
+
* Input args are sent as query parameters unless consumed by path variables.
|
|
208
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
209
|
+
*/
|
|
210
|
+
type DeleteToolAdapterConfig<T extends ZodRawShapeCompat> = Omit<HttpToolAdapterWithSchema<T>, "method">;
|
|
211
|
+
/**
|
|
212
|
+
* Core HTTP tool adapter. Registers any HTTP endpoint as an MCP tool.
|
|
213
|
+
*
|
|
214
|
+
* Prefer the method-specific adapters (`getToolAdapter`, `postToolAdapter`, etc.)
|
|
215
|
+
* for cleaner, self-documenting code.
|
|
172
216
|
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
217
|
+
* - Path variables (`:paramName`) are interpolated from input args.
|
|
218
|
+
* - GET/DELETE: remaining args → query parameters.
|
|
219
|
+
* - POST/PUT/PATCH: remaining args → JSON request body.
|
|
175
220
|
*
|
|
176
221
|
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
177
222
|
* @param server - The `McpServer` instance to register the tool on.
|
|
178
|
-
* @param adapter - HTTP tool configuration
|
|
223
|
+
* @param adapter - HTTP tool configuration.
|
|
224
|
+
*/
|
|
225
|
+
declare function httpToolAdapter<T extends ZodRawShapeCompat>(server: McpServer, adapter: HttpToolAdapterWithSchema<T>): void;
|
|
226
|
+
declare function httpToolAdapter(server: McpServer, adapter: HttpToolAdapterWithoutSchema): void;
|
|
227
|
+
/**
|
|
228
|
+
* Registers a GET endpoint as an MCP tool.
|
|
229
|
+
*
|
|
230
|
+
* - Path variables (`:paramName`) are interpolated from input args.
|
|
231
|
+
* - Remaining args are sent as **query parameters**.
|
|
232
|
+
*
|
|
233
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
234
|
+
* @param server - The `McpServer` instance to register the tool on.
|
|
235
|
+
* @param config - Tool configuration without `method`.
|
|
179
236
|
*
|
|
180
237
|
* @example
|
|
181
|
-
*
|
|
182
|
-
* name: "get-
|
|
183
|
-
* description: "Fetch
|
|
184
|
-
* endpoint: "https://api.example.com/
|
|
185
|
-
*
|
|
186
|
-
* inputSchema: { fen: z.string(), depth: z.number() },
|
|
238
|
+
* getToolAdapter(server, {
|
|
239
|
+
* name: "get-user",
|
|
240
|
+
* description: "Fetch a user by ID",
|
|
241
|
+
* endpoint: "https://api.example.com/users/:userId",
|
|
242
|
+
* inputSchema: { userId: z.string(), expand: z.string().optional() },
|
|
187
243
|
* auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
188
244
|
* });
|
|
245
|
+
* // GET https://api.example.com/users/abc123?expand=profile
|
|
189
246
|
*/
|
|
190
|
-
declare function
|
|
247
|
+
declare function getToolAdapter<T extends ZodRawShapeCompat>(server: McpServer, config: GetToolAdapterConfig<T>): void;
|
|
248
|
+
declare function getToolAdapter(server: McpServer, config: GetToolAdapterConfigNoSchema): void;
|
|
191
249
|
/**
|
|
192
|
-
* Registers
|
|
250
|
+
* Registers a POST endpoint as an MCP tool.
|
|
193
251
|
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
252
|
+
* - Path variables (`:paramName`) are interpolated from input args.
|
|
253
|
+
* - Remaining args are sent as the **JSON request body**.
|
|
254
|
+
*
|
|
255
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
256
|
+
* @param server - The `McpServer` instance to register the tool on.
|
|
257
|
+
* @param config - Tool configuration without `method`.
|
|
196
258
|
*
|
|
197
259
|
* @example
|
|
198
|
-
*
|
|
199
|
-
* name: "
|
|
200
|
-
* description: "
|
|
201
|
-
* endpoint: "https://api.example.com/
|
|
202
|
-
*
|
|
203
|
-
* auth: { type: "
|
|
260
|
+
* postToolAdapter(server, {
|
|
261
|
+
* name: "create-post",
|
|
262
|
+
* description: "Create a new post for a user",
|
|
263
|
+
* endpoint: "https://api.example.com/users/:userId/posts",
|
|
264
|
+
* inputSchema: { userId: z.string(), title: z.string(), body: z.string() },
|
|
265
|
+
* auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
204
266
|
* });
|
|
267
|
+
* // POST https://api.example.com/users/abc123/posts { title, body }
|
|
205
268
|
*/
|
|
206
|
-
declare function
|
|
269
|
+
declare function postToolAdapter<T extends ZodRawShapeCompat>(server: McpServer, config: PostToolAdapterConfig<T>): void;
|
|
270
|
+
declare function postToolAdapter(server: McpServer, config: PostToolAdapterConfigNoSchema): void;
|
|
271
|
+
/**
|
|
272
|
+
* Registers a PUT endpoint as an MCP tool.
|
|
273
|
+
*
|
|
274
|
+
* - Path variables (`:paramName`) are interpolated from input args.
|
|
275
|
+
* - Remaining args are sent as the **JSON request body**.
|
|
276
|
+
*
|
|
277
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
278
|
+
* @param server - The `McpServer` instance to register the tool on.
|
|
279
|
+
* @param config - Tool configuration without `method`.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* putToolAdapter(server, {
|
|
283
|
+
* name: "update-user",
|
|
284
|
+
* description: "Replace a user record",
|
|
285
|
+
* endpoint: "https://api.example.com/users/:userId",
|
|
286
|
+
* inputSchema: { userId: z.string(), name: z.string(), email: z.string() },
|
|
287
|
+
* auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
288
|
+
* });
|
|
289
|
+
* // PUT https://api.example.com/users/abc123 { name, email }
|
|
290
|
+
*/
|
|
291
|
+
declare function putToolAdapter<T extends ZodRawShapeCompat>(server: McpServer, config: PutToolAdapterConfig<T>): void;
|
|
292
|
+
/**
|
|
293
|
+
* Registers a PATCH endpoint as an MCP tool.
|
|
294
|
+
*
|
|
295
|
+
* - Path variables (`:paramName`) are interpolated from input args.
|
|
296
|
+
* - Remaining args are sent as the **JSON request body**.
|
|
297
|
+
*
|
|
298
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
299
|
+
* @param server - The `McpServer` instance to register the tool on.
|
|
300
|
+
* @param config - Tool configuration without `method`.
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* patchToolAdapter(server, {
|
|
304
|
+
* name: "update-post-title",
|
|
305
|
+
* description: "Partially update a post",
|
|
306
|
+
* endpoint: "https://api.example.com/posts/:postId",
|
|
307
|
+
* inputSchema: { postId: z.string(), title: z.string() },
|
|
308
|
+
* auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
309
|
+
* });
|
|
310
|
+
* // PATCH https://api.example.com/posts/xyz789 { title }
|
|
311
|
+
*/
|
|
312
|
+
declare function patchToolAdapter<T extends ZodRawShapeCompat>(server: McpServer, config: PatchToolAdapterConfig<T>): void;
|
|
313
|
+
/**
|
|
314
|
+
* Registers a DELETE endpoint as an MCP tool.
|
|
315
|
+
*
|
|
316
|
+
* - Path variables (`:paramName`) are interpolated from input args.
|
|
317
|
+
* - Remaining args are sent as **query parameters**.
|
|
318
|
+
*
|
|
319
|
+
* @template T - Zod raw shape inferred from `inputSchema`.
|
|
320
|
+
* @param server - The `McpServer` instance to register the tool on.
|
|
321
|
+
* @param config - Tool configuration without `method`.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* deleteToolAdapter(server, {
|
|
325
|
+
* name: "delete-post",
|
|
326
|
+
* description: "Delete a post by ID",
|
|
327
|
+
* endpoint: "https://api.example.com/posts/:postId",
|
|
328
|
+
* inputSchema: { postId: z.string() },
|
|
329
|
+
* auth: { type: "bearer", token: process.env.API_TOKEN! },
|
|
330
|
+
* });
|
|
331
|
+
* // DELETE https://api.example.com/posts/xyz789
|
|
332
|
+
*/
|
|
333
|
+
declare function deleteToolAdapter<T extends ZodRawShapeCompat>(server: McpServer, config: DeleteToolAdapterConfig<T>): void;
|
|
207
334
|
|
|
208
|
-
export { type ApiKeyAuth, type BasicAuth, type BearerAuth, type HttpAuth, type HttpMethod, type HttpToolAdapterWithSchema, type HttpToolAdapterWithoutSchema, httpToolAdapter, toolAdapter, toolContentAdapter };
|
|
335
|
+
export { type ApiKeyAuth, type BasicAuth, type BearerAuth, type DeleteToolAdapterConfig, type GetToolAdapterConfig, type GetToolAdapterConfigNoSchema, type HttpAuth, type HttpMethod, type HttpToolAdapterWithSchema, type HttpToolAdapterWithoutSchema, type PatchToolAdapterConfig, type PostToolAdapterConfig, type PostToolAdapterConfigNoSchema, type PutToolAdapterConfig, deleteToolAdapter, getToolAdapter, httpToolAdapter, patchToolAdapter, postToolAdapter, putToolAdapter, toolAdapter, toolContentAdapter };
|
package/dist/index.js
CHANGED
|
@@ -39,18 +39,32 @@ function buildAuthHeaders(auth) {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
+
function resolvePathParams(endpoint, args) {
|
|
43
|
+
const remainingArgs = { ...args };
|
|
44
|
+
const url = endpoint.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, (_, key) => {
|
|
45
|
+
if (key in remainingArgs) {
|
|
46
|
+
const value = remainingArgs[key];
|
|
47
|
+
delete remainingArgs[key];
|
|
48
|
+
return encodeURIComponent(String(value));
|
|
49
|
+
}
|
|
50
|
+
return `:${key}`;
|
|
51
|
+
});
|
|
52
|
+
return { url, remainingArgs };
|
|
53
|
+
}
|
|
42
54
|
async function executeRequest(endpoint, method, args, auth, extraConfig) {
|
|
43
55
|
try {
|
|
44
56
|
const authHeaders = auth ? buildAuthHeaders(auth) : {};
|
|
57
|
+
const { url, remainingArgs } = resolvePathParams(endpoint, args);
|
|
58
|
+
const isQueryMethod = method === "GET" || method === "DELETE";
|
|
45
59
|
const config = {
|
|
46
|
-
url
|
|
60
|
+
url,
|
|
47
61
|
method,
|
|
48
62
|
headers: {
|
|
49
63
|
"Content-Type": "application/json",
|
|
50
64
|
...authHeaders,
|
|
51
65
|
...extraConfig?.headers
|
|
52
66
|
},
|
|
53
|
-
...
|
|
67
|
+
...isQueryMethod ? { params: remainingArgs } : { data: remainingArgs },
|
|
54
68
|
...extraConfig
|
|
55
69
|
};
|
|
56
70
|
const response = await axios(config);
|
|
@@ -69,10 +83,7 @@ function httpToolAdapter(server, adapter) {
|
|
|
69
83
|
const { endpoint, method, auth, axiosConfig, inputSchema } = adapter;
|
|
70
84
|
toolAdapter(server, {
|
|
71
85
|
name: adapter.name,
|
|
72
|
-
config: {
|
|
73
|
-
description: adapter.description,
|
|
74
|
-
inputSchema
|
|
75
|
-
},
|
|
86
|
+
config: { description: adapter.description, inputSchema },
|
|
76
87
|
cb: (async (args) => {
|
|
77
88
|
const { data, error } = await executeRequest(endpoint, method, args, auth, axiosConfig);
|
|
78
89
|
return toolContentAdapter(data ?? {}, error);
|
|
@@ -81,9 +92,7 @@ function httpToolAdapter(server, adapter) {
|
|
|
81
92
|
} else {
|
|
82
93
|
toolAdapter(server, {
|
|
83
94
|
name: adapter.name,
|
|
84
|
-
config: {
|
|
85
|
-
description: adapter.description
|
|
86
|
-
},
|
|
95
|
+
config: { description: adapter.description },
|
|
87
96
|
cb: async () => {
|
|
88
97
|
const { data, error } = await executeRequest(
|
|
89
98
|
adapter.endpoint,
|
|
@@ -97,8 +106,28 @@ function httpToolAdapter(server, adapter) {
|
|
|
97
106
|
});
|
|
98
107
|
}
|
|
99
108
|
}
|
|
109
|
+
function getToolAdapter(server, config) {
|
|
110
|
+
httpToolAdapter(server, { ...config, method: "GET" });
|
|
111
|
+
}
|
|
112
|
+
function postToolAdapter(server, config) {
|
|
113
|
+
httpToolAdapter(server, { ...config, method: "POST" });
|
|
114
|
+
}
|
|
115
|
+
function putToolAdapter(server, config) {
|
|
116
|
+
httpToolAdapter(server, { ...config, method: "PUT" });
|
|
117
|
+
}
|
|
118
|
+
function patchToolAdapter(server, config) {
|
|
119
|
+
httpToolAdapter(server, { ...config, method: "PATCH" });
|
|
120
|
+
}
|
|
121
|
+
function deleteToolAdapter(server, config) {
|
|
122
|
+
httpToolAdapter(server, { ...config, method: "DELETE" });
|
|
123
|
+
}
|
|
100
124
|
export {
|
|
125
|
+
deleteToolAdapter,
|
|
126
|
+
getToolAdapter,
|
|
101
127
|
httpToolAdapter,
|
|
128
|
+
patchToolAdapter,
|
|
129
|
+
postToolAdapter,
|
|
130
|
+
putToolAdapter,
|
|
102
131
|
toolAdapter,
|
|
103
132
|
toolContentAdapter
|
|
104
133
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jalpp/mcp-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Adapter utilities for registering MCP tools with full TypeScript type safety — supports typed callbacks and automatic HTTP endpoint bridging with auth",
|
|
5
5
|
"author": "jalpp",
|
|
6
6
|
"license": "MIT",
|