Skip to content

fix: remove findup #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mcp-framework",
"version": "0.1.19",
"version": "0.1.20",
"description": "Framework for building Model Context Protocol (MCP) servers in Typescript",
"type": "module",
"author": "Alex Andru <[email protected]>",
Expand Down
82 changes: 56 additions & 26 deletions src/cli/framework/build.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,82 @@
#!/usr/bin/env node
import { execa } from "execa";
import { readFile, writeFile } from "fs/promises";
import { join } from "path";
import { findUp } from "find-up";

process.stderr.write("MCP Build Script Starting...\n");

export async function buildFramework() {
const projectRoot = await findUp(async directory => {
const pkgPath = join(directory, 'package.json');
const tsConfigPath = join(directory, 'tsconfig.json');
process.stderr.write("Finding project root...\n");

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

let projectRoot: string | null = null;
try {
const pkgPath = join(startDir, 'package.json');
const tsConfigPath = join(startDir, 'tsconfig.json');

try {
const [pkgContent, tsConfigContent] = await Promise.all([
readFile(pkgPath, 'utf8').catch(() => null),
readFile(tsConfigPath, 'utf8').catch(() => null)
]);

if (pkgContent && tsConfigContent) {
return directory;
}
} catch {
return undefined;
process.stderr.write(`Checking for package.json at: ${pkgPath}\n`);
const [pkgContent, _tsConfigContent] = await Promise.all([
readFile(pkgPath, 'utf8'),
readFile(tsConfigPath, 'utf8')
]);

const pkg = JSON.parse(pkgContent);
if (pkg.dependencies?.["mcp-framework"]) {
projectRoot = startDir;
process.stderr.write(`Found MCP project at current directory: ${projectRoot}\n`);
}
});
} catch (error) {
process.stderr.write(`Error checking current directory: ${error instanceof Error ? error.message : String(error)}\n`);
}

if (!projectRoot) {
throw new Error('Could not find target project root directory');
process.stderr.write("Error: Current directory is not an MCP project\n");
throw new Error('Current directory must be an MCP project with mcp-framework as a dependency');
}

try {
await execa("tsc", [], {
process.stderr.write(`Running tsc in ${projectRoot}\n`);

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

await execa(tscCommand[0], [tscCommand[1]], {
cwd: projectRoot,
stdio: "inherit",
reject: true,
cwd: projectRoot
env: {
...process.env,
ELECTRON_RUN_AS_NODE: "1",
FORCE_COLOR: "1"
}
});

const distPath = join(projectRoot, "dist");
const projectIndexPath = join(distPath, "index.js");
const shebang = "#!/usr/bin/env node\n";

const content = await readFile(projectIndexPath, "utf8");
if (!content.startsWith(shebang)) {
await writeFile(projectIndexPath, shebang + content);
process.stderr.write("Adding shebang to index.js...\n");
try {
const content = await readFile(projectIndexPath, "utf8");
if (!content.startsWith(shebang)) {
await writeFile(projectIndexPath, shebang + content);
}
} catch (error) {
process.stderr.write(`Error processing index.js: ${error instanceof Error ? error.message : String(error)}\n`);
throw error;
}

process.stderr.write("Build completed successfully!\n");
} catch (error) {
console.error("Build failed:", error instanceof Error ? error.message : error);
process.stderr.write(`Build error: ${error instanceof Error ? error.message : String(error)}\n`);
process.exit(1);
}
}

if (import.meta.url === new URL(import.meta.url).href) {
buildFramework().catch(console.error);
if (import.meta.url.startsWith('file:')) {
process.stderr.write("Script running as main module\n");
buildFramework().catch(error => {
process.stderr.write(`Fatal error: ${error instanceof Error ? error.message : String(error)}\n`);
process.exit(1);
});
}
2 changes: 1 addition & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const program = new Command();
program
.name("mcp")
.description("CLI for managing MCP server projects")
.version("0.1.8");
.version("0.1.20");

program
.command("build")
Expand Down