Skip to main content

Core Types

InstanceContext

Configuration context for individual n8n instances in multi-tenant deployments.
interface InstanceContext {
  n8nApiUrl?: string;
  n8nApiKey?: string;
  n8nApiTimeout?: number;
  n8nApiMaxRetries?: number;
  instanceId?: string;
  sessionId?: string;
  metadata?: Record<string, any>;
}
n8nApiUrl
string
n8n instance API URL (e.g., https://n8n.example.com). When provided, overrides the N8N_API_URL environment variable.
n8nApiKey
string
n8n instance API key. When provided, overrides the N8N_API_KEY environment variable.
n8nApiTimeout
number
Request timeout in milliseconds. Defaults to environment variable or 30000ms.
n8nApiMaxRetries
number
Maximum retry attempts for failed requests. Defaults to 3.
instanceId
string
Custom identifier for the n8n instance. Used for logging and session management.
sessionId
string
Session-specific identifier. May differ from MCP session ID in proxy configurations.
metadata
Record<string, any>
Extensible metadata field for custom application data. Can store user IDs, tenant info, etc.
import { InstanceContext } from 'n8n-mcp';

const context: InstanceContext = {
  n8nApiUrl: 'https://n8n.example.com',
  n8nApiKey: 'n8n_api_xxxxx',
  instanceId: 'tenant-123'
};

SessionState

Serializable session state for persistence across container restarts.
interface SessionState {
  sessionId: string;
  metadata: {
    createdAt: string;
    lastAccess: string;
  };
  context: {
    n8nApiUrl: string;
    n8nApiKey: string;
    instanceId?: string;
    sessionId?: string;
    metadata?: Record<string, any>;
  };
}
sessionId
string
required
Unique session identifier (UUID v4 or custom format)
metadata
object
required
Session timing information
createdAt
string
required
ISO 8601 timestamp when session was created
lastAccess
string
required
ISO 8601 timestamp of last session activity
context
object
required
n8n instance configuration
n8nApiUrl
string
required
n8n instance API URL
n8nApiKey
string
required
n8n instance API key (plaintext - encrypt before storage!)
instanceId
string
Instance identifier
sessionId
string
Session-specific ID
metadata
Record<string, any>
Custom metadata
Security: SessionState contains plaintext API keys. Always encrypt before persisting to disk. See Session Management for best practices.
const sessions: SessionState[] = engine.exportSessionState();

// Example output
[
  {
    sessionId: "550e8400-e29b-41d4-a716-446655440000",
    metadata: {
      createdAt: "2024-01-15T10:30:00.000Z",
      lastAccess: "2024-01-15T10:45:00.000Z"
    },
    context: {
      n8nApiUrl: "https://tenant1.n8n.cloud",
      n8nApiKey: "n8n_api_tenant1_xxxxx",
      instanceId: "tenant-123",
      metadata: { userId: "user-456" }
    }
  }
]

Engine Types

EngineOptions

Configuration options for N8NMCPEngine.
interface EngineOptions {
  sessionTimeout?: number;
  logLevel?: 'error' | 'warn' | 'info' | 'debug';
}
sessionTimeout
number
default:30
Session timeout in minutes. Inactive sessions are expired after this period.
logLevel
'error' | 'warn' | 'info' | 'debug'
default:"info"
Logging verbosity level

EngineHealth

Health status returned by healthCheck().
interface EngineHealth {
  status: 'healthy' | 'unhealthy';
  uptime: number;
  sessionActive: boolean;
  memoryUsage: {
    used: number;
    total: number;
    unit: string;
  };
  version: string;
}
status
'healthy' | 'unhealthy'
Overall health status
uptime
number
Server uptime in seconds
sessionActive
boolean
Whether any sessions are active
memoryUsage
object
Memory usage statistics in MB
version
string
n8n-MCP version number

Validation Functions

validateInstanceContext()

Validate and sanitize an InstanceContext object.
function validateInstanceContext(context: InstanceContext): {
  valid: boolean;
  errors?: string[];
}
context
InstanceContext
required
Context object to validate
Returns an object with:
  • valid: true if context is valid, false otherwise
  • errors: Array of validation error messages (if invalid)
import { validateInstanceContext } from 'n8n-mcp';

const context = {
  n8nApiUrl: 'https://n8n.example.com',
  n8nApiKey: 'valid_api_key_here',
  instanceId: 'tenant-123'
};

const result = validateInstanceContext(context);
console.log(result);
// { valid: true }

isInstanceContext()

Type guard to check if an object is a valid InstanceContext.
function isInstanceContext(obj: any): obj is InstanceContext
import { isInstanceContext } from 'n8n-mcp';

function processContext(obj: unknown) {
  if (isInstanceContext(obj)) {
    // TypeScript knows obj is InstanceContext here
    console.log(`Instance: ${obj.instanceId}`);
    console.log(`URL: ${obj.n8nApiUrl}`);
  } else {
    throw new Error('Invalid context object');
  }
}

MCP SDK Re-exports

For convenience, n8n-mcp re-exports commonly used types from the MCP SDK:
import {
  Tool,
  CallToolResult,
  ListToolsResult
} from 'n8n-mcp';
These are equivalent to importing from @modelcontextprotocol/sdk/types.js.

Utility Classes

ConsoleManager

Utility for managing console output during HTTP request handling.
import { ConsoleManager } from 'n8n-mcp';

const consoleManager = new ConsoleManager();

// Silence console during operation
await consoleManager.wrapOperation(async () => {
  // Console output is suppressed here
  console.log('This will not appear');
});
ConsoleManager is primarily used internally by the HTTP server to prevent console output from interfering with MCP protocol streams. Most library users won’t need to use it directly.

UI Types

Types for n8n-MCP UI app configurations:
import { UIAppConfig, UIMetadata, UI_APP_CONFIGS } from 'n8n-mcp';
UIAppConfig
interface
Configuration for UI apps like Flowise and Dify
UIMetadata
interface
Metadata about UI app configurations
UI_APP_CONFIGS
Record<string, UIAppConfig>
Registry of pre-configured UI app settings

Build docs developers (and LLMs) love