@ninebone/mcp 0.1.26
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.kr.md +71 -0
- package/README.md +72 -0
- package/bak/generate-query.md +28 -0
- package/bak/generator-source-mapper.md +123 -0
- package/bak/mcp-server.js +498 -0
- package/bak/nomenu-navigator.md +16 -0
- package/bak/system-brain.md +62 -0
- package/bak/table-filter.md +21 -0
- package/package.json +33 -0
- package/prompts/menu/generate-menu.md +89 -0
- package/prompts/source/generate-source-controller.md +120 -0
- package/prompts/source/generate-source-mapper.mysql.md +97 -0
- package/prompts/source/generate-source-mapper.oracle.md +90 -0
- package/prompts/source/generate-source-mapper.postgre.md +89 -0
- package/prompts/source/generate-source-service.md +116 -0
- package/prompts/source/generate-source-ui-react.md +174 -0
- package/prompts/system/generate-source-brain.md +57 -0
- package/prompts/system/modify-source-brain.md +50 -0
- package/prompts/system/system-brain.md +88 -0
- package/src/ai/AIProcessor.js +85 -0
- package/src/ai/AIService.js +24 -0
- package/src/core/init.js +116 -0
- package/src/database/config/database.js +42 -0
- package/src/database/core/DatabaseManager.js +115 -0
- package/src/database/core/Dialects.js +66 -0
- package/src/database/core/PoolManager.js +92 -0
- package/src/drivers/mysql.js +0 -0
- package/src/index.js +38 -0
- package/src/mcp/loaders/promptLoader.js +62 -0
- package/src/mcp/mcp-server.js +129 -0
- package/src/mcp/tools/generateSourceBrainTool.js +179 -0
- package/src/mcp/tools/modifySourceBrainTool.js +283 -0
- package/src/mcp/tools/staticTools.js +29 -0
- package/src/mcp/tools/systemBrain.js +182 -0
- package/src/mcp/utils/mcp-utils.js +30 -0
- package/src/mcp-handler.js +131 -0
- package/src/services/NoMenuService.js +43 -0
- package/src/services/QueryService.js +26 -0
- package/src/services/SourceService.js +32 -0
- package/src/utils/CustomWsTransport.js +52 -0
- package/src/utils/asyncHandler.js +13 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ๐ก [์ด์ ๋ฐ ๋ฌธ๋งฅ ๋งค์นญ] ๊ณต๋ฐฑ, ๋ค์ฌ์ฐ๊ธฐ, ๋ฐ์ดํ ๊ฒฉ์ฐจ๋ฅผ ์์ ํ ์ด์ํ ์ต์ข
์งํํ ๋จธ์ง ์์ง
|
|
5
|
+
*/
|
|
6
|
+
const applyPureDiff = (asisSource, sourceChanges) => {
|
|
7
|
+
if (!asisSource) return "";
|
|
8
|
+
if (!sourceChanges || sourceChanges.length === 0) return asisSource;
|
|
9
|
+
|
|
10
|
+
let finalSource = asisSource.replace(/\r/g, "");
|
|
11
|
+
|
|
12
|
+
for (const change of sourceChanges) {
|
|
13
|
+
let searchText = change.search_block || change.search_string || "";
|
|
14
|
+
let replaceText = change.replace_block || change.replace_string || "";
|
|
15
|
+
|
|
16
|
+
if (!searchText) continue;
|
|
17
|
+
|
|
18
|
+
// ๐ฏ 1. [๋งํฌ์
์ฒญ์]: ๋ผ์ธ ๋ฒํธ ํ์ ์ง์ฐ๊ธฐ
|
|
19
|
+
searchText = searchText.split("\n").map(line => line.replace(/^\[LN\d+\]\s*/, "")).join("\n");
|
|
20
|
+
replaceText = replaceText.split("\n").map(line => line.replace(/^\[LN\d+\]\s*/, "")).join("\n");
|
|
21
|
+
|
|
22
|
+
// ๐ฏ 2. [๊ณต๋ฐฑ/๋ฐ์ดํ ์ ๊ทํ ํฌํผ]: ๋ชจ๋ ๊ณต๋ฐฑ๊ณผ ๋ฐ์ดํ๋ฅผ ์์ถํ์ฌ ์์ '์๋งน์ด'๋ง ๋น๊ต
|
|
23
|
+
const normalizeLine = (line) => {
|
|
24
|
+
return (line || "").replace(/['"]/g, '').replace(/\s+/g, '').trim();
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// ๊ฒ์ ๋์ ๋ธ๋ก์ ๋ผ์ธ๋ณ ์๋งน์ด ๋ฐฐ์ด๋ก ๋ถ๋ฆฌ
|
|
28
|
+
const searchLinesNormalized = searchText.split("\n")
|
|
29
|
+
.map(l => normalizeLine(l))
|
|
30
|
+
.filter(Boolean);
|
|
31
|
+
|
|
32
|
+
if (searchLinesNormalized.length === 0) continue;
|
|
33
|
+
|
|
34
|
+
// ์๋ณธ ์์ค์ฝ๋ ๋ผ์ธ ๋ฐฐ์ด
|
|
35
|
+
const sourceLines = finalSource.split("\n");
|
|
36
|
+
let startIdx = -1;
|
|
37
|
+
let matchedLineCount = searchLinesNormalized.length;
|
|
38
|
+
|
|
39
|
+
// ๐ฏ 3. [์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์ ๋ฐ ํ์]: ์๋ณธ ์์ค๋ฅผ ๋๋ฉฐ ์๋งน์ด ์์๊ฐ ์๋ฒฝํ ์ผ์นํ๋ ๊ตฌ๊ฐ ํฌ์ฐฉ
|
|
40
|
+
for (let i = 0; i < sourceLines.length; i++) {
|
|
41
|
+
let matchCount = 0;
|
|
42
|
+
let sourceOffset = 0;
|
|
43
|
+
|
|
44
|
+
for (let j = 0; j < searchLinesNormalized.length; j++) {
|
|
45
|
+
// ์๋ณธ์ ๋น ์ค์ ๊ฑด๋๋ฐ๋ฉฐ ๋น๊ต ์ ์ฐ์ฑ ํ๋ณด
|
|
46
|
+
while (sourceLines[i + sourceOffset] !== undefined && normalizeLine(sourceLines[i + sourceOffset]) === "") {
|
|
47
|
+
sourceOffset++;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (sourceLines[i + sourceOffset] !== undefined &&
|
|
51
|
+
normalizeLine(sourceLines[i + sourceOffset]) === searchLinesNormalized[j]) {
|
|
52
|
+
matchCount++;
|
|
53
|
+
sourceOffset++;
|
|
54
|
+
} else {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (matchCount === searchLinesNormalized.length) {
|
|
60
|
+
startIdx = i;
|
|
61
|
+
matchedLineCount = sourceOffset; // ์ค์ ์๋ณธ์์ ๋จน์ด์น์ธ ์ค ์ (๊ณต๋ฐฑ ํฌํจ)
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ๐ฏ 4. [์นํ ์คํ]: ๋งค์นญ ๊ตฌ๊ฐ์ ์ฐพ์๋ค๋ฉด ํด๋น ์์ญ์ ๊น๋ํ๊ฒ ์ค์
|
|
67
|
+
if (startIdx !== -1) {
|
|
68
|
+
console.log(`๐ฏ [์ด์ ๋ฐ ๋ฌธ๋งฅ ๋งค์นญ ์ฑ๊ณต] ์๋ณธ ${startIdx + 1}ํ๋ถํฐ ${matchedLineCount}๊ฐ ํ ์นํ ์๋ฃ`);
|
|
69
|
+
const replaceLines = replaceText ? replaceText.split("\n") : [];
|
|
70
|
+
sourceLines.splice(startIdx, matchedLineCount, ...replaceLines);
|
|
71
|
+
finalSource = sourceLines.join("\n");
|
|
72
|
+
} else {
|
|
73
|
+
console.warn("[๋งค์นญ ์ต์ข
์คํจ] ์๋งน์ด ๋น๊ต ๋ถ์์ผ๋ก๋ ์๋ณธ์์ ๋์์ ์ฐพ์ง ๋ชปํ์ต๋๋ค.", searchLinesNormalized);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return finalSource;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* ๐ก [ํ์ฅ์ ๊ธฐ๋ฐ ์์ค ์ถ์ถ ๋ฐ ๊ฒฝ๋ก ๋ฐํ ํฌํผ]
|
|
82
|
+
*/
|
|
83
|
+
const extractFileByExtension = (fileArray, allowedExtensions) => {
|
|
84
|
+
if (!Array.isArray(fileArray) || fileArray.length === 0) return { contents: "", full_path: "" };
|
|
85
|
+
|
|
86
|
+
const matchedFile = fileArray.find(file => {
|
|
87
|
+
const fullPath = (file?.full_path || "").toLowerCase();
|
|
88
|
+
return allowedExtensions.some(ext => fullPath.endsWith(ext));
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return matchedFile
|
|
92
|
+
? { contents: matchedFile.contents || "", full_path: matchedFile.full_path || "" }
|
|
93
|
+
: { contents: fileArray[0]?.contents || "", full_path: fileArray[0]?.full_path || "" };
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
export const modifySourceBrainTool = (db, ai) => ({
|
|
98
|
+
name: "modify-source-brain",
|
|
99
|
+
description: "์ฌ์ฉ์์ ๊ตฌ์ฒด์ ์ธ ์์ ์์ฒญ์ ๋ถ์ํ์ฌ, ๊ธฐ์กด ์์ค์ฝ๋๋ฅผ ์์ ํ
์คํธ ๋งค์นญ ๊ธฐ๋ฐ์ผ๋ก ์ ๋ฐ ๊ฐฑ์ ํ๋ ๋๊ตฌ์
๋๋ค.",
|
|
100
|
+
schema: {
|
|
101
|
+
user_input: z.string(),
|
|
102
|
+
base_package: z.string(),
|
|
103
|
+
current_path: z.string(),
|
|
104
|
+
current_source: z.any(),
|
|
105
|
+
},
|
|
106
|
+
handler: async (params, context) => {
|
|
107
|
+
const schemaSummary = await db.getTableSchemaSummary();
|
|
108
|
+
const enrichedParams = { ...params, schema_summary: schemaSummary };
|
|
109
|
+
|
|
110
|
+
const rawToolResult = await ai.runChain("modify-source-brain", enrichedParams);
|
|
111
|
+
let decision = typeof rawToolResult === 'string' ? JSON.parse(rawToolResult) : rawToolResult;
|
|
112
|
+
|
|
113
|
+
if (decision.intent === "EXECUTE_MODIFY") {
|
|
114
|
+
await context.sendNotification({
|
|
115
|
+
method: "notifications/logging/message",
|
|
116
|
+
params: { level: "info", logger: "modify-source-brain", message: decision.message }
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const modifyList = decision.action?.modifyList || [];
|
|
120
|
+
const schema = await db.getTableSchema();
|
|
121
|
+
const basePackage = params.base_package;
|
|
122
|
+
const resultType = "com.ninelab.ai.util.CamelCaseMap";
|
|
123
|
+
const asisSources = params.current_source || {};
|
|
124
|
+
|
|
125
|
+
for (let target of modifyList) {
|
|
126
|
+
const filteredSchema = schema.filter(table => target.tableIds.includes(table.tableName));
|
|
127
|
+
const formattedPath = (target?.path || params.current_path || "").replaceAll("/", ".").replaceAll("-", "_");
|
|
128
|
+
const cleanPath = formattedPath.startsWith(".") ? formattedPath.substring(1) : formattedPath;
|
|
129
|
+
|
|
130
|
+
const pathParts = (target?.path || params.current_path || "").split("/");
|
|
131
|
+
const rawClassName = pathParts[pathParts.length - 1] || "Sample";
|
|
132
|
+
const baseClassName = rawClassName.split("-").map(word => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
133
|
+
|
|
134
|
+
const mybatisFile = extractFileByExtension(asisSources.mybatis, [".xml"]);
|
|
135
|
+
const serviceFile = extractFileByExtension(asisSources.service, [".java"]);
|
|
136
|
+
const controllerFile = extractFileByExtension(asisSources.controller, [".java"]);
|
|
137
|
+
const javascriptFile = extractFileByExtension(asisSources.javascript, [".js", ".jsx", ".ts", ".tsx"]);
|
|
138
|
+
|
|
139
|
+
const updatedOutputs = {
|
|
140
|
+
mapperSource: mybatisFile.contents,
|
|
141
|
+
serviceSource: serviceFile.contents,
|
|
142
|
+
controllerSource: controllerFile.contents,
|
|
143
|
+
uiSource: javascriptFile.contents
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const targetChains = [
|
|
147
|
+
{
|
|
148
|
+
id: "generate-source-mapper",
|
|
149
|
+
layerKey: "MyBatis",
|
|
150
|
+
label: "MyBatis ๋งตํผ ์์ ",
|
|
151
|
+
fullPath: mybatisFile.full_path,
|
|
152
|
+
getOriginal: () => updatedOutputs.mapperSource,
|
|
153
|
+
buildParams: () => ({
|
|
154
|
+
...params,
|
|
155
|
+
menu_description: target.description,
|
|
156
|
+
schema_detail: filteredSchema,
|
|
157
|
+
result_type: resultType,
|
|
158
|
+
namespace: `${basePackage}.${cleanPath}.mapper`,
|
|
159
|
+
asis_source: updatedOutputs.mapperSource,
|
|
160
|
+
modification_task: target.modificationTask
|
|
161
|
+
}),
|
|
162
|
+
saveOutput: (source) => { updatedOutputs.mapperSource = source; }
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
id: "generate-source-service",
|
|
166
|
+
layerKey: "Service",
|
|
167
|
+
label: "๋น์ฆ๋์ค ์๋น์ค ์์ ",
|
|
168
|
+
fullPath: serviceFile.full_path,
|
|
169
|
+
getOriginal: () => updatedOutputs.serviceSource,
|
|
170
|
+
buildParams: () => ({
|
|
171
|
+
...params,
|
|
172
|
+
menu_description: target.description,
|
|
173
|
+
service_package: `${basePackage}.${cleanPath}.service`,
|
|
174
|
+
mapper_package: `${basePackage}.${cleanPath}.mapper`,
|
|
175
|
+
asis_source: updatedOutputs.serviceSource,
|
|
176
|
+
modification_task: target.modificationTask,
|
|
177
|
+
mapper_source: updatedOutputs.mapperSource
|
|
178
|
+
}),
|
|
179
|
+
saveOutput: (source) => { updatedOutputs.serviceSource = source; }
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
id: "generate-source-controller",
|
|
183
|
+
layerKey: "Controller",
|
|
184
|
+
label: "์ปจํธ๋กค๋ฌ API ์์ ",
|
|
185
|
+
fullPath: controllerFile.full_path,
|
|
186
|
+
getOriginal: () => updatedOutputs.controllerSource,
|
|
187
|
+
buildParams: () => ({
|
|
188
|
+
...params,
|
|
189
|
+
menu_description: target.description,
|
|
190
|
+
api_base_url: target.path || params.current_path,
|
|
191
|
+
controller_package: `${basePackage}.${cleanPath}.controller`,
|
|
192
|
+
service_package: `${basePackage}.${cleanPath}.service`,
|
|
193
|
+
asis_source: updatedOutputs.controllerSource,
|
|
194
|
+
modification_task: target.modificationTask,
|
|
195
|
+
service_source: updatedOutputs.serviceSource
|
|
196
|
+
}),
|
|
197
|
+
saveOutput: (source) => { updatedOutputs.controllerSource = source; }
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
id: "generate-source-ui-react",
|
|
201
|
+
layerKey: "JavaScript",
|
|
202
|
+
label: "๋ฆฌ์กํธ UI ์ปดํฌ๋ํธ ์์ ",
|
|
203
|
+
fullPath: javascriptFile.full_path,
|
|
204
|
+
getOriginal: () => updatedOutputs.uiSource,
|
|
205
|
+
buildParams: () => ({
|
|
206
|
+
...params,
|
|
207
|
+
menu_description: target.description,
|
|
208
|
+
api_base_url: target.path || params.current_path,
|
|
209
|
+
menu_name: target.description,
|
|
210
|
+
base_class: baseClassName,
|
|
211
|
+
schema_detail: filteredSchema,
|
|
212
|
+
asis_source: updatedOutputs.uiSource,
|
|
213
|
+
modification_task: target.modificationTask,
|
|
214
|
+
mapper_source: updatedOutputs.mapperSource,
|
|
215
|
+
controller_source: updatedOutputs.controllerSource
|
|
216
|
+
}),
|
|
217
|
+
saveOutput: (source) => { updatedOutputs.uiSource = source; }
|
|
218
|
+
}
|
|
219
|
+
];
|
|
220
|
+
|
|
221
|
+
for (const chain of targetChains) {
|
|
222
|
+
if (!target.targetLayers.includes(chain.layerKey)) {
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ๐ก [ํต์ฌ] AI ๊ฐ ๊ณ ์น๊ธฐ ์ ์ ์์ ์ค๋ฆฌ์ง๋ ์์ค ์ค๋
์ท์ ๋ฏธ๋ฆฌ ์บก์ฒ
|
|
227
|
+
const originalSource = chain.getOriginal();
|
|
228
|
+
|
|
229
|
+
const specificToolParams = chain.buildParams();
|
|
230
|
+
console.log(`๐ ๏ธ [${target.componentName || "Component"}] ${chain.label} ์์ ์ค...`);
|
|
231
|
+
|
|
232
|
+
const rawSourceResult = await ai.runChain(chain.id, specificToolParams);
|
|
233
|
+
let resultObj = typeof rawSourceResult === 'string' ? JSON.parse(rawSourceResult) : rawSourceResult;
|
|
234
|
+
|
|
235
|
+
let finalSource = "";
|
|
236
|
+
if (resultObj.mode === "CREATE" || resultObj.full_source) {
|
|
237
|
+
finalSource = resultObj.full_source;
|
|
238
|
+
} else if (resultObj.mode === "UPDATE" || resultObj.source_changes) {
|
|
239
|
+
finalSource = applyPureDiff(originalSource, resultObj.source_changes);
|
|
240
|
+
} else {
|
|
241
|
+
finalSource = originalSource;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ์ฐ์ ๋ฐ์์ฉ ๋ฉ๋ชจ๋ฆฌ ๋ฒํผ ์ต์ ํ
|
|
245
|
+
chain.saveOutput(finalSource);
|
|
246
|
+
|
|
247
|
+
// ๐ก [ํด๋ผ์ด์ธํธ ๊ฐ์์ฑ ๊ฐ์ ] ์ค๋ฆฌ์ง๋ ์์ค์ ์ต์ข
์์ ์์ค๋ฅผ ํจ๊ป ์คํธ๋ฆฌ๋ฐ
|
|
248
|
+
await context.sendNotification({
|
|
249
|
+
method: "notifications/logging/message",
|
|
250
|
+
params: {
|
|
251
|
+
level: "info",
|
|
252
|
+
logger: "modify-source-brain",
|
|
253
|
+
message: `${chain.label} ์์ ์๋ฃ`,
|
|
254
|
+
data: {
|
|
255
|
+
layer: chain.layerKey,
|
|
256
|
+
full_path: chain.fullPath,
|
|
257
|
+
asis_source: originalSource, // ๐ ํด๋ผ์ด์ธํธ์์ Diff ํ์
์ฉ์ผ๋ก ๋งคํํ AS-IS ์๋ณธ ์์ค
|
|
258
|
+
source: finalSource // ๐ ํด๋ผ์ด์ธํธ์์ ๋ฎ์ด์์ฐ๊ฑฐ๋ ์ฐ์ธก ์ฐฝ์ ๋์ธ TO-BE ๊ฒฐ๊ณผ ์์ค
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
await context.sendNotification({
|
|
266
|
+
method: "notifications/logging/message",
|
|
267
|
+
params: {
|
|
268
|
+
level: "info",
|
|
269
|
+
logger: "modify-source-brain-completed", // ๐ ๋ช
ํํ๊ฒ ๊ตฌ๋ถ๋ ์ปดํ๋ฆฌํธ ์ ์ฉ ๋ก๊ฑฐ
|
|
270
|
+
message: "์์ฒญํ์ ์์
์ด ์๋ฃ๋์์ต๋๋ค. ์ต์ข
Diff ๋ช
์ธ๋ฅผ ๋ก๋ํฉ๋๋ค.",
|
|
271
|
+
data: {
|
|
272
|
+
status: "SUCCESS"
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
return { success: true, intent: "EXECUTE_MODIFY", message: null };
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
return { success: false, intent: "NONE", message: decision.message };
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const getStaticToolsConfig = () => [
|
|
4
|
+
{
|
|
5
|
+
name: "generate-menu",
|
|
6
|
+
description: "์ฌ์ฉ์์ ์์ฒญ์ ๋ถ์ํ์ฌ ์์คํ
๋ด์ ์กด์ฌํ์ง ์๋ '์ ๊ท ๋ฉ๋ด ๋ผ์ฐํธ ํธ๋ฆฌ'๋ฅผ ๋์ถํ๊ณ ์ค๊ณ ์ ๋ณด(๋ฉ๋ด๋ช
, URL Path, ์ฐ๊ด ํ
์ด๋ธ ๋งคํ ์ ๋ณด)๋ฅผ ํ๋ฉด์ ๊ฐฑ์ ๋ฐ ์ฌ๋ฐฐ์นํ๋ ๋ผ์ฐํธ ๊ตฌ์กฐ ์ค๊ณ ๋๊ตฌ์
๋๋ค. ์ค์ ์์ค์ฝ๋ ํ์ผ(MyBatis, Service, Controller, React UI)์ ์์ฑ ๋ฐ ์์ฑํ๋ ํ์๋ ์ ๋๋ก ์ํํ์ง ์์ต๋๋ค.",
|
|
7
|
+
schema: { user_input: z.string(), routes: z.any(), schema_summary: z.any() }
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
name: "generate-source",
|
|
11
|
+
description: "ํ
์ด๋ธ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์ค ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.",
|
|
12
|
+
schema: { user_input: z.string(), routes: z.any() }
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: "generate-source-controller",
|
|
16
|
+
description: "ํ
์ด๋ธ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์ค ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.",
|
|
17
|
+
schema: { user_input: z.string(), routes: z.any() }
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
name: "generate-source-service",
|
|
21
|
+
description: "ํ
์ด๋ธ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์ค ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.",
|
|
22
|
+
schema: { user_input: z.string(), routes: z.any() }
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "generate-source-mapper", // ๋ฒค๋ ์ข
์ ๋ช
์นญ ์ ๊ฑฐ ์๋ฃ
|
|
26
|
+
description: "ํ
์ด๋ธ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๋ง์ด๋ฐํฐ์ค ๋งตํผ XML ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.",
|
|
27
|
+
schema: { user_input: z.string(), routes: z.any() }
|
|
28
|
+
}
|
|
29
|
+
];
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const systemBrainTool = (db, ai, nineTools = [], generateSourceBrain = null, modifySourceBrain = null) => ({
|
|
4
|
+
name: "system-brain",
|
|
5
|
+
description: "์๋ ๋ถ์ ๋ฐ ์๋ ์คํ ๊ด์ ",
|
|
6
|
+
schema: {
|
|
7
|
+
base_package: z.string(),
|
|
8
|
+
chat_history: z.string(),
|
|
9
|
+
user_input: z.string(),
|
|
10
|
+
current_path: z.string(),
|
|
11
|
+
current_source: z.any(),
|
|
12
|
+
routes: z.any(), // ๋ฉ๋ด ์ง๋๋ ํด๋ผ์ด์ธํธ๊ฐ ๊ด๋ฆฌํ๋ ๊ฒฝ์ฐ ์ ๋ฌ ๋ฐ์
|
|
13
|
+
unmapped_routes: z.any(),
|
|
14
|
+
},
|
|
15
|
+
handler: async (params, context) => {
|
|
16
|
+
const schemaSummary = await db.getTableSchemaSummary();
|
|
17
|
+
//const { selected_tables } = await this.ai.filterTables(question, schemaSummary);
|
|
18
|
+
|
|
19
|
+
const allToolsConfig = [
|
|
20
|
+
...nineTools.map(t => ({ name: t.name, description: t.description, schema: t.schema })),
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// ๐ก ์ฃผ์
๋ฐ์ generateSourceBrain ๋ณธ์ฒด๊ฐ ์กด์ฌํ๋ค๋ฉด ํด๋น ๋ช
์ธ๋ ํด ์ปจํ
์คํธ์ ํฌํจ์ํต๋๋ค.
|
|
24
|
+
if (generateSourceBrain) {
|
|
25
|
+
allToolsConfig.push({
|
|
26
|
+
name: generateSourceBrain.name,
|
|
27
|
+
description: generateSourceBrain.description,
|
|
28
|
+
schema: generateSourceBrain.schema
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (modifySourceBrain) {
|
|
33
|
+
allToolsConfig.push({
|
|
34
|
+
name: modifySourceBrain.name,
|
|
35
|
+
description: modifySourceBrain.description,
|
|
36
|
+
schema: modifySourceBrain.schema
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const enrichedParams = {
|
|
41
|
+
...params,
|
|
42
|
+
schema_summary: schemaSummary,
|
|
43
|
+
tools: allToolsConfig
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// 1. ๊ด์ ํ(system-brain)์๊ฒ ๋จผ์ ๋ฌผ์ด๋ด
|
|
47
|
+
const brainResult = await ai.runChain("system-brain", enrichedParams);
|
|
48
|
+
const decision = typeof brainResult === 'string' ? JSON.parse(brainResult) : brainResult;
|
|
49
|
+
|
|
50
|
+
console.log(decision);
|
|
51
|
+
// ๐ก ์ ์ก ํจ์๋ฅผ ์คํํ๊ธฐ ์ ์, ์๋ฒ ๊ฐ์ฒด๊ฐ ์ด ์ธ์
์ ์ง์ง ์ด์ด๋ ๊ฒ ๋ง๋์ง ๋์ผ๋ก ํ์ธ
|
|
52
|
+
|
|
53
|
+
if (decision.action?.selected_tool === "generate-source-brain") {
|
|
54
|
+
if (!generateSourceBrain) throw new Error("generate-source-brain ์ธ์คํด์ค๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.");
|
|
55
|
+
|
|
56
|
+
await context.sendNotification({
|
|
57
|
+
method: "notifications/logging/message",
|
|
58
|
+
params: {
|
|
59
|
+
level: "info",
|
|
60
|
+
logger: "system-brain",
|
|
61
|
+
message: decision.message,//`์์ค ์์ฑ ์๋๊ฐ ํ์ ๋์ด [generate-source-brain] ๊ด์ ๋ก ํ๋ก์ธ์ค๋ฅผ ์์ํฉ๋๋ค.`
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ๐ก ๊ท๊ฒฉ์ ๋ง์ถฐ ํ๋ผ๋ฏธํฐ ๋น๋ ํ ํด ๋ณธ์ฒด ํธ๋ค๋ฌ ์ง์ ๊ตฌ๋
|
|
66
|
+
const generateSourceBrainParams = {
|
|
67
|
+
user_input: params.user_input,
|
|
68
|
+
base_package: params.base_package,
|
|
69
|
+
unmapped_routes: (params.unmapped_routes || []).filter(route => route["is-nine"] !== true),
|
|
70
|
+
current_source: "",
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// ๐ฏ ํธ๋ค๋ฌ๋ฅผ ์ง์ ํธ์ถํ๋ฏ๋ก source-brain ๋ด๋ถ์ await db.getTableSchemaSummary()๊ฐ
|
|
74
|
+
// ์๋ฒฝํ๊ฒ ์ ์ ๊ธฐ๋๋์ด LangChain ํ
ํ๋ฆฟ ์ฃผ์
์๋ฌ๊ฐ ๋ฐ์ํ์ง ์์ต๋๋ค.
|
|
75
|
+
return await generateSourceBrain.handler(generateSourceBrainParams, context);
|
|
76
|
+
} else if (decision.action?.selected_tool === "modify-source-brain") {
|
|
77
|
+
if (!modifySourceBrain) throw new Error("modify-source-brain ์ธ์คํด์ค๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.");
|
|
78
|
+
|
|
79
|
+
await context.sendNotification({
|
|
80
|
+
method: "notifications/logging/message",
|
|
81
|
+
params: {
|
|
82
|
+
level: "info",
|
|
83
|
+
logger: "system-brain",
|
|
84
|
+
message: decision.message,//`๐ง ์์ค ์์ ์๋๊ฐ ํ์ ๋์ด [modify-source-brain] ๊ด์ ๋ก ํ๋ก์ธ์ค๋ฅผ ์์ํฉ๋๋ค.`
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// ๐ก ๊ท๊ฒฉ์ ๋ง์ถฐ ํ๋ผ๋ฏธํฐ ๋น๋ ํ ํด ๋ณธ์ฒด ํธ๋ค๋ฌ ์ง์ ๊ตฌ๋
|
|
89
|
+
const modifySourceBrainParams = {
|
|
90
|
+
user_input: params.user_input,
|
|
91
|
+
current_path: params.current_path,
|
|
92
|
+
base_package: params.base_package,
|
|
93
|
+
//unmapped_routes: params.unmapped_routes || [],
|
|
94
|
+
current_source: params.current_source,
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// ๐ฏ ํธ๋ค๋ฌ๋ฅผ ์ง์ ํธ์ถํ๋ฏ๋ก source-brain ๋ด๋ถ์ await db.getTableSchemaSummary()๊ฐ
|
|
98
|
+
// ์๋ฒฝํ๊ฒ ์ ์ ๊ธฐ๋๋์ด LangChain ํ
ํ๋ฆฟ ์ฃผ์
์๋ฌ๊ฐ ๋ฐ์ํ์ง ์์ต๋๋ค.
|
|
99
|
+
return await modifySourceBrain.handler(modifySourceBrainParams, context);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 2. [๋ด๋ถ ๋ฃจํ] ์ ๋ณด๊ฐ ์ถฉ๋ถํ๊ณ ๋ค๋ฅธ ํด์ ์คํํด์ผ ํ๋ ๊ฒฝ์ฐ
|
|
103
|
+
if (decision.intent === "EXECUTE_TOOL" && decision.action?.selected_tool) {
|
|
104
|
+
|
|
105
|
+
await context.sendNotification({
|
|
106
|
+
method: "notifications/logging/message",
|
|
107
|
+
params: {
|
|
108
|
+
level: "info",
|
|
109
|
+
logger: "system-brain",
|
|
110
|
+
message: decision.message,
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
console.log(`๐ [Auto-Chain]: ${decision.action.selected_tool} ์คํ`);
|
|
115
|
+
|
|
116
|
+
// ๐ ๏ธ ์์ ํ ํ์ ํด ์ฒด์ด๋ ํ๋ผ๋ฏธํฐ ์ ๋ฌ ๋ฐฉ์
|
|
117
|
+
const toolParams = {
|
|
118
|
+
...enrichedParams, // ์๋ณธ ์ปจํ
์คํธ(routes, schema_summary ๋ฑ)๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ๊น๊ณ
|
|
119
|
+
...decision.action.params // AI๊ฐ ๊ฐ๊ณตํ๊ฑฐ๋ ์์ ํ ํ๋ผ๋ฏธํฐ๋ก ๋ฎ์ด์ฐ๊ธฐ
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const rawToolResult = await ai.runChain(decision.action.selected_tool, toolParams);
|
|
123
|
+
|
|
124
|
+
console.log(rawToolResult);
|
|
125
|
+
|
|
126
|
+
let processedResult = rawToolResult;
|
|
127
|
+
if (typeof rawToolResult === 'string') {
|
|
128
|
+
try {
|
|
129
|
+
processedResult = JSON.parse(rawToolResult);
|
|
130
|
+
} catch (e) {
|
|
131
|
+
processedResult = { message: rawToolResult, data: null };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
//console.log(decision.action.selected_tool);
|
|
136
|
+
|
|
137
|
+
switch (decision.action.selected_tool) {
|
|
138
|
+
case "generate-menu":
|
|
139
|
+
await context.sendNotification({
|
|
140
|
+
method: "notifications/logging/message",
|
|
141
|
+
params: {
|
|
142
|
+
level: "info",
|
|
143
|
+
logger: "generate-menu-completed",
|
|
144
|
+
message: processedResult.message,
|
|
145
|
+
data: processedResult.data,
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
switch (decision.action.selected_tool) {
|
|
152
|
+
case "generate-menu":
|
|
153
|
+
return {
|
|
154
|
+
intent: "EXECUTE_TOOL",
|
|
155
|
+
selected_tool: decision.action.selected_tool,
|
|
156
|
+
action: {
|
|
157
|
+
selected_tool: decision.action.selected_tool,
|
|
158
|
+
params: decision.action.params
|
|
159
|
+
},
|
|
160
|
+
message: null, // ๐ AI ์ํคํ
ํธ๊ฐ ์์ฑํ ์ง์ง ๋ถ์ ๊ทผ๊ฑฐ ๋ฉํธ๊ฐ ๋ค์ด๊ฐ
|
|
161
|
+
data: processedResult.data // ๐ ์ ๊ท ํตํฉ ๋ผ์ฐํธ ๋ฆฌ์คํธ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
// ๐ก [ํต์ฌ] ํ์ ํด์ด ์ ์ฑ๊ป ์์ฑํ ์ง์ง ๋ถ์ ์ด์ (message)์ ๊ฒฐ๊ณผ(data)๋ฅผ ๊ทธ๋๋ก ์ด๋ ค์ ๋ฐํํฉ๋๋ค!
|
|
167
|
+
return {
|
|
168
|
+
intent: "EXECUTE_TOOL",
|
|
169
|
+
selected_tool: decision.action.selected_tool,
|
|
170
|
+
action: {
|
|
171
|
+
selected_tool: decision.action.selected_tool,
|
|
172
|
+
params: decision.action.params
|
|
173
|
+
},
|
|
174
|
+
message: processedResult.message, // ๐ AI ์ํคํ
ํธ๊ฐ ์์ฑํ ์ง์ง ๋ถ์ ๊ทผ๊ฑฐ ๋ฉํธ๊ฐ ๋ค์ด๊ฐ
|
|
175
|
+
data: processedResult.data // ๐ ์ ๊ท ํตํฉ ๋ผ์ฐํธ ๋ฆฌ์คํธ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
return decision;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const safeExecute = async (fn) => {
|
|
2
|
+
try {
|
|
3
|
+
const result = await fn();
|
|
4
|
+
|
|
5
|
+
// [์๋ณต] ์ด์คํ ๋ถ๊ธฐ๋ ํ์ฑ ์์ด, ํด์ด ๋ฑ์ ๊ฒฐ๊ณผ ๊ฐ์ฒด๋ฅผ ๊ทธ๋๋ก ์ง๋ ฌํํ์ฌ ์ ๋ฌํฉ๋๋ค.
|
|
6
|
+
const textResult = typeof result === 'string'
|
|
7
|
+
? result
|
|
8
|
+
: JSON.stringify(result, null, 2);
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
content: [
|
|
12
|
+
{
|
|
13
|
+
type: "text",
|
|
14
|
+
text: textResult
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
};
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error("Tool Error:", error);
|
|
20
|
+
return {
|
|
21
|
+
content: [
|
|
22
|
+
{
|
|
23
|
+
type: "text",
|
|
24
|
+
text: JSON.stringify({ success: false, error: error.message })
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
isError: true
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
+
import {
|
|
3
|
+
ListToolsRequestSchema,
|
|
4
|
+
CallToolRequestSchema
|
|
5
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
6
|
+
|
|
7
|
+
// ๊ธฐ์กด ์๋น์ค ์ํฌํธ
|
|
8
|
+
// import { AIService } from './ai/AIService.js';
|
|
9
|
+
// import { SourceService } from './services/SourceService.js';
|
|
10
|
+
|
|
11
|
+
export async function createMCPServer(aiService, sourceService, queryService) {
|
|
12
|
+
const server = new Server({
|
|
13
|
+
name: "nine-mcp",
|
|
14
|
+
version: "0.1.25",
|
|
15
|
+
}, {
|
|
16
|
+
capabilities: { tools: {} }
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// 1. ๋๊ตฌ ๋ชฉ๋ก ์ ์
|
|
20
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
21
|
+
tools: [
|
|
22
|
+
{
|
|
23
|
+
name: "navigate_menu",
|
|
24
|
+
description: "ํ์ฌ ํ๋ฉด์ ๋ฉ๋ด ๊ตฌ์กฐ๋ฅผ ๋ถ์ํ์ฌ ์ต์ ์ ์ด๋ ๊ฒฝ๋ก๋ฅผ ์ฐพ์ต๋๋ค.",
|
|
25
|
+
inputSchema: {
|
|
26
|
+
type: "object",
|
|
27
|
+
properties: {
|
|
28
|
+
menuName: { type: "string", description: "์ด๋ํ๊ณ ์ ํ๋ ๋ฉ๋ด์ ์์ฐ์ด ๋ช
์นญ" },
|
|
29
|
+
currentRoutes: {
|
|
30
|
+
type: "array",
|
|
31
|
+
items: { type: "object" },
|
|
32
|
+
description: "ํ์ฌ React ์ฑ์ ์ ์๋ ์ต์ ๋ฉ๋ด ๊ตฌ์กฐ ๋ฐฐ์ด"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
required: ["menuName", "currentRoutes"]
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: "ask_query",
|
|
40
|
+
description: "์์ฐ์ด๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค ์กฐํ๋ฅผ ์์ฒญํฉ๋๋ค.",
|
|
41
|
+
inputSchema: {
|
|
42
|
+
type: "object",
|
|
43
|
+
properties: {
|
|
44
|
+
prompt: { type: "string", description: "์ง์ ๋ด์ฉ" }
|
|
45
|
+
},
|
|
46
|
+
required: ["prompt"]
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: "analyze_missing",
|
|
51
|
+
description: "ํ์ฌ ๋ฉ๋ด์ DB ์คํค๋ง๋ฅผ ๋น๊ตํ์ฌ ๋๋ฝ๋๊ฑฐ๋ ํ์ํ ๊ธฐ๋ฅ์ ๋ถ์ํฉ๋๋ค.",
|
|
52
|
+
inputSchema: {
|
|
53
|
+
type: "object",
|
|
54
|
+
properties: {
|
|
55
|
+
prompt: { type: "string" },
|
|
56
|
+
currentRoutes: { type: "array", items: { type: "object" } }
|
|
57
|
+
},
|
|
58
|
+
required: ["prompt", "currentRoutes"]
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: "generate_source",
|
|
63
|
+
description: "ํ
์ด๋ธ ์ ๋ณด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ ํ๋ ๋ ์ด์ด(MyBatis/Service/Controller/JS) ์์ค๋ฅผ ์์ฑํฉ๋๋ค.",
|
|
64
|
+
inputSchema: {
|
|
65
|
+
type: "object",
|
|
66
|
+
properties: {
|
|
67
|
+
tableName: { type: "string" },
|
|
68
|
+
// UI ์ฒดํฌ๋ฐ์ค ์ํ๋ฅผ ์ ๋ฌ๋ฐ์ ๊ฐ์ฒด ์ถ๊ฐ
|
|
69
|
+
options: {
|
|
70
|
+
type: "object",
|
|
71
|
+
properties: {
|
|
72
|
+
mybatis: { type: "boolean" },
|
|
73
|
+
service: { type: "boolean" },
|
|
74
|
+
controller: { type: "boolean" },
|
|
75
|
+
javascript: { type: "boolean" }
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
required: ["tableName"]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
}));
|
|
84
|
+
|
|
85
|
+
// 2. ๋๊ตฌ ์คํ ๋ก์ง
|
|
86
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
87
|
+
const { name, arguments: args } = request.params;
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
switch (name) {
|
|
91
|
+
case "navigate_menu":
|
|
92
|
+
// ์๋ฒ๊ฐ ์๋ ํด๋ผ์ด์ธํธ๊ฐ ์ ๋ฌํ args.currentRoutes๋ฅผ ์ฌ์ฉํจ
|
|
93
|
+
const navResult = await aiService.processNavigation(
|
|
94
|
+
args.menuName,
|
|
95
|
+
args.currentRoutes
|
|
96
|
+
);
|
|
97
|
+
return {
|
|
98
|
+
content: [{
|
|
99
|
+
type: "text",
|
|
100
|
+
text: JSON.stringify({
|
|
101
|
+
targetPath: navResult.path,
|
|
102
|
+
explanation: navResult.explanation,
|
|
103
|
+
success: navResult.success
|
|
104
|
+
})
|
|
105
|
+
}]
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
case "ask_query":
|
|
109
|
+
const queryResult = await queryService.ask(args.prompt);
|
|
110
|
+
return {
|
|
111
|
+
content: [{ type: "text", text: JSON.stringify(queryResult) }]
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
case "generate_source":
|
|
115
|
+
// args.options๋ฅผ sourceService์ ํจ๊ป ์ ๋ฌ
|
|
116
|
+
const sourceResult = await sourceService.generate(args.tableName, args.options);
|
|
117
|
+
return { content: [{ type: "text", text: JSON.stringify(sourceResult) }] };
|
|
118
|
+
|
|
119
|
+
default:
|
|
120
|
+
throw new Error(`์ง์ํ์ง ์๋ ๋๊ตฌ์
๋๋ค: ${name}`);
|
|
121
|
+
}
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return {
|
|
124
|
+
isError: true,
|
|
125
|
+
content: [{ type: "text", text: error.message }]
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
return server;
|
|
131
|
+
}
|