init-xmcp 0.0.18
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/.turbo/turbo-build.log +4 -0
- package/README.md +44 -0
- package/build-and-link.sh +73 -0
- package/dist/helpers/create-handler.d.ts +7 -0
- package/dist/helpers/create-handler.js +29 -0
- package/dist/helpers/create-tool.d.ts +6 -0
- package/dist/helpers/create-tool.js +53 -0
- package/dist/helpers/detect-framework.d.ts +3 -0
- package/dist/helpers/detect-framework.js +33 -0
- package/dist/helpers/generate-config.d.ts +7 -0
- package/dist/helpers/generate-config.js +29 -0
- package/dist/helpers/init.d.ts +10 -0
- package/dist/helpers/init.js +21 -0
- package/dist/helpers/install.d.ts +11 -0
- package/dist/helpers/install.js +44 -0
- package/dist/helpers/update-gitignore.d.ts +6 -0
- package/dist/helpers/update-gitignore.js +18 -0
- package/dist/helpers/update-package.d.ts +6 -0
- package/dist/helpers/update-package.js +28 -0
- package/dist/helpers/update-tsconfig.d.ts +6 -0
- package/dist/helpers/update-tsconfig.js +35 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +239 -0
- package/dist/utils/check-node.d.ts +4 -0
- package/dist/utils/check-node.js +15 -0
- package/index.js +23 -0
- package/package.json +57 -0
- package/src/helpers/create-handler.ts +35 -0
- package/src/helpers/create-tool.ts +57 -0
- package/src/helpers/detect-framework.ts +62 -0
- package/src/helpers/generate-config.ts +37 -0
- package/src/helpers/init.ts +40 -0
- package/src/helpers/install.ts +59 -0
- package/src/helpers/update-gitignore.ts +23 -0
- package/src/helpers/update-package.ts +31 -0
- package/src/helpers/update-tsconfig.ts +45 -0
- package/src/index.ts +312 -0
- package/src/utils/check-node.ts +23 -0
- package/tsconfig.json +15 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import inquirer from "inquirer";
|
|
5
|
+
import { init } from "./helpers/init.js";
|
|
6
|
+
import { detectFramework, detectTypeScript, } from "./helpers/detect-framework.js";
|
|
7
|
+
import { detectPackageManager } from "./helpers/install.js";
|
|
8
|
+
import fs from "fs-extra";
|
|
9
|
+
import path from "path";
|
|
10
|
+
import { checkNodeVersion } from "./utils/check-node.js";
|
|
11
|
+
import { fileURLToPath } from "url";
|
|
12
|
+
checkNodeVersion();
|
|
13
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
14
|
+
const __dirname = path.dirname(__filename);
|
|
15
|
+
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "../package.json"), "utf8"));
|
|
16
|
+
const program = new Command()
|
|
17
|
+
.name("init-xmcp")
|
|
18
|
+
.description("Initialize xmcp in an existing project")
|
|
19
|
+
.version(packageJson.version, "-v, --version", "Output the current version of init-xmcp.")
|
|
20
|
+
.helpOption("-h, --help", "Display help message.")
|
|
21
|
+
.option("-y, --yes", "Skip confirmation prompts", false)
|
|
22
|
+
.option("--package-manager <manager>", "Specify package manager (npm, yarn, pnpm)", "")
|
|
23
|
+
.option("--tools-path <path>", "Specify custom tools path", "")
|
|
24
|
+
.option("--route-path <path>", "Specify custom route path", "")
|
|
25
|
+
.option("--skip-tools", "Skip tool creation", false)
|
|
26
|
+
.option("--skip-route", "Skip route creation", false)
|
|
27
|
+
.action(async (options) => {
|
|
28
|
+
console.log(chalk.bold(`\ninit-xmcp@${packageJson.version}`));
|
|
29
|
+
const projectRoot = process.cwd();
|
|
30
|
+
// check if it's a node js project
|
|
31
|
+
if (!fs.existsSync(path.join(projectRoot, "package.json"))) {
|
|
32
|
+
console.error(chalk.red("Error: No package.json found. Please run this command in a Node.js project."));
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
// check if xmcp is already initialized
|
|
36
|
+
if (fs.existsSync(path.join(projectRoot, "xmcp.config.ts")) ||
|
|
37
|
+
fs.existsSync(path.join(projectRoot, "xmcp.config.json"))) {
|
|
38
|
+
console.error(chalk.red("Error: xmcp is already initialized in this project."));
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
// detect framework package manager and language
|
|
42
|
+
const detectedFramework = detectFramework(projectRoot);
|
|
43
|
+
const detectedPackageManager = detectPackageManager(projectRoot);
|
|
44
|
+
const detectedTypeScript = detectTypeScript(projectRoot);
|
|
45
|
+
if (!detectedTypeScript) {
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
// determine tools path
|
|
49
|
+
let toolsPath;
|
|
50
|
+
if (!options.skipTools) {
|
|
51
|
+
if (options.toolsPath) {
|
|
52
|
+
toolsPath = options.toolsPath;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// check if src folder exists and default to src/tools if it does, otherwise tools
|
|
56
|
+
const hasSrcFolder = fs.existsSync(path.join(projectRoot, "src"));
|
|
57
|
+
toolsPath = hasSrcFolder ? "src/tools" : "tools";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// determine route path
|
|
61
|
+
let routePath;
|
|
62
|
+
if (detectedFramework === "nextjs" && !options.skipRoute) {
|
|
63
|
+
if (options.routePath) {
|
|
64
|
+
routePath = options.routePath;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
// check if src folder exists and default to src/mcp if it does, otherwise mcp
|
|
68
|
+
const hasSrcFolder = fs.existsSync(path.join(projectRoot, "src"));
|
|
69
|
+
routePath = hasSrcFolder ? "src/app/mcp" : "app/mcp";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// determine package manager
|
|
73
|
+
let packageManager;
|
|
74
|
+
if (detectedPackageManager) {
|
|
75
|
+
packageManager = detectedPackageManager;
|
|
76
|
+
}
|
|
77
|
+
else if (options.packageManager) {
|
|
78
|
+
packageManager = options.packageManager;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// no package manager detected, will prompt user
|
|
82
|
+
packageManager = "npm"; // default fallback
|
|
83
|
+
}
|
|
84
|
+
// interactive prompts if not --yes
|
|
85
|
+
if (!options.yes) {
|
|
86
|
+
console.log(chalk.blue("\n📋 xmcp Initialization Settings:"));
|
|
87
|
+
console.log(` Framework: ${chalk.cyan(detectedFramework)}`);
|
|
88
|
+
const prompts = [];
|
|
89
|
+
if (!options.skipTools) {
|
|
90
|
+
prompts.push({
|
|
91
|
+
type: "input",
|
|
92
|
+
name: "toolsPath",
|
|
93
|
+
message: "Tools directory path:",
|
|
94
|
+
default: toolsPath,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (detectedFramework === "nextjs" && !options.skipRoute) {
|
|
98
|
+
prompts.push({
|
|
99
|
+
type: "input",
|
|
100
|
+
name: "routePath",
|
|
101
|
+
message: "Route directory path:",
|
|
102
|
+
default: routePath, // will not be undefined if detectedFramework is nextjs
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
if (!detectedPackageManager) {
|
|
106
|
+
prompts.push({
|
|
107
|
+
type: "list",
|
|
108
|
+
name: "packageManager",
|
|
109
|
+
message: "Package manager:",
|
|
110
|
+
choices: [
|
|
111
|
+
{ name: "npm", value: "npm" },
|
|
112
|
+
{ name: "yarn", value: "yarn" },
|
|
113
|
+
{ name: "pnpm", value: "pnpm" },
|
|
114
|
+
],
|
|
115
|
+
default: packageManager,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
console.log(chalk.green(` Package manager detected: ${packageManager}`));
|
|
120
|
+
}
|
|
121
|
+
prompts.push({
|
|
122
|
+
type: "confirm",
|
|
123
|
+
name: "confirmed",
|
|
124
|
+
message: "Continue with initialization?",
|
|
125
|
+
default: true,
|
|
126
|
+
});
|
|
127
|
+
const answers = await inquirer.prompt(prompts);
|
|
128
|
+
if (!answers.confirmed) {
|
|
129
|
+
console.log(chalk.yellow("❌ Aborting initialization."));
|
|
130
|
+
process.exit(0);
|
|
131
|
+
}
|
|
132
|
+
if (!options.skipTools) {
|
|
133
|
+
toolsPath = answers.toolsPath;
|
|
134
|
+
}
|
|
135
|
+
if (detectedFramework === "nextjs" &&
|
|
136
|
+
!options.skipRoute &&
|
|
137
|
+
answers.routePath) {
|
|
138
|
+
routePath = answers.routePath;
|
|
139
|
+
}
|
|
140
|
+
if (!detectedPackageManager) {
|
|
141
|
+
packageManager = answers.packageManager;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// check if tools directory already exists and has content
|
|
145
|
+
if (toolsPath) {
|
|
146
|
+
const toolsDirPath = path.join(projectRoot, toolsPath);
|
|
147
|
+
if (fs.existsSync(toolsDirPath)) {
|
|
148
|
+
const toolsDirContent = fs.readdirSync(toolsDirPath);
|
|
149
|
+
if (toolsDirContent.length > 0) {
|
|
150
|
+
console.warn(chalk.yellow(`Warning: Tools directory "${toolsPath}" already exists and is not empty.`));
|
|
151
|
+
if (!options.yes) {
|
|
152
|
+
const { overwrite } = await inquirer.prompt([
|
|
153
|
+
{
|
|
154
|
+
type: "confirm",
|
|
155
|
+
name: "overwrite",
|
|
156
|
+
message: "Do you want to continue? Existing files will be preserved.",
|
|
157
|
+
default: true,
|
|
158
|
+
},
|
|
159
|
+
]);
|
|
160
|
+
if (!overwrite) {
|
|
161
|
+
console.log(chalk.yellow("❌ Aborting initialization."));
|
|
162
|
+
process.exit(0);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// check if route directory already exists and has content
|
|
169
|
+
if (routePath) {
|
|
170
|
+
// means detectedFramework is nextjs
|
|
171
|
+
const routeDirPath = path.join(projectRoot, routePath);
|
|
172
|
+
if (fs.existsSync(routeDirPath)) {
|
|
173
|
+
const routeDirContent = fs.readdirSync(routeDirPath);
|
|
174
|
+
if (routeDirContent.length > 0) {
|
|
175
|
+
console.warn(chalk.yellow(`Warning: Route directory "${routePath}" already exists and is not empty.`));
|
|
176
|
+
if (!options.yes) {
|
|
177
|
+
const { overwrite } = await inquirer.prompt([
|
|
178
|
+
{
|
|
179
|
+
type: "confirm",
|
|
180
|
+
name: "overwrite",
|
|
181
|
+
message: "Do you want to continue? Existing files will be preserved.",
|
|
182
|
+
default: true,
|
|
183
|
+
},
|
|
184
|
+
]);
|
|
185
|
+
if (!overwrite) {
|
|
186
|
+
console.log(chalk.yellow("❌ Aborting initialization."));
|
|
187
|
+
process.exit(0);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
console.log(chalk.blue("\n Initializing xmcp..."));
|
|
195
|
+
await init({
|
|
196
|
+
projectRoot,
|
|
197
|
+
framework: detectedFramework,
|
|
198
|
+
toolsPath,
|
|
199
|
+
routePath,
|
|
200
|
+
packageManager,
|
|
201
|
+
});
|
|
202
|
+
console.log(chalk.green("\n✔ xmcp initialized successfully!"));
|
|
203
|
+
console.log(chalk.blue("\n❯ Files created:"));
|
|
204
|
+
console.log(` • xmcp.config.ts`);
|
|
205
|
+
if (toolsPath) {
|
|
206
|
+
console.log(` • ${toolsPath}/greet.ts`);
|
|
207
|
+
}
|
|
208
|
+
if (routePath) {
|
|
209
|
+
console.log(` • ${routePath}/route.ts`);
|
|
210
|
+
}
|
|
211
|
+
console.log(chalk.blue("\n❯ Files updated:"));
|
|
212
|
+
console.log(` • package.json`);
|
|
213
|
+
console.log(` • tsconfig.json`);
|
|
214
|
+
console.log(chalk.blue("\nNext steps:"));
|
|
215
|
+
// code integration for express projects (default)
|
|
216
|
+
if (detectedFramework !== "nextjs") {
|
|
217
|
+
const integrationCode = `import { xmcpHandler } from '@xmcp/adapter';
|
|
218
|
+
|
|
219
|
+
myApp.post("/mcp", xmcpHandler);
|
|
220
|
+
myApp.get("/mcp", xmcpHandler);`;
|
|
221
|
+
const lines = integrationCode.split("\n");
|
|
222
|
+
const maxLength = Math.max(...lines.map((line) => line.length)) + 2;
|
|
223
|
+
console.log("\nTo get started with the xmcpHandler in your Express application, add this code to your server:\n");
|
|
224
|
+
console.log(chalk.green(chalk.bgBlack(` ${" ".repeat(maxLength)}`)));
|
|
225
|
+
lines.forEach((line) => {
|
|
226
|
+
const padding = " ".repeat(maxLength - line.length);
|
|
227
|
+
console.log(chalk.green(chalk.bgBlack(` ${line}${padding}`)));
|
|
228
|
+
});
|
|
229
|
+
console.log(chalk.green(chalk.bgBlack(` ${" ".repeat(maxLength)}`)));
|
|
230
|
+
}
|
|
231
|
+
console.log(` \nRun "${packageManager} run dev" to start the development server\n`);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
console.error(chalk.red("\nFailed to initialize xmcp:"));
|
|
235
|
+
console.error(error);
|
|
236
|
+
process.exit(1);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
program.parse();
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
/**
|
|
3
|
+
* Check if the Node.js version is supported
|
|
4
|
+
*/
|
|
5
|
+
export function checkNodeVersion() {
|
|
6
|
+
const nodeVersion = process.version;
|
|
7
|
+
const majorVersion = parseInt(nodeVersion.slice(1).split(".")[0]);
|
|
8
|
+
if (majorVersion < 20) {
|
|
9
|
+
console.error(chalk.red(`❌ Node.js version ${nodeVersion} is not supported.`));
|
|
10
|
+
console.error(chalk.red(`xmcp requires Node.js 20 or higher.`));
|
|
11
|
+
console.error(chalk.yellow(`Please upgrade your Node.js version and try again.`));
|
|
12
|
+
console.error(chalk.blue(`Visit https://nodejs.org/ to download the latest version.`));
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// This is just a loader file to run the actual CLI from either dist/ or src/
|
|
4
|
+
// depending on whether we're in development or production mode
|
|
5
|
+
|
|
6
|
+
import fs from "fs";
|
|
7
|
+
import path from "path";
|
|
8
|
+
import { fileURLToPath } from "url";
|
|
9
|
+
|
|
10
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
11
|
+
const __dirname = path.dirname(__filename);
|
|
12
|
+
|
|
13
|
+
// Run the CLI
|
|
14
|
+
if (fs.existsSync(path.join(__dirname, "dist/index.js"))) {
|
|
15
|
+
// Production mode - load from dist
|
|
16
|
+
import("./dist/index.js");
|
|
17
|
+
} else {
|
|
18
|
+
// Development mode - use ts-node to run from src
|
|
19
|
+
import("ts-node").then((tsNode) => {
|
|
20
|
+
tsNode.register({ project: path.join(__dirname, "tsconfig.json") });
|
|
21
|
+
import("./src/index.js");
|
|
22
|
+
});
|
|
23
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "init-xmcp",
|
|
3
|
+
"version": "0.0.18",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Basement Studio",
|
|
6
|
+
"email": "hello@basement.studio",
|
|
7
|
+
"url": "https://basement.studio"
|
|
8
|
+
},
|
|
9
|
+
"contributors": [
|
|
10
|
+
{
|
|
11
|
+
"name": "Matias Gonzalez Fernandez",
|
|
12
|
+
"email": "matiasngf@hotmail.com",
|
|
13
|
+
"url": "https://github.com/matiasngf"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"name": "Valentina Bearzotti",
|
|
17
|
+
"email": "valebearzotti1@gmail.com",
|
|
18
|
+
"url": "https://github.com/valebearzotti"
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"mcp",
|
|
24
|
+
"modelcontextprotocol",
|
|
25
|
+
"http",
|
|
26
|
+
"stdio"
|
|
27
|
+
],
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/basementstudio/xmcp"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://xmcp.dev",
|
|
33
|
+
"description": "Initialize xmcp apps with one command",
|
|
34
|
+
"main": "dist/index.js",
|
|
35
|
+
"bin": {
|
|
36
|
+
"init-xmcp": "dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"type": "module",
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc",
|
|
41
|
+
"dev": "tsc --watch",
|
|
42
|
+
"start": "node index.js"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"chalk": "^5.3.0",
|
|
46
|
+
"commander": "^11.1.0",
|
|
47
|
+
"fs-extra": "^11.2.0",
|
|
48
|
+
"inquirer": "^9.2.12",
|
|
49
|
+
"ora": "^7.0.1"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/fs-extra": "^11.0.4",
|
|
53
|
+
"@types/inquirer": "^9.0.7",
|
|
54
|
+
"@types/node": "^20.17.47",
|
|
55
|
+
"typescript": "^5.3.3"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create mcp route folder and file with handler
|
|
7
|
+
* only on Next.js projects
|
|
8
|
+
* @param projectRoot - Project root directory
|
|
9
|
+
* @param handlerPath - Path for route folder (relative to project root)
|
|
10
|
+
*/
|
|
11
|
+
export function createRoute(projectRoot: string, handlerPath: string): void {
|
|
12
|
+
// normalize the path to handle any path separators correctly
|
|
13
|
+
const normalizedHandlerPath = path.normalize(handlerPath);
|
|
14
|
+
const handlerDirPath = path.join(projectRoot, normalizedHandlerPath);
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
// create route folder and all parent directories
|
|
18
|
+
fs.ensureDirSync(handlerDirPath);
|
|
19
|
+
|
|
20
|
+
const routeFilePath = path.join(handlerDirPath, "route.ts");
|
|
21
|
+
fs.writeFileSync(routeFilePath, handlerTemplate);
|
|
22
|
+
|
|
23
|
+
console.log(
|
|
24
|
+
chalk.green(`Created route: ${normalizedHandlerPath}/route.ts`)
|
|
25
|
+
);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error(chalk.red(`Failed to create route: ${error}`));
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const handlerTemplate = `import { xmcpHandler } from '@xmcp/adapter';
|
|
33
|
+
|
|
34
|
+
export { xmcpHandler as GET, xmcpHandler as POST };
|
|
35
|
+
`;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create tools directory and tool file
|
|
7
|
+
* @param projectRoot - Project root directory
|
|
8
|
+
* @param toolsPath - Path for tools directory (relative to project root)
|
|
9
|
+
*/
|
|
10
|
+
export function createTool(projectRoot: string, toolsPath: string): void {
|
|
11
|
+
// normalize the path to handle any path separators correctly
|
|
12
|
+
const normalizedToolsPath = path.normalize(toolsPath);
|
|
13
|
+
const toolsDirPath = path.join(projectRoot, normalizedToolsPath);
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
// create tools directory and all parent directories
|
|
17
|
+
fs.ensureDirSync(toolsDirPath);
|
|
18
|
+
|
|
19
|
+
const toolFilePath = path.join(toolsDirPath, "greet.ts");
|
|
20
|
+
fs.writeFileSync(toolFilePath, toolTemplate);
|
|
21
|
+
|
|
22
|
+
console.log(chalk.green(`Created tool: ${normalizedToolsPath}/greet.ts`));
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error(chalk.red(`Failed to create tool: ${error}`));
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const toolTemplate = `import { z } from "zod";
|
|
30
|
+
import { type InferSchema } from "xmcp";
|
|
31
|
+
|
|
32
|
+
// Define the schema for tool parameters
|
|
33
|
+
export const schema = {
|
|
34
|
+
name: z.string().describe("The name of the user to greet"),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Define tool metadata
|
|
38
|
+
export const metadata = {
|
|
39
|
+
name: "greet",
|
|
40
|
+
description: "Greet the user",
|
|
41
|
+
annotations: {
|
|
42
|
+
title: "Greet the user",
|
|
43
|
+
readOnlyHint: true,
|
|
44
|
+
destructiveHint: false,
|
|
45
|
+
idempotentHint: true,
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Tool implementation
|
|
50
|
+
export default async function greet({ name }: InferSchema<typeof schema>) {
|
|
51
|
+
const result = \`Hello, \${name}!\`;
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
content: [{ type: "text", text: result }],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
export type Framework = "nextjs" | "express";
|
|
6
|
+
|
|
7
|
+
export function detectFramework(projectRoot: string): Framework {
|
|
8
|
+
// check if project has been initialized with nextjs or default to express
|
|
9
|
+
const packageJson = JSON.parse(
|
|
10
|
+
fs.readFileSync(path.join(projectRoot, "package.json"), "utf-8")
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
if (!packageJson.dependencies?.next && !packageJson.devDependencies?.next) {
|
|
14
|
+
return "express";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return "nextjs";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function detectTypeScript(projectRoot: string): boolean {
|
|
21
|
+
// check if project has been initialized with typescript or default to javascript
|
|
22
|
+
const packageJson = JSON.parse(
|
|
23
|
+
fs.readFileSync(path.join(projectRoot, "package.json"), "utf-8")
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
if (
|
|
27
|
+
!packageJson.devDependencies?.typescript &&
|
|
28
|
+
!packageJson.dependencies?.typescript
|
|
29
|
+
) {
|
|
30
|
+
console.log(
|
|
31
|
+
chalk.yellow(
|
|
32
|
+
"Please initialize xmcp within a TypeScript project. Missing dependency: typescript"
|
|
33
|
+
)
|
|
34
|
+
);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// if typescript is in the dependencies, check for tsconfig.json and validate it
|
|
39
|
+
if (fs.existsSync(path.join(projectRoot, "tsconfig.json"))) {
|
|
40
|
+
try {
|
|
41
|
+
const tsconfigContent = fs.readJsonSync(
|
|
42
|
+
path.join(projectRoot, "tsconfig.json")
|
|
43
|
+
);
|
|
44
|
+
return typeof tsconfigContent === "object" && tsconfigContent !== null;
|
|
45
|
+
} catch {
|
|
46
|
+
console.log(
|
|
47
|
+
chalk.yellow(
|
|
48
|
+
"Please initialize xmcp within a TypeScript project. Invalid tsconfig.json file"
|
|
49
|
+
)
|
|
50
|
+
);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.log(
|
|
56
|
+
chalk.yellow(
|
|
57
|
+
"Please initialize xmcp within a TypeScript project. Missing file: tsconfig.json"
|
|
58
|
+
)
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Generate xmcp.config.ts based on selected framework adapter
|
|
6
|
+
* @param projectPath - Project directory path
|
|
7
|
+
* @param frameworkAdapter - Selected framework adapter
|
|
8
|
+
* @param toolsPath - Optional path for tools directory
|
|
9
|
+
*/
|
|
10
|
+
export function generateConfig(
|
|
11
|
+
projectPath: string,
|
|
12
|
+
frameworkAdapter: string,
|
|
13
|
+
toolsPath?: string
|
|
14
|
+
): void {
|
|
15
|
+
let configContent = `import { type XmcpConfig } from "xmcp";
|
|
16
|
+
|
|
17
|
+
const config: XmcpConfig = {
|
|
18
|
+
http: true,
|
|
19
|
+
experimental: {
|
|
20
|
+
adapter: "${frameworkAdapter}",
|
|
21
|
+
},`;
|
|
22
|
+
|
|
23
|
+
if (toolsPath) {
|
|
24
|
+
configContent += `
|
|
25
|
+
paths: {
|
|
26
|
+
tools: "${toolsPath}",
|
|
27
|
+
},`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
configContent += `
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default config;`;
|
|
34
|
+
|
|
35
|
+
const configPath = path.join(projectPath, "xmcp.config.ts");
|
|
36
|
+
fs.writeFileSync(configPath, configContent);
|
|
37
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { generateConfig } from "./generate-config.js";
|
|
2
|
+
import { install } from "./install.js";
|
|
3
|
+
import { updatePackageJson } from "./update-package.js";
|
|
4
|
+
import { updateTsConfig } from "./update-tsconfig.js";
|
|
5
|
+
import { updateGitignore } from "./update-gitignore.js";
|
|
6
|
+
import { createTool } from "./create-tool.js";
|
|
7
|
+
import { Framework } from "./detect-framework.js";
|
|
8
|
+
import { createRoute } from "./create-handler.js";
|
|
9
|
+
import chalk from "chalk";
|
|
10
|
+
|
|
11
|
+
interface InitOptions {
|
|
12
|
+
projectRoot: string;
|
|
13
|
+
framework: Framework;
|
|
14
|
+
toolsPath: string | undefined;
|
|
15
|
+
routePath: string | undefined;
|
|
16
|
+
packageManager: "npm" | "yarn" | "pnpm";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function init(options: InitOptions) {
|
|
20
|
+
const { projectRoot, framework, toolsPath, routePath, packageManager } =
|
|
21
|
+
options;
|
|
22
|
+
|
|
23
|
+
generateConfig(projectRoot, framework, toolsPath);
|
|
24
|
+
|
|
25
|
+
await install(projectRoot, packageManager);
|
|
26
|
+
|
|
27
|
+
updatePackageJson(projectRoot);
|
|
28
|
+
|
|
29
|
+
updateTsConfig(projectRoot);
|
|
30
|
+
|
|
31
|
+
updateGitignore(projectRoot);
|
|
32
|
+
|
|
33
|
+
if (toolsPath) {
|
|
34
|
+
createTool(projectRoot, toolsPath);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (framework === "nextjs" && routePath) {
|
|
38
|
+
createRoute(projectRoot, routePath);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import { execSync } from "child_process";
|
|
5
|
+
|
|
6
|
+
// based on create-xmcp-app CLI
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Detect which package manager is being used in the project
|
|
10
|
+
* @param projectPath - Project directory path
|
|
11
|
+
* @returns Package manager name (npm, yarn, or pnpm)
|
|
12
|
+
*/
|
|
13
|
+
export function detectPackageManager(
|
|
14
|
+
projectPath: string
|
|
15
|
+
): "npm" | "yarn" | "pnpm" | null {
|
|
16
|
+
if (fs.existsSync(path.join(projectPath, "yarn.lock"))) {
|
|
17
|
+
return "yarn";
|
|
18
|
+
}
|
|
19
|
+
if (fs.existsSync(path.join(projectPath, "pnpm-lock.yaml"))) {
|
|
20
|
+
return "pnpm";
|
|
21
|
+
}
|
|
22
|
+
if (fs.existsSync(path.join(projectPath, "package-lock.json"))) {
|
|
23
|
+
return "npm";
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Install xmcp dependencies using the specified package manager
|
|
30
|
+
* @param packageManager - Package manager to use (npm, yarn, pnpm, or bun)
|
|
31
|
+
*/
|
|
32
|
+
export async function install(
|
|
33
|
+
projectPath: string,
|
|
34
|
+
packageManager: "npm" | "pnpm" | "yarn"
|
|
35
|
+
) {
|
|
36
|
+
const dependencies = ["xmcp", "zod"];
|
|
37
|
+
const devDependencies = ["swc-loader"];
|
|
38
|
+
|
|
39
|
+
const commands = {
|
|
40
|
+
npm: `npm install ${dependencies.join(" ")} && npm install --save-dev ${devDependencies.join(" ")}`,
|
|
41
|
+
pnpm: `pnpm add ${dependencies.join(" ")} && pnpm add --save-dev ${devDependencies.join(" ")}`,
|
|
42
|
+
yarn: `yarn add ${dependencies.join(" ")} && yarn add --dev ${devDependencies.join(" ")}`,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
console.log(
|
|
47
|
+
chalk.blue(`\nInstalling dependencies using ${packageManager}...`)
|
|
48
|
+
);
|
|
49
|
+
execSync(commands[packageManager], { cwd: projectPath, stdio: "inherit" });
|
|
50
|
+
console.log(chalk.green("\nDependencies installed successfully!"));
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error(
|
|
53
|
+
chalk.red(
|
|
54
|
+
"\nFailed to install dependencies. You can install them manually:"
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
console.log(chalk.yellow(`\n${commands[packageManager]}`));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs-extra";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Update .gitignore to include xmcp and xmcp-env.d.ts entries
|
|
6
|
+
* @param projectPath - The root directory of the project
|
|
7
|
+
* @returns void
|
|
8
|
+
*/
|
|
9
|
+
export function updateGitignore(projectPath: string): void {
|
|
10
|
+
const gitignorePath = path.join(projectPath, ".gitignore");
|
|
11
|
+
|
|
12
|
+
// xmcp entries to add
|
|
13
|
+
const xmcpEntries = ["", "# xmcp", ".xmcp", "xmcp-env.d.ts"];
|
|
14
|
+
|
|
15
|
+
let gitignoreContent = "";
|
|
16
|
+
|
|
17
|
+
if (fs.existsSync(gitignorePath)) {
|
|
18
|
+
gitignoreContent = fs.readFileSync(gitignorePath, "utf-8");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const updatedContent = gitignoreContent + xmcpEntries.join("\n");
|
|
22
|
+
fs.writeFileSync(gitignorePath, updatedContent);
|
|
23
|
+
}
|