@kizunajs/mcp 2.0.0-beta.24 → 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 +38 -26
- package/dist/index.mjs +40 -28
- 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,16 +139,6 @@ const approval = (route, context) => {
|
|
|
130
139
|
};
|
|
131
140
|
};
|
|
132
141
|
/**
|
|
133
|
-
* A schema as JSON Schema, with a `z.date()` as the ISO string the wire
|
|
134
|
-
* carries. Zod alone throws on a date, which would empty the whole tool list.
|
|
135
|
-
*/
|
|
136
|
-
const toJsonSchema = (schema, io) => zod.z.toJSONSchema(schema, {
|
|
137
|
-
target: "draft-2020-12",
|
|
138
|
-
io,
|
|
139
|
-
unrepresentable: "any",
|
|
140
|
-
override: kizunajs_generator.encodeForWire
|
|
141
|
-
});
|
|
142
|
-
/**
|
|
143
142
|
* What the SDK publishes for a tool. Input passes through unchecked, so the
|
|
144
143
|
* call is validated once, by `validateRequest`, as an HTTP request is. Output
|
|
145
144
|
* keeps the route's own check.
|
|
@@ -154,13 +153,6 @@ const publishedSchema = (jsonSchema, check) => ({ "~standard": {
|
|
|
154
153
|
}
|
|
155
154
|
} });
|
|
156
155
|
/**
|
|
157
|
-
* MCP's recommended schema for a tool that takes nothing.
|
|
158
|
-
*/
|
|
159
|
-
const NO_INPUT = {
|
|
160
|
-
type: "object",
|
|
161
|
-
additionalProperties: false
|
|
162
|
-
};
|
|
163
|
-
/**
|
|
164
156
|
* What the route says about itself, over what its method already implies.
|
|
165
157
|
*/
|
|
166
158
|
const buildToolAnnotations = (route) => {
|
|
@@ -198,6 +190,23 @@ const buildMcpTools = (routes) => {
|
|
|
198
190
|
}
|
|
199
191
|
return definitions;
|
|
200
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
|
+
};
|
|
201
210
|
/**
|
|
202
211
|
* What a client puts in front of the model before it picks a tool.
|
|
203
212
|
*/
|
|
@@ -238,7 +247,7 @@ const toolEnvelope = (status, body) => {
|
|
|
238
247
|
return {
|
|
239
248
|
content: [{
|
|
240
249
|
type: "text",
|
|
241
|
-
text: JSON.stringify(envelope
|
|
250
|
+
text: JSON.stringify(envelope)
|
|
242
251
|
}],
|
|
243
252
|
...isError ? {} : { structuredContent: envelope },
|
|
244
253
|
isError
|
|
@@ -250,7 +259,7 @@ const toolError = (status, body) => ({
|
|
|
250
259
|
text: JSON.stringify({
|
|
251
260
|
status,
|
|
252
261
|
body
|
|
253
|
-
}
|
|
262
|
+
})
|
|
254
263
|
}],
|
|
255
264
|
isError: true
|
|
256
265
|
});
|
|
@@ -352,7 +361,7 @@ const executeToolCall = async (route, routeKey, args, router, handlerContext, gu
|
|
|
352
361
|
text: JSON.stringify({
|
|
353
362
|
status: 500,
|
|
354
363
|
body: { detail: `Handler not implemented: ${routeKey}` }
|
|
355
|
-
}
|
|
364
|
+
})
|
|
356
365
|
}],
|
|
357
366
|
isError: true
|
|
358
367
|
};
|
|
@@ -400,17 +409,20 @@ const createMcpServer = (api, options) => {
|
|
|
400
409
|
const guardSchema = api[kizunajs_adapter.GUARD_SCHEMA_META];
|
|
401
410
|
const contextResolvers = api[kizunajs_adapter.REQUEST_CONTEXT_META];
|
|
402
411
|
const contract = (0, kizunajs_adapter.contractOf)(api);
|
|
403
|
-
const
|
|
412
|
+
const tools = publishedToolsFor(api.routes);
|
|
404
413
|
const server = new _modelcontextprotocol_server.McpServer({
|
|
405
414
|
name: options?.name ?? "MCP Server",
|
|
406
415
|
version: options?.version ?? "1.0.0"
|
|
407
|
-
}, {
|
|
408
|
-
|
|
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, {
|
|
409
421
|
...definition.title === void 0 ? {} : { title: definition.title },
|
|
410
422
|
description: definition.description,
|
|
411
|
-
inputSchema
|
|
412
|
-
outputSchema
|
|
413
|
-
annotations
|
|
423
|
+
inputSchema,
|
|
424
|
+
outputSchema,
|
|
425
|
+
annotations
|
|
414
426
|
}, async (args, context) => {
|
|
415
427
|
const refusal = approval(definition.route, context);
|
|
416
428
|
if (refusal !== void 0) return refusal;
|
package/dist/index.mjs
CHANGED
|
@@ -3,8 +3,8 @@ 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, formatValidationError, guardDenyFor, insufficientScope, isGuardDenial, rawResponse, requiresDenial, resolveSecurityRequirements, selectToolRoutes, toolOptionsOf, validateRequest, withPermissions } from "kizunajs/adapter";
|
|
7
|
-
import { deriveToolNames,
|
|
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
|
+
import { deriveToolNames, isJsonMediaType, isStreamResponse, isSuccessStatus, isVoidSchema, resolveResponseBody, resolveResponseContentType } from "kizunajs/generator";
|
|
8
8
|
//#region src/oauth.ts
|
|
9
9
|
const McpOAuthSchema = z.object({
|
|
10
10
|
/**
|
|
@@ -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,16 +138,6 @@ const approval = (route, context) => {
|
|
|
129
138
|
};
|
|
130
139
|
};
|
|
131
140
|
/**
|
|
132
|
-
* A schema as JSON Schema, with a `z.date()` as the ISO string the wire
|
|
133
|
-
* carries. Zod alone throws on a date, which would empty the whole tool list.
|
|
134
|
-
*/
|
|
135
|
-
const toJsonSchema = (schema, io) => z.toJSONSchema(schema, {
|
|
136
|
-
target: "draft-2020-12",
|
|
137
|
-
io,
|
|
138
|
-
unrepresentable: "any",
|
|
139
|
-
override: encodeForWire
|
|
140
|
-
});
|
|
141
|
-
/**
|
|
142
141
|
* What the SDK publishes for a tool. Input passes through unchecked, so the
|
|
143
142
|
* call is validated once, by `validateRequest`, as an HTTP request is. Output
|
|
144
143
|
* keeps the route's own check.
|
|
@@ -153,13 +152,6 @@ const publishedSchema = (jsonSchema, check) => ({ "~standard": {
|
|
|
153
152
|
}
|
|
154
153
|
} });
|
|
155
154
|
/**
|
|
156
|
-
* MCP's recommended schema for a tool that takes nothing.
|
|
157
|
-
*/
|
|
158
|
-
const NO_INPUT = {
|
|
159
|
-
type: "object",
|
|
160
|
-
additionalProperties: false
|
|
161
|
-
};
|
|
162
|
-
/**
|
|
163
155
|
* What the route says about itself, over what its method already implies.
|
|
164
156
|
*/
|
|
165
157
|
const buildToolAnnotations = (route) => {
|
|
@@ -197,6 +189,23 @@ const buildMcpTools = (routes) => {
|
|
|
197
189
|
}
|
|
198
190
|
return definitions;
|
|
199
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
|
+
};
|
|
200
209
|
/**
|
|
201
210
|
* What a client puts in front of the model before it picks a tool.
|
|
202
211
|
*/
|
|
@@ -237,7 +246,7 @@ const toolEnvelope = (status, body) => {
|
|
|
237
246
|
return {
|
|
238
247
|
content: [{
|
|
239
248
|
type: "text",
|
|
240
|
-
text: JSON.stringify(envelope
|
|
249
|
+
text: JSON.stringify(envelope)
|
|
241
250
|
}],
|
|
242
251
|
...isError ? {} : { structuredContent: envelope },
|
|
243
252
|
isError
|
|
@@ -249,7 +258,7 @@ const toolError = (status, body) => ({
|
|
|
249
258
|
text: JSON.stringify({
|
|
250
259
|
status,
|
|
251
260
|
body
|
|
252
|
-
}
|
|
261
|
+
})
|
|
253
262
|
}],
|
|
254
263
|
isError: true
|
|
255
264
|
});
|
|
@@ -351,7 +360,7 @@ const executeToolCall = async (route, routeKey, args, router, handlerContext, gu
|
|
|
351
360
|
text: JSON.stringify({
|
|
352
361
|
status: 500,
|
|
353
362
|
body: { detail: `Handler not implemented: ${routeKey}` }
|
|
354
|
-
}
|
|
363
|
+
})
|
|
355
364
|
}],
|
|
356
365
|
isError: true
|
|
357
366
|
};
|
|
@@ -399,17 +408,20 @@ const createMcpServer = (api, options) => {
|
|
|
399
408
|
const guardSchema = api[GUARD_SCHEMA_META];
|
|
400
409
|
const contextResolvers = api[REQUEST_CONTEXT_META];
|
|
401
410
|
const contract = contractOf(api);
|
|
402
|
-
const
|
|
411
|
+
const tools = publishedToolsFor(api.routes);
|
|
403
412
|
const server = new McpServer({
|
|
404
413
|
name: options?.name ?? "MCP Server",
|
|
405
414
|
version: options?.version ?? "1.0.0"
|
|
406
|
-
}, {
|
|
407
|
-
|
|
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, {
|
|
408
420
|
...definition.title === void 0 ? {} : { title: definition.title },
|
|
409
421
|
description: definition.description,
|
|
410
|
-
inputSchema
|
|
411
|
-
outputSchema
|
|
412
|
-
annotations
|
|
422
|
+
inputSchema,
|
|
423
|
+
outputSchema,
|
|
424
|
+
annotations
|
|
413
425
|
}, async (args, context) => {
|
|
414
426
|
const refusal = approval(definition.route, context);
|
|
415
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",
|