@kizunajs/mcp 2.0.0-beta.10 → 2.0.0-beta.12
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 +99 -39
- package/dist/index.d.cts +61 -86
- package/dist/index.d.mts +61 -86
- package/dist/index.mjs +95 -42
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -7,6 +7,19 @@ let _modelcontextprotocol_server = require("@modelcontextprotocol/server");
|
|
|
7
7
|
let kizunajs_adapter = require("kizunajs/adapter");
|
|
8
8
|
let kizunajs_generator = require("kizunajs/generator");
|
|
9
9
|
//#region src/oauth.ts
|
|
10
|
+
const McpOAuthSchema = zod.z.object({
|
|
11
|
+
/**
|
|
12
|
+
* Canonical URI of the MCP endpoint as clients reach it: absolute, no
|
|
13
|
+
* fragment, ending in the endpoint's `path`. The RFC 9728 `resource`
|
|
14
|
+
* value and the RFC 8707 audience your guard checks tokens against.
|
|
15
|
+
*/
|
|
16
|
+
resource: zod.z.string(),
|
|
17
|
+
/**
|
|
18
|
+
* Name of the identity whose guard verifies the token on every request.
|
|
19
|
+
* Its `issuer` and flow scopes become the metadata document.
|
|
20
|
+
*/
|
|
21
|
+
scheme: zod.z.string()
|
|
22
|
+
});
|
|
10
23
|
const protectedResourceMetadataPath = (endpointPath) => `/.well-known/oauth-protected-resource${endpointPath}`;
|
|
11
24
|
/**
|
|
12
25
|
* Absolute URL the metadata route is served at: the resource's origin, any
|
|
@@ -527,14 +540,25 @@ const toolCallTarget = (body, enforcement) => {
|
|
|
527
540
|
};
|
|
528
541
|
};
|
|
529
542
|
/**
|
|
530
|
-
* What answers
|
|
531
|
-
* declared `tool`, and the calls a model makes against them.
|
|
543
|
+
* What answers one MCP endpoint: the tool list built from the routes that
|
|
544
|
+
* declared `tool`, and the calls a model makes against them. The OAuth setup
|
|
545
|
+
* reads the app's identities, so it's prepared once the api has assembled:
|
|
546
|
+
* `validate` prepares it at startup, and the handlers reuse it.
|
|
532
547
|
*/
|
|
533
|
-
const
|
|
548
|
+
const mcpEndpoint = (props, api) => {
|
|
534
549
|
const serverApi = api;
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
550
|
+
let prepared;
|
|
551
|
+
const enforcementOf = () => {
|
|
552
|
+
if (props.oauth === void 0) return void 0;
|
|
553
|
+
prepared ??= prepareOAuth(props.oauth, props.path ?? "/mcp", serverApi);
|
|
554
|
+
return prepared;
|
|
555
|
+
};
|
|
556
|
+
return {
|
|
557
|
+
validate: () => {
|
|
558
|
+
enforcementOf();
|
|
559
|
+
},
|
|
560
|
+
handle: async (args) => {
|
|
561
|
+
const enforcement = enforcementOf();
|
|
538
562
|
let transportAuth;
|
|
539
563
|
if (enforcement !== void 0) {
|
|
540
564
|
const target = toolCallTarget(args.body, enforcement);
|
|
@@ -569,58 +593,94 @@ const mcpServe = (props, api) => {
|
|
|
569
593
|
body: JSON.stringify(args.body)
|
|
570
594
|
})));
|
|
571
595
|
},
|
|
572
|
-
|
|
596
|
+
protectedResourceMetadata: () => (0, kizunajs_adapter.rawResponse)(new Response(JSON.stringify(enforcementOf()?.metadata), {
|
|
573
597
|
status: 200,
|
|
574
598
|
headers: { "content-type": "application/json" }
|
|
575
|
-
}))
|
|
576
|
-
}
|
|
599
|
+
}))
|
|
600
|
+
};
|
|
577
601
|
};
|
|
578
602
|
//#endregion
|
|
579
603
|
//#region src/plugin.ts
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
604
|
+
/**
|
|
605
|
+
* What the MCP endpoint is, and where. Which routes it publishes is each
|
|
606
|
+
* route's own business: declare `tool` on the ones a model may call.
|
|
607
|
+
*/
|
|
608
|
+
const McpPluginOptionsSchema = zod.z.object({
|
|
609
|
+
/**
|
|
610
|
+
* Path the endpoint is served from.
|
|
611
|
+
*
|
|
612
|
+
* @default '/mcp'
|
|
613
|
+
*/
|
|
614
|
+
path: kizunajs_plugin.RoutePathSchema.optional(),
|
|
615
|
+
/**
|
|
616
|
+
* Human-readable name shown to AI assistants.
|
|
617
|
+
*
|
|
618
|
+
* @default 'MCP Server'
|
|
619
|
+
*/
|
|
620
|
+
name: zod.z.string().optional(),
|
|
621
|
+
/**
|
|
622
|
+
* Semantic version string (e.g. "1.0.0").
|
|
623
|
+
*
|
|
624
|
+
* @default '1.0.0'
|
|
625
|
+
*/
|
|
626
|
+
version: zod.z.string().optional(),
|
|
627
|
+
/**
|
|
628
|
+
* Guidance for the model, appended to the overview built from the
|
|
629
|
+
* contract's tags.
|
|
630
|
+
*/
|
|
631
|
+
instructions: zod.z.string().optional(),
|
|
632
|
+
/**
|
|
633
|
+
* Serve the endpoint as an OAuth 2.1 resource server, per the MCP
|
|
634
|
+
* authorization specification: RFC 9728 metadata on a well-known route,
|
|
635
|
+
* and HTTP `401`/`403` challenges built from the named identity.
|
|
636
|
+
*/
|
|
637
|
+
oauth: McpOAuthSchema.optional()
|
|
638
|
+
});
|
|
603
639
|
/**
|
|
604
640
|
* Let AI assistants use the API. Serves an MCP (Model Context Protocol)
|
|
605
641
|
* endpoint where every route declaring `tool` is one an assistant can discover
|
|
606
642
|
* and call, behind the same guards as the HTTP endpoints.
|
|
607
643
|
*
|
|
608
644
|
* The endpoint is an ordinary kizuna route, so `api.mount` serves it on any
|
|
609
|
-
* adapter
|
|
610
|
-
* not see it.
|
|
645
|
+
* adapter.
|
|
611
646
|
*
|
|
612
647
|
* @example
|
|
613
648
|
* ```ts
|
|
614
649
|
* export default defineConfig({
|
|
615
650
|
* routes,
|
|
616
|
-
* plugins: [
|
|
651
|
+
* plugins: [
|
|
652
|
+
* mcpPlugin({
|
|
653
|
+
* name: 'My API',
|
|
654
|
+
* }),
|
|
655
|
+
* ],
|
|
617
656
|
* });
|
|
618
657
|
* ```
|
|
619
658
|
*/
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
}
|
|
659
|
+
const mcpPlugin = (0, kizunajs_plugin.definePlugin)({
|
|
660
|
+
slug: "mcp",
|
|
661
|
+
options: McpPluginOptionsSchema,
|
|
662
|
+
setup: ({ options, api }) => {
|
|
663
|
+
const endpointPath = options.path ?? "/mcp";
|
|
664
|
+
const endpoint = mcpEndpoint(options, api);
|
|
665
|
+
const routes = { endpoint: (0, kizunajs_plugin.route)({
|
|
666
|
+
method: "POST",
|
|
667
|
+
path: endpointPath,
|
|
668
|
+
summary: "MCP (Model Context Protocol) endpoint",
|
|
669
|
+
body: zod.z.unknown(),
|
|
670
|
+
responses: { 200: zod.z.unknown() }
|
|
671
|
+
}).handler(endpoint.handle) };
|
|
672
|
+
if (options.oauth !== void 0) routes.protectedResourceMetadata = (0, kizunajs_plugin.route)({
|
|
673
|
+
method: "GET",
|
|
674
|
+
path: protectedResourceMetadataPath(endpointPath),
|
|
675
|
+
summary: "OAuth protected resource metadata",
|
|
676
|
+
responses: { 200: kizunajs_schemas.ProtectedResourceMetadataSchema }
|
|
677
|
+
}).handler(endpoint.protectedResourceMetadata);
|
|
678
|
+
return {
|
|
679
|
+
routes,
|
|
680
|
+
validate: endpoint.validate
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
});
|
|
624
684
|
//#endregion
|
|
625
685
|
exports.buildInstructions = buildInstructions;
|
|
626
686
|
exports.createMcpServer = createMcpServer;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,114 +1,89 @@
|
|
|
1
|
+
import * as _$kizunajs_plugin0 from "kizunajs/plugin";
|
|
2
|
+
import { PluginRoutes } from "kizunajs/plugin";
|
|
1
3
|
import * as _$kizunajs from "kizunajs";
|
|
2
4
|
import { ApiDefinition, RouteDefinition } from "kizunajs";
|
|
3
5
|
import { z } from "zod";
|
|
4
|
-
import { RoutePath, WithSlug } from "kizunajs/plugin";
|
|
5
6
|
import { McpServer } from "@modelcontextprotocol/server";
|
|
6
7
|
import { ApiWithRouter, ToolInputSchema } from "kizunajs/adapter";
|
|
7
8
|
|
|
8
|
-
//#region src/oauth.d.ts
|
|
9
|
-
interface McpOAuthProps {
|
|
10
|
-
/**
|
|
11
|
-
* Canonical URI of the MCP endpoint as clients reach it: absolute, no
|
|
12
|
-
* fragment, ending in the endpoint's `path`. The RFC 9728 `resource`
|
|
13
|
-
* value and the RFC 8707 audience your guard checks tokens against.
|
|
14
|
-
*/
|
|
15
|
-
resource: string;
|
|
16
|
-
/**
|
|
17
|
-
* Name of the identity whose guard verifies the token on every request.
|
|
18
|
-
* Its `issuer` and flow scopes become the metadata document.
|
|
19
|
-
*/
|
|
20
|
-
scheme: string;
|
|
21
|
-
}
|
|
22
|
-
//#endregion
|
|
23
9
|
//#region src/plugin.d.ts
|
|
24
10
|
/**
|
|
25
11
|
* What the MCP endpoint is, and where. Which routes it publishes is each
|
|
26
12
|
* route's own business: declare `tool` on the ones a model may call.
|
|
27
13
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
* Human-readable name shown to AI assistants.
|
|
43
|
-
*
|
|
44
|
-
* @default 'MCP Server'
|
|
45
|
-
*/
|
|
46
|
-
name?: string;
|
|
47
|
-
/**
|
|
48
|
-
* Semantic version string (e.g. "1.0.0").
|
|
49
|
-
*
|
|
50
|
-
* @default '1.0.0'
|
|
51
|
-
*/
|
|
52
|
-
version?: string;
|
|
53
|
-
/**
|
|
54
|
-
* Guidance for the model, appended to the overview built from the
|
|
55
|
-
* contract's tags.
|
|
56
|
-
*/
|
|
57
|
-
instructions?: string;
|
|
58
|
-
/**
|
|
59
|
-
* Serve the endpoint as an OAuth 2.1 resource server, per the MCP
|
|
60
|
-
* authorization specification: RFC 9728 metadata on a well-known route,
|
|
61
|
-
* and HTTP `401`/`403` challenges built from the named identity.
|
|
62
|
-
*/
|
|
63
|
-
oauth?: McpOAuthProps;
|
|
64
|
-
}
|
|
65
|
-
declare const declare: (slug: string, props: McpPluginProps<string>) => _$kizunajs.PluginDeclaration<{
|
|
66
|
-
readonly protectedResourceMetadata?: {
|
|
67
|
-
readonly method: "GET";
|
|
68
|
-
readonly path: `/${string}`;
|
|
69
|
-
readonly summary: "OAuth protected resource metadata";
|
|
70
|
-
readonly responses: {
|
|
71
|
-
readonly 200: z.ZodObject<{
|
|
72
|
-
resource: z.ZodString;
|
|
73
|
-
authorization_servers: z.ZodArray<z.ZodString>;
|
|
74
|
-
scopes_supported: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
75
|
-
bearer_methods_supported: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
76
|
-
}, z.core.$strip>;
|
|
77
|
-
};
|
|
78
|
-
} | undefined;
|
|
79
|
-
readonly endpoint: {
|
|
80
|
-
readonly method: "POST";
|
|
81
|
-
readonly path: `/${string}`;
|
|
82
|
-
readonly summary: "MCP (Model Context Protocol) endpoint";
|
|
83
|
-
readonly body: z.ZodUnknown;
|
|
84
|
-
readonly responses: {
|
|
85
|
-
readonly 200: z.ZodUnknown;
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
}, McpPluginProps<string>, undefined, string, {
|
|
89
|
-
[key: string]: unknown;
|
|
90
|
-
body: unknown;
|
|
91
|
-
headers: Record<string, string | string[] | undefined>;
|
|
92
|
-
requestContext?: Record<string, unknown>;
|
|
93
|
-
}>;
|
|
14
|
+
declare const McpPluginOptionsSchema: z.ZodObject<{
|
|
15
|
+
path: z.ZodOptional<z.ZodCustom<`/${string}`, `/${string}`>>;
|
|
16
|
+
name: z.ZodOptional<z.ZodString>;
|
|
17
|
+
version: z.ZodOptional<z.ZodString>;
|
|
18
|
+
instructions: z.ZodOptional<z.ZodString>;
|
|
19
|
+
oauth: z.ZodOptional<z.ZodObject<{
|
|
20
|
+
resource: z.ZodString;
|
|
21
|
+
scheme: z.ZodString;
|
|
22
|
+
}, z.core.$strip>>;
|
|
23
|
+
}, z.core.$strip>;
|
|
24
|
+
/**
|
|
25
|
+
* What the MCP endpoint is, and where.
|
|
26
|
+
*/
|
|
27
|
+
type McpPluginProps = z.output<typeof McpPluginOptionsSchema>;
|
|
94
28
|
/**
|
|
95
29
|
* Let AI assistants use the API. Serves an MCP (Model Context Protocol)
|
|
96
30
|
* endpoint where every route declaring `tool` is one an assistant can discover
|
|
97
31
|
* and call, behind the same guards as the HTTP endpoints.
|
|
98
32
|
*
|
|
99
33
|
* The endpoint is an ordinary kizuna route, so `api.mount` serves it on any
|
|
100
|
-
* adapter
|
|
101
|
-
* not see it.
|
|
34
|
+
* adapter.
|
|
102
35
|
*
|
|
103
36
|
* @example
|
|
104
37
|
* ```ts
|
|
105
38
|
* export default defineConfig({
|
|
106
39
|
* routes,
|
|
107
|
-
* plugins: [
|
|
40
|
+
* plugins: [
|
|
41
|
+
* mcpPlugin({
|
|
42
|
+
* name: 'My API',
|
|
43
|
+
* }),
|
|
44
|
+
* ],
|
|
108
45
|
* });
|
|
109
46
|
* ```
|
|
110
47
|
*/
|
|
111
|
-
declare
|
|
48
|
+
declare const mcpPlugin: {
|
|
49
|
+
<const Slug extends string>(options: {
|
|
50
|
+
path?: `/${string}` | undefined;
|
|
51
|
+
name?: string | undefined;
|
|
52
|
+
version?: string | undefined;
|
|
53
|
+
instructions?: string | undefined;
|
|
54
|
+
oauth?: {
|
|
55
|
+
resource: string;
|
|
56
|
+
scheme: string;
|
|
57
|
+
} | undefined;
|
|
58
|
+
} & {
|
|
59
|
+
basePath?: never;
|
|
60
|
+
} & _$kizunajs_plugin0.SlugOption<Slug>): _$kizunajs.PluginDeclaration<Slug, {
|
|
61
|
+
routes: PluginRoutes;
|
|
62
|
+
validate: () => void;
|
|
63
|
+
}>;
|
|
64
|
+
(options?: ({
|
|
65
|
+
path?: `/${string}` | undefined;
|
|
66
|
+
name?: string | undefined;
|
|
67
|
+
version?: string | undefined;
|
|
68
|
+
instructions?: string | undefined;
|
|
69
|
+
oauth?: {
|
|
70
|
+
resource: string;
|
|
71
|
+
scheme: string;
|
|
72
|
+
} | undefined;
|
|
73
|
+
} & {
|
|
74
|
+
basePath?: never;
|
|
75
|
+
}) | undefined): _$kizunajs.PluginDeclaration<"mcp", {
|
|
76
|
+
routes: PluginRoutes;
|
|
77
|
+
validate: () => void;
|
|
78
|
+
}>;
|
|
79
|
+
};
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/oauth.d.ts
|
|
82
|
+
declare const McpOAuthSchema: z.ZodObject<{
|
|
83
|
+
resource: z.ZodString;
|
|
84
|
+
scheme: z.ZodString;
|
|
85
|
+
}, z.core.$strip>;
|
|
86
|
+
type McpOAuthProps = z.output<typeof McpOAuthSchema>;
|
|
112
87
|
//#endregion
|
|
113
88
|
//#region src/mcp-server.d.ts
|
|
114
89
|
interface McpServerOptions {
|
package/dist/index.d.mts
CHANGED
|
@@ -1,114 +1,89 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import
|
|
2
|
+
import * as _$kizunajs_plugin0 from "kizunajs/plugin";
|
|
3
|
+
import { PluginRoutes } from "kizunajs/plugin";
|
|
3
4
|
import * as _$kizunajs from "kizunajs";
|
|
4
5
|
import { ApiDefinition, RouteDefinition } from "kizunajs";
|
|
5
6
|
import { McpServer } from "@modelcontextprotocol/server";
|
|
6
7
|
import { ApiWithRouter, ToolInputSchema } from "kizunajs/adapter";
|
|
7
8
|
|
|
8
|
-
//#region src/oauth.d.ts
|
|
9
|
-
interface McpOAuthProps {
|
|
10
|
-
/**
|
|
11
|
-
* Canonical URI of the MCP endpoint as clients reach it: absolute, no
|
|
12
|
-
* fragment, ending in the endpoint's `path`. The RFC 9728 `resource`
|
|
13
|
-
* value and the RFC 8707 audience your guard checks tokens against.
|
|
14
|
-
*/
|
|
15
|
-
resource: string;
|
|
16
|
-
/**
|
|
17
|
-
* Name of the identity whose guard verifies the token on every request.
|
|
18
|
-
* Its `issuer` and flow scopes become the metadata document.
|
|
19
|
-
*/
|
|
20
|
-
scheme: string;
|
|
21
|
-
}
|
|
22
|
-
//#endregion
|
|
23
9
|
//#region src/plugin.d.ts
|
|
24
10
|
/**
|
|
25
11
|
* What the MCP endpoint is, and where. Which routes it publishes is each
|
|
26
12
|
* route's own business: declare `tool` on the ones a model may call.
|
|
27
13
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
* Human-readable name shown to AI assistants.
|
|
43
|
-
*
|
|
44
|
-
* @default 'MCP Server'
|
|
45
|
-
*/
|
|
46
|
-
name?: string;
|
|
47
|
-
/**
|
|
48
|
-
* Semantic version string (e.g. "1.0.0").
|
|
49
|
-
*
|
|
50
|
-
* @default '1.0.0'
|
|
51
|
-
*/
|
|
52
|
-
version?: string;
|
|
53
|
-
/**
|
|
54
|
-
* Guidance for the model, appended to the overview built from the
|
|
55
|
-
* contract's tags.
|
|
56
|
-
*/
|
|
57
|
-
instructions?: string;
|
|
58
|
-
/**
|
|
59
|
-
* Serve the endpoint as an OAuth 2.1 resource server, per the MCP
|
|
60
|
-
* authorization specification: RFC 9728 metadata on a well-known route,
|
|
61
|
-
* and HTTP `401`/`403` challenges built from the named identity.
|
|
62
|
-
*/
|
|
63
|
-
oauth?: McpOAuthProps;
|
|
64
|
-
}
|
|
65
|
-
declare const declare: (slug: string, props: McpPluginProps<string>) => _$kizunajs.PluginDeclaration<{
|
|
66
|
-
readonly protectedResourceMetadata?: {
|
|
67
|
-
readonly method: "GET";
|
|
68
|
-
readonly path: `/${string}`;
|
|
69
|
-
readonly summary: "OAuth protected resource metadata";
|
|
70
|
-
readonly responses: {
|
|
71
|
-
readonly 200: z.ZodObject<{
|
|
72
|
-
resource: z.ZodString;
|
|
73
|
-
authorization_servers: z.ZodArray<z.ZodString>;
|
|
74
|
-
scopes_supported: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
75
|
-
bearer_methods_supported: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
76
|
-
}, z.core.$strip>;
|
|
77
|
-
};
|
|
78
|
-
} | undefined;
|
|
79
|
-
readonly endpoint: {
|
|
80
|
-
readonly method: "POST";
|
|
81
|
-
readonly path: `/${string}`;
|
|
82
|
-
readonly summary: "MCP (Model Context Protocol) endpoint";
|
|
83
|
-
readonly body: z.ZodUnknown;
|
|
84
|
-
readonly responses: {
|
|
85
|
-
readonly 200: z.ZodUnknown;
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
}, McpPluginProps<string>, undefined, string, {
|
|
89
|
-
[key: string]: unknown;
|
|
90
|
-
body: unknown;
|
|
91
|
-
headers: Record<string, string | string[] | undefined>;
|
|
92
|
-
requestContext?: Record<string, unknown>;
|
|
93
|
-
}>;
|
|
14
|
+
declare const McpPluginOptionsSchema: z.ZodObject<{
|
|
15
|
+
path: z.ZodOptional<z.ZodCustom<`/${string}`, `/${string}`>>;
|
|
16
|
+
name: z.ZodOptional<z.ZodString>;
|
|
17
|
+
version: z.ZodOptional<z.ZodString>;
|
|
18
|
+
instructions: z.ZodOptional<z.ZodString>;
|
|
19
|
+
oauth: z.ZodOptional<z.ZodObject<{
|
|
20
|
+
resource: z.ZodString;
|
|
21
|
+
scheme: z.ZodString;
|
|
22
|
+
}, z.core.$strip>>;
|
|
23
|
+
}, z.core.$strip>;
|
|
24
|
+
/**
|
|
25
|
+
* What the MCP endpoint is, and where.
|
|
26
|
+
*/
|
|
27
|
+
type McpPluginProps = z.output<typeof McpPluginOptionsSchema>;
|
|
94
28
|
/**
|
|
95
29
|
* Let AI assistants use the API. Serves an MCP (Model Context Protocol)
|
|
96
30
|
* endpoint where every route declaring `tool` is one an assistant can discover
|
|
97
31
|
* and call, behind the same guards as the HTTP endpoints.
|
|
98
32
|
*
|
|
99
33
|
* The endpoint is an ordinary kizuna route, so `api.mount` serves it on any
|
|
100
|
-
* adapter
|
|
101
|
-
* not see it.
|
|
34
|
+
* adapter.
|
|
102
35
|
*
|
|
103
36
|
* @example
|
|
104
37
|
* ```ts
|
|
105
38
|
* export default defineConfig({
|
|
106
39
|
* routes,
|
|
107
|
-
* plugins: [
|
|
40
|
+
* plugins: [
|
|
41
|
+
* mcpPlugin({
|
|
42
|
+
* name: 'My API',
|
|
43
|
+
* }),
|
|
44
|
+
* ],
|
|
108
45
|
* });
|
|
109
46
|
* ```
|
|
110
47
|
*/
|
|
111
|
-
declare
|
|
48
|
+
declare const mcpPlugin: {
|
|
49
|
+
<const Slug extends string>(options: {
|
|
50
|
+
path?: `/${string}` | undefined;
|
|
51
|
+
name?: string | undefined;
|
|
52
|
+
version?: string | undefined;
|
|
53
|
+
instructions?: string | undefined;
|
|
54
|
+
oauth?: {
|
|
55
|
+
resource: string;
|
|
56
|
+
scheme: string;
|
|
57
|
+
} | undefined;
|
|
58
|
+
} & {
|
|
59
|
+
basePath?: never;
|
|
60
|
+
} & _$kizunajs_plugin0.SlugOption<Slug>): _$kizunajs.PluginDeclaration<Slug, {
|
|
61
|
+
routes: PluginRoutes;
|
|
62
|
+
validate: () => void;
|
|
63
|
+
}>;
|
|
64
|
+
(options?: ({
|
|
65
|
+
path?: `/${string}` | undefined;
|
|
66
|
+
name?: string | undefined;
|
|
67
|
+
version?: string | undefined;
|
|
68
|
+
instructions?: string | undefined;
|
|
69
|
+
oauth?: {
|
|
70
|
+
resource: string;
|
|
71
|
+
scheme: string;
|
|
72
|
+
} | undefined;
|
|
73
|
+
} & {
|
|
74
|
+
basePath?: never;
|
|
75
|
+
}) | undefined): _$kizunajs.PluginDeclaration<"mcp", {
|
|
76
|
+
routes: PluginRoutes;
|
|
77
|
+
validate: () => void;
|
|
78
|
+
}>;
|
|
79
|
+
};
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/oauth.d.ts
|
|
82
|
+
declare const McpOAuthSchema: z.ZodObject<{
|
|
83
|
+
resource: z.ZodString;
|
|
84
|
+
scheme: z.ZodString;
|
|
85
|
+
}, z.core.$strip>;
|
|
86
|
+
type McpOAuthProps = z.output<typeof McpOAuthSchema>;
|
|
112
87
|
//#endregion
|
|
113
88
|
//#region src/mcp-server.d.ts
|
|
114
89
|
interface McpServerOptions {
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
2
|
+
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
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
7
|
import { deriveToolNames, isJsonMediaType, isStreamResponse, isSuccessStatus, isVoidSchema, resolveResponseBody, resolveResponseContentType } from "kizunajs/generator";
|
|
8
8
|
//#region src/oauth.ts
|
|
9
|
+
const McpOAuthSchema = z.object({
|
|
10
|
+
/**
|
|
11
|
+
* Canonical URI of the MCP endpoint as clients reach it: absolute, no
|
|
12
|
+
* fragment, ending in the endpoint's `path`. The RFC 9728 `resource`
|
|
13
|
+
* value and the RFC 8707 audience your guard checks tokens against.
|
|
14
|
+
*/
|
|
15
|
+
resource: z.string(),
|
|
16
|
+
/**
|
|
17
|
+
* Name of the identity whose guard verifies the token on every request.
|
|
18
|
+
* Its `issuer` and flow scopes become the metadata document.
|
|
19
|
+
*/
|
|
20
|
+
scheme: z.string()
|
|
21
|
+
});
|
|
9
22
|
const protectedResourceMetadataPath = (endpointPath) => `/.well-known/oauth-protected-resource${endpointPath}`;
|
|
10
23
|
/**
|
|
11
24
|
* Absolute URL the metadata route is served at: the resource's origin, any
|
|
@@ -526,14 +539,25 @@ const toolCallTarget = (body, enforcement) => {
|
|
|
526
539
|
};
|
|
527
540
|
};
|
|
528
541
|
/**
|
|
529
|
-
* What answers
|
|
530
|
-
* declared `tool`, and the calls a model makes against them.
|
|
542
|
+
* What answers one MCP endpoint: the tool list built from the routes that
|
|
543
|
+
* declared `tool`, and the calls a model makes against them. The OAuth setup
|
|
544
|
+
* reads the app's identities, so it's prepared once the api has assembled:
|
|
545
|
+
* `validate` prepares it at startup, and the handlers reuse it.
|
|
531
546
|
*/
|
|
532
|
-
const
|
|
547
|
+
const mcpEndpoint = (props, api) => {
|
|
533
548
|
const serverApi = api;
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
549
|
+
let prepared;
|
|
550
|
+
const enforcementOf = () => {
|
|
551
|
+
if (props.oauth === void 0) return void 0;
|
|
552
|
+
prepared ??= prepareOAuth(props.oauth, props.path ?? "/mcp", serverApi);
|
|
553
|
+
return prepared;
|
|
554
|
+
};
|
|
555
|
+
return {
|
|
556
|
+
validate: () => {
|
|
557
|
+
enforcementOf();
|
|
558
|
+
},
|
|
559
|
+
handle: async (args) => {
|
|
560
|
+
const enforcement = enforcementOf();
|
|
537
561
|
let transportAuth;
|
|
538
562
|
if (enforcement !== void 0) {
|
|
539
563
|
const target = toolCallTarget(args.body, enforcement);
|
|
@@ -568,36 +592,11 @@ const mcpServe = (props, api) => {
|
|
|
568
592
|
body: JSON.stringify(args.body)
|
|
569
593
|
})));
|
|
570
594
|
},
|
|
571
|
-
|
|
595
|
+
protectedResourceMetadata: () => rawResponse(new Response(JSON.stringify(enforcementOf()?.metadata), {
|
|
572
596
|
status: 200,
|
|
573
597
|
headers: { "content-type": "application/json" }
|
|
574
|
-
}))
|
|
575
|
-
}
|
|
576
|
-
};
|
|
577
|
-
//#endregion
|
|
578
|
-
//#region src/plugin.ts
|
|
579
|
-
const declare = (slug, props) => {
|
|
580
|
-
const endpointPath = props.path ?? "/mcp";
|
|
581
|
-
return createPlugin({
|
|
582
|
-
slug,
|
|
583
|
-
routes: {
|
|
584
|
-
endpoint: {
|
|
585
|
-
method: "POST",
|
|
586
|
-
path: endpointPath,
|
|
587
|
-
summary: "MCP (Model Context Protocol) endpoint",
|
|
588
|
-
body: z.unknown(),
|
|
589
|
-
responses: { 200: z.unknown() }
|
|
590
|
-
},
|
|
591
|
-
...props.oauth === void 0 ? {} : { protectedResourceMetadata: {
|
|
592
|
-
method: "GET",
|
|
593
|
-
path: protectedResourceMetadataPath(endpointPath),
|
|
594
|
-
summary: "OAuth protected resource metadata",
|
|
595
|
-
responses: { 200: ProtectedResourceMetadataSchema }
|
|
596
|
-
} }
|
|
597
|
-
},
|
|
598
|
-
props,
|
|
599
|
-
serve: (pluginProps, api) => mcpServe(pluginProps, api)
|
|
600
|
-
});
|
|
598
|
+
}))
|
|
599
|
+
};
|
|
601
600
|
};
|
|
602
601
|
/**
|
|
603
602
|
* Let AI assistants use the API. Serves an MCP (Model Context Protocol)
|
|
@@ -605,20 +604,74 @@ const declare = (slug, props) => {
|
|
|
605
604
|
* and call, behind the same guards as the HTTP endpoints.
|
|
606
605
|
*
|
|
607
606
|
* The endpoint is an ordinary kizuna route, so `api.mount` serves it on any
|
|
608
|
-
* adapter
|
|
609
|
-
* not see it.
|
|
607
|
+
* adapter.
|
|
610
608
|
*
|
|
611
609
|
* @example
|
|
612
610
|
* ```ts
|
|
613
611
|
* export default defineConfig({
|
|
614
612
|
* routes,
|
|
615
|
-
* plugins: [
|
|
613
|
+
* plugins: [
|
|
614
|
+
* mcpPlugin({
|
|
615
|
+
* name: 'My API',
|
|
616
|
+
* }),
|
|
617
|
+
* ],
|
|
616
618
|
* });
|
|
617
619
|
* ```
|
|
618
620
|
*/
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
621
|
+
const mcpPlugin = definePlugin({
|
|
622
|
+
slug: "mcp",
|
|
623
|
+
options: z.object({
|
|
624
|
+
/**
|
|
625
|
+
* Path the endpoint is served from.
|
|
626
|
+
*
|
|
627
|
+
* @default '/mcp'
|
|
628
|
+
*/
|
|
629
|
+
path: RoutePathSchema.optional(),
|
|
630
|
+
/**
|
|
631
|
+
* Human-readable name shown to AI assistants.
|
|
632
|
+
*
|
|
633
|
+
* @default 'MCP Server'
|
|
634
|
+
*/
|
|
635
|
+
name: z.string().optional(),
|
|
636
|
+
/**
|
|
637
|
+
* Semantic version string (e.g. "1.0.0").
|
|
638
|
+
*
|
|
639
|
+
* @default '1.0.0'
|
|
640
|
+
*/
|
|
641
|
+
version: z.string().optional(),
|
|
642
|
+
/**
|
|
643
|
+
* Guidance for the model, appended to the overview built from the
|
|
644
|
+
* contract's tags.
|
|
645
|
+
*/
|
|
646
|
+
instructions: z.string().optional(),
|
|
647
|
+
/**
|
|
648
|
+
* Serve the endpoint as an OAuth 2.1 resource server, per the MCP
|
|
649
|
+
* authorization specification: RFC 9728 metadata on a well-known route,
|
|
650
|
+
* and HTTP `401`/`403` challenges built from the named identity.
|
|
651
|
+
*/
|
|
652
|
+
oauth: McpOAuthSchema.optional()
|
|
653
|
+
}),
|
|
654
|
+
setup: ({ options, api }) => {
|
|
655
|
+
const endpointPath = options.path ?? "/mcp";
|
|
656
|
+
const endpoint = mcpEndpoint(options, api);
|
|
657
|
+
const routes = { endpoint: route({
|
|
658
|
+
method: "POST",
|
|
659
|
+
path: endpointPath,
|
|
660
|
+
summary: "MCP (Model Context Protocol) endpoint",
|
|
661
|
+
body: z.unknown(),
|
|
662
|
+
responses: { 200: z.unknown() }
|
|
663
|
+
}).handler(endpoint.handle) };
|
|
664
|
+
if (options.oauth !== void 0) routes.protectedResourceMetadata = route({
|
|
665
|
+
method: "GET",
|
|
666
|
+
path: protectedResourceMetadataPath(endpointPath),
|
|
667
|
+
summary: "OAuth protected resource metadata",
|
|
668
|
+
responses: { 200: ProtectedResourceMetadataSchema }
|
|
669
|
+
}).handler(endpoint.protectedResourceMetadata);
|
|
670
|
+
return {
|
|
671
|
+
routes,
|
|
672
|
+
validate: endpoint.validate
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
});
|
|
623
676
|
//#endregion
|
|
624
677
|
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.12",
|
|
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.12"
|
|
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/express": "2.0.0-beta.
|
|
74
|
-
"kizunajs": "2.0.0-beta.
|
|
73
|
+
"@kizunajs/express": "2.0.0-beta.12",
|
|
74
|
+
"kizunajs": "2.0.0-beta.12"
|
|
75
75
|
},
|
|
76
76
|
"scripts": {
|
|
77
77
|
"build": "tsdown src/index.ts --format esm,cjs --dts --clean --external kizunajs --external zod --external @modelcontextprotocol/sdk",
|