@kizunajs/mcp 2.0.0-beta.10 → 2.0.0-beta.11

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 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 the MCP endpoint: the tool list built from the routes that
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 mcpServe = (props, api) => {
548
+ const mcpEndpoint = (props, api) => {
534
549
  const serverApi = api;
535
- const enforcement = props.oauth === void 0 ? void 0 : prepareOAuth(props.oauth, props.path ?? "/mcp", serverApi);
536
- return { router: {
537
- endpoint: async (args) => {
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
- ...enforcement === void 0 ? {} : { protectedResourceMetadata: () => (0, kizunajs_adapter.rawResponse)(new Response(JSON.stringify(enforcement.metadata), {
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
- const declare = (slug, props) => {
581
- const endpointPath = props.path ?? "/mcp";
582
- return (0, kizunajs_plugin.createPlugin)({
583
- slug,
584
- routes: {
585
- endpoint: {
586
- method: "POST",
587
- path: endpointPath,
588
- summary: "MCP (Model Context Protocol) endpoint",
589
- body: zod.z.unknown(),
590
- responses: { 200: zod.z.unknown() }
591
- },
592
- ...props.oauth === void 0 ? {} : { protectedResourceMetadata: {
593
- method: "GET",
594
- path: protectedResourceMetadataPath(endpointPath),
595
- summary: "OAuth protected resource metadata",
596
- responses: { 200: kizunajs_schemas.ProtectedResourceMetadataSchema }
597
- } }
598
- },
599
- props,
600
- serve: (pluginProps, api) => mcpServe(pluginProps, api)
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, and it stays out of `api.routes` so the client and the generators do
610
- * not see it.
645
+ * adapter.
611
646
  *
612
647
  * @example
613
648
  * ```ts
614
649
  * export default defineConfig({
615
650
  * routes,
616
- * plugins: [mcpPlugin({ name: 'My API' })],
651
+ * plugins: [
652
+ * mcpPlugin({
653
+ * name: 'My API',
654
+ * }),
655
+ * ],
617
656
  * });
618
657
  * ```
619
658
  */
620
- function mcpPlugin(props) {
621
- const settings = props ?? {};
622
- return declare(settings.slug ?? "mcp", settings);
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,86 @@
1
1
  import * as _$kizunajs from "kizunajs";
2
2
  import { ApiDefinition, RouteDefinition } from "kizunajs";
3
3
  import { z } from "zod";
4
- import { RoutePath, WithSlug } from "kizunajs/plugin";
4
+ import { PluginRoutes } from "kizunajs/plugin";
5
5
  import { McpServer } from "@modelcontextprotocol/server";
6
6
  import { ApiWithRouter, ToolInputSchema } from "kizunajs/adapter";
7
7
 
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
8
  //#region src/plugin.d.ts
24
9
  /**
25
10
  * What the MCP endpoint is, and where. Which routes it publishes is each
26
11
  * route's own business: declare `tool` on the ones a model may call.
27
12
  */
28
- interface McpPluginProps<Slug extends string = 'mcp'> {
29
- /**
30
- * What handlers reach this plugin under. Give a second MCP endpoint its own.
31
- *
32
- * @default 'mcp'
33
- */
34
- slug?: Slug;
35
- /**
36
- * Path the endpoint is served from.
37
- *
38
- * @default '/mcp'
39
- */
40
- path?: RoutePath;
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
- }>;
13
+ declare const McpPluginOptionsSchema: z.ZodObject<{
14
+ path: z.ZodOptional<z.ZodCustom<`/${string}`, `/${string}`>>;
15
+ name: z.ZodOptional<z.ZodString>;
16
+ version: z.ZodOptional<z.ZodString>;
17
+ instructions: z.ZodOptional<z.ZodString>;
18
+ oauth: z.ZodOptional<z.ZodObject<{
19
+ resource: z.ZodString;
20
+ scheme: z.ZodString;
21
+ }, z.core.$strip>>;
22
+ }, z.core.$strip>;
23
+ /**
24
+ * What the MCP endpoint is, and where.
25
+ */
26
+ type McpPluginProps = z.output<typeof McpPluginOptionsSchema>;
94
27
  /**
95
28
  * Let AI assistants use the API. Serves an MCP (Model Context Protocol)
96
29
  * endpoint where every route declaring `tool` is one an assistant can discover
97
30
  * and call, behind the same guards as the HTTP endpoints.
98
31
  *
99
32
  * The endpoint is an ordinary kizuna route, so `api.mount` serves it on any
100
- * adapter, and it stays out of `api.routes` so the client and the generators do
101
- * not see it.
33
+ * adapter.
102
34
  *
103
35
  * @example
104
36
  * ```ts
105
37
  * export default defineConfig({
106
38
  * routes,
107
- * plugins: [mcpPlugin({ name: 'My API' })],
39
+ * plugins: [
40
+ * mcpPlugin({
41
+ * name: 'My API',
42
+ * }),
43
+ * ],
108
44
  * });
109
45
  * ```
110
46
  */
111
- declare function mcpPlugin<const Slug extends string = 'mcp'>(props?: McpPluginProps<Slug>): WithSlug<ReturnType<typeof declare>, Slug>;
47
+ declare const mcpPlugin: {
48
+ <const Slug extends string>(options: {
49
+ path?: `/${string}` | undefined;
50
+ name?: string | undefined;
51
+ version?: string | undefined;
52
+ instructions?: string | undefined;
53
+ oauth?: {
54
+ resource: string;
55
+ scheme: string;
56
+ } | undefined;
57
+ } & {
58
+ slug: Slug;
59
+ }): _$kizunajs.PluginDeclaration<Slug, {
60
+ routes: PluginRoutes;
61
+ validate: () => void;
62
+ }>;
63
+ (options?: {
64
+ path?: `/${string}` | undefined;
65
+ name?: string | undefined;
66
+ version?: string | undefined;
67
+ instructions?: string | undefined;
68
+ oauth?: {
69
+ resource: string;
70
+ scheme: string;
71
+ } | undefined;
72
+ } | undefined): _$kizunajs.PluginDeclaration<"mcp", {
73
+ routes: PluginRoutes;
74
+ validate: () => void;
75
+ }>;
76
+ };
77
+ //#endregion
78
+ //#region src/oauth.d.ts
79
+ declare const McpOAuthSchema: z.ZodObject<{
80
+ resource: z.ZodString;
81
+ scheme: z.ZodString;
82
+ }, z.core.$strip>;
83
+ type McpOAuthProps = z.output<typeof McpOAuthSchema>;
112
84
  //#endregion
113
85
  //#region src/mcp-server.d.ts
114
86
  interface McpServerOptions {
package/dist/index.d.mts CHANGED
@@ -1,114 +1,86 @@
1
1
  import { z } from "zod";
2
- import { RoutePath, WithSlug } from "kizunajs/plugin";
2
+ import { PluginRoutes } from "kizunajs/plugin";
3
3
  import * as _$kizunajs from "kizunajs";
4
4
  import { ApiDefinition, RouteDefinition } from "kizunajs";
5
5
  import { McpServer } from "@modelcontextprotocol/server";
6
6
  import { ApiWithRouter, ToolInputSchema } from "kizunajs/adapter";
7
7
 
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
8
  //#region src/plugin.d.ts
24
9
  /**
25
10
  * What the MCP endpoint is, and where. Which routes it publishes is each
26
11
  * route's own business: declare `tool` on the ones a model may call.
27
12
  */
28
- interface McpPluginProps<Slug extends string = 'mcp'> {
29
- /**
30
- * What handlers reach this plugin under. Give a second MCP endpoint its own.
31
- *
32
- * @default 'mcp'
33
- */
34
- slug?: Slug;
35
- /**
36
- * Path the endpoint is served from.
37
- *
38
- * @default '/mcp'
39
- */
40
- path?: RoutePath;
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
- }>;
13
+ declare const McpPluginOptionsSchema: z.ZodObject<{
14
+ path: z.ZodOptional<z.ZodCustom<`/${string}`, `/${string}`>>;
15
+ name: z.ZodOptional<z.ZodString>;
16
+ version: z.ZodOptional<z.ZodString>;
17
+ instructions: z.ZodOptional<z.ZodString>;
18
+ oauth: z.ZodOptional<z.ZodObject<{
19
+ resource: z.ZodString;
20
+ scheme: z.ZodString;
21
+ }, z.core.$strip>>;
22
+ }, z.core.$strip>;
23
+ /**
24
+ * What the MCP endpoint is, and where.
25
+ */
26
+ type McpPluginProps = z.output<typeof McpPluginOptionsSchema>;
94
27
  /**
95
28
  * Let AI assistants use the API. Serves an MCP (Model Context Protocol)
96
29
  * endpoint where every route declaring `tool` is one an assistant can discover
97
30
  * and call, behind the same guards as the HTTP endpoints.
98
31
  *
99
32
  * The endpoint is an ordinary kizuna route, so `api.mount` serves it on any
100
- * adapter, and it stays out of `api.routes` so the client and the generators do
101
- * not see it.
33
+ * adapter.
102
34
  *
103
35
  * @example
104
36
  * ```ts
105
37
  * export default defineConfig({
106
38
  * routes,
107
- * plugins: [mcpPlugin({ name: 'My API' })],
39
+ * plugins: [
40
+ * mcpPlugin({
41
+ * name: 'My API',
42
+ * }),
43
+ * ],
108
44
  * });
109
45
  * ```
110
46
  */
111
- declare function mcpPlugin<const Slug extends string = 'mcp'>(props?: McpPluginProps<Slug>): WithSlug<ReturnType<typeof declare>, Slug>;
47
+ declare const mcpPlugin: {
48
+ <const Slug extends string>(options: {
49
+ path?: `/${string}` | undefined;
50
+ name?: string | undefined;
51
+ version?: string | undefined;
52
+ instructions?: string | undefined;
53
+ oauth?: {
54
+ resource: string;
55
+ scheme: string;
56
+ } | undefined;
57
+ } & {
58
+ slug: Slug;
59
+ }): _$kizunajs.PluginDeclaration<Slug, {
60
+ routes: PluginRoutes;
61
+ validate: () => void;
62
+ }>;
63
+ (options?: {
64
+ path?: `/${string}` | undefined;
65
+ name?: string | undefined;
66
+ version?: string | undefined;
67
+ instructions?: string | undefined;
68
+ oauth?: {
69
+ resource: string;
70
+ scheme: string;
71
+ } | undefined;
72
+ } | undefined): _$kizunajs.PluginDeclaration<"mcp", {
73
+ routes: PluginRoutes;
74
+ validate: () => void;
75
+ }>;
76
+ };
77
+ //#endregion
78
+ //#region src/oauth.d.ts
79
+ declare const McpOAuthSchema: z.ZodObject<{
80
+ resource: z.ZodString;
81
+ scheme: z.ZodString;
82
+ }, z.core.$strip>;
83
+ type McpOAuthProps = z.output<typeof McpOAuthSchema>;
112
84
  //#endregion
113
85
  //#region src/mcp-server.d.ts
114
86
  interface McpServerOptions {
package/dist/index.mjs CHANGED
@@ -1,11 +1,24 @@
1
1
  import { z } from "zod";
2
- import { createPlugin } from "kizunajs/plugin";
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 the MCP endpoint: the tool list built from the routes that
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 mcpServe = (props, api) => {
547
+ const mcpEndpoint = (props, api) => {
533
548
  const serverApi = api;
534
- const enforcement = props.oauth === void 0 ? void 0 : prepareOAuth(props.oauth, props.path ?? "/mcp", serverApi);
535
- return { router: {
536
- endpoint: async (args) => {
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
- ...enforcement === void 0 ? {} : { protectedResourceMetadata: () => rawResponse(new Response(JSON.stringify(enforcement.metadata), {
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, and it stays out of `api.routes` so the client and the generators do
609
- * not see it.
607
+ * adapter.
610
608
  *
611
609
  * @example
612
610
  * ```ts
613
611
  * export default defineConfig({
614
612
  * routes,
615
- * plugins: [mcpPlugin({ name: 'My API' })],
613
+ * plugins: [
614
+ * mcpPlugin({
615
+ * name: 'My API',
616
+ * }),
617
+ * ],
616
618
  * });
617
619
  * ```
618
620
  */
619
- function mcpPlugin(props) {
620
- const settings = props ?? {};
621
- return declare(settings.slug ?? "mcp", settings);
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.10",
3
+ "version": "2.0.0-beta.11",
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.10"
64
+ "kizunajs": "2.0.0-beta.11"
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.10",
74
- "kizunajs": "2.0.0-beta.10"
73
+ "kizunajs": "2.0.0-beta.11",
74
+ "@kizunajs/express": "2.0.0-beta.11"
75
75
  },
76
76
  "scripts": {
77
77
  "build": "tsdown src/index.ts --format esm,cjs --dts --clean --external kizunajs --external zod --external @modelcontextprotocol/sdk",