npm-run-mcp-server 0.2.2 → 0.2.4

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.
Files changed (3) hide show
  1. package/README.md +12 -6
  2. package/dist/index.js +52 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -43,9 +43,14 @@ npx npm-run-mcp-server
43
43
 
44
44
  ### As an MCP Server
45
45
 
46
- Add this server to your MCP host configuration. It uses stdio and dynamically exposes each script from the closest `package.json` (walking up from `process.cwd()`).
46
+ Add this server to your MCP host configuration. It uses stdio and automatically detects your project's `package.json` using workspace environment variables or by walking up from the current working directory.
47
47
 
48
- The tool names match your script names. Each tool accepts an optional `args` string that is appended after `--` when running the script. The server detects your package manager (npm, pnpm, yarn, bun).
48
+ **Key Features:**
49
+ - **Automatic Workspace Detection**: Works seamlessly across different projects without configuration changes
50
+ - **Smart Tool Names**: Script names with colons (like `install:discord`) are automatically converted to valid tool names (`install_discord`)
51
+ - **Rich Descriptions**: Each tool includes the actual script command in its description
52
+ - **Package Manager Detection**: Automatically detects npm, pnpm, yarn, or bun
53
+ - **Optional Arguments**: Each tool accepts an optional `args` string that is appended after `--` when running the script
49
54
 
50
55
  ### As a CLI Tool
51
56
 
@@ -95,7 +100,7 @@ Option B — user settings (`settings.json`):
95
100
  }
