Adds NPM_MCP_ENABLE_DESTRUCTIVE_TOOLS (default: false) that gates all write operations (create, update, delete, enable, disable). Deployments are read-only by default and must explicitly opt in to mutations. Closes #6
This commit is contained in:
6 files changed
+53
No files matched your search
@@ -64,6 +64,7 @@ NPM_PROXY_DEFAULTS='{"certificate_id": 24, "ssl_forced": true}'
|
||||
| `NPM_MCP_HOST` | No | `0.0.0.0` | MCP server bind address |
|
||||
| `NPM_MCP_PORT` | No | `8000` | MCP server port |
|
||||
| `NPM_MCP_TRANSPORT` | No | `stdio` | Transport mode (`stdio` or `http`) |
|
||||
| `NPM_MCP_ENABLE_DESTRUCTIVE_TOOLS` | No | `false` | Enable write/destructive tools (create, update, delete, enable, disable) |
|
||||
| `NPM_LOG_DIR` | No | - | Path to mounted NPM log directory (enables `get_proxy_host_logs`) |
|
||||
| `NPM_PROXY_DEFAULTS` | No | `{}` | JSON defaults for `create_proxy_host` |
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ services:
|
||||
- NPM_MCP_TRANSPORT=http
|
||||
- NPM_MCP_HOST=0.0.0.0
|
||||
- NPM_MCP_PORT=8000
|
||||
# Optional: Enable write/destructive operations (default: disabled)
|
||||
# - NPM_MCP_ENABLE_DESTRUCTIVE_TOOLS=true
|
||||
# Optional: Log access (requires volume mount below)
|
||||
# - NPM_LOG_DIR=/data/npm-logs
|
||||
# volumes:
|
||||
|
||||
@@ -8,6 +8,9 @@ NPM_MCP_HOST=0.0.0.0
|
||||
NPM_MCP_PORT=8000
|
||||
NPM_MCP_TRANSPORT=stdio # stdio or http
|
||||
|
||||
# Enable destructive/write operations (create, update, delete, enable, disable)
|
||||
# NPM_MCP_ENABLE_DESTRUCTIVE_TOOLS=true
|
||||
|
||||
# Log Access (optional)
|
||||
# Mount NPM's /data/logs directory and set this to the mount path.
|
||||
# Enables the get_proxy_host_logs tool for reading nginx access/error logs.
|
||||
|
||||
@@ -42,6 +42,9 @@ class Settings(BaseSettings):
|
||||
mcp_port: int = 8000
|
||||
mcp_transport: str = "stdio" # "stdio" or "http"
|
||||
|
||||
# Runtime guard for destructive/write operations
|
||||
mcp_enable_destructive_tools: bool = False
|
||||
|
||||
# Path to NPM log directory (mount NPM's /data/logs here)
|
||||
log_dir: str = ""
|
||||
|
||||
|
||||
@@ -51,6 +51,19 @@ mcp = FastMCP(
|
||||
)
|
||||
|
||||
|
||||
DESTRUCTIVE_DISABLED_MSG = (
|
||||
"Destructive operations are disabled. "
|
||||
"Set NPM_MCP_ENABLE_DESTRUCTIVE_TOOLS=true to enable."
|
||||
)
|
||||
|
||||
|
||||
def _check_destructive() -> str | None:
|
||||
"""Return error message if destructive tools are disabled, else None."""
|
||||
if not settings.mcp_enable_destructive_tools:
|
||||
return DESTRUCTIVE_DISABLED_MSG
|
||||
return None
|
||||
|
||||
|
||||
def _format_error(e: Exception) -> str:
|
||||
"""Format exception for tool response."""
|
||||
if isinstance(e, NpmAuthenticationError):
|
||||
@@ -341,6 +354,8 @@ async def create_proxy_host(
|
||||
certificate_id=24, # *.example.com wildcard
|
||||
)
|
||||
"""
|
||||
if msg := _check_destructive():
|
||||
return msg
|
||||
try:
|
||||
# Get defaults from config, then override with provided values
|
||||
defaults = settings.get_proxy_defaults()
|
||||
@@ -421,6 +436,8 @@ async def update_proxy_host(
|
||||
Returns:
|
||||
Details of the updated proxy host.
|
||||
"""
|
||||
if msg := _check_destructive():
|
||||
return msg
|
||||
try:
|
||||
client = get_client()
|
||||
kwargs = {}
|
||||
@@ -477,6 +494,8 @@ async def delete_proxy_host(host_id: int) -> str:
|
||||
Example:
|
||||
delete_proxy_host(42) # permanently remove proxy host 42
|
||||
"""
|
||||
if msg := _check_destructive():
|
||||
return msg
|
||||
try:
|
||||
client = get_client()
|
||||
|
||||
@@ -516,6 +535,8 @@ async def enable_proxy_host(host_id: int) -> str:
|
||||
Example:
|
||||
enable_proxy_host(42) # bring proxy host 42 back online
|
||||
"""
|
||||
if msg := _check_destructive():
|
||||
return msg
|
||||
try:
|
||||
client = get_client()
|
||||
await client.enable_proxy_host(host_id)
|
||||
@@ -550,6 +571,8 @@ async def disable_proxy_host(host_id: int) -> str:
|
||||
Example:
|
||||
disable_proxy_host(42) # take proxy host 42 offline, keep its config
|
||||
"""
|
||||
if msg := _check_destructive():
|
||||
return msg
|
||||
try:
|
||||
client = get_client()
|
||||
|
||||
@@ -652,6 +675,8 @@ async def create_certificate(
|
||||
Details of the created certificate including its ID.
|
||||
Use the returned ID with create_proxy_host or update_proxy_host.
|
||||
"""
|
||||
if msg := _check_destructive():
|
||||
return msg
|
||||
try:
|
||||
client = get_client()
|
||||
cert = await client.create_certificate(
|
||||
|
||||
@@ -89,3 +89,22 @@ class TestProxyDefaults:
|
||||
# Other defaults preserved
|
||||
assert defaults["ssl_forced"] is True
|
||||
assert defaults["block_exploits"] is True
|
||||
|
||||
|
||||
class TestDestructiveToolsGuard:
|
||||
"""Test NPM_MCP_ENABLE_DESTRUCTIVE_TOOLS setting."""
|
||||
|
||||
def test_destructive_tools_default_disabled(self):
|
||||
settings = Settings(identity="test", secret="test")
|
||||
assert settings.mcp_enable_destructive_tools is False
|
||||
|
||||
def test_destructive_tools_enabled(self):
|
||||
settings = Settings(identity="test", secret="test", mcp_enable_destructive_tools=True)
|
||||
assert settings.mcp_enable_destructive_tools is True
|
||||
|
||||
def test_destructive_tools_env_var(self, monkeypatch):
|
||||
monkeypatch.setenv("NPM_IDENTITY", "test")
|
||||
monkeypatch.setenv("NPM_SECRET", "test")
|
||||
monkeypatch.setenv("NPM_MCP_ENABLE_DESTRUCTIVE_TOOLS", "true")
|
||||
settings = Settings()
|
||||
assert settings.mcp_enable_destructive_tools is True
|
||||
Reference in new issue
Block a user