fix: resolve tool count discrepancy in Docker environments (#6)
- Implement dynamic n8n API configuration checking - Remove static config export in favor of lazy getter function - Fix management tools not being registered when env vars set after startup - Optimize logger performance with cached environment variables - Clean up debug logging and remove console.error usage - Bump version to 2.7.1 This ensures all 38 tools (22 documentation + 16 management) are properly registered regardless of when environment variables become available. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "n8n-mcp",
|
"name": "n8n-mcp",
|
||||||
"version": "2.7.0",
|
"version": "2.7.1",
|
||||||
"description": "Integration between n8n workflow automation and Model Context Protocol (MCP)",
|
"description": "Integration between n8n workflow automation and Model Context Protocol (MCP)",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ import { z } from 'zod';
|
|||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import { logger } from '../utils/logger';
|
import { logger } from '../utils/logger';
|
||||||
|
|
||||||
// Load environment variables
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
// n8n API configuration schema
|
// n8n API configuration schema
|
||||||
const n8nApiConfigSchema = z.object({
|
const n8nApiConfigSchema = z.object({
|
||||||
N8N_API_URL: z.string().url().optional(),
|
N8N_API_URL: z.string().url().optional(),
|
||||||
@@ -13,12 +10,20 @@ const n8nApiConfigSchema = z.object({
|
|||||||
N8N_API_MAX_RETRIES: z.coerce.number().positive().default(3),
|
N8N_API_MAX_RETRIES: z.coerce.number().positive().default(3),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Track if we've loaded env vars
|
||||||
|
let envLoaded = false;
|
||||||
|
|
||||||
// Parse and validate n8n API configuration
|
// Parse and validate n8n API configuration
|
||||||
export function loadN8nApiConfig() {
|
export function getN8nApiConfig() {
|
||||||
|
// Load environment variables on first access
|
||||||
|
if (!envLoaded) {
|
||||||
|
dotenv.config();
|
||||||
|
envLoaded = true;
|
||||||
|
}
|
||||||
|
|
||||||
const result = n8nApiConfigSchema.safeParse(process.env);
|
const result = n8nApiConfigSchema.safeParse(process.env);
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
logger.warn('n8n API configuration validation failed:', result.error.format());
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,16 +31,9 @@ export function loadN8nApiConfig() {
|
|||||||
|
|
||||||
// Check if both URL and API key are provided
|
// Check if both URL and API key are provided
|
||||||
if (!config.N8N_API_URL || !config.N8N_API_KEY) {
|
if (!config.N8N_API_URL || !config.N8N_API_KEY) {
|
||||||
logger.info('n8n API not configured. Management tools will be disabled.');
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info('n8n API configured successfully', {
|
|
||||||
url: config.N8N_API_URL,
|
|
||||||
timeout: config.N8N_API_TIMEOUT,
|
|
||||||
maxRetries: config.N8N_API_MAX_RETRIES,
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
baseUrl: config.N8N_API_URL,
|
baseUrl: config.N8N_API_URL,
|
||||||
apiKey: config.N8N_API_KEY,
|
apiKey: config.N8N_API_KEY,
|
||||||
@@ -44,13 +42,11 @@ export function loadN8nApiConfig() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export the configuration (null if not configured)
|
// Helper to check if n8n API is configured (lazy check)
|
||||||
export const n8nApiConfig = loadN8nApiConfig();
|
|
||||||
|
|
||||||
// Helper to check if n8n API is configured
|
|
||||||
export function isN8nApiConfigured(): boolean {
|
export function isN8nApiConfigured(): boolean {
|
||||||
return n8nApiConfig !== null;
|
const config = getN8nApiConfig();
|
||||||
|
return config !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type export
|
// Type export
|
||||||
export type N8nApiConfig = NonNullable<ReturnType<typeof loadN8nApiConfig>>;
|
export type N8nApiConfig = NonNullable<ReturnType<typeof getN8nApiConfig>>;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { N8nApiClient } from '../services/n8n-api-client';
|
import { N8nApiClient } from '../services/n8n-api-client';
|
||||||
import { n8nApiConfig } from '../config/n8n-api';
|
import { getN8nApiConfig } from '../config/n8n-api';
|
||||||
import {
|
import {
|
||||||
Workflow,
|
Workflow,
|
||||||
WorkflowNode,
|
WorkflowNode,
|
||||||
@@ -26,15 +26,26 @@ import { NodeRepository } from '../database/node-repository';
|
|||||||
|
|
||||||
// Singleton n8n API client instance
|
// Singleton n8n API client instance
|
||||||
let apiClient: N8nApiClient | null = null;
|
let apiClient: N8nApiClient | null = null;
|
||||||
|
let lastConfigUrl: string | null = null;
|
||||||
|
|
||||||
// Get or create API client
|
// Get or create API client (with lazy config loading)
|
||||||
export function getN8nApiClient(): N8nApiClient | null {
|
export function getN8nApiClient(): N8nApiClient | null {
|
||||||
if (!n8nApiConfig) {
|
const config = getN8nApiConfig();
|
||||||
|
|
||||||
|
if (!config) {
|
||||||
|
if (apiClient) {
|
||||||
|
logger.info('n8n API configuration removed, clearing client');
|
||||||
|
apiClient = null;
|
||||||
|
lastConfigUrl = null;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!apiClient) {
|
// Check if config has changed
|
||||||
apiClient = new N8nApiClient(n8nApiConfig);
|
if (!apiClient || lastConfigUrl !== config.baseUrl) {
|
||||||
|
logger.info('n8n API client initialized', { url: config.baseUrl });
|
||||||
|
apiClient = new N8nApiClient(config);
|
||||||
|
lastConfigUrl = config.baseUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return apiClient;
|
return apiClient;
|
||||||
@@ -754,7 +765,7 @@ export async function handleHealthCheck(): Promise<McpToolResponse> {
|
|||||||
instanceId: health.instanceId,
|
instanceId: health.instanceId,
|
||||||
n8nVersion: health.n8nVersion,
|
n8nVersion: health.n8nVersion,
|
||||||
features: health.features,
|
features: health.features,
|
||||||
apiUrl: n8nApiConfig?.baseUrl
|
apiUrl: getN8nApiConfig()?.baseUrl
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -764,7 +775,7 @@ export async function handleHealthCheck(): Promise<McpToolResponse> {
|
|||||||
error: getUserFriendlyErrorMessage(error),
|
error: getUserFriendlyErrorMessage(error),
|
||||||
code: error.code,
|
code: error.code,
|
||||||
details: {
|
details: {
|
||||||
apiUrl: n8nApiConfig?.baseUrl,
|
apiUrl: getN8nApiConfig()?.baseUrl,
|
||||||
hint: 'Check if n8n is running and API is enabled'
|
hint: 'Check if n8n is running and API is enabled'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -811,17 +822,18 @@ export async function handleListAvailableTools(): Promise<McpToolResponse> {
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const apiConfigured = n8nApiConfig !== null;
|
const config = getN8nApiConfig();
|
||||||
|
const apiConfigured = config !== null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: {
|
data: {
|
||||||
tools,
|
tools,
|
||||||
apiConfigured,
|
apiConfigured,
|
||||||
configuration: apiConfigured ? {
|
configuration: config ? {
|
||||||
apiUrl: n8nApiConfig!.baseUrl,
|
apiUrl: config.baseUrl,
|
||||||
timeout: n8nApiConfig!.timeout,
|
timeout: config.timeout,
|
||||||
maxRetries: n8nApiConfig!.maxRetries
|
maxRetries: config.maxRetries
|
||||||
} : null,
|
} : null,
|
||||||
limitations: [
|
limitations: [
|
||||||
'Cannot activate/deactivate workflows via API',
|
'Cannot activate/deactivate workflows via API',
|
||||||
@@ -846,7 +858,8 @@ export async function handleDiagnostic(request: any): Promise<McpToolResponse> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Check API configuration
|
// Check API configuration
|
||||||
const apiConfigured = n8nApiConfig !== null;
|
const apiConfig = getN8nApiConfig();
|
||||||
|
const apiConfigured = apiConfig !== null;
|
||||||
const apiClient = getN8nApiClient();
|
const apiClient = getN8nApiClient();
|
||||||
|
|
||||||
// Test API connectivity if configured
|
// Test API connectivity if configured
|
||||||
@@ -879,10 +892,10 @@ export async function handleDiagnostic(request: any): Promise<McpToolResponse> {
|
|||||||
apiConfiguration: {
|
apiConfiguration: {
|
||||||
configured: apiConfigured,
|
configured: apiConfigured,
|
||||||
status: apiStatus,
|
status: apiStatus,
|
||||||
config: apiConfigured && n8nApiConfig ? {
|
config: apiConfig ? {
|
||||||
baseUrl: n8nApiConfig.baseUrl,
|
baseUrl: apiConfig.baseUrl,
|
||||||
timeout: n8nApiConfig.timeout,
|
timeout: apiConfig.timeout,
|
||||||
maxRetries: n8nApiConfig.maxRetries
|
maxRetries: apiConfig.maxRetries
|
||||||
} : null
|
} : null
|
||||||
},
|
},
|
||||||
toolsAvailability: {
|
toolsAvailability: {
|
||||||
|
|||||||
@@ -40,11 +40,14 @@ const workflowDiffSchema = z.object({
|
|||||||
|
|
||||||
export async function handleUpdatePartialWorkflow(args: unknown): Promise<McpToolResponse> {
|
export async function handleUpdatePartialWorkflow(args: unknown): Promise<McpToolResponse> {
|
||||||
try {
|
try {
|
||||||
// Debug: Log what Claude Desktop sends
|
// Debug logging (only in debug mode)
|
||||||
logger.info('[DEBUG] Full args from Claude Desktop:', JSON.stringify(args, null, 2));
|
if (process.env.DEBUG_MCP === 'true') {
|
||||||
logger.info('[DEBUG] Args type:', typeof args);
|
logger.debug('Workflow diff request received', {
|
||||||
if (args && typeof args === 'object') {
|
argsType: typeof args,
|
||||||
logger.info('[DEBUG] Args keys:', Object.keys(args as any));
|
hasWorkflowId: args && typeof args === 'object' && 'workflowId' in args,
|
||||||
|
operationCount: args && typeof args === 'object' && 'operations' in args ?
|
||||||
|
(args as any).operations?.length : 0
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate input
|
// Validate input
|
||||||
|
|||||||
@@ -78,6 +78,14 @@ export class N8NDocumentationMCPServer {
|
|||||||
|
|
||||||
logger.info('Initializing n8n Documentation MCP server');
|
logger.info('Initializing n8n Documentation MCP server');
|
||||||
|
|
||||||
|
// Log n8n API configuration status at startup
|
||||||
|
const apiConfigured = isN8nApiConfigured();
|
||||||
|
const totalTools = apiConfigured ?
|
||||||
|
n8nDocumentationToolsFinal.length + n8nManagementTools.length :
|
||||||
|
n8nDocumentationToolsFinal.length;
|
||||||
|
|
||||||
|
logger.info(`MCP server initialized with ${totalTools} tools (n8n API: ${apiConfigured ? 'configured' : 'not configured'})`);
|
||||||
|
|
||||||
this.server = new Server(
|
this.server = new Server(
|
||||||
{
|
{
|
||||||
name: 'n8n-documentation-mcp',
|
name: 'n8n-documentation-mcp',
|
||||||
@@ -126,9 +134,9 @@ export class N8NDocumentationMCPServer {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// Debug: Log to stderr to see if handler is called
|
// Debug logging
|
||||||
if (process.env.DEBUG_MCP === 'true') {
|
if (process.env.DEBUG_MCP === 'true') {
|
||||||
console.error('Initialize handler called, returning:', JSON.stringify(response));
|
logger.debug('Initialize handler called', { response });
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
@@ -138,12 +146,13 @@ export class N8NDocumentationMCPServer {
|
|||||||
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
|
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||||
// Combine documentation tools with management tools if API is configured
|
// Combine documentation tools with management tools if API is configured
|
||||||
const tools = [...n8nDocumentationToolsFinal];
|
const tools = [...n8nDocumentationToolsFinal];
|
||||||
|
const isConfigured = isN8nApiConfigured();
|
||||||
|
|
||||||
if (isN8nApiConfigured()) {
|
if (isConfigured) {
|
||||||
tools.push(...n8nManagementTools);
|
tools.push(...n8nManagementTools);
|
||||||
logger.info('n8n management tools enabled');
|
logger.debug(`Tool listing: ${tools.length} tools available (${n8nDocumentationToolsFinal.length} documentation + ${n8nManagementTools.length} management)`);
|
||||||
} else {
|
} else {
|
||||||
logger.info('n8n management tools disabled (API not configured)');
|
logger.debug(`Tool listing: ${tools.length} tools available (documentation only)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { tools };
|
return { tools };
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { config } from 'dotenv';
|
import { config } from 'dotenv';
|
||||||
import { logger } from '../utils/logger';
|
import { logger } from '../utils/logger';
|
||||||
import { isN8nApiConfigured, n8nApiConfig } from '../config/n8n-api';
|
import { isN8nApiConfigured, getN8nApiConfig } from '../config/n8n-api';
|
||||||
import { getN8nApiClient } from '../mcp/handlers-n8n-manager';
|
import { getN8nApiClient } from '../mcp/handlers-n8n-manager';
|
||||||
import { N8nApiClient } from '../services/n8n-api-client';
|
import { N8nApiClient } from '../services/n8n-api-client';
|
||||||
import { Workflow, ExecutionStatus } from '../types/n8n-api';
|
import { Workflow, ExecutionStatus } from '../types/n8n-api';
|
||||||
@@ -21,10 +21,11 @@ async function testN8nManagerIntegration() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const apiConfig = getN8nApiConfig();
|
||||||
logger.info('n8n API Configuration:', {
|
logger.info('n8n API Configuration:', {
|
||||||
url: n8nApiConfig!.baseUrl,
|
url: apiConfig!.baseUrl,
|
||||||
timeout: n8nApiConfig!.timeout,
|
timeout: apiConfig!.timeout,
|
||||||
maxRetries: n8nApiConfig!.maxRetries
|
maxRetries: apiConfig!.maxRetries
|
||||||
});
|
});
|
||||||
|
|
||||||
const client = getN8nApiClient();
|
const client = getN8nApiClient();
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ export class Logger {
|
|||||||
private static instance: Logger;
|
private static instance: Logger;
|
||||||
private useFileLogging = false;
|
private useFileLogging = false;
|
||||||
private fileStream: any = null;
|
private fileStream: any = null;
|
||||||
|
// Cache environment variables for performance
|
||||||
|
private readonly isStdio = process.env.MCP_MODE === 'stdio';
|
||||||
|
private readonly isDisabled = process.env.DISABLE_CONSOLE_OUTPUT === 'true';
|
||||||
|
private readonly isHttp = process.env.MCP_MODE === 'http';
|
||||||
|
|
||||||
constructor(config?: Partial<LoggerConfig>) {
|
constructor(config?: Partial<LoggerConfig>) {
|
||||||
this.config = {
|
this.config = {
|
||||||
@@ -53,10 +57,7 @@ export class Logger {
|
|||||||
private log(level: LogLevel, levelName: string, message: string, ...args: any[]): void {
|
private log(level: LogLevel, levelName: string, message: string, ...args: any[]): void {
|
||||||
// Check environment variables FIRST, before level check
|
// Check environment variables FIRST, before level check
|
||||||
// In stdio mode, suppress ALL console output to avoid corrupting JSON-RPC
|
// In stdio mode, suppress ALL console output to avoid corrupting JSON-RPC
|
||||||
const isStdio = process.env.MCP_MODE === 'stdio';
|
if (this.isStdio || this.isDisabled) {
|
||||||
const isDisabled = process.env.DISABLE_CONSOLE_OUTPUT === 'true';
|
|
||||||
|
|
||||||
if (isStdio || isDisabled) {
|
|
||||||
// Silently drop all logs in stdio mode
|
// Silently drop all logs in stdio mode
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -66,7 +67,7 @@ export class Logger {
|
|||||||
|
|
||||||
// In HTTP mode during request handling, suppress console output
|
// In HTTP mode during request handling, suppress console output
|
||||||
// The ConsoleManager will handle this, but we add a safety check
|
// The ConsoleManager will handle this, but we add a safety check
|
||||||
if (process.env.MCP_MODE === 'http' && process.env.MCP_REQUEST_ACTIVE === 'true') {
|
if (this.isHttp && process.env.MCP_REQUEST_ACTIVE === 'true') {
|
||||||
// Silently drop the log during active MCP requests
|
// Silently drop the log during active MCP requests
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user