@kizunajs/mcp 2.0.0-beta.0
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/LICENSE.MD +9 -0
- package/README.md +51 -0
- package/dist/index.cjs +709 -0
- package/dist/index.d.cts +203 -0
- package/dist/index.d.mts +203 -0
- package/dist/index.mjs +705 -0
- package/package.json +80 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import * as _$kizunajs from "kizunajs";
|
|
2
|
+
import { ApiDefinition, RouteDefinition, Routes } from "kizunajs";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { RoutePath, WithSlug } from "kizunajs/plugin";
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/server";
|
|
6
|
+
import { ApiWithRouter } from "kizunajs/adapter";
|
|
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
|
+
//#region src/plugin.d.ts
|
|
24
|
+
/**
|
|
25
|
+
* What the MCP endpoint is, and where. Which routes it publishes is each
|
|
26
|
+
* route's own business: declare `tool` on the ones a model may call.
|
|
27
|
+
*/
|
|
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
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Let AI assistants use the API. Serves an MCP (Model Context Protocol)
|
|
96
|
+
* endpoint where every route declaring `tool` is one an assistant can discover
|
|
97
|
+
* and call, behind the same guards as the HTTP endpoints.
|
|
98
|
+
*
|
|
99
|
+
* 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.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* export default defineConfig({
|
|
106
|
+
* routes,
|
|
107
|
+
* plugins: [mcpPlugin({ name: 'My API' })],
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare function mcpPlugin<const Slug extends string = 'mcp'>(props?: McpPluginProps<Slug>): WithSlug<ReturnType<typeof declare>, Slug>;
|
|
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
|
+
//#region src/mcp-server.d.ts
|
|
122
|
+
interface McpServerOptions {
|
|
123
|
+
/**
|
|
124
|
+
* Human-readable name for the MCP server.
|
|
125
|
+
*
|
|
126
|
+
* @default 'MCP Server'
|
|
127
|
+
*/
|
|
128
|
+
name?: string;
|
|
129
|
+
/**
|
|
130
|
+
* Semantic version string (e.g. "1.0.0").
|
|
131
|
+
*
|
|
132
|
+
* @default '1.0.0'
|
|
133
|
+
*/
|
|
134
|
+
version?: string;
|
|
135
|
+
/**
|
|
136
|
+
* Keep only the methods RFC 9110 calls safe, so no tool an assistant calls
|
|
137
|
+
* can change data.
|
|
138
|
+
*
|
|
139
|
+
* @default false
|
|
140
|
+
*/
|
|
141
|
+
onlyReadOnly?: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Guidance for the model, appended to the overview built from the
|
|
144
|
+
* contract's tags. Use it for what belongs to no single route: the order
|
|
145
|
+
* operations happen in, the conventions every route shares.
|
|
146
|
+
*/
|
|
147
|
+
instructions?: string;
|
|
148
|
+
/**
|
|
149
|
+
* Extra context spread into every handler call.
|
|
150
|
+
*
|
|
151
|
+
* Adapter-specific endpoints populate this automatically with the
|
|
152
|
+
* framework's request object (e.g. `{ req, res }` for Express,
|
|
153
|
+
* `{ request }` for Next.js).
|
|
154
|
+
*/
|
|
155
|
+
handlerContext?: Record<string, unknown>;
|
|
156
|
+
/**
|
|
157
|
+
* Headers of the MCP transport request, used to extract credentials for
|
|
158
|
+
* secured routes so their guards can run per tool call. Adapter-specific
|
|
159
|
+
* endpoints populate this automatically; configure the MCP client to send
|
|
160
|
+
* the credential (e.g. an `Authorization` header) on its connection.
|
|
161
|
+
*/
|
|
162
|
+
credentialHeaders?: Record<string, string | string[] | undefined>;
|
|
163
|
+
/**
|
|
164
|
+
* A scheme the transport has already verified for this request. Its guard
|
|
165
|
+
* is skipped per tool call, and `context` reaches handlers under
|
|
166
|
+
* `auth.<scheme>`.
|
|
167
|
+
*/
|
|
168
|
+
transportAuth?: {
|
|
169
|
+
scheme: string;
|
|
170
|
+
context?: Record<string, unknown>;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
interface ToolDefinition {
|
|
174
|
+
name: string;
|
|
175
|
+
title: string | undefined;
|
|
176
|
+
description: string;
|
|
177
|
+
inputSchema: ToolInputSchema;
|
|
178
|
+
outputSchema: z.ZodType;
|
|
179
|
+
route: RouteDefinition;
|
|
180
|
+
routeKey: string;
|
|
181
|
+
tags: string[];
|
|
182
|
+
}
|
|
183
|
+
declare const buildToolDefinitions: (routes: Routes) => ToolDefinition[];
|
|
184
|
+
/**
|
|
185
|
+
* What a client puts in front of the model before it picks a tool.
|
|
186
|
+
*/
|
|
187
|
+
declare const buildInstructions: (contract: ApiDefinition | undefined, definitions: readonly ToolDefinition[], authored: string | undefined) => string;
|
|
188
|
+
/**
|
|
189
|
+
* Create an MCP server from a kizuna API.
|
|
190
|
+
*
|
|
191
|
+
* Each route in the routes becomes an MCP tool. When an AI assistant calls
|
|
192
|
+
* a tool, the corresponding handler is invoked directly.
|
|
193
|
+
*
|
|
194
|
+
* ```ts
|
|
195
|
+
* import { createMcpServer } from '@kizunajs/mcp';
|
|
196
|
+
* import { api } from './api';
|
|
197
|
+
*
|
|
198
|
+
* const server = createMcpServer(api);
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
declare const createMcpServer: (api: ApiWithRouter, options?: McpServerOptions) => McpServer;
|
|
202
|
+
//#endregion
|
|
203
|
+
export { type McpOAuthProps, type McpPluginProps, type McpServerOptions, type ToolDefinition, buildInstructions, buildToolDefinitions, createMcpServer, mcpPlugin };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { RoutePath, WithSlug } from "kizunajs/plugin";
|
|
3
|
+
import * as _$kizunajs from "kizunajs";
|
|
4
|
+
import { ApiDefinition, RouteDefinition, Routes } from "kizunajs";
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/server";
|
|
6
|
+
import { ApiWithRouter } from "kizunajs/adapter";
|
|
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
|
+
//#region src/plugin.d.ts
|
|
24
|
+
/**
|
|
25
|
+
* What the MCP endpoint is, and where. Which routes it publishes is each
|
|
26
|
+
* route's own business: declare `tool` on the ones a model may call.
|
|
27
|
+
*/
|
|
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
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Let AI assistants use the API. Serves an MCP (Model Context Protocol)
|
|
96
|
+
* endpoint where every route declaring `tool` is one an assistant can discover
|
|
97
|
+
* and call, behind the same guards as the HTTP endpoints.
|
|
98
|
+
*
|
|
99
|
+
* 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.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* export default defineConfig({
|
|
106
|
+
* routes,
|
|
107
|
+
* plugins: [mcpPlugin({ name: 'My API' })],
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
declare function mcpPlugin<const Slug extends string = 'mcp'>(props?: McpPluginProps<Slug>): WithSlug<ReturnType<typeof declare>, Slug>;
|
|
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
|
+
//#region src/mcp-server.d.ts
|
|
122
|
+
interface McpServerOptions {
|
|
123
|
+
/**
|
|
124
|
+
* Human-readable name for the MCP server.
|
|
125
|
+
*
|
|
126
|
+
* @default 'MCP Server'
|
|
127
|
+
*/
|
|
128
|
+
name?: string;
|
|
129
|
+
/**
|
|
130
|
+
* Semantic version string (e.g. "1.0.0").
|
|
131
|
+
*
|
|
132
|
+
* @default '1.0.0'
|
|
133
|
+
*/
|
|
134
|
+
version?: string;
|
|
135
|
+
/**
|
|
136
|
+
* Keep only the methods RFC 9110 calls safe, so no tool an assistant calls
|
|
137
|
+
* can change data.
|
|
138
|
+
*
|
|
139
|
+
* @default false
|
|
140
|
+
*/
|
|
141
|
+
onlyReadOnly?: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Guidance for the model, appended to the overview built from the
|
|
144
|
+
* contract's tags. Use it for what belongs to no single route: the order
|
|
145
|
+
* operations happen in, the conventions every route shares.
|
|
146
|
+
*/
|
|
147
|
+
instructions?: string;
|
|
148
|
+
/**
|
|
149
|
+
* Extra context spread into every handler call.
|
|
150
|
+
*
|
|
151
|
+
* Adapter-specific endpoints populate this automatically with the
|
|
152
|
+
* framework's request object (e.g. `{ req, res }` for Express,
|
|
153
|
+
* `{ request }` for Next.js).
|
|
154
|
+
*/
|
|
155
|
+
handlerContext?: Record<string, unknown>;
|
|
156
|
+
/**
|
|
157
|
+
* Headers of the MCP transport request, used to extract credentials for
|
|
158
|
+
* secured routes so their guards can run per tool call. Adapter-specific
|
|
159
|
+
* endpoints populate this automatically; configure the MCP client to send
|
|
160
|
+
* the credential (e.g. an `Authorization` header) on its connection.
|
|
161
|
+
*/
|
|
162
|
+
credentialHeaders?: Record<string, string | string[] | undefined>;
|
|
163
|
+
/**
|
|
164
|
+
* A scheme the transport has already verified for this request. Its guard
|
|
165
|
+
* is skipped per tool call, and `context` reaches handlers under
|
|
166
|
+
* `auth.<scheme>`.
|
|
167
|
+
*/
|
|
168
|
+
transportAuth?: {
|
|
169
|
+
scheme: string;
|
|
170
|
+
context?: Record<string, unknown>;
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
interface ToolDefinition {
|
|
174
|
+
name: string;
|
|
175
|
+
title: string | undefined;
|
|
176
|
+
description: string;
|
|
177
|
+
inputSchema: ToolInputSchema;
|
|
178
|
+
outputSchema: z.ZodType;
|
|
179
|
+
route: RouteDefinition;
|
|
180
|
+
routeKey: string;
|
|
181
|
+
tags: string[];
|
|
182
|
+
}
|
|
183
|
+
declare const buildToolDefinitions: (routes: Routes) => ToolDefinition[];
|
|
184
|
+
/**
|
|
185
|
+
* What a client puts in front of the model before it picks a tool.
|
|
186
|
+
*/
|
|
187
|
+
declare const buildInstructions: (contract: ApiDefinition | undefined, definitions: readonly ToolDefinition[], authored: string | undefined) => string;
|
|
188
|
+
/**
|
|
189
|
+
* Create an MCP server from a kizuna API.
|
|
190
|
+
*
|
|
191
|
+
* Each route in the routes becomes an MCP tool. When an AI assistant calls
|
|
192
|
+
* a tool, the corresponding handler is invoked directly.
|
|
193
|
+
*
|
|
194
|
+
* ```ts
|
|
195
|
+
* import { createMcpServer } from '@kizunajs/mcp';
|
|
196
|
+
* import { api } from './api';
|
|
197
|
+
*
|
|
198
|
+
* const server = createMcpServer(api);
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
declare const createMcpServer: (api: ApiWithRouter, options?: McpServerOptions) => McpServer;
|
|
202
|
+
//#endregion
|
|
203
|
+
export { type McpOAuthProps, type McpPluginProps, type McpServerOptions, type ToolDefinition, buildInstructions, buildToolDefinitions, createMcpServer, mcpPlugin };
|