-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcreate.ts
174 lines (146 loc) · 4.26 KB
/
create.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { spawnSync } from "child_process";
import { mkdir, writeFile } from "fs/promises";
import { join } from "path";
import prompts from "prompts";
import { generateReadme } from "../templates/readme.js";
export async function createProject(name?: string) {
let projectName: string;
if (!name) {
const response = await prompts([
{
type: "text",
name: "projectName",
message: "What is the name of your MCP server project?",
validate: (value: string) =>
/^[a-z0-9-]+$/.test(value)
? true
: "Project name can only contain lowercase letters, numbers, and hyphens",
},
]);
if (!response.projectName) {
console.log("Project creation cancelled");
process.exit(1);
}
projectName = response.projectName as string;
} else {
projectName = name;
}
if (!projectName) {
throw new Error("Project name is required");
}
const projectDir = join(process.cwd(), projectName);
const srcDir = join(projectDir, "src");
const toolsDir = join(srcDir, "tools");
try {
console.log("Creating project structure...");
await mkdir(projectDir);
await mkdir(srcDir);
await mkdir(toolsDir);
const packageJson = {
name: projectName,
version: "0.0.1",
description: `${projectName} MCP server`,
type: "module",
bin: {
[projectName]: "./dist/index.js",
},
files: ["dist"],
scripts: {
build: "mcp-build",
prepare: "npm run build",
watch: "tsc --watch",
},
dependencies: {
"mcp-framework": "^0.1.8",
},
devDependencies: {
"@types/node": "^20.11.24",
typescript: "^5.3.3",
},
};
const tsconfig = {
compilerOptions: {
target: "ESNext",
module: "ESNext",
moduleResolution: "node",
outDir: "./dist",
rootDir: "./src",
strict: true,
esModuleInterop: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true,
},
include: ["src/**/*"],
exclude: ["node_modules"],
};
const indexTs = `import { MCPServer } from "mcp-framework";
const server = new MCPServer();
server.start().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});`;
const exampleToolTs = `import { MCPTool } from "mcp-framework";
import { z } from "zod";
interface ExampleInput {
message: string;
}
class ExampleTool extends MCPTool<ExampleInput> {
name = "example_tool";
description = "An example tool that processes messages";
schema = {
message: {
type: z.string(),
description: "Message to process",
},
};
async execute(input: ExampleInput) {
return \`Processed: \${input.message}\`;
}
}
export default ExampleTool;`;
console.log("Creating project files...");
await Promise.all([
writeFile(join(projectDir, "package.json"), JSON.stringify(packageJson, null, 2)),
writeFile(join(projectDir, "tsconfig.json"), JSON.stringify(tsconfig, null, 2)),
writeFile(join(projectDir, "README.md"), generateReadme(projectName)),
writeFile(join(srcDir, "index.ts"), indexTs),
writeFile(join(toolsDir, "ExampleTool.ts"), exampleToolTs),
]);
process.chdir(projectDir);
console.log("Initializing git repository...");
const gitInit = spawnSync("git", ["init"], {
stdio: "inherit",
shell: true,
});
if (gitInit.status !== 0) {
throw new Error("Failed to initialize git repository");
}
console.log("Installing dependencies...");
const npmInstall = spawnSync("npm", ["install"], {
stdio: "inherit",
shell: true,
});
if (npmInstall.status !== 0) {
throw new Error("Failed to install dependencies");
}
console.log("Building project...");
const npmBuild = spawnSync("npm", ["run", "build"], {
stdio: "inherit",
shell: true,
env: process.env
});
if (npmBuild.status !== 0) {
throw new Error("Failed to build project");
}
console.log(`
Project ${projectName} created and built successfully!
You can now:
1. cd ${projectName}
2. Add more tools using:
mcp add tool <name>
`);
} catch (error) {
console.error("Error creating project:", error);
process.exit(1);
}
}