npm vs npx: When to Use Each in MCP Server Configuration
Understand when to use npm vs npx for MCP server configuration, including the critical -y flag for automated setups.
npm manages packages. npx executes them. This distinction is critical when configuring MCP servers.
Core Difference
npm (Node Package Manager) installs and manages dependencies:
1
npm install @modelcontextprotocol/server-memory
npx (Node Package Execute) runs packages without permanent installation:
1
npx @modelcontextprotocol/server-memory
npx checks in order: $PATH → local node_modules/.bin → downloads temporarily if not found.
Essential npm Commands
1
2
3
4
5
6
7
8
9
10
11
12
13
# Install to local node_modules/
npm install <package>
npm i <package>
# Install globally (available system-wide)
npm install -g <package>
# Install as dev dependency
npm install --save-dev <package>
npm i -D <package>
# Create package.json with defaults (skip prompts)
npm init -y
npx in MCP Configuration
MCP servers use npx to execute packages on-demand:
1
2
3
4
5
6
7
8
{
"mcpServers": {
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory"]
}
}
}
The -y flag auto-confirms installation prompts. Without it, MCP clients hang waiting for user input that never comes.
Server with Arguments
1
2
3
4
5
6
7
8
9
10
11
12
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/me/allowed-directory"
]
}
}
}
Arguments after the package name pass directly to the server.
When to Use Each
Use npx for MCP when:
- Testing or developing locally
- Running latest version automatically
- Avoiding global package pollution
- Quick debugging sessions
Consider npm install when:
- Production deployments (predictable versions)
- Offline environments
- Containerized setups with locked dependencies
Key Differences
| Aspect | npm | npx |
|---|---|---|
| Purpose | Install packages | Execute packages |
| Persistence | Permanent | Temporary |
| Version control | Uses installed | Can specify any |
| MCP use case | Production lock | Development/testing |
Version Pinning with npx
Specify exact versions for reproducibility:
1
2
3
4
5
6
7
8
{
"mcpServers": {
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory@1.0.0"]
}
}
}
Troubleshooting
Server not starting?
1
2
3
4
5
# Test manually first
npx -y @modelcontextprotocol/server-memory
# Check if package exists
npm view @modelcontextprotocol/server-memory
Permission errors?
1
2
3
4
5
# Clear npx cache
rm -rf ~/.npm/_npx
# Verify node/npm installation
which node && which npx
The -y flag is non-negotiable for MCP configs. Automated systems cannot respond to interactive prompts.
☕ Support My Work
If you found this post helpful and want to support more content like this, you can buy me a coffee!
Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