@kubb/plugin-mcp 5.0.0-alpha.9 → 5.0.0-beta.4
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 +17 -10
- package/README.md +1 -4
- package/dist/index.cjs +957 -92
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +207 -4
- package/dist/index.js +923 -90
- package/dist/index.js.map +1 -1
- package/extension.yaml +470 -0
- package/package.json +42 -64
- package/src/components/McpHandler.tsx +173 -0
- package/src/components/Server.tsx +89 -109
- package/src/generators/mcpGenerator.tsx +65 -84
- package/src/generators/serverGenerator.tsx +95 -58
- package/src/index.ts +11 -2
- package/src/plugin.ts +87 -135
- package/src/resolvers/resolverMcp.ts +25 -0
- package/src/types.ts +46 -28
- package/src/utils.ts +113 -0
- package/dist/Server-DV9zFrUP.cjs +0 -221
- package/dist/Server-DV9zFrUP.cjs.map +0 -1
- package/dist/Server-KWLMg0Lm.js +0 -173
- package/dist/Server-KWLMg0Lm.js.map +0 -1
- package/dist/components.cjs +0 -3
- package/dist/components.d.ts +0 -41
- package/dist/components.js +0 -2
- package/dist/generators-CWAFnA94.cjs +0 -285
- package/dist/generators-CWAFnA94.cjs.map +0 -1
- package/dist/generators-TtEOkDB1.js +0 -274
- package/dist/generators-TtEOkDB1.js.map +0 -1
- package/dist/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -508
- package/dist/generators.js +0 -2
- package/dist/types-DXZDZ3vf.d.ts +0 -64
- package/src/components/index.ts +0 -1
- package/src/generators/index.ts +0 -2
package/package.json
CHANGED
|
@@ -1,70 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-mcp",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-beta.4",
|
|
4
4
|
"description": "Model Context Protocol (MCP) plugin for Kubb, generating MCP-compatible tools and schemas from OpenAPI specifications for AI assistants.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"mcp",
|
|
7
|
-
"model-context-protocol",
|
|
8
6
|
"ai",
|
|
9
|
-
"llm",
|
|
10
|
-
"claude",
|
|
11
7
|
"ai-tools",
|
|
12
|
-
"
|
|
13
|
-
"swagger",
|
|
14
|
-
"typescript",
|
|
8
|
+
"claude",
|
|
15
9
|
"code-generator",
|
|
16
10
|
"codegen",
|
|
11
|
+
"kubb",
|
|
12
|
+
"llm",
|
|
13
|
+
"mcp",
|
|
14
|
+
"model-context-protocol",
|
|
15
|
+
"openapi",
|
|
17
16
|
"plugins",
|
|
18
|
-
"
|
|
17
|
+
"swagger",
|
|
18
|
+
"typescript"
|
|
19
19
|
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "stijnvanhulle",
|
|
20
22
|
"repository": {
|
|
21
23
|
"type": "git",
|
|
22
|
-
"url": "git+https://github.com/kubb-labs/
|
|
24
|
+
"url": "git+https://github.com/kubb-labs/plugins.git",
|
|
23
25
|
"directory": "packages/plugin-mcp"
|
|
24
26
|
},
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
"files": [
|
|
28
|
+
"src",
|
|
29
|
+
"dist",
|
|
30
|
+
"extension.yaml",
|
|
31
|
+
"!/**/**.test.**",
|
|
32
|
+
"!/**/__tests__/**",
|
|
33
|
+
"!/**/__snapshots__/**"
|
|
34
|
+
],
|
|
28
35
|
"type": "module",
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"main": "./dist/index.cjs",
|
|
38
|
+
"module": "./dist/index.js",
|
|
39
|
+
"types": "./dist/index.d.ts",
|
|
29
40
|
"exports": {
|
|
30
41
|
".": {
|
|
31
42
|
"import": "./dist/index.js",
|
|
32
43
|
"require": "./dist/index.cjs"
|
|
33
44
|
},
|
|
34
|
-
"./components": {
|
|
35
|
-
"import": "./dist/components.js",
|
|
36
|
-
"require": "./dist/components.cjs"
|
|
37
|
-
},
|
|
38
|
-
"./generators": {
|
|
39
|
-
"import": "./dist/generators.js",
|
|
40
|
-
"require": "./dist/generators.cjs"
|
|
41
|
-
},
|
|
42
45
|
"./package.json": "./package.json"
|
|
43
46
|
},
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public",
|
|
49
|
+
"registry": "https://registry.npmjs.org/"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@kubb/core": "5.0.0-beta.4",
|
|
53
|
+
"@kubb/renderer-jsx": "5.0.0-beta.4",
|
|
54
|
+
"@kubb/plugin-client": "5.0.0-beta.4",
|
|
55
|
+
"@kubb/plugin-ts": "5.0.0-beta.4",
|
|
56
|
+
"@kubb/plugin-zod": "5.0.0-beta.4"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@internals/utils": "0.0.0"
|
|
60
|
+
},
|
|
61
|
+
"peerDependencies": {
|
|
62
|
+
"@kubb/renderer-jsx": "5.0.0-beta.4"
|
|
60
63
|
},
|
|
61
|
-
"files": [
|
|
62
|
-
"src",
|
|
63
|
-
"dist",
|
|
64
|
-
"!/**/**.test.**",
|
|
65
|
-
"!/**/__tests__/**",
|
|
66
|
-
"!/**/__snapshots__/**"
|
|
67
|
-
],
|
|
68
64
|
"size-limit": [
|
|
69
65
|
{
|
|
70
66
|
"path": "./dist/*.js",
|
|
@@ -72,32 +68,14 @@
|
|
|
72
68
|
"gzip": true
|
|
73
69
|
}
|
|
74
70
|
],
|
|
75
|
-
"dependencies": {
|
|
76
|
-
"@kubb/react-fabric": "0.14.0",
|
|
77
|
-
"@kubb/core": "5.0.0-alpha.9",
|
|
78
|
-
"@kubb/oas": "5.0.0-alpha.9",
|
|
79
|
-
"@kubb/plugin-client": "5.0.0-alpha.9",
|
|
80
|
-
"@kubb/plugin-oas": "5.0.0-alpha.9",
|
|
81
|
-
"@kubb/plugin-ts": "5.0.0-alpha.9",
|
|
82
|
-
"@kubb/plugin-zod": "5.0.0-alpha.9"
|
|
83
|
-
},
|
|
84
|
-
"devDependencies": {
|
|
85
|
-
"@internals/utils": "0.0.0"
|
|
86
|
-
},
|
|
87
71
|
"engines": {
|
|
88
72
|
"node": ">=22"
|
|
89
73
|
},
|
|
90
|
-
"publishConfig": {
|
|
91
|
-
"access": "public",
|
|
92
|
-
"registry": "https://registry.npmjs.org/"
|
|
93
|
-
},
|
|
94
|
-
"main": "./dist/index.cjs",
|
|
95
|
-
"module": "./dist/index.js",
|
|
96
74
|
"scripts": {
|
|
97
75
|
"build": "tsdown && size-limit",
|
|
98
76
|
"clean": "npx rimraf ./dist",
|
|
99
|
-
"lint": "
|
|
100
|
-
"lint:fix": "
|
|
77
|
+
"lint": "oxlint .",
|
|
78
|
+
"lint:fix": "oxlint --fix .",
|
|
101
79
|
"release": "pnpm publish --no-git-check",
|
|
102
80
|
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
103
81
|
"start": "tsdown --watch",
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { isValidVarName, URLPath } from '@internals/utils'
|
|
2
|
+
import { ast } from '@kubb/core'
|
|
3
|
+
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
4
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
5
|
+
import { File, Function } from '@kubb/renderer-jsx'
|
|
6
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
7
|
+
import type { PluginMcp } from '../types.ts'
|
|
8
|
+
import { getComments, getParamsMapping } from '../utils.ts'
|
|
9
|
+
|
|
10
|
+
type Props = {
|
|
11
|
+
/**
|
|
12
|
+
* Name of the handler function.
|
|
13
|
+
*/
|
|
14
|
+
name: string
|
|
15
|
+
/**
|
|
16
|
+
* AST operation node.
|
|
17
|
+
*/
|
|
18
|
+
node: ast.OperationNode
|
|
19
|
+
/**
|
|
20
|
+
* TypeScript resolver for resolving param/data/response type names.
|
|
21
|
+
*/
|
|
22
|
+
resolver: ResolverTs
|
|
23
|
+
/**
|
|
24
|
+
* Base URL prepended to every generated request URL.
|
|
25
|
+
*/
|
|
26
|
+
baseURL: string | undefined
|
|
27
|
+
/**
|
|
28
|
+
* Return type when calling fetch.
|
|
29
|
+
* - 'data' returns response data only.
|
|
30
|
+
* - 'full' returns the full response object.
|
|
31
|
+
* @default 'data'
|
|
32
|
+
*/
|
|
33
|
+
dataReturnType: PluginMcp['resolvedOptions']['client']['dataReturnType']
|
|
34
|
+
/**
|
|
35
|
+
* How to style your params.
|
|
36
|
+
*/
|
|
37
|
+
paramsCasing?: PluginMcp['resolvedOptions']['paramsCasing']
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Generate a remapping statement: `const mappedX = x ? { "orig": x.camel, ... } : undefined`
|
|
42
|
+
*/
|
|
43
|
+
function buildRemappingCode(mapping: Record<string, string>, varName: string, sourceName: string): string {
|
|
44
|
+
const pairs = Object.entries(mapping)
|
|
45
|
+
.map(([orig, camel]) => `"${orig}": ${sourceName}.${camel}`)
|
|
46
|
+
.join(', ')
|
|
47
|
+
return `const ${varName} = ${sourceName} ? { ${pairs} } : undefined`
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
51
|
+
|
|
52
|
+
export function McpHandler({ name, node, resolver, baseURL, dataReturnType, paramsCasing }: Props): KubbReactNode {
|
|
53
|
+
const urlPath = new URLPath(node.path)
|
|
54
|
+
const contentType = node.requestBody?.content?.[0]?.contentType
|
|
55
|
+
const isFormData = contentType === 'multipart/form-data'
|
|
56
|
+
|
|
57
|
+
const casedParams = ast.caseParams(node.parameters, paramsCasing)
|
|
58
|
+
const queryParams = casedParams.filter((p) => p.in === 'query')
|
|
59
|
+
const headerParams = casedParams.filter((p) => p.in === 'header')
|
|
60
|
+
|
|
61
|
+
// Use original (uncased) parameters for mapping so original→camelCase difference is detected
|
|
62
|
+
const originalPathParams = node.parameters.filter((p) => p.in === 'path')
|
|
63
|
+
const originalQueryParams = node.parameters.filter((p) => p.in === 'query')
|
|
64
|
+
const originalHeaderParams = node.parameters.filter((p) => p.in === 'header')
|
|
65
|
+
|
|
66
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
|
|
67
|
+
const responseName = resolver.resolveResponseName(node)
|
|
68
|
+
|
|
69
|
+
const errorResponses = node.responses.filter((r) => Number(r.statusCode) >= 400).map((r) => resolver.resolveResponseStatusName(node, r.statusCode))
|
|
70
|
+
const errorType = errorResponses.length > 0 ? errorResponses.join(' | ') : 'Error'
|
|
71
|
+
|
|
72
|
+
const TError = `ResponseErrorConfig<${errorType}>`
|
|
73
|
+
const generics = [responseName, TError, requestName || 'unknown'].filter(Boolean)
|
|
74
|
+
|
|
75
|
+
const paramsNode = ast.createOperationParams(node, {
|
|
76
|
+
paramsType: 'object',
|
|
77
|
+
pathParamsType: 'inline',
|
|
78
|
+
resolver,
|
|
79
|
+
paramsCasing,
|
|
80
|
+
})
|
|
81
|
+
const baseParamsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
82
|
+
const paramsSignature = baseParamsSignature
|
|
83
|
+
? `${baseParamsSignature}, request: RequestHandlerExtra<ServerRequest, ServerNotification>`
|
|
84
|
+
: 'request: RequestHandlerExtra<ServerRequest, ServerNotification>'
|
|
85
|
+
|
|
86
|
+
const pathParamsMapping = paramsCasing ? getParamsMapping(originalPathParams) : undefined
|
|
87
|
+
const queryParamsMapping = paramsCasing ? getParamsMapping(originalQueryParams) : undefined
|
|
88
|
+
const headerParamsMapping = paramsCasing ? getParamsMapping(originalHeaderParams) : undefined
|
|
89
|
+
|
|
90
|
+
const contentTypeHeader =
|
|
91
|
+
contentType && contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` : undefined
|
|
92
|
+
const headers = [headerParams.length ? (headerParamsMapping ? '...mappedHeaders' : '...headers') : undefined, contentTypeHeader].filter(Boolean)
|
|
93
|
+
|
|
94
|
+
const fetchConfig: string[] = []
|
|
95
|
+
fetchConfig.push(`method: ${JSON.stringify(node.method.toUpperCase())}`)
|
|
96
|
+
fetchConfig.push(`url: ${urlPath.template}`)
|
|
97
|
+
if (baseURL) fetchConfig.push(`baseURL: \`${baseURL}\``)
|
|
98
|
+
if (queryParams.length) fetchConfig.push(queryParamsMapping ? 'params: mappedParams' : 'params')
|
|
99
|
+
if (requestName) fetchConfig.push(`data: ${isFormData ? 'formData as FormData' : 'requestData'}`)
|
|
100
|
+
if (headers.length) fetchConfig.push(`headers: { ${headers.join(', ')} }`)
|
|
101
|
+
|
|
102
|
+
const callToolResult =
|
|
103
|
+
dataReturnType === 'data'
|
|
104
|
+
? `return {
|
|
105
|
+
content: [
|
|
106
|
+
{
|
|
107
|
+
type: 'text',
|
|
108
|
+
text: JSON.stringify(res.data)
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
structuredContent: { data: res.data }
|
|
112
|
+
}`
|
|
113
|
+
: `return {
|
|
114
|
+
content: [
|
|
115
|
+
{
|
|
116
|
+
type: 'text',
|
|
117
|
+
text: JSON.stringify(res)
|
|
118
|
+
}
|
|
119
|
+
],
|
|
120
|
+
structuredContent: { data: res.data }
|
|
121
|
+
}`
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<File.Source name={name} isExportable isIndexable>
|
|
125
|
+
<Function
|
|
126
|
+
name={name}
|
|
127
|
+
async
|
|
128
|
+
export
|
|
129
|
+
params={paramsSignature}
|
|
130
|
+
JSDoc={{
|
|
131
|
+
comments: getComments(node),
|
|
132
|
+
}}
|
|
133
|
+
returnType={'Promise<CallToolResult>'}
|
|
134
|
+
>
|
|
135
|
+
{''}
|
|
136
|
+
<br />
|
|
137
|
+
<br />
|
|
138
|
+
{pathParamsMapping &&
|
|
139
|
+
Object.entries(pathParamsMapping)
|
|
140
|
+
.filter(([originalName, camelCaseName]) => originalName !== camelCaseName && isValidVarName(originalName))
|
|
141
|
+
.map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`)
|
|
142
|
+
.join('\n')}
|
|
143
|
+
{pathParamsMapping && (
|
|
144
|
+
<>
|
|
145
|
+
<br />
|
|
146
|
+
<br />
|
|
147
|
+
</>
|
|
148
|
+
)}
|
|
149
|
+
{queryParamsMapping && queryParams.length > 0 && (
|
|
150
|
+
<>
|
|
151
|
+
{buildRemappingCode(queryParamsMapping, 'mappedParams', 'params')}
|
|
152
|
+
<br />
|
|
153
|
+
<br />
|
|
154
|
+
</>
|
|
155
|
+
)}
|
|
156
|
+
{headerParamsMapping && headerParams.length > 0 && (
|
|
157
|
+
<>
|
|
158
|
+
{buildRemappingCode(headerParamsMapping, 'mappedHeaders', 'headers')}
|
|
159
|
+
<br />
|
|
160
|
+
<br />
|
|
161
|
+
</>
|
|
162
|
+
)}
|
|
163
|
+
{requestName && 'const requestData = data'}
|
|
164
|
+
<br />
|
|
165
|
+
{isFormData && requestName && 'const formData = buildFormData(requestData)'}
|
|
166
|
+
<br />
|
|
167
|
+
{`const res = await fetch<${generics.join(', ')}>({ ${fetchConfig.join(', ')} }, request)`}
|
|
168
|
+
<br />
|
|
169
|
+
{callToolResult}
|
|
170
|
+
</Function>
|
|
171
|
+
</File.Source>
|
|
172
|
+
)
|
|
173
|
+
}
|
|
@@ -1,16 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import type {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
2
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
3
|
+
import { Const, File, Function } from '@kubb/renderer-jsx'
|
|
4
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
5
|
+
import type { PluginMcp } from '../types.ts'
|
|
6
|
+
import type { ZodParam } from '../utils.ts'
|
|
7
|
+
import { zodExprFromSchemaNode, zodGroupExpr } from '../utils.ts'
|
|
8
8
|
|
|
9
9
|
type Props = {
|
|
10
|
+
/**
|
|
11
|
+
* Variable name for the MCP server instance (e.g. 'server').
|
|
12
|
+
*/
|
|
10
13
|
name: string
|
|
14
|
+
/**
|
|
15
|
+
* Human-readable server name passed to `new McpServer({ name })`.
|
|
16
|
+
*/
|
|
11
17
|
serverName: string
|
|
18
|
+
/**
|
|
19
|
+
* Semantic version string passed to `new McpServer({ version })`.
|
|
20
|
+
*/
|
|
12
21
|
serverVersion: string
|
|
13
|
-
|
|
22
|
+
/**
|
|
23
|
+
* How to style your params.
|
|
24
|
+
*/
|
|
25
|
+
paramsCasing?: PluginMcp['resolvedOptions']['paramsCasing']
|
|
26
|
+
/**
|
|
27
|
+
* Operations to register as MCP tools, each carrying its handler,
|
|
28
|
+
* zod schema, and AST node metadata.
|
|
29
|
+
*/
|
|
14
30
|
operations: Array<{
|
|
15
31
|
tool: {
|
|
16
32
|
name: string
|
|
@@ -19,99 +35,28 @@ type Props = {
|
|
|
19
35
|
}
|
|
20
36
|
mcp: {
|
|
21
37
|
name: string
|
|
22
|
-
file:
|
|
38
|
+
file: ast.FileNode
|
|
23
39
|
}
|
|
24
40
|
zod: {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
pathParams: Array<ZodParam>
|
|
42
|
+
/**
|
|
43
|
+
* Query params — individual schemas to compose into `z.object({ ... })`.
|
|
44
|
+
*/
|
|
45
|
+
queryParams?: string | Array<ZodParam>
|
|
46
|
+
/**
|
|
47
|
+
* Header params — individual schemas to compose into `z.object({ ... })`.
|
|
48
|
+
*/
|
|
49
|
+
headerParams?: string | Array<ZodParam>
|
|
50
|
+
requestName?: string
|
|
51
|
+
responseName?: string
|
|
31
52
|
}
|
|
53
|
+
node: ast.OperationNode
|
|
32
54
|
}>
|
|
33
55
|
}
|
|
34
56
|
|
|
35
|
-
|
|
36
|
-
schemas: OperationSchemas
|
|
37
|
-
paramsCasing?: 'camelcase'
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function zodExprFromOasSchema(schema: SchemaObject): string {
|
|
41
|
-
const types = Array.isArray(schema.type) ? schema.type : [schema.type]
|
|
42
|
-
const baseType = types.find((t) => t && t !== 'null')
|
|
43
|
-
const isNullableType = types.includes('null')
|
|
44
|
-
|
|
45
|
-
let expr: string
|
|
46
|
-
switch (baseType) {
|
|
47
|
-
case 'integer':
|
|
48
|
-
expr = 'z.coerce.number()'
|
|
49
|
-
break
|
|
50
|
-
case 'number':
|
|
51
|
-
expr = 'z.number()'
|
|
52
|
-
break
|
|
53
|
-
case 'boolean':
|
|
54
|
-
expr = 'z.boolean()'
|
|
55
|
-
break
|
|
56
|
-
case 'array':
|
|
57
|
-
expr = 'z.array(z.unknown())'
|
|
58
|
-
break
|
|
59
|
-
default:
|
|
60
|
-
expr = 'z.string()'
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (isNullableType) {
|
|
64
|
-
expr = `${expr}.nullable()`
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return expr
|
|
68
|
-
}
|
|
57
|
+
const keysPrinter = functionPrinter({ mode: 'keys' })
|
|
69
58
|
|
|
70
|
-
function
|
|
71
|
-
const pathParamProperties = schemas.pathParams?.schema?.properties ?? {}
|
|
72
|
-
const requiredFields = Array.isArray(schemas.pathParams?.schema?.required) ? schemas.pathParams.schema.required : []
|
|
73
|
-
|
|
74
|
-
const pathParamEntries = Object.entries(pathParamProperties).reduce<Record<string, { value: string; optional: boolean }>>(
|
|
75
|
-
(acc, [originalKey, propSchema]) => {
|
|
76
|
-
const key = paramsCasing === 'camelcase' || !isValidVarName(originalKey) ? camelCase(originalKey) : originalKey
|
|
77
|
-
acc[key] = {
|
|
78
|
-
value: zodExprFromOasSchema(propSchema as SchemaObject),
|
|
79
|
-
optional: !requiredFields.includes(originalKey),
|
|
80
|
-
}
|
|
81
|
-
return acc
|
|
82
|
-
},
|
|
83
|
-
{},
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
return FunctionParams.factory({
|
|
87
|
-
data: {
|
|
88
|
-
mode: 'object',
|
|
89
|
-
children: {
|
|
90
|
-
...pathParamEntries,
|
|
91
|
-
data: schemas.request?.name
|
|
92
|
-
? {
|
|
93
|
-
value: schemas.request?.name,
|
|
94
|
-
optional: isOptional(schemas.request?.schema),
|
|
95
|
-
}
|
|
96
|
-
: undefined,
|
|
97
|
-
params: schemas.queryParams?.name
|
|
98
|
-
? {
|
|
99
|
-
value: schemas.queryParams?.name,
|
|
100
|
-
optional: isOptional(schemas.queryParams?.schema),
|
|
101
|
-
}
|
|
102
|
-
: undefined,
|
|
103
|
-
headers: schemas.headerParams?.name
|
|
104
|
-
? {
|
|
105
|
-
value: schemas.headerParams?.name,
|
|
106
|
-
optional: isOptional(schemas.headerParams?.schema),
|
|
107
|
-
}
|
|
108
|
-
: undefined,
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
})
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function Server({ name, serverName, serverVersion, paramsCasing, operations }: Props): FabricReactNode {
|
|
59
|
+
export function Server({ name, serverName, serverVersion, paramsCasing, operations }: Props): KubbReactNode {
|
|
115
60
|
return (
|
|
116
61
|
<File.Source name={name} isExportable isIndexable>
|
|
117
62
|
<Const name={'server'} export>
|
|
@@ -124,9 +69,46 @@ export function Server({ name, serverName, serverVersion, paramsCasing, operatio
|
|
|
124
69
|
</Const>
|
|
125
70
|
|
|
126
71
|
{operations
|
|
127
|
-
.map(({ tool, mcp, zod }) => {
|
|
128
|
-
const
|
|
129
|
-
const
|
|
72
|
+
.map(({ tool, mcp, zod, node }) => {
|
|
73
|
+
const casedParams = ast.caseParams(node.parameters, paramsCasing)
|
|
74
|
+
const pathParams = casedParams.filter((p) => p.in === 'path')
|
|
75
|
+
|
|
76
|
+
const pathEntries: Array<{ key: string; value: string }> = []
|
|
77
|
+
const otherEntries: Array<{ key: string; value: string }> = []
|
|
78
|
+
|
|
79
|
+
for (const p of pathParams) {
|
|
80
|
+
const zodParam = zod.pathParams.find((zp) => zp.name === p.name)
|
|
81
|
+
pathEntries.push({ key: p.name, value: zodParam ? zodParam.schemaName : zodExprFromSchemaNode(p.schema) })
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (zod.requestName) {
|
|
85
|
+
otherEntries.push({ key: 'data', value: zod.requestName })
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (zod.queryParams) {
|
|
89
|
+
otherEntries.push({ key: 'params', value: zodGroupExpr(zod.queryParams) })
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (zod.headerParams) {
|
|
93
|
+
otherEntries.push({ key: 'headers', value: zodGroupExpr(zod.headerParams) })
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
otherEntries.sort((a, b) => a.key.localeCompare(b.key))
|
|
97
|
+
const entries = [...pathEntries, ...otherEntries]
|
|
98
|
+
|
|
99
|
+
const paramsNode = entries.length
|
|
100
|
+
? ast.createFunctionParameters({
|
|
101
|
+
params: [
|
|
102
|
+
ast.createParameterGroup({
|
|
103
|
+
properties: entries.map((e) => ast.createFunctionParameter({ name: e.key, optional: false })),
|
|
104
|
+
}),
|
|
105
|
+
],
|
|
106
|
+
})
|
|
107
|
+
: undefined
|
|
108
|
+
|
|
109
|
+
const destructured = paramsNode ? (keysPrinter.print(paramsNode) ?? '') : ''
|
|
110
|
+
const inputSchema = entries.length ? `{ ${entries.map((e) => `${e.key}: ${e.value}`).join(', ')} }` : undefined
|
|
111
|
+
const outputSchema = zod.responseName
|
|
130
112
|
|
|
131
113
|
const config = [
|
|
132
114
|
tool.title ? `title: ${JSON.stringify(tool.title)}` : null,
|
|
@@ -136,13 +118,13 @@ export function Server({ name, serverName, serverVersion, paramsCasing, operatio
|
|
|
136
118
|
.filter(Boolean)
|
|
137
119
|
.join(',\n ')
|
|
138
120
|
|
|
139
|
-
if (
|
|
121
|
+
if (inputSchema) {
|
|
140
122
|
return `
|
|
141
123
|
server.registerTool(${JSON.stringify(tool.name)}, {
|
|
142
124
|
${config},
|
|
143
|
-
inputSchema: ${
|
|
144
|
-
}, async (${
|
|
145
|
-
return ${mcp.name}(${
|
|
125
|
+
inputSchema: ${inputSchema},
|
|
126
|
+
}, async (${destructured}, request) => {
|
|
127
|
+
return ${mcp.name}(${destructured}, request)
|
|
146
128
|
})
|
|
147
129
|
`
|
|
148
130
|
}
|
|
@@ -150,25 +132,23 @@ server.registerTool(${JSON.stringify(tool.name)}, {
|
|
|
150
132
|
return `
|
|
151
133
|
server.registerTool(${JSON.stringify(tool.name)}, {
|
|
152
134
|
${config},
|
|
153
|
-
}, async () => {
|
|
154
|
-
return ${mcp.name}(
|
|
135
|
+
}, async (request) => {
|
|
136
|
+
return ${mcp.name}(request)
|
|
155
137
|
})
|
|
156
138
|
`
|
|
157
139
|
})
|
|
158
140
|
.filter(Boolean)}
|
|
159
141
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
try {
|
|
142
|
+
<Function name="startServer" async export>
|
|
143
|
+
{`try {
|
|
163
144
|
const transport = new StdioServerTransport()
|
|
164
145
|
await server.connect(transport)
|
|
165
146
|
|
|
166
147
|
} catch (error) {
|
|
167
148
|
console.error('Failed to start server:', error)
|
|
168
149
|
process.exit(1)
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
`}
|
|
150
|
+
}`}
|
|
151
|
+
</Function>
|
|
172
152
|
</File.Source>
|
|
173
153
|
)
|
|
174
154
|
}
|