@kizunajs/mcp 2.0.0-beta.0 → 2.0.0-beta.10
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 +21 -103
- package/dist/index.d.cts +5 -14
- package/dist/index.d.mts +5 -14
- package/dist/index.mjs +22 -103
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -46,40 +46,6 @@ const isSafeMethod = (method) => SAFE_METHODS.has(method);
|
|
|
46
46
|
const isIdempotentMethod = (method) => IDEMPOTENT_METHODS.has(method);
|
|
47
47
|
//#endregion
|
|
48
48
|
//#region src/schema.ts
|
|
49
|
-
const buildToolInputSchema = (route) => {
|
|
50
|
-
const shape = {};
|
|
51
|
-
let hasParams = false;
|
|
52
|
-
let hasQuery = false;
|
|
53
|
-
let hasBody = false;
|
|
54
|
-
const paramNames = (0, kizunajs_adapter.parsePath)(route.path).paramNames;
|
|
55
|
-
if (paramNames.length > 0) {
|
|
56
|
-
hasParams = true;
|
|
57
|
-
const paramShape = {};
|
|
58
|
-
const explicitShape = route.pathParams ? (0, kizunajs_generator.readObjectShape)(route.pathParams) : void 0;
|
|
59
|
-
for (const name of paramNames) paramShape[name] = explicitShape?.[name] ?? zod.z.string();
|
|
60
|
-
shape["params"] = zod.z.object(paramShape);
|
|
61
|
-
}
|
|
62
|
-
if (route.query) {
|
|
63
|
-
hasQuery = true;
|
|
64
|
-
shape["query"] = route.query.safeParse({}).success ? route.query.optional() : route.query;
|
|
65
|
-
}
|
|
66
|
-
if (route.body && !(0, kizunajs_generator.isVoidSchema)(route.body)) {
|
|
67
|
-
hasBody = true;
|
|
68
|
-
shape["body"] = route.body.safeParse(void 0).success ? route.body.optional() : route.body;
|
|
69
|
-
}
|
|
70
|
-
if (Object.keys(shape).length === 0) return {
|
|
71
|
-
shape: void 0,
|
|
72
|
-
hasParams,
|
|
73
|
-
hasQuery,
|
|
74
|
-
hasBody
|
|
75
|
-
};
|
|
76
|
-
return {
|
|
77
|
-
shape,
|
|
78
|
-
hasParams,
|
|
79
|
-
hasQuery,
|
|
80
|
-
hasBody
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
49
|
/**
|
|
84
50
|
* The `{ status, body }` envelope a tool returns, with `body` carrying the
|
|
85
51
|
* route's JSON success bodies. A route with none gets `status` alone.
|
|
@@ -112,29 +78,6 @@ const buildToolOutputSchema = (route) => {
|
|
|
112
78
|
});
|
|
113
79
|
};
|
|
114
80
|
//#endregion
|
|
115
|
-
//#region src/tool-selection.ts
|
|
116
|
-
/**
|
|
117
|
-
* Tool input is JSON, so a route that reads a form body has nothing to receive
|
|
118
|
-
* it.
|
|
119
|
-
*/
|
|
120
|
-
const takesJsonInput = (route) => route.contentType === void 0 || route.contentType === "application/json";
|
|
121
|
-
/**
|
|
122
|
-
* What a route says about publishing itself, or `undefined` when it says
|
|
123
|
-
* nothing.
|
|
124
|
-
*/
|
|
125
|
-
const toolOptionsOf = (route) => {
|
|
126
|
-
if (route.tool === void 0 || route.tool === false) return void 0;
|
|
127
|
-
return route.tool === true ? {} : route.tool;
|
|
128
|
-
};
|
|
129
|
-
/**
|
|
130
|
-
* The routes a model may call: the ones that declared `tool`, less the ones
|
|
131
|
-
* whose shape a tool call cannot carry.
|
|
132
|
-
*/
|
|
133
|
-
const selectToolRoutes = (routes) => routes.filter(({ route }) => {
|
|
134
|
-
if (toolOptionsOf(route) === void 0) return false;
|
|
135
|
-
return takesJsonInput(route) && !(0, kizunajs.routeStreams)(route);
|
|
136
|
-
});
|
|
137
|
-
//#endregion
|
|
138
81
|
//#region src/mcp-server.ts
|
|
139
82
|
/**
|
|
140
83
|
* Hints from the method's HTTP semantics, per RFC 9110. MCP already defaults
|
|
@@ -144,28 +87,27 @@ const selectToolRoutes = (routes) => routes.filter(({ route }) => {
|
|
|
144
87
|
/**
|
|
145
88
|
* What the elicitation answer comes back under, and the key the retry echoes.
|
|
146
89
|
*/
|
|
147
|
-
const
|
|
90
|
+
const APPROVAL_KEY = "approved";
|
|
148
91
|
/**
|
|
149
|
-
* Ask before running, when the
|
|
150
|
-
*
|
|
151
|
-
*
|
|
92
|
+
* Ask the person before running, when the call needs approval. Returns the
|
|
93
|
+
* elicitation on the first call, a refusal when they declined, and nothing once
|
|
94
|
+
* they approved.
|
|
152
95
|
*/
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
message,
|
|
96
|
+
const approval = (route, context) => {
|
|
97
|
+
if ((0, kizunajs_adapter.toolOptionsOf)(route)?.needsApproval !== true) return void 0;
|
|
98
|
+
const answer = (0, _modelcontextprotocol_server.acceptedContent)(context.mcpReq?.inputResponses, APPROVAL_KEY);
|
|
99
|
+
if (answer === void 0) return (0, _modelcontextprotocol_server.inputRequired)({ inputRequests: { [APPROVAL_KEY]: _modelcontextprotocol_server.inputRequired.elicit({
|
|
100
|
+
message: `Allow "${(0, kizunajs_adapter.toolOptionsOf)(route)?.title ?? route.summary ?? `${route.method} ${route.path}`}"?`,
|
|
159
101
|
requestedSchema: {
|
|
160
102
|
type: "object",
|
|
161
|
-
properties: {
|
|
103
|
+
properties: { approved: {
|
|
162
104
|
type: "boolean",
|
|
163
|
-
title: "
|
|
105
|
+
title: "Allow"
|
|
164
106
|
} },
|
|
165
|
-
required: [
|
|
107
|
+
required: [APPROVAL_KEY]
|
|
166
108
|
}
|
|
167
109
|
}) } });
|
|
168
|
-
if (answer.
|
|
110
|
+
if (answer.approved === true) return void 0;
|
|
169
111
|
return {
|
|
170
112
|
content: [{
|
|
171
113
|
type: "text",
|
|
@@ -178,7 +120,7 @@ const confirmed = (route, context) => {
|
|
|
178
120
|
* What the route says about itself, over what its method already implies.
|
|
179
121
|
*/
|
|
180
122
|
const buildToolAnnotations = (route) => {
|
|
181
|
-
const declared = toolOptionsOf(route) ?? {};
|
|
123
|
+
const declared = (0, kizunajs_adapter.toolOptionsOf)(route) ?? {};
|
|
182
124
|
return {
|
|
183
125
|
...isSafeMethod(route.method) ? { readOnlyHint: true } : {},
|
|
184
126
|
...isIdempotentMethod(route.method) ? { idempotentHint: true } : {},
|
|
@@ -190,31 +132,8 @@ const buildToolAnnotations = (route) => {
|
|
|
190
132
|
...declared.openWorldHint === void 0 ? {} : { openWorldHint: declared.openWorldHint }
|
|
191
133
|
};
|
|
192
134
|
};
|
|
193
|
-
const
|
|
194
|
-
const
|
|
195
|
-
const requires = route.requires;
|
|
196
|
-
if (requires === void 0) return void 0;
|
|
197
|
-
const names = Object.entries(requires).flatMap(([resource, verbs]) => verbs.map((verb) => `${resource}:${verb}`));
|
|
198
|
-
return names.length > 0 ? `Permissions: ${names.join(", ")}` : void 0;
|
|
199
|
-
};
|
|
200
|
-
const buildToolDescription = (route) => {
|
|
201
|
-
const parts = [];
|
|
202
|
-
const declared = toolOptionsOf(route)?.description;
|
|
203
|
-
if (declared) parts.push(declared);
|
|
204
|
-
else if (route.summary) parts.push(route.summary);
|
|
205
|
-
if (route.description) parts.push(route.description);
|
|
206
|
-
if (parts.length === 0) parts.push(`${route.method} ${route.path}`);
|
|
207
|
-
parts.push(`\nHTTP: ${route.method} ${route.path}`);
|
|
208
|
-
const requirements = (0, kizunajs_adapter.resolveSecurityRequirements)(route);
|
|
209
|
-
if (requirements.length > 0) parts.push(`Requires: ${requirements.map(({ scheme }) => scheme).join(", ")}`);
|
|
210
|
-
const roles = describeRoles(route);
|
|
211
|
-
if (roles !== void 0) parts.push(roles);
|
|
212
|
-
const permissions = describeRequires(route);
|
|
213
|
-
if (permissions !== void 0) parts.push(permissions);
|
|
214
|
-
return parts.join("\n");
|
|
215
|
-
};
|
|
216
|
-
const buildToolDefinitions = (routes) => {
|
|
217
|
-
const selected = selectToolRoutes((0, kizunajs_adapter.flattenRoutes)(routes));
|
|
135
|
+
const buildMcpTools = (routes) => {
|
|
136
|
+
const selected = (0, kizunajs_adapter.selectToolRoutes)((0, kizunajs_adapter.flattenRoutes)(routes));
|
|
218
137
|
const names = (0, kizunajs_generator.deriveToolNames)(selected.map(({ routeKey }) => ({
|
|
219
138
|
key: routeKey,
|
|
220
139
|
origin: "route"
|
|
@@ -225,8 +144,8 @@ const buildToolDefinitions = (routes) => {
|
|
|
225
144
|
definitions.push({
|
|
226
145
|
name,
|
|
227
146
|
title: route.summary,
|
|
228
|
-
description:
|
|
229
|
-
inputSchema: buildToolInputSchema(route),
|
|
147
|
+
description: (0, kizunajs_adapter.describeTool)(route),
|
|
148
|
+
inputSchema: (0, kizunajs_adapter.buildToolInputSchema)(route),
|
|
230
149
|
outputSchema: buildToolOutputSchema(route),
|
|
231
150
|
route,
|
|
232
151
|
routeKey,
|
|
@@ -447,7 +366,7 @@ const createMcpServer = (api, options) => {
|
|
|
447
366
|
const guardSchema = api[kizunajs_adapter.GUARD_SCHEMA_META];
|
|
448
367
|
const contextResolvers = api[kizunajs_adapter.REQUEST_CONTEXT_META];
|
|
449
368
|
const contract = (0, kizunajs_adapter.contractOf)(api);
|
|
450
|
-
const definitions =
|
|
369
|
+
const definitions = buildMcpTools(api.routes);
|
|
451
370
|
const server = new _modelcontextprotocol_server.McpServer({
|
|
452
371
|
name: options?.name ?? "MCP Server",
|
|
453
372
|
version: options?.version ?? "1.0.0"
|
|
@@ -459,7 +378,7 @@ const createMcpServer = (api, options) => {
|
|
|
459
378
|
outputSchema: definition.outputSchema,
|
|
460
379
|
annotations: buildToolAnnotations(definition.route)
|
|
461
380
|
}, async (args, context) => {
|
|
462
|
-
const refusal =
|
|
381
|
+
const refusal = approval(definition.route, context);
|
|
463
382
|
if (refusal !== void 0) return refusal;
|
|
464
383
|
return executeToolCall(definition.route, definition.routeKey, args ?? {}, router, options?.handlerContext, guards, schemes, options?.credentialHeaders, contextResolvers, options?.transportAuth, guardSchema);
|
|
465
384
|
});
|
|
@@ -573,7 +492,7 @@ const prepareOAuth = (oauth, endpointPath, api) => {
|
|
|
573
492
|
}),
|
|
574
493
|
metadataUrl: protectedResourceMetadataUrl(oauth, endpointPath),
|
|
575
494
|
scopesSupported: (0, kizunajs.declaredScopes)(schemeDefinition),
|
|
576
|
-
tools: new Map(
|
|
495
|
+
tools: new Map(buildMcpTools(api.routes).map((definition) => [definition.name, definition]))
|
|
577
496
|
};
|
|
578
497
|
};
|
|
579
498
|
/**
|
|
@@ -704,6 +623,5 @@ function mcpPlugin(props) {
|
|
|
704
623
|
}
|
|
705
624
|
//#endregion
|
|
706
625
|
exports.buildInstructions = buildInstructions;
|
|
707
|
-
exports.buildToolDefinitions = buildToolDefinitions;
|
|
708
626
|
exports.createMcpServer = createMcpServer;
|
|
709
627
|
exports.mcpPlugin = mcpPlugin;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as _$kizunajs from "kizunajs";
|
|
2
|
-
import { ApiDefinition, RouteDefinition
|
|
2
|
+
import { ApiDefinition, RouteDefinition } from "kizunajs";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import { RoutePath, WithSlug } from "kizunajs/plugin";
|
|
5
5
|
import { McpServer } from "@modelcontextprotocol/server";
|
|
6
|
-
import { ApiWithRouter } from "kizunajs/adapter";
|
|
6
|
+
import { ApiWithRouter, ToolInputSchema } from "kizunajs/adapter";
|
|
7
7
|
|
|
8
8
|
//#region src/oauth.d.ts
|
|
9
9
|
interface McpOAuthProps {
|
|
@@ -110,14 +110,6 @@ declare const declare: (slug: string, props: McpPluginProps<string>) => _$kizuna
|
|
|
110
110
|
*/
|
|
111
111
|
declare function mcpPlugin<const Slug extends string = 'mcp'>(props?: McpPluginProps<Slug>): WithSlug<ReturnType<typeof declare>, Slug>;
|
|
112
112
|
//#endregion
|
|
113
|
-
//#region src/schema.d.ts
|
|
114
|
-
interface ToolInputSchema {
|
|
115
|
-
shape: Record<string, z.ZodType> | undefined;
|
|
116
|
-
hasParams: boolean;
|
|
117
|
-
hasQuery: boolean;
|
|
118
|
-
hasBody: boolean;
|
|
119
|
-
}
|
|
120
|
-
//#endregion
|
|
121
113
|
//#region src/mcp-server.d.ts
|
|
122
114
|
interface McpServerOptions {
|
|
123
115
|
/**
|
|
@@ -170,7 +162,7 @@ interface McpServerOptions {
|
|
|
170
162
|
context?: Record<string, unknown>;
|
|
171
163
|
};
|
|
172
164
|
}
|
|
173
|
-
interface
|
|
165
|
+
interface McpTool {
|
|
174
166
|
name: string;
|
|
175
167
|
title: string | undefined;
|
|
176
168
|
description: string;
|
|
@@ -180,11 +172,10 @@ interface ToolDefinition {
|
|
|
180
172
|
routeKey: string;
|
|
181
173
|
tags: string[];
|
|
182
174
|
}
|
|
183
|
-
declare const buildToolDefinitions: (routes: Routes) => ToolDefinition[];
|
|
184
175
|
/**
|
|
185
176
|
* What a client puts in front of the model before it picks a tool.
|
|
186
177
|
*/
|
|
187
|
-
declare const buildInstructions: (contract: ApiDefinition | undefined, definitions: readonly
|
|
178
|
+
declare const buildInstructions: (contract: ApiDefinition | undefined, definitions: readonly McpTool[], authored: string | undefined) => string;
|
|
188
179
|
/**
|
|
189
180
|
* Create an MCP server from a kizuna API.
|
|
190
181
|
*
|
|
@@ -200,4 +191,4 @@ declare const buildInstructions: (contract: ApiDefinition | undefined, definitio
|
|
|
200
191
|
*/
|
|
201
192
|
declare const createMcpServer: (api: ApiWithRouter, options?: McpServerOptions) => McpServer;
|
|
202
193
|
//#endregion
|
|
203
|
-
export { type McpOAuthProps, type McpPluginProps, type McpServerOptions,
|
|
194
|
+
export { type McpOAuthProps, type McpPluginProps, type McpServerOptions, buildInstructions, createMcpServer, mcpPlugin };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { RoutePath, WithSlug } from "kizunajs/plugin";
|
|
3
3
|
import * as _$kizunajs from "kizunajs";
|
|
4
|
-
import { ApiDefinition, RouteDefinition
|
|
4
|
+
import { ApiDefinition, RouteDefinition } from "kizunajs";
|
|
5
5
|
import { McpServer } from "@modelcontextprotocol/server";
|
|
6
|
-
import { ApiWithRouter } from "kizunajs/adapter";
|
|
6
|
+
import { ApiWithRouter, ToolInputSchema } from "kizunajs/adapter";
|
|
7
7
|
|
|
8
8
|
//#region src/oauth.d.ts
|
|
9
9
|
interface McpOAuthProps {
|
|
@@ -110,14 +110,6 @@ declare const declare: (slug: string, props: McpPluginProps<string>) => _$kizuna
|
|
|
110
110
|
*/
|
|
111
111
|
declare function mcpPlugin<const Slug extends string = 'mcp'>(props?: McpPluginProps<Slug>): WithSlug<ReturnType<typeof declare>, Slug>;
|
|
112
112
|
//#endregion
|
|
113
|
-
//#region src/schema.d.ts
|
|
114
|
-
interface ToolInputSchema {
|
|
115
|
-
shape: Record<string, z.ZodType> | undefined;
|
|
116
|
-
hasParams: boolean;
|
|
117
|
-
hasQuery: boolean;
|
|
118
|
-
hasBody: boolean;
|
|
119
|
-
}
|
|
120
|
-
//#endregion
|
|
121
113
|
//#region src/mcp-server.d.ts
|
|
122
114
|
interface McpServerOptions {
|
|
123
115
|
/**
|
|
@@ -170,7 +162,7 @@ interface McpServerOptions {
|
|
|
170
162
|
context?: Record<string, unknown>;
|
|
171
163
|
};
|
|
172
164
|
}
|
|
173
|
-
interface
|
|
165
|
+
interface McpTool {
|
|
174
166
|
name: string;
|
|
175
167
|
title: string | undefined;
|
|
176
168
|
description: string;
|
|
@@ -180,11 +172,10 @@ interface ToolDefinition {
|
|
|
180
172
|
routeKey: string;
|
|
181
173
|
tags: string[];
|
|
182
174
|
}
|
|
183
|
-
declare const buildToolDefinitions: (routes: Routes) => ToolDefinition[];
|
|
184
175
|
/**
|
|
185
176
|
* What a client puts in front of the model before it picks a tool.
|
|
186
177
|
*/
|
|
187
|
-
declare const buildInstructions: (contract: ApiDefinition | undefined, definitions: readonly
|
|
178
|
+
declare const buildInstructions: (contract: ApiDefinition | undefined, definitions: readonly McpTool[], authored: string | undefined) => string;
|
|
188
179
|
/**
|
|
189
180
|
* Create an MCP server from a kizuna API.
|
|
190
181
|
*
|
|
@@ -200,4 +191,4 @@ declare const buildInstructions: (contract: ApiDefinition | undefined, definitio
|
|
|
200
191
|
*/
|
|
201
192
|
declare const createMcpServer: (api: ApiWithRouter, options?: McpServerOptions) => McpServer;
|
|
202
193
|
//#endregion
|
|
203
|
-
export { type McpOAuthProps, type McpPluginProps, type McpServerOptions,
|
|
194
|
+
export { type McpOAuthProps, type McpPluginProps, type McpServerOptions, buildInstructions, createMcpServer, mcpPlugin };
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { createPlugin } from "kizunajs/plugin";
|
|
3
3
|
import { ProtectedResourceMetadataSchema } from "kizunajs/schemas";
|
|
4
|
-
import { assertCanonicalResourceUri, buildProtectedResourceMetadata, declaredScopes, problemDetails, problemFromBody
|
|
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, contractOf, extractCredential, flattenRoutes, guardDenyFor, insufficientScope, isGuardDenial,
|
|
7
|
-
import { deriveToolNames, isJsonMediaType, isStreamResponse, isSuccessStatus, isVoidSchema,
|
|
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";
|
|
7
|
+
import { deriveToolNames, isJsonMediaType, isStreamResponse, isSuccessStatus, isVoidSchema, resolveResponseBody, resolveResponseContentType } from "kizunajs/generator";
|
|
8
8
|
//#region src/oauth.ts
|
|
9
9
|
const protectedResourceMetadataPath = (endpointPath) => `/.well-known/oauth-protected-resource${endpointPath}`;
|
|
10
10
|
/**
|
|
@@ -45,40 +45,6 @@ const isSafeMethod = (method) => SAFE_METHODS.has(method);
|
|
|
45
45
|
const isIdempotentMethod = (method) => IDEMPOTENT_METHODS.has(method);
|
|
46
46
|
//#endregion
|
|
47
47
|
//#region src/schema.ts
|
|
48
|
-
const buildToolInputSchema = (route) => {
|
|
49
|
-
const shape = {};
|
|
50
|
-
let hasParams = false;
|
|
51
|
-
let hasQuery = false;
|
|
52
|
-
let hasBody = false;
|
|
53
|
-
const paramNames = parsePath(route.path).paramNames;
|
|
54
|
-
if (paramNames.length > 0) {
|
|
55
|
-
hasParams = true;
|
|
56
|
-
const paramShape = {};
|
|
57
|
-
const explicitShape = route.pathParams ? readObjectShape(route.pathParams) : void 0;
|
|
58
|
-
for (const name of paramNames) paramShape[name] = explicitShape?.[name] ?? z.string();
|
|
59
|
-
shape["params"] = z.object(paramShape);
|
|
60
|
-
}
|
|
61
|
-
if (route.query) {
|
|
62
|
-
hasQuery = true;
|
|
63
|
-
shape["query"] = route.query.safeParse({}).success ? route.query.optional() : route.query;
|
|
64
|
-
}
|
|
65
|
-
if (route.body && !isVoidSchema(route.body)) {
|
|
66
|
-
hasBody = true;
|
|
67
|
-
shape["body"] = route.body.safeParse(void 0).success ? route.body.optional() : route.body;
|
|
68
|
-
}
|
|
69
|
-
if (Object.keys(shape).length === 0) return {
|
|
70
|
-
shape: void 0,
|
|
71
|
-
hasParams,
|
|
72
|
-
hasQuery,
|
|
73
|
-
hasBody
|
|
74
|
-
};
|
|
75
|
-
return {
|
|
76
|
-
shape,
|
|
77
|
-
hasParams,
|
|
78
|
-
hasQuery,
|
|
79
|
-
hasBody
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
48
|
/**
|
|
83
49
|
* The `{ status, body }` envelope a tool returns, with `body` carrying the
|
|
84
50
|
* route's JSON success bodies. A route with none gets `status` alone.
|
|
@@ -111,29 +77,6 @@ const buildToolOutputSchema = (route) => {
|
|
|
111
77
|
});
|
|
112
78
|
};
|
|
113
79
|
//#endregion
|
|
114
|
-
//#region src/tool-selection.ts
|
|
115
|
-
/**
|
|
116
|
-
* Tool input is JSON, so a route that reads a form body has nothing to receive
|
|
117
|
-
* it.
|
|
118
|
-
*/
|
|
119
|
-
const takesJsonInput = (route) => route.contentType === void 0 || route.contentType === "application/json";
|
|
120
|
-
/**
|
|
121
|
-
* What a route says about publishing itself, or `undefined` when it says
|
|
122
|
-
* nothing.
|
|
123
|
-
*/
|
|
124
|
-
const toolOptionsOf = (route) => {
|
|
125
|
-
if (route.tool === void 0 || route.tool === false) return void 0;
|
|
126
|
-
return route.tool === true ? {} : route.tool;
|
|
127
|
-
};
|
|
128
|
-
/**
|
|
129
|
-
* The routes a model may call: the ones that declared `tool`, less the ones
|
|
130
|
-
* whose shape a tool call cannot carry.
|
|
131
|
-
*/
|
|
132
|
-
const selectToolRoutes = (routes) => routes.filter(({ route }) => {
|
|
133
|
-
if (toolOptionsOf(route) === void 0) return false;
|
|
134
|
-
return takesJsonInput(route) && !routeStreams(route);
|
|
135
|
-
});
|
|
136
|
-
//#endregion
|
|
137
80
|
//#region src/mcp-server.ts
|
|
138
81
|
/**
|
|
139
82
|
* Hints from the method's HTTP semantics, per RFC 9110. MCP already defaults
|
|
@@ -143,28 +86,27 @@ const selectToolRoutes = (routes) => routes.filter(({ route }) => {
|
|
|
143
86
|
/**
|
|
144
87
|
* What the elicitation answer comes back under, and the key the retry echoes.
|
|
145
88
|
*/
|
|
146
|
-
const
|
|
89
|
+
const APPROVAL_KEY = "approved";
|
|
147
90
|
/**
|
|
148
|
-
* Ask before running, when the
|
|
149
|
-
*
|
|
150
|
-
*
|
|
91
|
+
* Ask the person before running, when the call needs approval. Returns the
|
|
92
|
+
* elicitation on the first call, a refusal when they declined, and nothing once
|
|
93
|
+
* they approved.
|
|
151
94
|
*/
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
message,
|
|
95
|
+
const approval = (route, context) => {
|
|
96
|
+
if (toolOptionsOf(route)?.needsApproval !== true) return void 0;
|
|
97
|
+
const answer = acceptedContent(context.mcpReq?.inputResponses, APPROVAL_KEY);
|
|
98
|
+
if (answer === void 0) return inputRequired({ inputRequests: { [APPROVAL_KEY]: inputRequired.elicit({
|
|
99
|
+
message: `Allow "${toolOptionsOf(route)?.title ?? route.summary ?? `${route.method} ${route.path}`}"?`,
|
|
158
100
|
requestedSchema: {
|
|
159
101
|
type: "object",
|
|
160
|
-
properties: {
|
|
102
|
+
properties: { approved: {
|
|
161
103
|
type: "boolean",
|
|
162
|
-
title: "
|
|
104
|
+
title: "Allow"
|
|
163
105
|
} },
|
|
164
|
-
required: [
|
|
106
|
+
required: [APPROVAL_KEY]
|
|
165
107
|
}
|
|
166
108
|
}) } });
|
|
167
|
-
if (answer.
|
|
109
|
+
if (answer.approved === true) return void 0;
|
|
168
110
|
return {
|
|
169
111
|
content: [{
|
|
170
112
|
type: "text",
|
|
@@ -189,30 +131,7 @@ const buildToolAnnotations = (route) => {
|
|
|
189
131
|
...declared.openWorldHint === void 0 ? {} : { openWorldHint: declared.openWorldHint }
|
|
190
132
|
};
|
|
191
133
|
};
|
|
192
|
-
const
|
|
193
|
-
const describeRequires = (route) => {
|
|
194
|
-
const requires = route.requires;
|
|
195
|
-
if (requires === void 0) return void 0;
|
|
196
|
-
const names = Object.entries(requires).flatMap(([resource, verbs]) => verbs.map((verb) => `${resource}:${verb}`));
|
|
197
|
-
return names.length > 0 ? `Permissions: ${names.join(", ")}` : void 0;
|
|
198
|
-
};
|
|
199
|
-
const buildToolDescription = (route) => {
|
|
200
|
-
const parts = [];
|
|
201
|
-
const declared = toolOptionsOf(route)?.description;
|
|
202
|
-
if (declared) parts.push(declared);
|
|
203
|
-
else if (route.summary) parts.push(route.summary);
|
|
204
|
-
if (route.description) parts.push(route.description);
|
|
205
|
-
if (parts.length === 0) parts.push(`${route.method} ${route.path}`);
|
|
206
|
-
parts.push(`\nHTTP: ${route.method} ${route.path}`);
|
|
207
|
-
const requirements = resolveSecurityRequirements(route);
|
|
208
|
-
if (requirements.length > 0) parts.push(`Requires: ${requirements.map(({ scheme }) => scheme).join(", ")}`);
|
|
209
|
-
const roles = describeRoles(route);
|
|
210
|
-
if (roles !== void 0) parts.push(roles);
|
|
211
|
-
const permissions = describeRequires(route);
|
|
212
|
-
if (permissions !== void 0) parts.push(permissions);
|
|
213
|
-
return parts.join("\n");
|
|
214
|
-
};
|
|
215
|
-
const buildToolDefinitions = (routes) => {
|
|
134
|
+
const buildMcpTools = (routes) => {
|
|
216
135
|
const selected = selectToolRoutes(flattenRoutes(routes));
|
|
217
136
|
const names = deriveToolNames(selected.map(({ routeKey }) => ({
|
|
218
137
|
key: routeKey,
|
|
@@ -224,7 +143,7 @@ const buildToolDefinitions = (routes) => {
|
|
|
224
143
|
definitions.push({
|
|
225
144
|
name,
|
|
226
145
|
title: route.summary,
|
|
227
|
-
description:
|
|
146
|
+
description: describeTool(route),
|
|
228
147
|
inputSchema: buildToolInputSchema(route),
|
|
229
148
|
outputSchema: buildToolOutputSchema(route),
|
|
230
149
|
route,
|
|
@@ -446,7 +365,7 @@ const createMcpServer = (api, options) => {
|
|
|
446
365
|
const guardSchema = api[GUARD_SCHEMA_META];
|
|
447
366
|
const contextResolvers = api[REQUEST_CONTEXT_META];
|
|
448
367
|
const contract = contractOf(api);
|
|
449
|
-
const definitions =
|
|
368
|
+
const definitions = buildMcpTools(api.routes);
|
|
450
369
|
const server = new McpServer({
|
|
451
370
|
name: options?.name ?? "MCP Server",
|
|
452
371
|
version: options?.version ?? "1.0.0"
|
|
@@ -458,7 +377,7 @@ const createMcpServer = (api, options) => {
|
|
|
458
377
|
outputSchema: definition.outputSchema,
|
|
459
378
|
annotations: buildToolAnnotations(definition.route)
|
|
460
379
|
}, async (args, context) => {
|
|
461
|
-
const refusal =
|
|
380
|
+
const refusal = approval(definition.route, context);
|
|
462
381
|
if (refusal !== void 0) return refusal;
|
|
463
382
|
return executeToolCall(definition.route, definition.routeKey, args ?? {}, router, options?.handlerContext, guards, schemes, options?.credentialHeaders, contextResolvers, options?.transportAuth, guardSchema);
|
|
464
383
|
});
|
|
@@ -572,7 +491,7 @@ const prepareOAuth = (oauth, endpointPath, api) => {
|
|
|
572
491
|
}),
|
|
573
492
|
metadataUrl: protectedResourceMetadataUrl(oauth, endpointPath),
|
|
574
493
|
scopesSupported: declaredScopes(schemeDefinition),
|
|
575
|
-
tools: new Map(
|
|
494
|
+
tools: new Map(buildMcpTools(api.routes).map((definition) => [definition.name, definition]))
|
|
576
495
|
};
|
|
577
496
|
};
|
|
578
497
|
/**
|
|
@@ -702,4 +621,4 @@ function mcpPlugin(props) {
|
|
|
702
621
|
return declare(settings.slug ?? "mcp", settings);
|
|
703
622
|
}
|
|
704
623
|
//#endregion
|
|
705
|
-
export { buildInstructions,
|
|
624
|
+
export { buildInstructions, createMcpServer, mcpPlugin };
|
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.10",
|
|
4
4
|
"description": "MCP endpoint for Kizuna.js, so AI assistants can call your routes",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kizunajs",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
63
|
"zod": "^4.0.0",
|
|
64
|
-
"kizunajs": "2.0.0-beta.
|
|
64
|
+
"kizunajs": "2.0.0-beta.10"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@modelcontextprotocol/client": "^2.0.0",
|
|
@@ -70,8 +70,8 @@
|
|
|
70
70
|
"tsdown": "^0.21.0",
|
|
71
71
|
"typescript": "^6.0.3",
|
|
72
72
|
"zod": "^4.0.0",
|
|
73
|
-
"kizunajs": "2.0.0-beta.
|
|
74
|
-
"
|
|
73
|
+
"@kizunajs/express": "2.0.0-beta.10",
|
|
74
|
+
"kizunajs": "2.0.0-beta.10"
|
|
75
75
|
},
|
|
76
76
|
"scripts": {
|
|
77
77
|
"build": "tsdown src/index.ts --format esm,cjs --dts --clean --external kizunajs --external zod --external @modelcontextprotocol/sdk",
|