@kubb/plugin-mcp 5.0.0-beta.4 → 5.0.0-beta.56
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/README.md +39 -22
- package/dist/index.cjs +422 -349
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +85 -34
- package/dist/index.js +423 -350
- package/dist/index.js.map +1 -1
- package/package.json +13 -21
- package/src/components/McpHandler.tsx +26 -42
- package/src/components/Server.tsx +76 -71
- package/src/generators/mcpGenerator.tsx +19 -21
- package/src/generators/serverGenerator.tsx +20 -20
- package/src/plugin.ts +39 -19
- package/src/resolvers/resolverMcp.ts +18 -8
- package/src/types.ts +31 -21
- package/src/utils.ts +15 -80
- package/extension.yaml +0 -470
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getOperationParameters } from '@internals/shared'
|
|
1
2
|
import { ast } from '@kubb/core'
|
|
2
3
|
import { functionPrinter } from '@kubb/plugin-ts'
|
|
3
4
|
import { Const, File, Function } from '@kubb/renderer-jsx'
|
|
@@ -42,13 +43,13 @@ type Props = {
|
|
|
42
43
|
/**
|
|
43
44
|
* Query params — individual schemas to compose into `z.object({ ... })`.
|
|
44
45
|
*/
|
|
45
|
-
queryParams?: string | Array<ZodParam>
|
|
46
|
+
queryParams?: string | Array<ZodParam> | null
|
|
46
47
|
/**
|
|
47
48
|
* Header params — individual schemas to compose into `z.object({ ... })`.
|
|
48
49
|
*/
|
|
49
|
-
headerParams?: string | Array<ZodParam>
|
|
50
|
-
requestName?: string
|
|
51
|
-
responseName?: string
|
|
50
|
+
headerParams?: string | Array<ZodParam> | null
|
|
51
|
+
requestName?: string | null
|
|
52
|
+
responseName?: string | null
|
|
52
53
|
}
|
|
53
54
|
node: ast.OperationNode
|
|
54
55
|
}>
|
|
@@ -57,69 +58,57 @@ type Props = {
|
|
|
57
58
|
const keysPrinter = functionPrinter({ mode: 'keys' })
|
|
58
59
|
|
|
59
60
|
export function Server({ name, serverName, serverVersion, paramsCasing, operations }: Props): KubbReactNode {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const outputSchema = zod.responseName
|
|
112
|
-
|
|
113
|
-
const config = [
|
|
114
|
-
tool.title ? `title: ${JSON.stringify(tool.title)}` : null,
|
|
115
|
-
`description: ${JSON.stringify(tool.description)}`,
|
|
116
|
-
outputSchema ? `outputSchema: { data: ${outputSchema} }` : null,
|
|
117
|
-
]
|
|
118
|
-
.filter(Boolean)
|
|
119
|
-
.join(',\n ')
|
|
120
|
-
|
|
121
|
-
if (inputSchema) {
|
|
122
|
-
return `
|
|
61
|
+
const registrations = operations
|
|
62
|
+
.map(({ tool, mcp, zod, node }) => {
|
|
63
|
+
const { path: pathParams } = getOperationParameters(node, { paramsCasing })
|
|
64
|
+
|
|
65
|
+
const pathEntries: Array<{ key: string; value: string }> = []
|
|
66
|
+
const otherEntries: Array<{ key: string; value: string }> = []
|
|
67
|
+
|
|
68
|
+
for (const p of pathParams) {
|
|
69
|
+
const zodParam = zod.pathParams.find((zp) => zp.name === p.name)
|
|
70
|
+
pathEntries.push({ key: p.name, value: zodParam ? zodParam.schemaName : zodExprFromSchemaNode(p.schema) })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (zod.requestName) {
|
|
74
|
+
otherEntries.push({ key: 'data', value: zod.requestName })
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (zod.queryParams) {
|
|
78
|
+
otherEntries.push({ key: 'params', value: zodGroupExpr(zod.queryParams) })
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (zod.headerParams) {
|
|
82
|
+
otherEntries.push({ key: 'headers', value: zodGroupExpr(zod.headerParams) })
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
otherEntries.sort((a, b) => a.key.localeCompare(b.key))
|
|
86
|
+
const entries = [...pathEntries, ...otherEntries]
|
|
87
|
+
|
|
88
|
+
const paramsNode = entries.length
|
|
89
|
+
? ast.createFunctionParameters({
|
|
90
|
+
params: [
|
|
91
|
+
ast.createParameterGroup({
|
|
92
|
+
properties: entries.map((e) => ast.createFunctionParameter({ name: e.key, optional: false })),
|
|
93
|
+
}),
|
|
94
|
+
],
|
|
95
|
+
})
|
|
96
|
+
: null
|
|
97
|
+
|
|
98
|
+
const destructured = paramsNode ? (keysPrinter.print(paramsNode) ?? '') : ''
|
|
99
|
+
const inputSchema = entries.length ? `{ ${entries.map((e) => `${e.key}: ${e.value}`).join(', ')} }` : null
|
|
100
|
+
const outputSchema = zod.responseName
|
|
101
|
+
|
|
102
|
+
const config = [
|
|
103
|
+
tool.title ? `title: ${JSON.stringify(tool.title)}` : null,
|
|
104
|
+
`description: ${JSON.stringify(tool.description)}`,
|
|
105
|
+
outputSchema ? `outputSchema: { data: ${outputSchema} }` : null,
|
|
106
|
+
]
|
|
107
|
+
.filter(Boolean)
|
|
108
|
+
.join(',\n ')
|
|
109
|
+
|
|
110
|
+
if (inputSchema) {
|
|
111
|
+
return `
|
|
123
112
|
server.registerTool(${JSON.stringify(tool.name)}, {
|
|
124
113
|
${config},
|
|
125
114
|
inputSchema: ${inputSchema},
|
|
@@ -127,17 +116,33 @@ server.registerTool(${JSON.stringify(tool.name)}, {
|
|
|
127
116
|
return ${mcp.name}(${destructured}, request)
|
|
128
117
|
})
|
|
129
118
|
`
|
|
130
|
-
|
|
119
|
+
}
|
|
131
120
|
|
|
132
|
-
|
|
121
|
+
return `
|
|
133
122
|
server.registerTool(${JSON.stringify(tool.name)}, {
|
|
134
123
|
${config},
|
|
135
124
|
}, async (request) => {
|
|
136
125
|
return ${mcp.name}(request)
|
|
137
126
|
})
|
|
138
127
|
`
|
|
139
|
-
|
|
140
|
-
|
|
128
|
+
})
|
|
129
|
+
.filter(Boolean)
|
|
130
|
+
.join('\n')
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<File.Source name={name} isExportable isIndexable>
|
|
134
|
+
<Function name="getServer" export>
|
|
135
|
+
{`const server = new McpServer({
|
|
136
|
+
name: '${serverName}',
|
|
137
|
+
version: '${serverVersion}',
|
|
138
|
+
})
|
|
139
|
+
${registrations}
|
|
140
|
+
return server`}
|
|
141
|
+
</Function>
|
|
142
|
+
|
|
143
|
+
<Const name={'server'} export>
|
|
144
|
+
{'getServer()'}
|
|
145
|
+
</Const>
|
|
141
146
|
|
|
142
147
|
<Function name="startServer" async export>
|
|
143
148
|
{`try {
|
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
|
+
import { resolveOperationTypeNames } from '@internals/shared'
|
|
2
3
|
import { ast, defineGenerator } from '@kubb/core'
|
|
3
4
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
4
5
|
import { File, jsxRenderer } from '@kubb/renderer-jsx'
|
|
5
6
|
import { McpHandler } from '../components/McpHandler.tsx'
|
|
6
7
|
import type { PluginMcp } from '../types.ts'
|
|
7
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Built-in operation generator for `@kubb/plugin-mcp`. Emits one MCP tool
|
|
11
|
+
* handler per OpenAPI operation, wiring the input Zod schema, the HTTP call,
|
|
12
|
+
* and the response shape into a single function that an MCP server can
|
|
13
|
+
* register as a callable tool.
|
|
14
|
+
*/
|
|
8
15
|
export const mcpGenerator = defineGenerator<PluginMcp>({
|
|
9
16
|
name: 'mcp',
|
|
10
17
|
renderer: jsxRenderer,
|
|
11
18
|
operation(node, ctx) {
|
|
19
|
+
if (!ast.isHttpOperationNode(node)) return null
|
|
12
20
|
const { resolver, driver, root } = ctx
|
|
13
21
|
const { output, client, paramsCasing, group } = ctx.options
|
|
14
22
|
|
|
@@ -20,30 +28,20 @@ export const mcpGenerator = defineGenerator<PluginMcp>({
|
|
|
20
28
|
|
|
21
29
|
const tsResolver = driver.getResolver(pluginTsName)
|
|
22
30
|
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
const pathParams = casedParams.filter((p) => p.in === 'path')
|
|
26
|
-
const queryParams = casedParams.filter((p) => p.in === 'query')
|
|
27
|
-
const headerParams = casedParams.filter((p) => p.in === 'header')
|
|
28
|
-
|
|
29
|
-
const importedTypeNames = [
|
|
30
|
-
...pathParams.map((p) => tsResolver.resolvePathParamsName(node, p)),
|
|
31
|
-
...queryParams.map((p) => tsResolver.resolveQueryParamsName(node, p)),
|
|
32
|
-
...headerParams.map((p) => tsResolver.resolveHeaderParamsName(node, p)),
|
|
33
|
-
node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : undefined,
|
|
34
|
-
tsResolver.resolveResponseName(node),
|
|
35
|
-
...node.responses.filter((r) => Number(r.statusCode) >= 400).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode)),
|
|
36
|
-
].filter(Boolean)
|
|
31
|
+
const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing, responseStatusNames: 'error' })
|
|
37
32
|
|
|
38
33
|
const meta = {
|
|
39
|
-
name: resolver.
|
|
40
|
-
file: resolver.resolveFile(
|
|
34
|
+
name: resolver.resolveHandlerName(node),
|
|
35
|
+
file: resolver.resolveFile(
|
|
36
|
+
{ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
|
|
37
|
+
{ root, output, group: group ?? undefined },
|
|
38
|
+
),
|
|
41
39
|
fileTs: tsResolver.resolveFile(
|
|
42
40
|
{ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
|
|
43
41
|
{
|
|
44
42
|
root,
|
|
45
43
|
output: pluginTs.options?.output ?? output,
|
|
46
|
-
group: pluginTs.options?.group,
|
|
44
|
+
group: pluginTs.options?.group ?? undefined,
|
|
47
45
|
},
|
|
48
46
|
),
|
|
49
47
|
} as const
|
|
@@ -59,7 +57,7 @@ export const mcpGenerator = defineGenerator<PluginMcp>({
|
|
|
59
57
|
{client.importPath ? (
|
|
60
58
|
<>
|
|
61
59
|
<File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={client.importPath} isTypeOnly />
|
|
62
|
-
<File.Import name={'
|
|
60
|
+
<File.Import name={'client'} path={client.importPath} />
|
|
63
61
|
{client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={client.importPath} isTypeOnly />}
|
|
64
62
|
</>
|
|
65
63
|
) : (
|
|
@@ -67,12 +65,12 @@ export const mcpGenerator = defineGenerator<PluginMcp>({
|
|
|
67
65
|
<File.Import
|
|
68
66
|
name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
|
|
69
67
|
root={meta.file.path}
|
|
70
|
-
path={path.resolve(root, '.kubb/
|
|
68
|
+
path={path.resolve(root, '.kubb/client.ts')}
|
|
71
69
|
isTypeOnly
|
|
72
70
|
/>
|
|
73
|
-
<File.Import name={['
|
|
71
|
+
<File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />
|
|
74
72
|
{client.dataReturnType === 'full' && (
|
|
75
|
-
<File.Import name={['ResponseConfig']} root={meta.file.path} path={path.resolve(root, '.kubb/
|
|
73
|
+
<File.Import name={['ResponseConfig']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} isTypeOnly />
|
|
76
74
|
)}
|
|
77
75
|
</>
|
|
78
76
|
)}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
|
+
import { findSuccessStatusCode, getOperationParameters } from '@internals/shared'
|
|
2
3
|
import { ast, defineGenerator } from '@kubb/core'
|
|
3
4
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
4
5
|
import { File, jsxRenderer } from '@kubb/renderer-jsx'
|
|
5
6
|
import { Server } from '../components/Server.tsx'
|
|
6
7
|
import type { PluginMcp } from '../types.ts'
|
|
7
|
-
import { findSuccessStatusCode } from '../utils.ts'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Default v5 server generator for `@kubb/plugin-mcp`.
|
|
@@ -17,7 +17,7 @@ export const serverGenerator = defineGenerator<PluginMcp>({
|
|
|
17
17
|
name: 'operations',
|
|
18
18
|
renderer: jsxRenderer,
|
|
19
19
|
operations(nodes, ctx) {
|
|
20
|
-
const {
|
|
20
|
+
const { config, resolver, plugin, driver, root } = ctx
|
|
21
21
|
const { output, paramsCasing, group } = ctx.options
|
|
22
22
|
|
|
23
23
|
const pluginZod = driver.getPlugin(pluginZodName)
|
|
@@ -43,26 +43,26 @@ export const serverGenerator = defineGenerator<PluginMcp>({
|
|
|
43
43
|
meta: { pluginName: plugin.name },
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const operationsMapped = nodes.map((node) => {
|
|
47
|
-
const
|
|
48
|
-
const pathParams = casedParams.filter((p) => p.in === 'path')
|
|
49
|
-
const queryParams = casedParams.filter((p) => p.in === 'query')
|
|
50
|
-
const headerParams = casedParams.filter((p) => p.in === 'header')
|
|
46
|
+
const operationsMapped = nodes.filter(ast.isHttpOperationNode).map((node) => {
|
|
47
|
+
const { path: pathParams, query: queryParams, header: headerParams } = getOperationParameters(node, { paramsCasing })
|
|
51
48
|
|
|
52
|
-
const mcpFile = resolver.resolveFile(
|
|
49
|
+
const mcpFile = resolver.resolveFile(
|
|
50
|
+
{ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
|
|
51
|
+
{ root, output, group: group ?? undefined },
|
|
52
|
+
)
|
|
53
53
|
|
|
54
54
|
const zodFile = zodResolver.resolveFile(
|
|
55
55
|
{ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
|
|
56
56
|
{
|
|
57
57
|
root,
|
|
58
58
|
output: pluginZod.options?.output ?? output,
|
|
59
|
-
group: pluginZod.options?.group,
|
|
59
|
+
group: pluginZod.options?.group ?? undefined,
|
|
60
60
|
},
|
|
61
61
|
)
|
|
62
62
|
|
|
63
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName(node) :
|
|
63
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName(node) : null
|
|
64
64
|
const successStatus = findSuccessStatusCode(node.responses)
|
|
65
|
-
const responseName = successStatus ? zodResolver.resolveResponseStatusName(node, successStatus) :
|
|
65
|
+
const responseName = successStatus ? zodResolver.resolveResponseStatusName(node, successStatus) : null
|
|
66
66
|
|
|
67
67
|
const resolveParams = (params: typeof pathParams) => params.map((p) => ({ name: p.name, schemaName: zodResolver.resolveParamName(node, p) }))
|
|
68
68
|
|
|
@@ -73,13 +73,13 @@ export const serverGenerator = defineGenerator<PluginMcp>({
|
|
|
73
73
|
description: node.description || `Make a ${node.method.toUpperCase()} request to ${node.path}`,
|
|
74
74
|
},
|
|
75
75
|
mcp: {
|
|
76
|
-
name: resolver.
|
|
76
|
+
name: resolver.resolveHandlerName(node),
|
|
77
77
|
file: mcpFile,
|
|
78
78
|
},
|
|
79
79
|
zod: {
|
|
80
80
|
pathParams: resolveParams(pathParams),
|
|
81
|
-
queryParams: queryParams.length ? resolveParams(queryParams) :
|
|
82
|
-
headerParams: headerParams.length ? resolveParams(headerParams) :
|
|
81
|
+
queryParams: queryParams.length ? resolveParams(queryParams) : null,
|
|
82
|
+
headerParams: headerParams.length ? resolveParams(headerParams) : null,
|
|
83
83
|
requestName,
|
|
84
84
|
responseName,
|
|
85
85
|
file: zodFile,
|
|
@@ -95,7 +95,7 @@ export const serverGenerator = defineGenerator<PluginMcp>({
|
|
|
95
95
|
...(zod.headerParams ?? []).map((p) => p.schemaName),
|
|
96
96
|
zod.requestName,
|
|
97
97
|
zod.responseName,
|
|
98
|
-
].filter(Boolean)
|
|
98
|
+
].filter((name): name is string => Boolean(name))
|
|
99
99
|
|
|
100
100
|
const uniqueNames = [...new Set(zodNames)].sort()
|
|
101
101
|
|
|
@@ -111,8 +111,8 @@ export const serverGenerator = defineGenerator<PluginMcp>({
|
|
|
111
111
|
baseName={serverFile.baseName}
|
|
112
112
|
path={serverFile.path}
|
|
113
113
|
meta={serverFile.meta}
|
|
114
|
-
banner={resolver.resolveBanner(
|
|
115
|
-
footer={resolver.resolveFooter(
|
|
114
|
+
banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: serverFile.path, baseName: serverFile.baseName } })}
|
|
115
|
+
footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: serverFile.path, baseName: serverFile.baseName } })}
|
|
116
116
|
>
|
|
117
117
|
<File.Import name={['McpServer']} path={'@modelcontextprotocol/sdk/server/mcp'} />
|
|
118
118
|
<File.Import name={['z']} path={'zod'} />
|
|
@@ -121,8 +121,8 @@ export const serverGenerator = defineGenerator<PluginMcp>({
|
|
|
121
121
|
{imports}
|
|
122
122
|
<Server
|
|
123
123
|
name={name}
|
|
124
|
-
serverName={
|
|
125
|
-
serverVersion={
|
|
124
|
+
serverName={ctx.meta.title ?? 'server'}
|
|
125
|
+
serverVersion={ctx.meta.version ?? '0.0.0'}
|
|
126
126
|
paramsCasing={paramsCasing}
|
|
127
127
|
operations={operationsMapped}
|
|
128
128
|
/>
|
|
@@ -133,7 +133,7 @@ export const serverGenerator = defineGenerator<PluginMcp>({
|
|
|
133
133
|
{`
|
|
134
134
|
{
|
|
135
135
|
"mcpServers": {
|
|
136
|
-
"${
|
|
136
|
+
"${ctx.meta.title || 'server'}": {
|
|
137
137
|
"type": "stdio",
|
|
138
138
|
"command": "npx",
|
|
139
139
|
"args": ["tsx", "${path.relative(path.dirname(jsonFile.path), serverFile.path)}"]
|
package/src/plugin.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
|
-
import {
|
|
2
|
+
import { createGroupConfig } from '@internals/shared'
|
|
3
3
|
|
|
4
|
-
import { ast, definePlugin
|
|
4
|
+
import { ast, definePlugin } from '@kubb/core'
|
|
5
5
|
import { pluginClientName } from '@kubb/plugin-client'
|
|
6
6
|
import { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'
|
|
7
7
|
import { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'
|
|
@@ -13,11 +13,43 @@ import { serverGenerator } from './generators/serverGenerator.tsx'
|
|
|
13
13
|
import { resolverMcp } from './resolvers/resolverMcp.ts'
|
|
14
14
|
import type { PluginMcp } from './types.ts'
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Canonical plugin name for `@kubb/plugin-mcp`. Used for driver lookups and
|
|
18
|
+
* cross-plugin dependency references.
|
|
19
|
+
*/
|
|
16
20
|
export const pluginMcpName = 'plugin-mcp' satisfies PluginMcp['name']
|
|
17
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Generates a Model Context Protocol (MCP) server from an OpenAPI spec. Every
|
|
24
|
+
* operation becomes a typed MCP tool that AI assistants (Claude Desktop, Claude
|
|
25
|
+
* Code, MCP-compatible clients) can call directly.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { defineConfig } from 'kubb'
|
|
30
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
31
|
+
* import { pluginClient } from '@kubb/plugin-client'
|
|
32
|
+
* import { pluginZod } from '@kubb/plugin-zod'
|
|
33
|
+
* import { pluginMcp } from '@kubb/plugin-mcp'
|
|
34
|
+
*
|
|
35
|
+
* export default defineConfig({
|
|
36
|
+
* input: { path: './petStore.yaml' },
|
|
37
|
+
* output: { path: './src/gen' },
|
|
38
|
+
* plugins: [
|
|
39
|
+
* pluginTs(),
|
|
40
|
+
* pluginClient(),
|
|
41
|
+
* pluginZod(),
|
|
42
|
+
* pluginMcp({
|
|
43
|
+
* output: { path: './mcp' },
|
|
44
|
+
* client: { baseURL: 'https://petstore.swagger.io/v2' },
|
|
45
|
+
* }),
|
|
46
|
+
* ],
|
|
47
|
+
* })
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
18
50
|
export const pluginMcp = definePlugin<PluginMcp>((options) => {
|
|
19
51
|
const {
|
|
20
|
-
output = { path: 'mcp',
|
|
52
|
+
output = { path: 'mcp', barrel: { type: 'named' } },
|
|
21
53
|
group,
|
|
22
54
|
exclude = [],
|
|
23
55
|
include,
|
|
@@ -32,19 +64,7 @@ export const pluginMcp = definePlugin<PluginMcp>((options) => {
|
|
|
32
64
|
const clientName = client?.client ?? 'axios'
|
|
33
65
|
const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)
|
|
34
66
|
|
|
35
|
-
const groupConfig = group
|
|
36
|
-
? ({
|
|
37
|
-
...group,
|
|
38
|
-
name: group.name
|
|
39
|
-
? group.name
|
|
40
|
-
: (ctx: { group: string }) => {
|
|
41
|
-
if (group.type === 'path') {
|
|
42
|
-
return `${ctx.group.split('/')[1]}`
|
|
43
|
-
}
|
|
44
|
-
return `${camelCase(ctx.group)}Requests`
|
|
45
|
-
},
|
|
46
|
-
} satisfies Group)
|
|
47
|
-
: undefined
|
|
67
|
+
const groupConfig = createGroupConfig(group)
|
|
48
68
|
|
|
49
69
|
return {
|
|
50
70
|
name: pluginMcpName,
|
|
@@ -87,11 +107,11 @@ export const pluginMcp = definePlugin<PluginMcp>((options) => {
|
|
|
87
107
|
|
|
88
108
|
if (client?.bundle && !hasClientPlugin && !clientImportPath) {
|
|
89
109
|
ctx.injectFile({
|
|
90
|
-
baseName: '
|
|
91
|
-
path: path.resolve(root, '.kubb/
|
|
110
|
+
baseName: 'client.ts',
|
|
111
|
+
path: path.resolve(root, '.kubb/client.ts'),
|
|
92
112
|
sources: [
|
|
93
113
|
ast.createSource({
|
|
94
|
-
name: '
|
|
114
|
+
name: 'client',
|
|
95
115
|
nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],
|
|
96
116
|
isExportable: true,
|
|
97
117
|
isIndexable: true,
|
|
@@ -1,25 +1,35 @@
|
|
|
1
|
-
import { camelCase } from '@internals/utils'
|
|
1
|
+
import { camelCase, toFilePath } from '@internals/utils'
|
|
2
2
|
import { defineResolver } from '@kubb/core'
|
|
3
3
|
import type { PluginMcp } from '../types.ts'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Default resolver used by `@kubb/plugin-mcp`. Decides the names and file
|
|
7
|
+
* paths for every generated MCP tool handler. Function names get a `Handler`
|
|
8
|
+
* suffix so an operation `addPet` becomes `addPetHandler`.
|
|
7
9
|
*
|
|
8
|
-
*
|
|
10
|
+
* @example Resolve a handler name
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { resolverMcp } from '@kubb/plugin-mcp'
|
|
9
13
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
14
|
+
* resolverMcp.default('addPet', 'function') // 'addPetHandler'
|
|
15
|
+
* ```
|
|
12
16
|
*/
|
|
13
|
-
export const resolverMcp = defineResolver<PluginMcp>((
|
|
17
|
+
export const resolverMcp = defineResolver<PluginMcp>(() => ({
|
|
14
18
|
name: 'default',
|
|
15
19
|
pluginName: 'plugin-mcp',
|
|
16
20
|
default(name, type) {
|
|
17
21
|
if (type === 'file') {
|
|
18
|
-
return
|
|
22
|
+
return toFilePath(name)
|
|
19
23
|
}
|
|
20
24
|
return camelCase(name, { suffix: 'handler' })
|
|
21
25
|
},
|
|
22
26
|
resolveName(name) {
|
|
23
|
-
return
|
|
27
|
+
return this.default(name, 'function')
|
|
28
|
+
},
|
|
29
|
+
resolvePathName(name, type) {
|
|
30
|
+
return this.default(name, type)
|
|
31
|
+
},
|
|
32
|
+
resolveHandlerName(node) {
|
|
33
|
+
return this.resolveName(node.operationId)
|
|
24
34
|
},
|
|
25
35
|
}))
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ast, Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
|
|
1
|
+
import type { ast, Exclude, Generator, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
|
|
2
2
|
import type { ClientImportPath, PluginClient } from '@kubb/plugin-client'
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -6,54 +6,64 @@ import type { ClientImportPath, PluginClient } from '@kubb/plugin-client'
|
|
|
6
6
|
*/
|
|
7
7
|
export type ResolverMcp = Resolver & {
|
|
8
8
|
/**
|
|
9
|
-
* Resolves the handler function name for an operation.
|
|
9
|
+
* Resolves the base handler function name for an operation.
|
|
10
10
|
*
|
|
11
11
|
* @example Resolving handler function names
|
|
12
12
|
* `resolver.resolveName('show pet by id') // -> 'showPetByIdHandler'`
|
|
13
13
|
*/
|
|
14
14
|
resolveName(this: ResolverMcp, name: string): string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type Options = {
|
|
18
15
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @default { path: 'mcp', barrelType: 'named' }
|
|
16
|
+
* Resolves the output file name for an MCP module.
|
|
21
17
|
*/
|
|
22
|
-
|
|
18
|
+
resolvePathName(this: ResolverMcp, name: string, type?: 'file' | 'function' | 'type' | 'const'): string
|
|
23
19
|
/**
|
|
24
|
-
*
|
|
20
|
+
* Resolves the handler function name for an operation.
|
|
25
21
|
*/
|
|
26
|
-
|
|
22
|
+
resolveHandlerName(this: ResolverMcp, node: ast.OperationNode): string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Where the generated MCP tool handlers are written and how they are exported, plus the optional
|
|
27
|
+
* `group` strategy. The `group` option organizes `output.mode: 'directory'` output into per-tag or per-path subdirectories.
|
|
28
|
+
*
|
|
29
|
+
* @default { path: 'mcp', barrel: { type: 'named' } }
|
|
30
|
+
*/
|
|
31
|
+
export type Options = OutputOptions & {
|
|
27
32
|
/**
|
|
28
|
-
*
|
|
33
|
+
* HTTP client used by each MCP handler to call the underlying API. Mirrors a
|
|
34
|
+
* subset of `pluginClient` options.
|
|
29
35
|
*/
|
|
30
|
-
|
|
36
|
+
client?: ClientImportPath & Pick<PluginClient['options'], 'clientType' | 'dataReturnType' | 'baseURL' | 'bundle' | 'paramsCasing'>
|
|
31
37
|
/**
|
|
32
|
-
*
|
|
38
|
+
* Rename parameter properties in the generated handlers. The HTTP layer still
|
|
39
|
+
* uses the original spec names; Kubb writes the mapping for you.
|
|
40
|
+
*
|
|
41
|
+
* @note Must match the value of `paramsCasing` on `@kubb/plugin-ts`.
|
|
33
42
|
*/
|
|
34
|
-
|
|
43
|
+
paramsCasing?: 'camelcase'
|
|
35
44
|
/**
|
|
36
|
-
*
|
|
45
|
+
* Skip operations matching at least one entry in the list.
|
|
37
46
|
*/
|
|
38
47
|
exclude?: Array<Exclude>
|
|
39
48
|
/**
|
|
40
|
-
*
|
|
49
|
+
* Restrict generation to operations matching at least one entry in the list.
|
|
41
50
|
*/
|
|
42
51
|
include?: Array<Include>
|
|
43
52
|
/**
|
|
44
|
-
*
|
|
53
|
+
* Apply a different options object to operations matching a pattern.
|
|
45
54
|
*/
|
|
46
55
|
override?: Array<Override<ResolvedOptions>>
|
|
47
56
|
/**
|
|
48
|
-
* Override
|
|
57
|
+
* Override how handler names and file paths are built. Methods you omit fall
|
|
58
|
+
* back to the default `resolverMcp`.
|
|
49
59
|
*/
|
|
50
60
|
resolver?: Partial<ResolverMcp> & ThisType<ResolverMcp>
|
|
51
61
|
/**
|
|
52
|
-
* AST visitor to
|
|
62
|
+
* AST visitor applied to each operation node before printing.
|
|
53
63
|
*/
|
|
54
64
|
transformer?: ast.Visitor
|
|
55
65
|
/**
|
|
56
|
-
*
|
|
66
|
+
* Custom generators that run alongside the built-in MCP generators.
|
|
57
67
|
*/
|
|
58
68
|
generators?: Array<Generator<PluginMcp>>
|
|
59
69
|
}
|
|
@@ -63,7 +73,7 @@ type ResolvedOptions = {
|
|
|
63
73
|
exclude: Array<Exclude>
|
|
64
74
|
include: Array<Include> | undefined
|
|
65
75
|
override: Array<Override<ResolvedOptions>>
|
|
66
|
-
group: Group |
|
|
76
|
+
group: Group | null
|
|
67
77
|
client: Pick<PluginClient['options'], 'client' | 'clientType' | 'dataReturnType' | 'importPath' | 'baseURL' | 'bundle' | 'paramsCasing'>
|
|
68
78
|
paramsCasing: Options['paramsCasing']
|
|
69
79
|
resolver: ResolverMcp
|