Skip to content

Commit bf0e0d4

Browse files
authored
Merge pull request #31 from QuantGeekDev/fix/cli-add-functionality
feat: fix directory validation issue
2 parents ab023ba + bc6ab31 commit bf0e0d4

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mcp-framework",
3-
"version": "0.1.27",
3+
"version": "0.1.28",
44

55
"description": "Framework for building Model Context Protocol (MCP) servers in Typescript",
66
"type": "module",
@@ -18,7 +18,7 @@
1818
],
1919
"bin": {
2020
"mcp": "dist/cli/index.js",
21-
"mcp-build": "dist/cli/framework/build.js"
21+
"mcp-build": "dist/cli/framework/build-cli.js"
2222
},
2323
"scripts": {
2424
"build": "tsc",

src/cli/framework/build-cli.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env node
2+
import { buildFramework } from './build.js';
3+
4+
if (process.argv[1].endsWith('mcp-build')) {
5+
buildFramework().catch(error => {
6+
process.stderr.write(`Fatal error: ${error instanceof Error ? error.message : String(error)}\n`);
7+
process.exit(1);
8+
});
9+
}

src/cli/framework/build.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
#!/usr/bin/env node
22
import { execa } from "execa";
33
import { readFile, writeFile } from "fs/promises";
4-
import { join } from "path";
5-
6-
process.stderr.write("MCP Build Script Starting...\n");
4+
import { join, dirname } from "path";
5+
import { findUp } from 'find-up';
76

87
export async function buildFramework() {
8+
process.stderr.write("MCP Build Script Starting...\n");
99
process.stderr.write("Finding project root...\n");
1010

1111
const startDir = process.cwd();
1212
process.stderr.write(`Starting search from: ${startDir}\n`);
1313

14-
if (process.argv.includes('create')) {
15-
process.stderr.write(`Skipping build for create command\n`);
16-
return;
14+
const skipValidation = process.env.MCP_SKIP_VALIDATION === 'true';
15+
if (skipValidation) {
16+
process.stderr.write(`Skipping dependency validation\n`);
1717
}
1818

1919
try {
20-
const pkgPath = join(startDir, 'package.json');
21-
const pkgContent = await readFile(pkgPath, 'utf8');
22-
const pkg = JSON.parse(pkgContent);
20+
const pkgPath = await findUp('package.json');
21+
if (!pkgPath) {
22+
throw new Error("Could not find package.json in current directory or any parent directories");
23+
}
2324

24-
if (!pkg.dependencies?.["mcp-framework"]) {
25-
throw new Error("This directory is not an MCP project (mcp-framework not found in dependencies)");
25+
const projectRoot = dirname(pkgPath);
26+
27+
if (!skipValidation) {
28+
const pkgContent = await readFile(pkgPath, 'utf8');
29+
const pkg = JSON.parse(pkgContent);
30+
31+
if (!pkg.dependencies?.["mcp-framework"]) {
32+
throw new Error("This directory is not an MCP project (mcp-framework not found in dependencies)");
33+
}
2634
}
2735

28-
process.stderr.write(`Running tsc in ${startDir}\n`);
36+
process.stderr.write(`Running tsc in ${projectRoot}\n`);
2937

3038
const tscCommand = process.platform === 'win32' ? ['npx.cmd', 'tsc'] : ['npx', 'tsc'];
3139

3240
await execa(tscCommand[0], [tscCommand[1]], {
33-
cwd: startDir,
41+
cwd: projectRoot,
3442
stdio: "inherit",
3543
env: {
3644
...process.env,
@@ -39,7 +47,7 @@ export async function buildFramework() {
3947
}
4048
});
4149

42-
const distPath = join(startDir, "dist");
50+
const distPath = join(projectRoot, "dist");
4351
const projectIndexPath = join(distPath, "index.js");
4452
const shebang = "#!/usr/bin/env node\n";
4553

@@ -61,12 +69,4 @@ export async function buildFramework() {
6169
}
6270
}
6371

64-
if (import.meta.url.startsWith('file:')) {
65-
process.stderr.write("Script running as main module\n");
66-
buildFramework().catch(error => {
67-
process.stderr.write(`Fatal error: ${error instanceof Error ? error.message : String(error)}\n`);
68-
process.exit(1);
69-
});
70-
}
71-
7272
export default buildFramework;

src/cli/project/create.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ export async function createProject(name?: string) {
5555
},
5656
files: ["dist"],
5757
scripts: {
58-
build: "mcp-build",
58+
build: "tsc && mcp-build",
5959
prepare: "npm run build",
6060
watch: "tsc --watch",
6161
start: "node dist/index.js"
6262
},
6363
dependencies: {
64-
"mcp-framework": "^0.1.27",
64+
"mcp-framework": "0.1.28-beta.6"
6565
},
6666
devDependencies: {
6767
"@types/node": "^20.11.24",
68-
typescript: "^5.3.3",
68+
"typescript": "^5.3.3"
6969
},
7070
};
7171

@@ -140,14 +140,14 @@ export default ExampleTool;`;
140140
console.log("Installing dependencies...");
141141
const npmInstall = spawnSync("npm", ["install"], {
142142
stdio: "inherit",
143-
shell: true,
143+
shell: true
144144
});
145145

146146
if (npmInstall.status !== 0) {
147147
throw new Error("Failed to install dependencies");
148148
}
149149

150-
console.log("Building TypeScript...");
150+
console.log("Building project...");
151151
const tscBuild = await execa('npx', ['tsc'], {
152152
cwd: projectDir,
153153
stdio: "inherit",
@@ -157,18 +157,17 @@ export default ExampleTool;`;
157157
throw new Error("Failed to build TypeScript");
158158
}
159159

160-
console.log("Adding shebang...");
161-
const mcpBuild = spawnSync("npm", ["run", "build"], {
160+
const mcpBuild = await execa('npx', ['mcp-build'], {
161+
cwd: projectDir,
162162
stdio: "inherit",
163-
shell: true,
164163
env: {
165164
...process.env,
166-
FORCE_COLOR: "1"
165+
MCP_SKIP_VALIDATION: "true"
167166
}
168167
});
169168

170-
if (mcpBuild.status !== 0) {
171-
throw new Error("Failed to add shebang");
169+
if (mcpBuild.exitCode !== 0) {
170+
throw new Error("Failed to run mcp-build");
172171
}
173172

174173
console.log(`

src/cli/utils/validate-project.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import { access } from "fs/promises";
2-
import { join } from "path";
1+
import { readFile } from "fs/promises";
2+
import { findUp } from 'find-up';
33

44
export async function validateMCPProject() {
55
try {
6-
const packageJsonPath = join(process.cwd(), "package.json");
7-
await access(packageJsonPath);
6+
const packageJsonPath = await findUp('package.json');
7+
8+
if (!packageJsonPath) {
9+
throw new Error("Could not find package.json in current directory or any parent directories");
10+
}
811

9-
const package_json = (
10-
await import(packageJsonPath, { assert: { type: "json" } })
11-
).default;
12+
const packageJsonContent = await readFile(packageJsonPath, 'utf-8');
13+
const package_json = JSON.parse(packageJsonContent);
1214

13-
if (!package_json.dependencies?.["mcp-framework"]) {
15+
if (
16+
!package_json.dependencies?.["mcp-framework"] &&
17+
!package_json.devDependencies?.["mcp-framework"]
18+
) {
1419
throw new Error(
15-
"This directory is not an MCP project (mcp-framework not found in dependencies)"
20+
"This directory is not an MCP project (mcp-framework not found in dependencies or devDependencies)"
1621
);
1722
}
1823
} catch (error) {

0 commit comments

Comments
 (0)