@kubb/plugin-mcp 0.0.0-canary-20250414193247

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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -0
  3. package/dist/chunk-5AJXTSAA.cjs +179 -0
  4. package/dist/chunk-5AJXTSAA.cjs.map +1 -0
  5. package/dist/chunk-RN55DM2O.cjs +97 -0
  6. package/dist/chunk-RN55DM2O.cjs.map +1 -0
  7. package/dist/chunk-VPK2OEBV.js +95 -0
  8. package/dist/chunk-VPK2OEBV.js.map +1 -0
  9. package/dist/chunk-WEY2R35B.js +176 -0
  10. package/dist/chunk-WEY2R35B.js.map +1 -0
  11. package/dist/components.cjs +12 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +27 -0
  14. package/dist/components.d.ts +27 -0
  15. package/dist/components.js +3 -0
  16. package/dist/components.js.map +1 -0
  17. package/dist/generators.cjs +17 -0
  18. package/dist/generators.cjs.map +1 -0
  19. package/dist/generators.d.cts +11 -0
  20. package/dist/generators.d.ts +11 -0
  21. package/dist/generators.js +4 -0
  22. package/dist/generators.js.map +1 -0
  23. package/dist/index.cjs +108 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +10 -0
  26. package/dist/index.d.ts +10 -0
  27. package/dist/index.js +101 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-B4DlUSkU.d.cts +54 -0
  30. package/dist/types-B4DlUSkU.d.ts +54 -0
  31. package/package.json +117 -0
  32. package/src/components/Server.tsx +141 -0
  33. package/src/components/index.ts +1 -0
  34. package/src/generators/__snapshots__/.mcp.json +9 -0
  35. package/src/generators/__snapshots__/createPet.ts +19 -0
  36. package/src/generators/__snapshots__/deletePet.ts +18 -0
  37. package/src/generators/__snapshots__/getPets.ts +19 -0
  38. package/src/generators/__snapshots__/server.ts +37 -0
  39. package/src/generators/__snapshots__/showPetById.ts +25 -0
  40. package/src/generators/index.ts +2 -0
  41. package/src/generators/mcpGenerator.tsx +89 -0
  42. package/src/generators/serverGenerator.tsx +93 -0
  43. package/src/index.ts +2 -0
  44. package/src/plugin.ts +120 -0
  45. package/src/types.ts +54 -0
