feat: Add DISABLED_TOOLS environment variable for tool filtering (Issue #410)

Added DISABLED_TOOLS environment variable to filter specific tools from registration at startup, enabling deployment-specific tool configuration for multi-tenant deployments, security hardening, and feature flags.

## Implementation

- Added getDisabledTools() method to parse comma-separated tool names from env var
- Modified ListToolsRequestSchema handler to filter both documentation and management tools
- Modified CallToolRequestSchema handler to reject disabled tool calls with clear error messages
- Added defense-in-depth guard in executeTool() method

## Features

- Environment variable format: DISABLED_TOOLS=tool1,tool2,tool3
- O(1) lookup performance using Set data structure
- Clear error messages with TOOL_DISABLED code
- Backward compatible (no DISABLED_TOOLS = all tools enabled)
- Comprehensive logging for observability

## Use Cases

- Multi-tenant: Hide tools that check global env vars
- Security: Disable management tools in production
- Feature flags: Gradually roll out new tools
- Deployment-specific: Different tool sets for cloud vs self-hosted

## Testing

- 45 comprehensive tests (all passing)
- 95% feature code coverage
- Unit tests + additional test scenarios
- Performance tested with 1000 tools (<100ms)

## Files Modified

- src/mcp/server.ts - Core implementation (~40 lines)
- .env.example, .env.docker - Configuration documentation
- tests/unit/mcp/disabled-tools*.test.ts - Comprehensive tests
- package.json, package.runtime.json - Version bump to 2.22.14
- CHANGELOG.md - Full documentation

Resolves #410

Conceived by Romuald Członkowski - www.aiadvisors.pl/en
This commit is contained in:
czlonkowski
2025-11-09 16:26:47 +01:00
parent 2010d77ed8
commit 53252adc68
10 changed files with 1645 additions and 13 deletions

View File

@@ -7,6 +7,177 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [2.22.14] - 2025-01-09
### ✨ New Features
**Issue #410: DISABLED_TOOLS Environment Variable for Tool Filtering**
Added `DISABLED_TOOLS` environment variable to filter specific tools from registration at startup, enabling deployment-specific tool configuration for multi-tenant deployments, security hardening, and feature flags.
#### Problem
In multi-tenant deployments, some tools don't work correctly because they check global environment variables instead of per-instance context. Examples:
- `n8n_diagnostic` shows global env vars (`NODE_ENV`, `process.env.N8N_API_URL`) which are meaningless in multi-tenant mode where each user has their own n8n instance credentials
- `n8n_health_check` checks global n8n API configuration instead of instance-specific settings
- These tools appear in the tools list but either don't work correctly (show wrong data), hang/error, or create confusing UX
Additionally, some deployments need to disable certain tools for:
- **Security**: Disable management tools in production for certain users
- **Feature flags**: Gradually roll out new tools
- **Deployment-specific**: Different tool sets for cloud vs self-hosted
#### Solution
**Environment Variable Format:**
```bash
DISABLED_TOOLS=n8n_diagnostic,n8n_health_check,custom_tool
```
**Implementation:**
1. **`getDisabledTools()` Method** (`src/mcp/server.ts` lines 326-348)
- Parses comma-separated tool names from `DISABLED_TOOLS` env var
- Returns `Set<string>` for O(1) lookup performance
- Handles whitespace trimming and empty entries
- Logs configured disabled tools for debugging
2. **ListToolsRequestSchema Handler** (`src/mcp/server.ts` lines 401-449)
- Filters both `n8nDocumentationToolsFinal` and `n8nManagementTools` arrays
- Removes disabled tools before returning to client
- Logs filtered tool count for observability
3. **CallToolRequestSchema Handler** (`src/mcp/server.ts` lines 491-505)
- Checks if requested tool is disabled before execution
- Returns clear error message with `TOOL_DISABLED` code
- Includes list of all disabled tools in error response
4. **executeTool() Guard** (`src/mcp/server.ts` lines 909-913)
- Defense in depth: additional check at execution layer
- Throws error if disabled tool somehow reaches execution
- Ensures complete protection against disabled tool calls
**Error Response Format:**
```json
{
"error": "TOOL_DISABLED",
"message": "Tool 'n8n_diagnostic' is not available in this deployment. It has been disabled via DISABLED_TOOLS environment variable.",
"disabledTools": ["n8n_diagnostic", "n8n_health_check"]
}
```
#### Usage Examples
**Multi-tenant deployment:**
```bash
# Hide tools that check global env vars
DISABLED_TOOLS=n8n_diagnostic,n8n_health_check
```
**Security hardening:**
```bash
# Disable destructive management tools
DISABLED_TOOLS=n8n_delete_workflow,n8n_update_full_workflow
```
**Feature flags:**
```bash
# Gradually roll out experimental tools
DISABLED_TOOLS=experimental_feature_1,beta_tool_2
```
**Deployment-specific:**
```bash
# Different tool sets for cloud vs self-hosted
DISABLED_TOOLS=local_only_tool,debug_tool
```
#### Benefits
-**Clean Implementation**: ~40 lines of code, simple and maintainable
-**Environment Variable Based**: Standard configuration pattern
-**Backward Compatible**: No `DISABLED_TOOLS` = all tools enabled
-**Defense in Depth**: Filtering at registration + runtime rejection
-**Performance**: O(1) lookup using Set data structure
-**Observability**: Logs configuration and filter counts
-**Clear Error Messages**: Users understand why tools aren't available
#### Test Coverage
**45 comprehensive tests (all passing):**
**Original Tests (21 scenarios):**
- Environment variable parsing (8 tests)
- Tool filtering for both doc & mgmt tools (5 tests)
- ExecuteTool guard (3 tests)
- Invalid tool names (2 tests)
- Real-world use cases (3 tests)
**Additional Tests by test-automator (24 scenarios):**
- Error response structure validation (3 tests)
- Multi-tenant mode interaction (3 tests)
- Special characters & unicode (5 tests)
- Performance at scale (3 tests)
- Environment variable edge cases (4 tests)
- Defense in depth verification (3 tests)
- Real-world deployment scenarios (3 tests)
**Coverage:** 95% of feature code, exceeds >90% requirement
#### Files Modified
**Core Implementation (1 file):**
- `src/mcp/server.ts` - Added filtering logic (~40 lines)
**Configuration (4 files):**
- `.env.example` - Added `DISABLED_TOOLS` documentation with examples
- `.env.docker` - Added `DISABLED_TOOLS` example
- `package.json` - Version bump to 2.22.14
- `package.runtime.json` - Version bump to 2.22.14
**Tests (2 files):**
- `tests/unit/mcp/disabled-tools.test.ts` - 21 comprehensive test scenarios
- `tests/unit/mcp/disabled-tools-additional.test.ts` - 24 additional test scenarios
**Documentation (2 files):**
- `DISABLED_TOOLS_TEST_COVERAGE_ANALYSIS.md` - Detailed coverage analysis
- `DISABLED_TOOLS_TEST_SUMMARY.md` - Executive summary
#### Impact
**Before:**
- ❌ Multi-tenant deployments showed incorrect diagnostic information
- ❌ No way to disable problematic tools at deployment level
- ❌ All-or-nothing approach (either all tools or no tools)
**After:**
- ✅ Fine-grained control over available tools per deployment
- ✅ Multi-tenant deployments can hide env-var-based tools
- ✅ Security hardening via tool filtering
- ✅ Feature flag support for gradual rollout
- ✅ Clean, simple configuration via environment variable
#### Technical Details
**Performance:**
- O(1) lookup performance using `Set<string>`
- Tested with 1000 tools: filtering completes in <100ms
- No runtime overhead for tool execution
**Security:**
- Defense in depth: filtering + runtime rejection
- Clear error messages prevent information leakage
- No way to bypass disabled tool restrictions
**Compatibility:**
- 100% backward compatible
- No breaking changes
- Easy rollback (unset environment variable)
Resolves #410
Conceived by Romuald Członkowski - [www.aiadvisors.pl/en](https://www.aiadvisors.pl/en)
## [2.22.13] - 2025-01-08
### 🎯 Improvements