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:
czlonkowski
2025-07-01 10:41:09 +02:00
parent d291709774
commit 71385cc7ef
7 changed files with 78 additions and 55 deletions

View File

@@ -1,5 +1,5 @@
import { N8nApiClient } from '../services/n8n-api-client';
import { n8nApiConfig } from '../config/n8n-api';
import { getN8nApiConfig } from '../config/n8n-api';
import {
Workflow,
WorkflowNode,
@@ -26,15 +26,26 @@ import { NodeRepository } from '../database/node-repository';
// Singleton n8n API client instance
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 {
if (!n8nApiConfig) {
const config = getN8nApiConfig();
if (!config) {
if (apiClient) {
logger.info('n8n API configuration removed, clearing client');
apiClient = null;
lastConfigUrl = null;
}
return null;
}
if (!apiClient) {
apiClient = new N8nApiClient(n8nApiConfig);
// Check if config has changed
if (!apiClient || lastConfigUrl !== config.baseUrl) {
logger.info('n8n API client initialized', { url: config.baseUrl });
apiClient = new N8nApiClient(config);
lastConfigUrl = config.baseUrl;
}
return apiClient;
@@ -754,7 +765,7 @@ export async function handleHealthCheck(): Promise<McpToolResponse> {
instanceId: health.instanceId,
n8nVersion: health.n8nVersion,
features: health.features,
apiUrl: n8nApiConfig?.baseUrl
apiUrl: getN8nApiConfig()?.baseUrl
}
};
} catch (error) {
@@ -764,7 +775,7 @@ export async function handleHealthCheck(): Promise<McpToolResponse> {
error: getUserFriendlyErrorMessage(error),
code: error.code,
details: {
apiUrl: n8nApiConfig?.baseUrl,
apiUrl: getN8nApiConfig()?.baseUrl,
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 {
success: true,
data: {
tools,
apiConfigured,
configuration: apiConfigured ? {
apiUrl: n8nApiConfig!.baseUrl,
timeout: n8nApiConfig!.timeout,
maxRetries: n8nApiConfig!.maxRetries
configuration: config ? {
apiUrl: config.baseUrl,
timeout: config.timeout,
maxRetries: config.maxRetries
} : null,
limitations: [
'Cannot activate/deactivate workflows via API',
@@ -846,7 +858,8 @@ export async function handleDiagnostic(request: any): Promise<McpToolResponse> {
};
// Check API configuration
const apiConfigured = n8nApiConfig !== null;
const apiConfig = getN8nApiConfig();
const apiConfigured = apiConfig !== null;
const apiClient = getN8nApiClient();
// Test API connectivity if configured
@@ -879,10 +892,10 @@ export async function handleDiagnostic(request: any): Promise<McpToolResponse> {
apiConfiguration: {
configured: apiConfigured,
status: apiStatus,
config: apiConfigured && n8nApiConfig ? {
baseUrl: n8nApiConfig.baseUrl,
timeout: n8nApiConfig.timeout,
maxRetries: n8nApiConfig.maxRetries
config: apiConfig ? {
baseUrl: apiConfig.baseUrl,
timeout: apiConfig.timeout,
maxRetries: apiConfig.maxRetries
} : null
},
toolsAvailability: {

View File

@@ -40,11 +40,14 @@ const workflowDiffSchema = z.object({
export async function handleUpdatePartialWorkflow(args: unknown): Promise<McpToolResponse> {
try {
// Debug: Log what Claude Desktop sends
logger.info('[DEBUG] Full args from Claude Desktop:', JSON.stringify(args, null, 2));
logger.info('[DEBUG] Args type:', typeof args);
if (args && typeof args === 'object') {
logger.info('[DEBUG] Args keys:', Object.keys(args as any));
// Debug logging (only in debug mode)
if (process.env.DEBUG_MCP === 'true') {
logger.debug('Workflow diff request received', {
argsType: typeof args,
hasWorkflowId: args && typeof args === 'object' && 'workflowId' in args,
operationCount: args && typeof args === 'object' && 'operations' in args ?
(args as any).operations?.length : 0
});
}
// Validate input

View File

@@ -78,6 +78,14 @@ export class N8NDocumentationMCPServer {
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(
{
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') {
console.error('Initialize handler called, returning:', JSON.stringify(response));
logger.debug('Initialize handler called', { response });
}
return response;
@@ -138,12 +146,13 @@ export class N8NDocumentationMCPServer {
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
// Combine documentation tools with management tools if API is configured
const tools = [...n8nDocumentationToolsFinal];
const isConfigured = isN8nApiConfigured();
if (isN8nApiConfigured()) {
if (isConfigured) {
tools.push(...n8nManagementTools);
logger.info('n8n management tools enabled');
logger.debug(`Tool listing: ${tools.length} tools available (${n8nDocumentationToolsFinal.length} documentation + ${n8nManagementTools.length} management)`);
} else {
logger.info('n8n management tools disabled (API not configured)');
logger.debug(`Tool listing: ${tools.length} tools available (documentation only)`);
}
return { tools };