@kizunajs/mcp 2.0.0-beta.23 → 2.0.0-beta.25
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/dist/index.cjs +63 -33
- package/dist/index.mjs +64 -34
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -102,6 +102,15 @@ const buildToolOutputSchema = (route) => {
|
|
|
102
102
|
*/
|
|
103
103
|
const APPROVAL_KEY = "approved";
|
|
104
104
|
/**
|
|
105
|
+
* How long a client may keep the tool list. It changes only when the app
|
|
106
|
+
* deploys. `private`, because the endpoint may sit behind a credential a shared
|
|
107
|
+
* cache should not answer for.
|
|
108
|
+
*/
|
|
109
|
+
const TOOL_LIST_CACHE = {
|
|
110
|
+
ttlMs: 300 * 1e3,
|
|
111
|
+
cacheScope: "private"
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
105
114
|
* Ask the person before running, when the call needs approval. Returns the
|
|
106
115
|
* elicitation on the first call, a refusal when they declined, and nothing once
|
|
107
116
|
* they approved.
|
|
@@ -130,6 +139,20 @@ const approval = (route, context) => {
|
|
|
130
139
|
};
|
|
131
140
|
};
|
|
132
141
|
/**
|
|
142
|
+
* What the SDK publishes for a tool. Input passes through unchecked, so the
|
|
143
|
+
* call is validated once, by `validateRequest`, as an HTTP request is. Output
|
|
144
|
+
* keeps the route's own check.
|
|
145
|
+
*/
|
|
146
|
+
const publishedSchema = (jsonSchema, check) => ({ "~standard": {
|
|
147
|
+
version: 1,
|
|
148
|
+
vendor: "kizuna",
|
|
149
|
+
validate: check === void 0 ? (value) => ({ value }) : check["~standard"].validate,
|
|
150
|
+
jsonSchema: {
|
|
151
|
+
input: () => jsonSchema,
|
|
152
|
+
output: () => jsonSchema
|
|
153
|
+
}
|
|
154
|
+
} });
|
|
155
|
+
/**
|
|
133
156
|
* What the route says about itself, over what its method already implies.
|
|
134
157
|
*/
|
|
135
158
|
const buildToolAnnotations = (route) => {
|
|
@@ -167,6 +190,23 @@ const buildMcpTools = (routes) => {
|
|
|
167
190
|
}
|
|
168
191
|
return definitions;
|
|
169
192
|
};
|
|
193
|
+
const publishedTools = /* @__PURE__ */ new WeakMap();
|
|
194
|
+
/**
|
|
195
|
+
* Every tool with its schemas converted, built once per API rather than on
|
|
196
|
+
* every MCP request.
|
|
197
|
+
*/
|
|
198
|
+
const publishedToolsFor = (routes) => {
|
|
199
|
+
const cached = publishedTools.get(routes);
|
|
200
|
+
if (cached) return cached;
|
|
201
|
+
const built = buildMcpTools(routes).map((definition) => ({
|
|
202
|
+
definition,
|
|
203
|
+
inputSchema: publishedSchema((0, kizunajs_adapter.toolInputJsonSchema)(definition.route)),
|
|
204
|
+
outputSchema: publishedSchema((0, kizunajs_adapter.toolJsonSchema)(definition.outputSchema, "output"), definition.outputSchema),
|
|
205
|
+
annotations: buildToolAnnotations(definition.route)
|
|
206
|
+
}));
|
|
207
|
+
publishedTools.set(routes, built);
|
|
208
|
+
return built;
|
|
209
|
+
};
|
|
170
210
|
/**
|
|
171
211
|
* What a client puts in front of the model before it picks a tool.
|
|
172
212
|
*/
|
|
@@ -207,7 +247,7 @@ const toolEnvelope = (status, body) => {
|
|
|
207
247
|
return {
|
|
208
248
|
content: [{
|
|
209
249
|
type: "text",
|
|
210
|
-
text: JSON.stringify(envelope
|
|
250
|
+
text: JSON.stringify(envelope)
|
|
211
251
|
}],
|
|
212
252
|
...isError ? {} : { structuredContent: envelope },
|
|
213
253
|
isError
|
|
@@ -219,7 +259,7 @@ const toolError = (status, body) => ({
|
|
|
219
259
|
text: JSON.stringify({
|
|
220
260
|
status,
|
|
221
261
|
body
|
|
222
|
-
}
|
|
262
|
+
})
|
|
223
263
|
}],
|
|
224
264
|
isError: true
|
|
225
265
|
});
|
|
@@ -304,21 +344,16 @@ const executeToolCall = async (route, routeKey, args, router, handlerContext, gu
|
|
|
304
344
|
params,
|
|
305
345
|
query,
|
|
306
346
|
body,
|
|
307
|
-
headers: {}
|
|
347
|
+
headers: credentialHeaders ?? {}
|
|
308
348
|
});
|
|
309
|
-
if (!validation.ok)
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
}, null, 2)
|
|
319
|
-
}],
|
|
320
|
-
isError: true
|
|
321
|
-
};
|
|
349
|
+
if (!validation.ok) {
|
|
350
|
+
const formatted = (0, kizunajs_adapter.formatValidationError)(validation.error);
|
|
351
|
+
return toolEnvelope(400, (0, kizunajs.problemDetails)(400, formatted.detail, { errors: formatted.issues.map((issue) => ({
|
|
352
|
+
code: issue.code ?? "custom",
|
|
353
|
+
path: issue.path,
|
|
354
|
+
message: issue.message
|
|
355
|
+
})) }));
|
|
356
|
+
}
|
|
322
357
|
const handler = resolveHandler(router, routeKey);
|
|
323
358
|
if (typeof handler !== "function") return {
|
|
324
359
|
content: [{
|
|
@@ -326,7 +361,7 @@ const executeToolCall = async (route, routeKey, args, router, handlerContext, gu
|
|
|
326
361
|
text: JSON.stringify({
|
|
327
362
|
status: 500,
|
|
328
363
|
body: { detail: `Handler not implemented: ${routeKey}` }
|
|
329
|
-
}
|
|
364
|
+
})
|
|
330
365
|
}],
|
|
331
366
|
isError: true
|
|
332
367
|
};
|
|
@@ -350,16 +385,8 @@ const executeToolCall = async (route, routeKey, args, router, handlerContext, gu
|
|
|
350
385
|
return toolEnvelope(result.status, result.body);
|
|
351
386
|
} catch (error) {
|
|
352
387
|
if (error instanceof kizunajs_adapter.ResponseError) return toolEnvelope(error.status, error.body);
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
type: "text",
|
|
356
|
-
text: JSON.stringify({
|
|
357
|
-
status: 500,
|
|
358
|
-
body: { detail: error instanceof Error ? error.message : "Internal Server Error" }
|
|
359
|
-
}, null, 2)
|
|
360
|
-
}],
|
|
361
|
-
isError: true
|
|
362
|
-
};
|
|
388
|
+
console.error(`[kizuna] tool "${routeKey}" threw:`, error);
|
|
389
|
+
return toolEnvelope(500, (0, kizunajs.problemDetails)(500, "Internal Server Error"));
|
|
363
390
|
}
|
|
364
391
|
};
|
|
365
392
|
/**
|
|
@@ -382,17 +409,20 @@ const createMcpServer = (api, options) => {
|
|
|
382
409
|
const guardSchema = api[kizunajs_adapter.GUARD_SCHEMA_META];
|
|
383
410
|
const contextResolvers = api[kizunajs_adapter.REQUEST_CONTEXT_META];
|
|
384
411
|
const contract = (0, kizunajs_adapter.contractOf)(api);
|
|
385
|
-
const
|
|
412
|
+
const tools = publishedToolsFor(api.routes);
|
|
386
413
|
const server = new _modelcontextprotocol_server.McpServer({
|
|
387
414
|
name: options?.name ?? "MCP Server",
|
|
388
415
|
version: options?.version ?? "1.0.0"
|
|
389
|
-
}, {
|
|
390
|
-
|
|
416
|
+
}, {
|
|
417
|
+
instructions: buildInstructions(contract, tools.map(({ definition }) => definition), options?.instructions),
|
|
418
|
+
cacheHints: { "tools/list": TOOL_LIST_CACHE }
|
|
419
|
+
});
|
|
420
|
+
for (const { definition, inputSchema, outputSchema, annotations } of tools) server.registerTool(definition.name, {
|
|
391
421
|
...definition.title === void 0 ? {} : { title: definition.title },
|
|
392
422
|
description: definition.description,
|
|
393
|
-
inputSchema
|
|
394
|
-
outputSchema
|
|
395
|
-
annotations
|
|
423
|
+
inputSchema,
|
|
424
|
+
outputSchema,
|
|
425
|
+
annotations
|
|
396
426
|
}, async (args, context) => {
|
|
397
427
|
const refusal = approval(definition.route, context);
|
|
398
428
|
if (refusal !== void 0) return refusal;
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { RoutePathSchema, definePlugin, route } from "kizunajs/plugin";
|
|
|
3
3
|
import { ProtectedResourceMetadataSchema } from "kizunajs/schemas";
|
|
4
4
|
import { assertCanonicalResourceUri, buildProtectedResourceMetadata, declaredScopes, problemDetails, problemFromBody } from "kizunajs";
|
|
5
5
|
import { McpServer, acceptedContent, createMcpHandler, inputRequired } from "@modelcontextprotocol/server";
|
|
6
|
-
import { GUARDS_META, GUARD_SCHEMA_META, REQUEST_CONTEXT_META, ROUTER_META, ResponseError, SCHEMES_META, adapterContextOf, bearerChallenge, buildToolInputSchema, contractOf, describeTool, extractCredential, flattenRoutes, guardDenyFor, insufficientScope, isGuardDenial, rawResponse, requiresDenial, resolveSecurityRequirements, selectToolRoutes, toolOptionsOf, validateRequest, withPermissions } from "kizunajs/adapter";
|
|
6
|
+
import { GUARDS_META, GUARD_SCHEMA_META, REQUEST_CONTEXT_META, ROUTER_META, ResponseError, SCHEMES_META, adapterContextOf, bearerChallenge, buildToolInputSchema, contractOf, describeTool, extractCredential, flattenRoutes, formatValidationError, guardDenyFor, insufficientScope, isGuardDenial, rawResponse, requiresDenial, resolveSecurityRequirements, selectToolRoutes, toolInputJsonSchema, toolJsonSchema, toolOptionsOf, validateRequest, withPermissions } from "kizunajs/adapter";
|
|
7
7
|
import { deriveToolNames, isJsonMediaType, isStreamResponse, isSuccessStatus, isVoidSchema, resolveResponseBody, resolveResponseContentType } from "kizunajs/generator";
|
|
8
8
|
//#region src/oauth.ts
|
|
9
9
|
const McpOAuthSchema = z.object({
|
|
@@ -101,6 +101,15 @@ const buildToolOutputSchema = (route) => {
|
|
|
101
101
|
*/
|
|
102
102
|
const APPROVAL_KEY = "approved";
|
|
103
103
|
/**
|
|
104
|
+
* How long a client may keep the tool list. It changes only when the app
|
|
105
|
+
* deploys. `private`, because the endpoint may sit behind a credential a shared
|
|
106
|
+
* cache should not answer for.
|
|
107
|
+
*/
|
|
108
|
+
const TOOL_LIST_CACHE = {
|
|
109
|
+
ttlMs: 300 * 1e3,
|
|
110
|
+
cacheScope: "private"
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
104
113
|
* Ask the person before running, when the call needs approval. Returns the
|
|
105
114
|
* elicitation on the first call, a refusal when they declined, and nothing once
|
|
106
115
|
* they approved.
|
|
@@ -129,6 +138,20 @@ const approval = (route, context) => {
|
|
|
129
138
|
};
|
|
130
139
|
};
|
|
131
140
|
/**
|
|
141
|
+
* What the SDK publishes for a tool. Input passes through unchecked, so the
|
|
142
|
+
* call is validated once, by `validateRequest`, as an HTTP request is. Output
|
|
143
|
+
* keeps the route's own check.
|
|
144
|
+
*/
|
|
145
|
+
const publishedSchema = (jsonSchema, check) => ({ "~standard": {
|
|
146
|
+
version: 1,
|
|
147
|
+
vendor: "kizuna",
|
|
148
|
+
validate: check === void 0 ? (value) => ({ value }) : check["~standard"].validate,
|
|
149
|
+
jsonSchema: {
|
|
150
|
+
input: () => jsonSchema,
|
|
151
|
+
output: () => jsonSchema
|
|
152
|
+
}
|
|
153
|
+
} });
|
|
154
|
+
/**
|
|
132
155
|
* What the route says about itself, over what its method already implies.
|
|
133
156
|
*/
|
|
134
157
|
const buildToolAnnotations = (route) => {
|
|
@@ -166,6 +189,23 @@ const buildMcpTools = (routes) => {
|
|
|
166
189
|
}
|
|
167
190
|
return definitions;
|
|
168
191
|
};
|
|
192
|
+
const publishedTools = /* @__PURE__ */ new WeakMap();
|
|
193
|
+
/**
|
|
194
|
+
* Every tool with its schemas converted, built once per API rather than on
|
|
195
|
+
* every MCP request.
|
|
196
|
+
*/
|
|
197
|
+
const publishedToolsFor = (routes) => {
|
|
198
|
+
const cached = publishedTools.get(routes);
|
|
199
|
+
if (cached) return cached;
|
|
200
|
+
const built = buildMcpTools(routes).map((definition) => ({
|
|
201
|
+
definition,
|
|
202
|
+
inputSchema: publishedSchema(toolInputJsonSchema(definition.route)),
|
|
203
|
+
outputSchema: publishedSchema(toolJsonSchema(definition.outputSchema, "output"), definition.outputSchema),
|
|
204
|
+
annotations: buildToolAnnotations(definition.route)
|
|
205
|
+
}));
|
|
206
|
+
publishedTools.set(routes, built);
|
|
207
|
+
return built;
|
|
208
|
+
};
|
|
169
209
|
/**
|
|
170
210
|
* What a client puts in front of the model before it picks a tool.
|
|
171
211
|
*/
|
|
@@ -206,7 +246,7 @@ const toolEnvelope = (status, body) => {
|
|
|
206
246
|
return {
|
|
207
247
|
content: [{
|
|
208
248
|
type: "text",
|
|
209
|
-
text: JSON.stringify(envelope
|
|
249
|
+
text: JSON.stringify(envelope)
|
|
210
250
|
}],
|
|
211
251
|
...isError ? {} : { structuredContent: envelope },
|
|
212
252
|
isError
|
|
@@ -218,7 +258,7 @@ const toolError = (status, body) => ({
|
|
|
218
258
|
text: JSON.stringify({
|
|
219
259
|
status,
|
|
220
260
|
body
|
|
221
|
-
}
|
|
261
|
+
})
|
|
222
262
|
}],
|
|
223
263
|
isError: true
|
|
224
264
|
});
|
|
@@ -303,21 +343,16 @@ const executeToolCall = async (route, routeKey, args, router, handlerContext, gu
|
|
|
303
343
|
params,
|
|
304
344
|
query,
|
|
305
345
|
body,
|
|
306
|
-
headers: {}
|
|
346
|
+
headers: credentialHeaders ?? {}
|
|
307
347
|
});
|
|
308
|
-
if (!validation.ok)
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}
|
|
317
|
-
}, null, 2)
|
|
318
|
-
}],
|
|
319
|
-
isError: true
|
|
320
|
-
};
|
|
348
|
+
if (!validation.ok) {
|
|
349
|
+
const formatted = formatValidationError(validation.error);
|
|
350
|
+
return toolEnvelope(400, problemDetails(400, formatted.detail, { errors: formatted.issues.map((issue) => ({
|
|
351
|
+
code: issue.code ?? "custom",
|
|
352
|
+
path: issue.path,
|
|
353
|
+
message: issue.message
|
|
354
|
+
})) }));
|
|
355
|
+
}
|
|
321
356
|
const handler = resolveHandler(router, routeKey);
|
|
322
357
|
if (typeof handler !== "function") return {
|
|
323
358
|
content: [{
|
|
@@ -325,7 +360,7 @@ const executeToolCall = async (route, routeKey, args, router, handlerContext, gu
|
|
|
325
360
|
text: JSON.stringify({
|
|
326
361
|
status: 500,
|
|
327
362
|
body: { detail: `Handler not implemented: ${routeKey}` }
|
|
328
|
-
}
|
|
363
|
+
})
|
|
329
364
|
}],
|
|
330
365
|
isError: true
|
|
331
366
|
};
|
|
@@ -349,16 +384,8 @@ const executeToolCall = async (route, routeKey, args, router, handlerContext, gu
|
|
|
349
384
|
return toolEnvelope(result.status, result.body);
|
|
350
385
|
} catch (error) {
|
|
351
386
|
if (error instanceof ResponseError) return toolEnvelope(error.status, error.body);
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
type: "text",
|
|
355
|
-
text: JSON.stringify({
|
|
356
|
-
status: 500,
|
|
357
|
-
body: { detail: error instanceof Error ? error.message : "Internal Server Error" }
|
|
358
|
-
}, null, 2)
|
|
359
|
-
}],
|
|
360
|
-
isError: true
|
|
361
|
-
};
|
|
387
|
+
console.error(`[kizuna] tool "${routeKey}" threw:`, error);
|
|
388
|
+
return toolEnvelope(500, problemDetails(500, "Internal Server Error"));
|
|
362
389
|
}
|
|
363
390
|
};
|
|
364
391
|
/**
|
|
@@ -381,17 +408,20 @@ const createMcpServer = (api, options) => {
|
|
|
381
408
|
const guardSchema = api[GUARD_SCHEMA_META];
|
|
382
409
|
const contextResolvers = api[REQUEST_CONTEXT_META];
|
|
383
410
|
const contract = contractOf(api);
|
|
384
|
-
const
|
|
411
|
+
const tools = publishedToolsFor(api.routes);
|
|
385
412
|
const server = new McpServer({
|
|
386
413
|
name: options?.name ?? "MCP Server",
|
|
387
414
|
version: options?.version ?? "1.0.0"
|
|
388
|
-
}, {
|
|
389
|
-
|
|
415
|
+
}, {
|
|
416
|
+
instructions: buildInstructions(contract, tools.map(({ definition }) => definition), options?.instructions),
|
|
417
|
+
cacheHints: { "tools/list": TOOL_LIST_CACHE }
|
|
418
|
+
});
|
|
419
|
+
for (const { definition, inputSchema, outputSchema, annotations } of tools) server.registerTool(definition.name, {
|
|
390
420
|
...definition.title === void 0 ? {} : { title: definition.title },
|
|
391
421
|
description: definition.description,
|
|
392
|
-
inputSchema
|
|
393
|
-
outputSchema
|
|
394
|
-
annotations
|
|
422
|
+
inputSchema,
|
|
423
|
+
outputSchema,
|
|
424
|
+
annotations
|
|
395
425
|
}, async (args, context) => {
|
|
396
426
|
const refusal = approval(definition.route, context);
|
|
397
427
|
if (refusal !== void 0) return refusal;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kizunajs/mcp",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.25",
|
|
4
4
|
"description": "MCP endpoint for Kizuna.js, so AI assistants can call your routes",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kizunajs",
|
|
@@ -60,13 +60,13 @@
|
|
|
60
60
|
"@modelcontextprotocol/server": "^2.0.0"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
|
-
"kizunajs": "2.0.0-beta.
|
|
63
|
+
"kizunajs": "2.0.0-beta.25",
|
|
64
64
|
"zod": "^4.0.0"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@modelcontextprotocol/client": "^2.0.0",
|
|
68
|
-
"kizunajs": "2.0.0-beta.
|
|
69
|
-
"@kizunajs/express": "2.0.0-beta.
|
|
68
|
+
"kizunajs": "2.0.0-beta.25",
|
|
69
|
+
"@kizunajs/express": "2.0.0-beta.25",
|
|
70
70
|
"@types/express": "^5.0.0",
|
|
71
71
|
"express": "^5.0.0",
|
|
72
72
|
"tsdown": "^0.21.0",
|