96
101
  ```
97
102
 
98
- **Note**: The server automatically detects the current project's `package.json` using `process.cwd()`, so no hardcoded paths are needed. It works seamlessly across all your projects.
103
+ **Note**: The server automatically detects the current project's `package.json` using workspace environment variables (like `WORKSPACE_FOLDER_PATHS`) or by walking up from the current working directory. No hardcoded paths are needed - it works seamlessly across all your projects.
99
104
 
100
105
  Then open Copilot Chat, switch to Agent mode, and start the `npm-scripts` server from the tools panel.
101
106
 
@@ -103,12 +108,13 @@ Then open Copilot Chat, switch to Agent mode, and start the `npm-scripts` server
103
108
 
104
109
  The MCP server is designed to work seamlessly across multiple projects without configuration changes:
105
110
 
106
- - **VS Code/Cursor**: Use workspace settings (`.vscode/mcp.json` or `.vscode/settings.json`) - the server automatically targets the current project
111
+ - **VS Code/Cursor**: The server automatically detects the current workspace using environment variables like `WORKSPACE_FOLDER_PATHS`
107
112
  - **Claude Desktop**: The server uses the working directory where Claude is launched
108
113
  - **No Hardcoded Paths**: All examples use `npx npm-run-mcp-server` without `--cwd` flags
109
- - **Automatic Detection**: The server walks up the directory tree to find the nearest `package.json`
114
+ - **Smart Detection**: The server first tries workspace environment variables, then falls back to walking up the directory tree to find the nearest `package.json`
115
+ - **Cross-Platform**: Handles Windows/WSL path conversions automatically
110
116
 
111
- This means you can use the same MCP configuration across all your projects, and the server will automatically target the correct project based on where your MCP client is running.
117
+ This means you can use the same MCP configuration across all your projects, and the server will automatically target the correct project based on your current workspace.
112
118
 
113
119
  ### Install in Claude Code (VS Code extension)
114
120
 
package/dist/index.js CHANGED
@@ -85,7 +85,55 @@ function trimOutput(out, limit = 12000) {
85
85
  }
86
86
  async function main() {
87
87
  const args = parseCliArgs(process.argv);
88
- const startCwd = args.cwd ? resolve(String(args.cwd)) : process.cwd();
88
+ // Try to detect workspace directory from environment variables
89
+ let startCwd = process.cwd(); // Initialize with fallback
90
+ if (args.cwd) {
91
+ startCwd = resolve(String(args.cwd));
92
+ }
93
+ else if (process.env.WORKSPACE_FOLDER_PATHS) {
94
+ // Cursor sets this as a semicolon-separated list, take the first one
95
+ const workspacePaths = process.env.WORKSPACE_FOLDER_PATHS.split(';');
96
+ let workspacePath = workspacePaths[0];
97
+ // Convert Windows path to WSL path if running in WSL
98
+ if (process.platform === 'linux' && workspacePath.match(/^[A-Za-z]:\\/)) {
99
+ // Convert H:\path\to\project to /mnt/h/path/to/project
100
+ const drive = workspacePath[0].toLowerCase();
101
+ const path = workspacePath.slice(3).replace(/\\/g, '/');
102
+ workspacePath = `/mnt/${drive}${path}`;
103
+ }
104
+ startCwd = workspacePath;
105
+ }
106
+ else if (process.env.VSCODE_WORKSPACE_FOLDER) {
107
+ startCwd = process.env.VSCODE_WORKSPACE_FOLDER;
108
+ }
109
+ else if (process.env.CURSOR_WORKSPACE_FOLDER) {
110
+ startCwd = process.env.CURSOR_WORKSPACE_FOLDER;
111
+ }
112
+ else {
113
+ // Fallback: try to find a workspace by looking for common patterns
114
+ const currentDir = process.cwd();
115
+ // If we're in the MCP server directory, try to find a parent directory with package.json
116
+ if (currentDir.includes('npm-run-mcp-server')) {
117
+ // Try going up directories to find a workspace
118
+ let testDir = dirname(currentDir);
119
+ let foundWorkspace = false;
120
+ for (let i = 0; i < 5; i++) {
121
+ const testPkgJson = resolve(testDir, 'package.json');
122
+ if (existsSync(testPkgJson)) {
123
+ startCwd = testDir;
124
+ foundWorkspace = true;
125
+ break;
126
+ }
127
+ testDir = dirname(testDir);
128
+ }
129
+ if (!foundWorkspace) {
130
+ startCwd = currentDir;
131
+ }
132
+ }
133
+ else {
134
+ startCwd = currentDir;
135
+ }
136
+ }
89
137
  const pkgJsonPath = await findNearestPackageJson(startCwd);
90
138
  let projectDir = null;
91
139
  let projectPkg = null;
@@ -102,6 +150,7 @@ async function main() {
102
150
  (process.env.DEBUG && process.env.DEBUG.toLowerCase().includes('mcp')));
103
151
  if (verbose) {
104
152
  console.error(`[mcp] server starting: cwd=${startCwd}`);
153
+ console.error(`[mcp] detected workspace: ${process.env.VSCODE_WORKSPACE_FOLDER || process.env.CURSOR_WORKSPACE_FOLDER || 'none'}`);
105
154
  if (pkgJsonPath) {
106
155
  console.error(`[mcp] using package.json: ${pkgJsonPath}`);
107
156
  }
@@ -162,12 +211,9 @@ async function main() {
162
211
  const toolName = scriptName.toLowerCase().replace(/[^a-z0-9_-]/g, '_');
163
212
  // Create a more descriptive description
164
213
  const scriptCommand = scripts[scriptName];
165
- const description = `Run npm script ${scriptName} - ${scriptCommand}`;
166
- if (verbose) {
167
- console.error(`[mcp] registering tool: ${toolName} with description: ${description}`);
168
- }
214
+ const description = `Run script ${scriptName}`;
169
215
  server.tool(toolName, {
170
- description,
216
+ description: description,
171
217
  inputSchema: {
172
218
  type: 'object',
173
219
  properties: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "npm-run-mcp-server",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "An MCP server that exposes package.json scripts as tools for agents.",
5
5
  "bin": {
6
6
  "npm-run-mcp-server": "dist/index.js"