@@ -0,0 +1,93 @@
1
+ import { createReactGenerator } from '@kubb/plugin-oas'
2
+ import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks'
3
+ import { getBanner, getFooter } from '@kubb/plugin-oas/utils'
4
+ import { File, useApp } from '@kubb/react'
5
+ import { Server } from '../components/Server'
6
+ import type { PluginMcp } from '../types'
7
+ import { pluginZodName } from '@kubb/plugin-zod'
8
+ import { pluginTsName } from '@kubb/plugin-ts'
9
+
10
+ export const serverGenerator = createReactGenerator<PluginMcp>({
11
+ name: 'operations',
12
+ Operations({ operations, options }) {
13
+ const { pluginManager, plugin } = useApp<PluginMcp>()
14
+ const oas = useOas()
15
+ const { getFile, getName, getSchemas } = useOperationManager()
16
+
17
+ const name = 'server'
18
+ const file = pluginManager.getFile({ name, extname: '.ts', pluginKey: plugin.key })
19
+
20
+ const jsonFile = pluginManager.getFile({ name: '.mcp', extname: '.json', pluginKey: plugin.key })
21
+
22
+ const operationsMapped = operations.map((operation) => {
23
+ return {
24
+ operationId: operation.getOperationId(),
25
+ description: operation.getDescription(),
26
+ mcp: {
27
+ name: getName(operation, {
28
+ type: 'function',
29
+ suffix: 'handler',
30
+ }),
31
+ file: getFile(operation),
32
+ },
33
+ zod: {
34
+ name: getName(operation, {
35
+ type: 'function',
36
+ pluginKey: [pluginZodName],
37
+ }),
38
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
39
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
40
+ },
41
+ type: {
42
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
43
+ },
44
+ }
45
+ })
46
+
47
+ const imports = operationsMapped.flatMap(({ mcp, zod }) => {
48
+ return [
49
+ <File.Import key={mcp.name} name={[mcp.name]} root={file.path} path={mcp.file.path} />,
50
+ <File.Import
51
+ key={zod.name}
52
+ name={[zod.schemas.request?.name, zod.schemas.pathParams?.name, zod.schemas.queryParams?.name, zod.schemas.headerParams?.name].filter(Boolean)}
53
+ root={file.path}
54
+ path={zod.file.path}
55
+ />,
56
+ ]
57
+ })
58
+
59
+ return (
60
+ <>
61
+ <File
62
+ baseName={file.baseName}
63
+ path={file.path}
64
+ meta={file.meta}
65
+ banner={getBanner({ oas, output: options.output, config: pluginManager.config })}
66
+ footer={getFooter({ oas, output: options.output })}
67
+ >
68
+ <File.Import name={['McpServer']} path={'@modelcontextprotocol/sdk/server/mcp'} />
69
+ <File.Import name={['StdioServerTransport']} path={'@modelcontextprotocol/sdk/server/stdio'} />
70
+
71
+ {imports}
72
+ <Server name={name} serverName={oas.api.info?.title} serverVersion={oas.getVersion()} operations={operationsMapped} />
73
+ </File>
74
+
75
+ <File baseName={jsonFile.baseName} path={jsonFile.path} meta={jsonFile.meta}>
76
+ <File.Source name={name}>
77
+ {`
78
+ {
79
+ "mcpServers": {
80
+ "${oas.api.info?.title || 'server'}": {
81
+ "type": "stdio",
82
+ "command": "npx",
83
+ "args": ["tsx", "${file.path}"]
84
+ }
85
+ }
86
+ }
87
+ `}
88
+ </File.Source>
89
+ </File>
90
+ </>
91
+ )
92
+ },
93
+ })
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { pluginMcp, pluginMcpName } from './plugin.ts'
2
+ export type { PluginMcp } from './types.ts'
package/src/plugin.ts ADDED
@@ -0,0 +1,120 @@
1
+ import path from 'node:path'
2
+
3
+ import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
4
+ import { camelCase } from '@kubb/core/transformers'
5
+ import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
6
+ import { pluginTsName } from '@kubb/plugin-ts'
7
+
8
+ import type { Plugin } from '@kubb/core'
9
+ import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
10
+ import { mcpGenerator, serverGenerator } from './generators'
11
+ import type { PluginMcp } from './types.ts'
12
+ import { pluginZodName } from '@kubb/plugin-zod'
13
+
14
+ export const pluginMcpName = 'plugin-mcp' satisfies PluginMcp['name']
15
+
16
+ export const pluginMcp = createPlugin<PluginMcp>((options) => {
17
+ const {
18
+ output = { path: 'mcp', barrelType: 'named' },
19
+ group,
20
+ exclude = [],
21
+ include,
22
+ override = [],
23
+ transformers = {},
24
+ generators = [mcpGenerator, serverGenerator].filter(Boolean),
25
+ contentType,
26
+ } = options
27
+
28
+ return {
29
+ name: pluginMcpName,
30
+ options: {
31
+ output,
32
+ group,
33
+ client: {
34
+ importPath: '@kubb/plugin-client/clients/axios',
35
+ dataReturnType: 'data',
36
+ ...options.client,
37
+ },
38
+ },
39
+ pre: [pluginOasName, pluginTsName, pluginZodName].filter(Boolean),
40
+ resolvePath(baseName, pathMode, options) {
41
+ const root = path.resolve(this.config.root, this.config.output.path)
42
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
43
+
44
+ if (mode === 'single') {
45
+ /**
46
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
47
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
48
+ */
49
+ return path.resolve(root, output.path)
50
+ }
51
+
52
+ if (group && (options?.group?.path || options?.group?.tag)) {
53
+ const groupName: Group['name'] = group?.name
54
+ ? group.name
55
+ : (ctx) => {
56
+ if (group?.type === 'path') {
57
+ return `${ctx.group.split('/')[1]}`
58
+ }
59
+ return `${camelCase(ctx.group)}Requests`
60
+ }
61
+
62
+ return path.resolve(
63
+ root,
64
+ output.path,
65
+ groupName({
66
+ group: group.type === 'path' ? options.group.path! : options.group.tag!,
67
+ }),
68
+ baseName,
69
+ )
70
+ }
71
+
72
+ return path.resolve(root, output.path, baseName)
73
+ },
74
+ resolveName(name, type) {
75
+ const resolvedName = camelCase(name, {
76
+ isFile: type === 'file',
77
+ })
78
+
79
+ if (type) {
80
+ return transformers?.name?.(resolvedName, type) || resolvedName
81
+ }
82
+
83
+ return resolvedName
84
+ },
85
+ async buildStart() {
86
+ const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
87
+
88
+ const oas = await swaggerPlugin.context.getOas()
89
+ const root = path.resolve(this.config.root, this.config.output.path)
90
+ const mode = FileManager.getMode(path.resolve(root, output.path))
91
+
92
+ const operationGenerator = new OperationGenerator(this.plugin.options, {
93
+ oas,
94
+ pluginManager: this.pluginManager,
95
+ plugin: this.plugin,
96
+ contentType,
97
+ exclude,
98
+ include,
99
+ override,
100
+ mode,
101
+ })
102
+
103
+ const files = await operationGenerator.build(...generators)
104
+ await this.addFile(...files)
105
+
106
+ const barrelFiles = await this.fileManager.getBarrelFiles({
107
+ type: output.barrelType ?? 'named',
108
+ root,
109
+ output,
110
+ files: this.fileManager.files,
111
+ meta: {
112
+ pluginKey: this.plugin.key,
113
+ },
114
+ logger: this.logger,
115
+ })
116
+
117
+ await this.addFile(...barrelFiles)
118
+ },
119
+ }
120
+ })
package/src/types.ts ADDED
@@ -0,0 +1,54 @@
1
+ import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+
3
+ import type { Oas, contentType } from '@kubb/oas'
4
+ import type { Exclude, Include, Override, ResolvePathOptions, Generator } from '@kubb/plugin-oas'
5
+ import type { PluginClient } from '@kubb/plugin-client'
6
+
7
+ export type Options = {
8
+ /**
9
+ * Specify the export location for the files and define the behavior of the output
10
+ * @default { path: 'mcp', barrelType: 'named' }
11
+ */
12
+ output?: Output<Oas>
13
+ /**
14
+ * Define which contentType should be used.
15
+ * By default, the first JSON valid mediaType will be used
16
+ */
17
+ contentType?: contentType
18
+ client?: Pick<PluginClient['options'], 'dataReturnType' | 'importPath' | 'baseURL'>
19
+
20
+ /**
21
+ * Group the mcp requests based on the provided name.
22
+ */
23
+ group?: Group
24
+ /**
25
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
26
+ */
27
+ exclude?: Array<Exclude>
28
+ /**
29
+ * Array containing include parameters to include tags/operations/methods/paths.
30
+ */
31
+ include?: Array<Include>
32
+ /**
33
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
34
+ */
35
+ override?: Array<Override<ResolvedOptions>>
36
+ transformers?: {
37
+ /**
38
+ * Customize the names based on the type that is provided by the plugin.
39
+ */
40
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
41
+ }
42
+ /**
43
+ * Define some generators next to the Mcp generators.
44
+ */
45
+ generators?: Array<Generator<PluginMcp>>
46
+ }
47
+
48
+ type ResolvedOptions = {
49
+ output: Output<Oas>
50
+ group: Options['group']
51
+ client: Required<Omit<NonNullable<PluginMcp['options']['client']>, 'baseURL'>> & { baseURL?: string }
52
+ }
53
+
54
+ export type PluginMcp = PluginFactoryOptions<'plugin-mcp', Options, ResolvedOptions, never, ResolvePathOptions>