Skip to content

Commit 91b1079

Browse files
chore(build): build a universal ESM and CommonJS package (#371)
Co-authored-by: Himanshu Singh <[email protected]>
1 parent 42837a4 commit 91b1079

18 files changed

+261
-102
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ jobs:
5555
rm -rf node_modules
5656
npm pkg set scripts.prepare="exit 0"
5757
npm install --omit=dev
58-
- run: npx -y @modelcontextprotocol/inspector --cli --method tools/list -- node dist/index.js
58+
- run: npx -y @modelcontextprotocol/inspector --cli --method tools/list -- node dist/esm/index.js

.github/workflows/prepare_release.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
id: bump-version
3333
run: |
3434
echo "NEW_VERSION=$(npm version ${{ inputs.version }} --no-git-tag-version)" >> $GITHUB_OUTPUT
35+
npm run build:update-package-version
3536
- name: Create release PR
3637
uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # 7.0.8
3738
id: create-pr

.smithery/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ RUN npm ci --production --ignore-scripts
2727
# Expose no ports (stdio only)
2828

2929
# Default command
30-
CMD ["node", "dist/index.js"]
30+
CMD ["node", "dist/esm/index.js"]

.smithery/smithery.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ startCommand:
4040
# A function that produces the CLI command to start the MCP on stdio.
4141
|-
4242
(config) => {
43-
const args = ['dist/index.js'];
43+
const args = ['dist/esm/index.js'];
4444
if (config) {
4545
if (config.atlasClientId) {
4646
args.push('--apiClientId');

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This project implements a Model Context Protocol (MCP) server for MongoDB and Mo
3333
{
3434
"mcpServers": {
3535
"MongoDB": {
36-
"command": "/path/to/mongodb-mcp-server/dist/index.js"
36+
"command": "/path/to/mongodb-mcp-server/dist/esm/index.js"
3737
}
3838
}
3939
}
@@ -104,7 +104,7 @@ npm run inspect
104104
This is equivalent to:
105105

106106
```shell
107-
npx @modelcontextprotocol/inspector -- node dist/index.js
107+
npx @modelcontextprotocol/inspector -- node dist/esm/index.js
108108
```
109109

110110
## Pull Request Guidelines

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,47 @@
22
"name": "mongodb-mcp-server",
33
"description": "MongoDB Model Context Protocol Server",
44
"version": "0.2.0",
5-
"main": "dist/index.js",
5+
"type": "module",
6+
"exports": {
7+
".": {
8+
"import": {
9+
"types": "./dist/esm/lib.d.ts",
10+
"default": "./dist/esm/lib.js"
11+
},
12+
"require": {
13+
"types": "./dist/cjs/lib.d.ts",
14+
"default": "./dist/cjs/lib.js"
15+
}
16+
}
17+
},
18+
"main": "./dist/cjs/lib.js",
19+
"types": "./dist/cjs/lib.d.ts",
620
"author": "MongoDB <[email protected]>",
721
"homepage": "https://github.com/mongodb-js/mongodb-mcp-server",
822
"repository": {
923
"url": "https://github.com/mongodb-js/mongodb-mcp-server.git"
1024
},
1125
"bin": {
12-
"mongodb-mcp-server": "dist/index.js"
26+
"mongodb-mcp-server": "dist/esm/index.js"
1327
},
1428
"publishConfig": {
1529
"access": "public"
1630
},
17-
"type": "module",
31+
"files": [
32+
"dist"
33+
],
1834
"scripts": {
1935
"start": "node dist/index.js --transport http --loggers stderr mcp",
2036
"start:stdio": "node dist/index.js --transport stdio --loggers stderr mcp",
2137
"prepare": "npm run build",
2238
"build:clean": "rm -rf dist",
23-
"build:compile": "tsc --project tsconfig.build.json",
24-
"build:chmod": "chmod +x dist/index.js",
25-
"build": "npm run build:clean && npm run build:compile && npm run build:chmod",
26-
"inspect": "npm run build && mcp-inspector -- dist/index.js",
39+
"build:update-package-version": "tsx scripts/updatePackageVersion.ts",
40+
"build:esm": "tsc --project tsconfig.esm.json",
41+
"build:cjs": "tsc --project tsconfig.cjs.json",
42+
"build:universal-package": "tsx scripts/createUniversalPackage.ts",
43+
"build:chmod": "chmod +x dist/esm/index.js",
44+
"build": "npm run build:clean && npm run build:esm && npm run build:cjs && npm run build:universal-package && npm run build:chmod",
45+
"inspect": "npm run build && mcp-inspector -- dist/esm/index.js",
2746
"prettier": "prettier",
2847
"check": "npm run build && npm run check:types && npm run check:lint && npm run check:format",
2948
"check:lint": "eslint .",

scripts/createUniversalPackage.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env tsx
2+
3+
import { writeFileSync, mkdirSync } from "fs";
4+
import { resolve } from "path";
5+
6+
const distDir = resolve("dist");
7+
8+
/**
9+
* Node uses the package.json to know whether files with a .js extensions
10+
* should be interpreted as CommonJS or ESM.
11+
*/
12+
// ESM package.json
13+
const esmPath = resolve(distDir, "esm", "package.json");
14+
mkdirSync(resolve(distDir, "esm"), { recursive: true });
15+
writeFileSync(esmPath, JSON.stringify({ type: "module" }));
16+
17+
// CJS package.json
18+
const cjsPath = resolve(distDir, "cjs", "package.json");
19+
mkdirSync(resolve(distDir, "cjs"), { recursive: true });
20+
writeFileSync(cjsPath, JSON.stringify({ type: "commonjs" }));
21+
22+
// Create a dist/index.js file that imports the ESM index.js file
23+
// To minimize breaking changes from pre-universal package time.
24+
const indexPath = resolve(distDir, "index.js");
25+
writeFileSync(indexPath, `import "./esm/index.js";`);

scripts/updatePackageVersion.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
3+
import { readFileSync, writeFileSync } from "fs";
4+
import { join } from "path";
5+
6+
// Read package.json
7+
const packageJsonPath = join(import.meta.dirname, "..", "package.json");
8+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as {
9+
version: string;
10+
};
11+
12+
// Define the packageInfo.ts content
13+
const packageInfoContent = `// This file was generated by scripts/updatePackageVersion.ts - Do not edit it manually.
14+
export const packageInfo = {
15+
version: "${packageJson.version}",
16+
mcpServerName: "MongoDB MCP Server",
17+
};
18+
`;
19+
20+
// Write to packageInfo.ts
21+
const packageInfoPath = join(import.meta.dirname, "..", "src", "common", "packageInfo.ts");
22+
writeFileSync(packageInfoPath, packageInfoContent);

src/common/packageInfo.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import packageJson from "../../package.json" with { type: "json" };
2-
1+
// This file was generated by scripts/updatePackageVersion.ts - Do not edit it manually.
32
export const packageInfo = {
4-
version: packageJson.version,
3+
version: "0.2.0",
54
mcpServerName: "MongoDB MCP Server",
65
};

0 commit comments

Comments
 (0)