feat: Complete overhaul to enhanced documentation-only MCP server

- Removed all workflow execution capabilities per user requirements
- Implemented enhanced documentation extraction with operations and API mappings
- Fixed credential code extraction for all nodes
- Fixed package info extraction (name and version)
- Enhanced operations parser to handle n8n markdown format
- Fixed documentation search to prioritize app nodes over trigger nodes
- Comprehensive test coverage for Slack node extraction
- All node information now includes:
  - Complete operations list (42 for Slack)
  - API method mappings with documentation URLs
  - Source code and credential definitions
  - Package metadata
  - Related resources and templates

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-06-08 11:07:51 +00:00
parent 887e98ca0b
commit 3d7fdeba02
48 changed files with 9247 additions and 11057 deletions

View File

@@ -1,49 +0,0 @@
import { Request, Response, NextFunction } from 'express';
import { logger } from './logger';
/**
* Express middleware for authenticating requests with Bearer tokens
*/
export function authenticateRequest(authToken?: string) {
return (req: Request, res: Response, next: NextFunction): void => {
if (!authToken) {
// No auth required
return next();
}
const authHeader = req.headers['authorization'];
if (!authHeader) {
logger.warn('Missing authorization header', {
ip: req.ip,
path: req.path,
});
res.status(401).json({
error: 'Unauthorized',
message: 'Missing authorization header',
});
return;
}
// Support both "Bearer TOKEN" and just "TOKEN" formats
const providedToken = authHeader.startsWith('Bearer ')
? authHeader.substring(7)
: authHeader;
if (providedToken !== authToken) {
logger.warn('Invalid authentication token', {
ip: req.ip,
path: req.path,
});
res.status(401).json({
error: 'Unauthorized',
message: 'Invalid authentication token',
});
return;
}
next();
};
}

View File

@@ -1,241 +1,2 @@
import { promises as fs } from 'fs';
import path from 'path';
import { logger } from './logger';
import { execSync } from 'child_process';
interface NodeDocumentation {
markdown: string;
url: string;
examples?: any[];
}
export class DocumentationFetcher {
private docsPath: string;
private docsRepoUrl = 'https://github.com/n8n-io/n8n-docs.git';
private cloned = false;
constructor(docsPath?: string) {
this.docsPath = docsPath || path.join(process.cwd(), 'temp', 'n8n-docs');
}
/**
* Clone or update the n8n-docs repository
*/
async ensureDocsRepository(): Promise<void> {
try {
// Check if directory exists
const exists = await fs.access(this.docsPath).then(() => true).catch(() => false);
if (!exists) {
logger.info('Cloning n8n-docs repository...');
await fs.mkdir(path.dirname(this.docsPath), { recursive: true });
execSync(`git clone --depth 1 ${this.docsRepoUrl} ${this.docsPath}`, {
stdio: 'pipe'
});
logger.info('n8n-docs repository cloned successfully');
} else {
logger.info('Updating n8n-docs repository...');
execSync('git pull --ff-only', {
cwd: this.docsPath,
stdio: 'pipe'
});
logger.info('n8n-docs repository updated');
}
this.cloned = true;
} catch (error) {
logger.error('Failed to clone/update n8n-docs repository:', error);
throw error;
}
}
/**
* Get documentation for a specific node
*/
async getNodeDocumentation(nodeType: string): Promise<NodeDocumentation | null> {
if (!this.cloned) {
await this.ensureDocsRepository();
}
try {
// Convert node type to documentation path
// e.g., "n8n-nodes-base.if" -> "if"
const nodeName = this.extractNodeName(nodeType);
// Common documentation paths to check
const possiblePaths = [
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'core-nodes', `${nodeName}.md`),
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'app-nodes', `${nodeName}.md`),
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'trigger-nodes', `${nodeName}.md`),
path.join(this.docsPath, 'docs', 'code-examples', 'expressions', `${nodeName}.md`),
// Generic search in docs folder
path.join(this.docsPath, 'docs', '**', `${nodeName}.md`)
];
for (const docPath of possiblePaths) {
try {
const content = await fs.readFile(docPath, 'utf-8');
const url = this.generateDocUrl(docPath);
return {
markdown: content,
url,
examples: this.extractExamples(content)
};
} catch (error) {
// Continue to next path
continue;
}
}
// If no exact match, try to find by searching
const foundPath = await this.searchForNodeDoc(nodeName);
if (foundPath) {
const content = await fs.readFile(foundPath, 'utf-8');
return {
markdown: content,
url: this.generateDocUrl(foundPath),
examples: this.extractExamples(content)
};
}
logger.warn(`No documentation found for node: ${nodeType}`);
return null;
} catch (error) {
logger.error(`Failed to get documentation for ${nodeType}:`, error);
return null;
}
}
/**
* Extract node name from node type
*/
private extractNodeName(nodeType: string): string {
// Handle different node type formats
// "n8n-nodes-base.if" -> "if"
// "@n8n/n8n-nodes-langchain.Agent" -> "agent"
const parts = nodeType.split('.');
const name = parts[parts.length - 1];
return name.toLowerCase();
}
/**
* Search for node documentation file
*/
private async searchForNodeDoc(nodeName: string): Promise<string | null> {
try {
const result = execSync(
`find ${this.docsPath}/docs -name "*.md" -type f | grep -i "${nodeName}" | head -1`,
{ encoding: 'utf-8', stdio: 'pipe' }
).trim();
return result || null;
} catch (error) {
return null;
}
}
/**
* Generate documentation URL from file path
*/
private generateDocUrl(filePath: string): string {
const relativePath = path.relative(this.docsPath, filePath);
const urlPath = relativePath
.replace(/^docs\//, '')
.replace(/\.md$/, '')
.replace(/\\/g, '/');
return `https://docs.n8n.io/${urlPath}`;
}
/**
* Extract code examples from markdown content
*/
private extractExamples(markdown: string): any[] {
const examples: any[] = [];
// Extract JSON code blocks
const jsonCodeBlockRegex = /```json\n([\s\S]*?)```/g;
let match;
while ((match = jsonCodeBlockRegex.exec(markdown)) !== null) {
try {
const json = JSON.parse(match[1]);
examples.push(json);
} catch (error) {
// Not valid JSON, skip
}
}
// Extract workflow examples
const workflowExampleRegex = /## Example.*?\n([\s\S]*?)(?=\n##|\n#|$)/gi;
while ((match = workflowExampleRegex.exec(markdown)) !== null) {
const exampleText = match[1];
// Try to find JSON in the example section
const jsonMatch = exampleText.match(/```json\n([\s\S]*?)```/);
if (jsonMatch) {
try {
const json = JSON.parse(jsonMatch[1]);
examples.push(json);
} catch (error) {
// Not valid JSON
}
}
}
return examples;
}
/**
* Get all available documentation files
*/
async getAllDocumentationFiles(): Promise<Map<string, string>> {
if (!this.cloned) {
await this.ensureDocsRepository();
}
const docMap = new Map<string, string>();
try {
const findDocs = execSync(
`find ${this.docsPath}/docs -name "*.md" -type f | grep -E "(core-nodes|app-nodes|trigger-nodes)/"`,
{ encoding: 'utf-8', stdio: 'pipe' }
).trim().split('\n');
for (const docPath of findDocs) {
if (!docPath) continue;
const filename = path.basename(docPath, '.md');
const content = await fs.readFile(docPath, 'utf-8');
// Try to extract the node type from the content
const nodeTypeMatch = content.match(/node[_-]?type[:\s]+["']?([^"'\s]+)["']?/i);
if (nodeTypeMatch) {
docMap.set(nodeTypeMatch[1], docPath);
} else {
// Use filename as fallback
docMap.set(filename, docPath);
}
}
logger.info(`Found ${docMap.size} documentation files`);
return docMap;
} catch (error) {
logger.error('Failed to get documentation files:', error);
return docMap;
}
}
/**
* Clean up cloned repository
*/
async cleanup(): Promise<void> {
try {
await fs.rm(this.docsPath, { recursive: true, force: true });
this.cloned = false;
logger.info('Cleaned up documentation repository');
} catch (error) {
logger.error('Failed to cleanup docs repository:', error);
}
}
}
// Re-export everything from enhanced-documentation-fetcher
export * from './enhanced-documentation-fetcher';

View File

@@ -0,0 +1,621 @@
import { promises as fs } from 'fs';
import path from 'path';
import { logger } from './logger';
import { execSync } from 'child_process';
// Enhanced documentation structure with rich content
export interface EnhancedNodeDocumentation {
markdown: string;
url: string;
title?: string;
description?: string;
operations?: OperationInfo[];
apiMethods?: ApiMethodMapping[];
examples?: CodeExample[];
templates?: TemplateInfo[];
relatedResources?: RelatedResource[];
requiredScopes?: string[];
metadata?: DocumentationMetadata;
}
export interface OperationInfo {
resource: string;
operation: string;
description: string;
subOperations?: string[];
}
export interface ApiMethodMapping {
resource: string;
operation: string;
apiMethod: string;
apiUrl: string;
}
export interface CodeExample {
title?: string;
description?: string;
type: 'json' | 'javascript' | 'yaml' | 'text';
code: string;
language?: string;
}
export interface TemplateInfo {
name: string;
description?: string;
url?: string;
}
export interface RelatedResource {
title: string;
url: string;
type: 'documentation' | 'api' | 'tutorial' | 'external';
}
export interface DocumentationMetadata {
contentType?: string[];
priority?: string;
tags?: string[];
lastUpdated?: Date;
}
export class EnhancedDocumentationFetcher {
private docsPath: string;
private docsRepoUrl = 'https://github.com/n8n-io/n8n-docs.git';
private cloned = false;
constructor(docsPath?: string) {
this.docsPath = docsPath || path.join(process.cwd(), 'temp', 'n8n-docs');
}
/**
* Clone or update the n8n-docs repository
*/
async ensureDocsRepository(): Promise<void> {
try {
const exists = await fs.access(this.docsPath).then(() => true).catch(() => false);
if (!exists) {
logger.info('Cloning n8n-docs repository...');
await fs.mkdir(path.dirname(this.docsPath), { recursive: true });
execSync(`git clone --depth 1 ${this.docsRepoUrl} ${this.docsPath}`, {
stdio: 'pipe'
});
logger.info('n8n-docs repository cloned successfully');
} else {
logger.info('Updating n8n-docs repository...');
execSync('git pull --ff-only', {
cwd: this.docsPath,
stdio: 'pipe'
});
logger.info('n8n-docs repository updated');
}
this.cloned = true;
} catch (error) {
logger.error('Failed to clone/update n8n-docs repository:', error);
throw error;
}
}
/**
* Get enhanced documentation for a specific node
*/
async getEnhancedNodeDocumentation(nodeType: string): Promise<EnhancedNodeDocumentation | null> {
if (!this.cloned) {
await this.ensureDocsRepository();
}
try {
const nodeName = this.extractNodeName(nodeType);
// Common documentation paths to check
const possiblePaths = [
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'app-nodes', `${nodeType}.md`),
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'core-nodes', `${nodeType}.md`),
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'trigger-nodes', `${nodeType}.md`),
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'core-nodes', `${nodeName}.md`),
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'app-nodes', `${nodeName}.md`),
path.join(this.docsPath, 'docs', 'integrations', 'builtin', 'trigger-nodes', `${nodeName}.md`),
];
for (const docPath of possiblePaths) {
try {
const content = await fs.readFile(docPath, 'utf-8');
logger.debug(`Checking doc path: ${docPath}`);
// Skip credential documentation files
if (this.isCredentialDoc(docPath, content)) {
logger.debug(`Skipping credential doc: ${docPath}`);
continue;
}
logger.info(`Found documentation for ${nodeType} at: ${docPath}`);
return this.parseEnhancedDocumentation(content, docPath);
} catch (error) {
// File doesn't exist, continue
continue;
}
}
// If no exact match, try to find by searching
logger.debug(`No exact match found, searching for ${nodeType}...`);
const foundPath = await this.searchForNodeDoc(nodeType);
if (foundPath) {
logger.info(`Found documentation via search at: ${foundPath}`);
const content = await fs.readFile(foundPath, 'utf-8');
if (!this.isCredentialDoc(foundPath, content)) {
return this.parseEnhancedDocumentation(content, foundPath);
}
}
logger.warn(`No documentation found for node: ${nodeType}`);
return null;
} catch (error) {
logger.error(`Failed to get documentation for ${nodeType}:`, error);
return null;
}
}
/**
* Parse markdown content into enhanced documentation structure
*/
private parseEnhancedDocumentation(markdown: string, filePath: string): EnhancedNodeDocumentation {
const doc: EnhancedNodeDocumentation = {
markdown,
url: this.generateDocUrl(filePath),
};
// Extract frontmatter metadata
const metadata = this.extractFrontmatter(markdown);
if (metadata) {
doc.metadata = metadata;
doc.title = metadata.title;
doc.description = metadata.description;
}
// Extract title and description from content if not in frontmatter
if (!doc.title) {
doc.title = this.extractTitle(markdown);
}
if (!doc.description) {
doc.description = this.extractDescription(markdown);
}
// Extract operations
doc.operations = this.extractOperations(markdown);
// Extract API method mappings
doc.apiMethods = this.extractApiMethods(markdown);
// Extract code examples
doc.examples = this.extractCodeExamples(markdown);
// Extract templates
doc.templates = this.extractTemplates(markdown);
// Extract related resources
doc.relatedResources = this.extractRelatedResources(markdown);
// Extract required scopes
doc.requiredScopes = this.extractRequiredScopes(markdown);
return doc;
}
/**
* Extract frontmatter metadata
*/
private extractFrontmatter(markdown: string): any {
const frontmatterMatch = markdown.match(/^---\n([\s\S]*?)\n---/);
if (!frontmatterMatch) return null;
const frontmatter: any = {};
const lines = frontmatterMatch[1].split('\n');
for (const line of lines) {
if (line.includes(':')) {
const [key, ...valueParts] = line.split(':');
const value = valueParts.join(':').trim();
// Parse arrays
if (value.startsWith('[') && value.endsWith(']')) {
frontmatter[key.trim()] = value
.slice(1, -1)
.split(',')
.map(v => v.trim());
} else {
frontmatter[key.trim()] = value;
}
}
}
return frontmatter;
}
/**
* Extract title from markdown
*/
private extractTitle(markdown: string): string | undefined {
const match = markdown.match(/^#\s+(.+)$/m);
return match ? match[1].trim() : undefined;
}
/**
* Extract description from markdown
*/
private extractDescription(markdown: string): string | undefined {
// Remove frontmatter
const content = markdown.replace(/^---[\s\S]*?---\n/, '');
// Find first paragraph after title
const lines = content.split('\n');
let foundTitle = false;
let description = '';
for (const line of lines) {
if (line.startsWith('#')) {
foundTitle = true;
continue;
}
if (foundTitle && line.trim() && !line.startsWith('#') && !line.startsWith('*') && !line.startsWith('-')) {
description = line.trim();
break;
}
}
return description || undefined;
}
/**
* Extract operations from markdown
*/
private extractOperations(markdown: string): OperationInfo[] {
const operations: OperationInfo[] = [];
// Find operations section
const operationsMatch = markdown.match(/##\s+Operations\s*\n([\s\S]*?)(?=\n##|\n#|$)/i);
if (!operationsMatch) return operations;
const operationsText = operationsMatch[1];
// Parse operation structure - handle nested bullet points
let currentResource: string | null = null;
const lines = operationsText.split('\n');
for (const line of lines) {
const trimmedLine = line.trim();
// Skip empty lines
if (!trimmedLine) continue;
// Resource level - non-indented bullet with bold text (e.g., "* **Channel**")
if (line.match(/^\*\s+\*\*[^*]+\*\*\s*$/) && !line.match(/^\s+/)) {
const match = trimmedLine.match(/^\*\s+\*\*([^*]+)\*\*/);
if (match) {
currentResource = match[1].trim();
}
continue;
}
// Skip if we don't have a current resource
if (!currentResource) continue;
// Operation level - indented bullets (any whitespace + *)
if (line.match(/^\s+\*\s+/) && currentResource) {
// Extract operation name and description
const operationMatch = trimmedLine.match(/^\*\s+\*\*([^*]+)\*\*(.*)$/);
if (operationMatch) {
const operation = operationMatch[1].trim();
let description = operationMatch[2].trim();
// Clean up description
description = description.replace(/^:\s*/, '').replace(/\.$/, '').trim();
operations.push({
resource: currentResource,
operation,
description: description || operation,
});
} else {
// Handle operations without bold formatting or with different format
const simpleMatch = trimmedLine.match(/^\*\s+(.+)$/);
if (simpleMatch) {
const text = simpleMatch[1].trim();
// Split by colon to separate operation from description
const colonIndex = text.indexOf(':');
if (colonIndex > 0) {
operations.push({
resource: currentResource,
operation: text.substring(0, colonIndex).trim(),
description: text.substring(colonIndex + 1).trim() || text,
});
} else {
operations.push({
resource: currentResource,
operation: text,
description: text,
});
}
}
}
}
}
return operations;
}
/**
* Extract API method mappings from markdown tables
*/
private extractApiMethods(markdown: string): ApiMethodMapping[] {
const apiMethods: ApiMethodMapping[] = [];
// Find API method tables
const tableRegex = /\|.*Resource.*\|.*Operation.*\|.*(?:Slack API method|API method|Method).*\|[\s\S]*?\n(?=\n[^|]|$)/gi;
const tables = markdown.match(tableRegex);
if (!tables) return apiMethods;
for (const table of tables) {
const rows = table.split('\n').filter(row => row.trim() && !row.includes('---'));
// Skip header row
for (let i = 1; i < rows.length; i++) {
const cells = rows[i].split('|').map(cell => cell.trim()).filter(Boolean);
if (cells.length >= 3) {
const resource = cells[0];
const operation = cells[1];
const apiMethodCell = cells[2];
// Extract API method and URL from markdown link
const linkMatch = apiMethodCell.match(/\[([^\]]+)\]\(([^)]+)\)/);
if (linkMatch) {
apiMethods.push({
resource,
operation,
apiMethod: linkMatch[1],
apiUrl: linkMatch[2],
});
} else {
apiMethods.push({
resource,
operation,
apiMethod: apiMethodCell,
apiUrl: '',
});
}
}
}
}
return apiMethods;
}
/**
* Extract code examples from markdown
*/
private extractCodeExamples(markdown: string): CodeExample[] {
const examples: CodeExample[] = [];
// Extract all code blocks with language
const codeBlockRegex = /```(\w+)?\n([\s\S]*?)```/g;
let match;
while ((match = codeBlockRegex.exec(markdown)) !== null) {
const language = match[1] || 'text';
const code = match[2].trim();
// Look for title or description before the code block
const beforeCodeIndex = match.index;
const beforeText = markdown.substring(Math.max(0, beforeCodeIndex - 200), beforeCodeIndex);
const titleMatch = beforeText.match(/(?:###|####)\s+(.+)$/m);
const example: CodeExample = {
type: this.mapLanguageToType(language),
language,
code,
};
if (titleMatch) {
example.title = titleMatch[1].trim();
}
// Try to parse JSON examples
if (language === 'json') {
try {
JSON.parse(code);
examples.push(example);
} catch (e) {
// Skip invalid JSON
}
} else {
examples.push(example);
}
}
return examples;
}
/**
* Extract template information
*/
private extractTemplates(markdown: string): TemplateInfo[] {
const templates: TemplateInfo[] = [];
// Look for template widget
const templateWidgetMatch = markdown.match(/\[\[\s*templatesWidget\s*\(\s*[^,]+,\s*'([^']+)'\s*\)\s*\]\]/);
if (templateWidgetMatch) {
templates.push({
name: templateWidgetMatch[1],
description: `Templates for ${templateWidgetMatch[1]}`,
});
}
return templates;
}
/**
* Extract related resources
*/
private extractRelatedResources(markdown: string): RelatedResource[] {
const resources: RelatedResource[] = [];
// Find related resources section
const relatedMatch = markdown.match(/##\s+(?:Related resources|Related|Resources)\s*\n([\s\S]*?)(?=\n##|\n#|$)/i);
if (!relatedMatch) return resources;
const relatedText = relatedMatch[1];
// Extract links
const linkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
let match;
while ((match = linkRegex.exec(relatedText)) !== null) {
const title = match[1];
const url = match[2];
// Determine resource type
let type: RelatedResource['type'] = 'external';
if (url.includes('docs.n8n.io') || url.startsWith('/')) {
type = 'documentation';
} else if (url.includes('api.')) {
type = 'api';
}
resources.push({ title, url, type });
}
return resources;
}
/**
* Extract required scopes
*/
private extractRequiredScopes(markdown: string): string[] {
const scopes: string[] = [];
// Find required scopes section
const scopesMatch = markdown.match(/##\s+(?:Required scopes|Scopes)\s*\n([\s\S]*?)(?=\n##|\n#|$)/i);
if (!scopesMatch) return scopes;
const scopesText = scopesMatch[1];
// Extract scope patterns (common formats)
const scopeRegex = /`([a-z:._-]+)`/gi;
let match;
while ((match = scopeRegex.exec(scopesText)) !== null) {
const scope = match[1];
if (scope.includes(':') || scope.includes('.')) {
scopes.push(scope);
}
}
return [...new Set(scopes)]; // Remove duplicates
}
/**
* Map language to code example type
*/
private mapLanguageToType(language: string): CodeExample['type'] {
switch (language.toLowerCase()) {
case 'json':
return 'json';
case 'js':
case 'javascript':
case 'typescript':
case 'ts':
return 'javascript';
case 'yaml':
case 'yml':
return 'yaml';
default:
return 'text';
}
}
/**
* Check if this is a credential documentation
*/
private isCredentialDoc(filePath: string, content: string): boolean {
return filePath.includes('/credentials/') ||
(content.includes('title: ') &&
content.includes(' credentials') &&
!content.includes(' node documentation'));
}
/**
* Extract node name from node type
*/
private extractNodeName(nodeType: string): string {
const parts = nodeType.split('.');
const name = parts[parts.length - 1];
return name.toLowerCase();
}
/**
* Search for node documentation file
*/
private async searchForNodeDoc(nodeType: string): Promise<string | null> {
try {
// First try exact match with nodeType
let result = execSync(
`find ${this.docsPath}/docs/integrations/builtin -name "${nodeType}.md" -type f | grep -v credentials | head -1`,
{ encoding: 'utf-8', stdio: 'pipe' }
).trim();
if (result) return result;
// Try lowercase nodeType
const lowerNodeType = nodeType.toLowerCase();
result = execSync(
`find ${this.docsPath}/docs/integrations/builtin -name "${lowerNodeType}.md" -type f | grep -v credentials | head -1`,
{ encoding: 'utf-8', stdio: 'pipe' }
).trim();
if (result) return result;
// Try node name pattern but exclude trigger nodes
const nodeName = this.extractNodeName(nodeType);
result = execSync(
`find ${this.docsPath}/docs/integrations/builtin -name "*${nodeName}.md" -type f | grep -v credentials | grep -v trigger | head -1`,
{ encoding: 'utf-8', stdio: 'pipe' }
).trim();
return result || null;
} catch (error) {
return null;
}
}
/**
* Generate documentation URL from file path
*/
private generateDocUrl(filePath: string): string {
const relativePath = path.relative(this.docsPath, filePath);
const urlPath = relativePath
.replace(/^docs\//, '')
.replace(/\.md$/, '')
.replace(/\\/g, '/');
return `https://docs.n8n.io/${urlPath}`;
}
/**
* Clean up cloned repository
*/
async cleanup(): Promise<void> {
try {
await fs.rm(this.docsPath, { recursive: true, force: true });
this.cloned = false;
logger.info('Cleaned up documentation repository');
} catch (error) {
logger.error('Failed to cleanup docs repository:', error);
}
}
}

View File

@@ -1,267 +1,140 @@
import { logger } from './logger';
interface NodeExample {
nodes: any[];
connections: any;
pinData?: any;
meta?: any;
}
interface NodeParameter {
name: string;
type: string;
default?: any;
options?: any[];
displayOptions?: any;
}
/**
* Generates example workflows and parameters for n8n nodes
*/
export class ExampleGenerator {
/**
* Generate example workflow for a node
* Generate an example workflow from node definition
*/
static generateNodeExample(nodeType: string, nodeData: any): NodeExample {
const nodeName = this.getNodeName(nodeType);
const nodeId = this.generateNodeId();
static generateFromNodeDefinition(nodeDefinition: any): any {
const nodeName = nodeDefinition.displayName || 'Example Node';
const nodeType = nodeDefinition.name || 'n8n-nodes-base.exampleNode';
// Base example structure
const example: NodeExample = {
nodes: [{
parameters: this.generateExampleParameters(nodeType, nodeData),
type: nodeType,
typeVersion: nodeData.typeVersion || 1,
position: [220, 120],
id: nodeId,
name: nodeName
}],
connections: {
[nodeName]: {
main: [[]]
}
},
pinData: {},
meta: {
templateCredsSetupCompleted: true,
instanceId: this.generateInstanceId()
}
return {
name: `${nodeName} Example Workflow`,
nodes: [
{
parameters: this.generateExampleParameters(nodeDefinition),
id: this.generateNodeId(),
name: nodeName,
type: nodeType,
typeVersion: nodeDefinition.version || 1,
position: [250, 300],
},
],
connections: {},
active: false,
settings: {},
tags: ['example', 'generated'],
};
// Add specific configurations based on node type
this.addNodeSpecificConfig(nodeType, example, nodeData);
return example;
}
/**
* Generate example parameters based on node type
* Generate example parameters based on node properties
*/
private static generateExampleParameters(nodeType: string, nodeData: any): any {
static generateExampleParameters(nodeDefinition: any): any {
const params: any = {};
// Extract node name for specific handling
const nodeName = nodeType.split('.').pop()?.toLowerCase() || '';
// Common node examples
switch (nodeName) {
case 'if':
return {
conditions: {
options: {
caseSensitive: true,
leftValue: "",
typeValidation: "strict",
version: 2
},
conditions: [{
id: this.generateNodeId(),
leftValue: "={{ $json }}",
rightValue: "",
operator: {
type: "object",
operation: "notEmpty",
singleValue: true
}
}],
combinator: "and"
},
options: {}
};
case 'webhook':
return {
httpMethod: "POST",
path: "webhook-path",
responseMode: "onReceived",
responseData: "allEntries",
options: {}
};
case 'httprequest':
return {
method: "GET",
url: "https://api.example.com/data",
authentication: "none",
options: {},
headerParametersUi: {
parameter: []
}
};
case 'function':
return {
functionCode: "// Add your JavaScript code here\nreturn $input.all();"
};
case 'set':
return {
mode: "manual",
duplicateItem: false,
values: {
string: [{
name: "myField",
value: "myValue"
}]
}
};
case 'split':
return {
batchSize: 10,
options: {}
};
default:
// Generate generic parameters from node properties
return this.generateGenericParameters(nodeData);
}
}
/**
* Generate generic parameters from node properties
*/
private static generateGenericParameters(nodeData: any): any {
const params: any = {};
if (nodeData.properties) {
for (const prop of nodeData.properties) {
if (prop.default !== undefined) {
params[prop.name] = prop.default;
} else if (prop.type === 'string') {
params[prop.name] = '';
} else if (prop.type === 'number') {
params[prop.name] = 0;
} else if (prop.type === 'boolean') {
params[prop.name] = false;
} else if (prop.type === 'options' && prop.options?.length > 0) {
params[prop.name] = prop.options[0].value;
// If properties are available, generate examples based on them
if (Array.isArray(nodeDefinition.properties)) {
for (const prop of nodeDefinition.properties) {
if (prop.name && prop.type) {
params[prop.name] = this.generateExampleValue(prop);
}
}
}
// Add common parameters based on node type
if (nodeDefinition.displayName?.toLowerCase().includes('trigger')) {
params.pollTimes = {
item: [
{
mode: 'everyMinute',
},
],
};
}
return params;
}
/**
* Add node-specific configurations
* Generate example value based on property definition
*/
private static addNodeSpecificConfig(nodeType: string, example: NodeExample, nodeData: any): void {
const nodeName = nodeType.split('.').pop()?.toLowerCase() || '';
// Add specific connection structures for different node types
switch (nodeName) {
case 'if':
// IF node has true/false outputs
example.connections[example.nodes[0].name] = {
main: [[], []] // Two outputs: true, false
};
break;
private static generateExampleValue(property: any): any {
switch (property.type) {
case 'string':
if (property.name.toLowerCase().includes('url')) {
return 'https://example.com';
}
if (property.name.toLowerCase().includes('email')) {
return 'user@example.com';
}
if (property.name.toLowerCase().includes('name')) {
return 'Example Name';
}
return property.default || 'example-value';
case 'switch':
// Switch node can have multiple outputs
const outputs = nodeData.outputs || 3;
example.connections[example.nodes[0].name] = {
main: Array(outputs).fill([])
};
break;
case 'number':
return property.default || 10;
case 'merge':
// Merge node has multiple inputs
example.nodes[0].position = [400, 120];
// Add dummy input nodes
example.nodes.push({
parameters: {},
type: "n8n-nodes-base.noOp",
typeVersion: 1,
position: [200, 60],
id: this.generateNodeId(),
name: "Input 1"
});
example.nodes.push({
parameters: {},
type: "n8n-nodes-base.noOp",
typeVersion: 1,
position: [200, 180],
id: this.generateNodeId(),
name: "Input 2"
});
example.connections = {
"Input 1": { main: [[{ node: example.nodes[0].name, type: "main", index: 0 }]] },
"Input 2": { main: [[{ node: example.nodes[0].name, type: "main", index: 1 }]] },
[example.nodes[0].name]: { main: [[]] }
};
break;
}
// Add credentials if needed
if (nodeData.credentials?.length > 0) {
example.nodes[0].credentials = {};
for (const cred of nodeData.credentials) {
example.nodes[0].credentials[cred.name] = {
id: this.generateNodeId(),
name: `${cred.name} account`
};
}
case 'boolean':
return property.default !== undefined ? property.default : true;
case 'options':
if (property.options && property.options.length > 0) {
return property.options[0].value;
}
return property.default || '';
case 'collection':
case 'fixedCollection':
return {};
default:
return property.default || null;
}
}
/**
* Extract display name from node type
*/
private static getNodeName(nodeType: string): string {
const parts = nodeType.split('.');
const name = parts[parts.length - 1];
return name.charAt(0).toUpperCase() + name.slice(1);
}
/**
* Generate a random node ID
* Generate a unique node ID
*/
private static generateNodeId(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
return Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
}
/**
* Generate instance ID
* Generate example based on node operations
*/
private static generateInstanceId(): string {
return Array(64).fill(0).map(() => Math.floor(Math.random() * 16).toString(16)).join('');
}
/**
* Generate example from node definition
*/
static generateFromNodeDefinition(nodeDefinition: any): NodeExample {
const nodeType = nodeDefinition.description?.name || 'n8n-nodes-base.node';
const nodeData = {
typeVersion: nodeDefinition.description?.version || 1,
properties: nodeDefinition.description?.properties || [],
credentials: nodeDefinition.description?.credentials || [],
outputs: nodeDefinition.description?.outputs || ['main']
};
static generateFromOperations(operations: any[]): any {
const examples: any[] = [];
return this.generateNodeExample(nodeType, nodeData);
if (!operations || operations.length === 0) {
return examples;
}
// Group operations by resource
const resourceMap = new Map<string, any[]>();
for (const op of operations) {
if (!resourceMap.has(op.resource)) {
resourceMap.set(op.resource, []);
}
resourceMap.get(op.resource)!.push(op);
}
// Generate example for each resource
for (const [resource, ops] of resourceMap) {
examples.push({
resource,
operation: ops[0].operation,
description: `Example: ${ops[0].description}`,
parameters: {
resource,
operation: ops[0].operation,
},
});
}
return examples;
}
}

View File

@@ -21,6 +21,8 @@ export class NodeSourceExtractor {
'/n8n-modules',
// Common n8n installation paths
process.env.N8N_CUSTOM_EXTENSIONS || '',
// Additional local path for testing
path.join(process.cwd(), 'node_modules'),
].filter(Boolean);
/**
@@ -75,35 +77,45 @@ export class NodeSourceExtractor {
nodeName: string
): Promise<NodeSourceInfo | null> {
try {
// First, try standard patterns
const standardPatterns = [
`${packageName}/dist/nodes/${nodeName}/${nodeName}.node.js`,
`${packageName}/dist/nodes/${nodeName}.node.js`,
`${packageName}/nodes/${nodeName}/${nodeName}.node.js`,
`${packageName}/nodes/${nodeName}.node.js`,
`${nodeName}/${nodeName}.node.js`,
`${nodeName}.node.js`,
// Try both the provided case and capitalized first letter
const nodeNameVariants = [
nodeName,
nodeName.charAt(0).toUpperCase() + nodeName.slice(1), // Capitalize first letter
nodeName.toLowerCase(), // All lowercase
nodeName.toUpperCase(), // All uppercase
];
// First, try standard patterns with all case variants
for (const nameVariant of nodeNameVariants) {
const standardPatterns = [
`${packageName}/dist/nodes/${nameVariant}/${nameVariant}.node.js`,
`${packageName}/dist/nodes/${nameVariant}.node.js`,
`${packageName}/nodes/${nameVariant}/${nameVariant}.node.js`,
`${packageName}/nodes/${nameVariant}.node.js`,
`${nameVariant}/${nameVariant}.node.js`,
`${nameVariant}.node.js`,
];
// Additional patterns for nested node structures (e.g., agents/Agent)
const nestedPatterns = [
`${packageName}/dist/nodes/*/${nodeName}/${nodeName}.node.js`,
`${packageName}/dist/nodes/**/${nodeName}/${nodeName}.node.js`,
`${packageName}/nodes/*/${nodeName}/${nodeName}.node.js`,
`${packageName}/nodes/**/${nodeName}/${nodeName}.node.js`,
];
// Additional patterns for nested node structures (e.g., agents/Agent)
const nestedPatterns = [
`${packageName}/dist/nodes/*/${nameVariant}/${nameVariant}.node.js`,
`${packageName}/dist/nodes/**/${nameVariant}/${nameVariant}.node.js`,
`${packageName}/nodes/*/${nameVariant}/${nameVariant}.node.js`,
`${packageName}/nodes/**/${nameVariant}/${nameVariant}.node.js`,
];
// Try standard patterns first
for (const pattern of standardPatterns) {
const fullPath = path.join(basePath, pattern);
const result = await this.tryLoadNodeFile(fullPath, packageName, nodeName, basePath);
if (result) return result;
}
// Try standard patterns first
for (const pattern of standardPatterns) {
const fullPath = path.join(basePath, pattern);
const result = await this.tryLoadNodeFile(fullPath, packageName, nodeName, basePath);
if (result) return result;
}
// Try nested patterns (with glob-like search)
for (const pattern of nestedPatterns) {
const result = await this.searchWithGlobPattern(basePath, pattern, packageName, nodeName);
if (result) return result;
// Try nested patterns (with glob-like search)
for (const pattern of nestedPatterns) {
const result = await this.searchWithGlobPattern(basePath, pattern, packageName, nodeName);
if (result) return result;
}
}
// If basePath contains .pnpm, search in pnpm structure
@@ -250,13 +262,49 @@ export class NodeSourceExtractor {
try {
const sourceCode = await fs.readFile(fullPath, 'utf-8');
// Try to find credential file
const credentialPath = fullPath.replace('.node.js', '.credentials.js');
// Try to find credential files
let credentialCode: string | undefined;
// First, try alongside the node file
const credentialPath = fullPath.replace('.node.js', '.credentials.js');
try {
credentialCode = await fs.readFile(credentialPath, 'utf-8');
} catch {
// Credential file is optional
// Try in the credentials directory
const possibleCredentialPaths = [
// Standard n8n structure: dist/credentials/NodeNameApi.credentials.js
path.join(packageBasePath, packageName, 'dist/credentials', `${nodeName}Api.credentials.js`),
path.join(packageBasePath, packageName, 'dist/credentials', `${nodeName}OAuth2Api.credentials.js`),
path.join(packageBasePath, packageName, 'credentials', `${nodeName}Api.credentials.js`),
path.join(packageBasePath, packageName, 'credentials', `${nodeName}OAuth2Api.credentials.js`),
// Without packageName in path
path.join(packageBasePath, 'dist/credentials', `${nodeName}Api.credentials.js`),
path.join(packageBasePath, 'dist/credentials', `${nodeName}OAuth2Api.credentials.js`),
path.join(packageBasePath, 'credentials', `${nodeName}Api.credentials.js`),
path.join(packageBasePath, 'credentials', `${nodeName}OAuth2Api.credentials.js`),
// Try relative to node location
path.join(path.dirname(path.dirname(fullPath)), 'credentials', `${nodeName}Api.credentials.js`),
path.join(path.dirname(path.dirname(fullPath)), 'credentials', `${nodeName}OAuth2Api.credentials.js`),
path.join(path.dirname(path.dirname(path.dirname(fullPath))), 'credentials', `${nodeName}Api.credentials.js`),
path.join(path.dirname(path.dirname(path.dirname(fullPath))), 'credentials', `${nodeName}OAuth2Api.credentials.js`),
];
// Try to find any credential file
const allCredentials: string[] = [];
for (const credPath of possibleCredentialPaths) {
try {
const content = await fs.readFile(credPath, 'utf-8');
allCredentials.push(content);
logger.debug(`Found credential file at: ${credPath}`);
} catch {
// Continue searching
}
}
// If we found credentials, combine them
if (allCredentials.length > 0) {
credentialCode = allCredentials.join('\n\n// --- Next Credential File ---\n\n');
}
}
// Try to get package.json info
@@ -266,12 +314,16 @@ export class NodeSourceExtractor {
path.join(packageBasePath, packageName, 'package.json'),
path.join(path.dirname(path.dirname(fullPath)), 'package.json'),
path.join(path.dirname(path.dirname(path.dirname(fullPath))), 'package.json'),
// Try to go up from the node location to find package.json
path.join(fullPath.split('/dist/')[0], 'package.json'),
path.join(fullPath.split('/nodes/')[0], 'package.json'),
];
for (const packageJsonPath of possiblePackageJsonPaths) {
try {
const packageJson = await fs.readFile(packageJsonPath, 'utf-8');
packageInfo = JSON.parse(packageJson);
logger.debug(`Found package.json at: ${packageJsonPath}`);
break;
} catch {
// Try next path
@@ -295,10 +347,26 @@ export class NodeSourceExtractor {
*/
async listAvailableNodes(category?: string, search?: string): Promise<any[]> {
const nodes: any[] = [];
const seenNodes = new Set<string>(); // Track unique nodes
for (const basePath of this.n8nBasePaths) {
try {
await this.scanDirectoryForNodes(basePath, nodes, category, search);
// Check for n8n-nodes-base specifically
const n8nNodesBasePath = path.join(basePath, 'n8n-nodes-base', 'dist', 'nodes');
try {
await fs.access(n8nNodesBasePath);
await this.scanDirectoryForNodes(n8nNodesBasePath, nodes, category, search, seenNodes);
} catch {
// Try without dist
const altPath = path.join(basePath, 'n8n-nodes-base', 'nodes');
try {
await fs.access(altPath);
await this.scanDirectoryForNodes(altPath, nodes, category, search, seenNodes);
} catch {
// Try the base path directly
await this.scanDirectoryForNodes(basePath, nodes, category, search, seenNodes);
}
}
} catch (error) {
logger.debug(`Failed to scan ${basePath}: ${error}`);
}
@@ -314,7 +382,8 @@ export class NodeSourceExtractor {
dirPath: string,
nodes: any[],
category?: string,
search?: string
search?: string,
seenNodes?: Set<string>
): Promise<void> {
try {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
@@ -330,8 +399,15 @@ export class NodeSourceExtractor {
const descriptionMatch = content.match(/description:\s*['"`]([^'"`]+)['"`]/);
if (nameMatch) {
const nodeName = entry.name.replace('.node.js', '');
// Skip if we've already seen this node
if (seenNodes && seenNodes.has(nodeName)) {
continue;
}
const nodeInfo = {
name: entry.name.replace('.node.js', ''),
name: nodeName,
displayName: nameMatch[1],
description: descriptionMatch ? descriptionMatch[1] : '',
location: fullPath,
@@ -347,6 +423,9 @@ export class NodeSourceExtractor {
}
nodes.push(nodeInfo);
if (seenNodes) {
seenNodes.add(nodeName);
}
}
} catch {
// Skip files we can't read
@@ -354,10 +433,10 @@ export class NodeSourceExtractor {
} else if (entry.isDirectory()) {
// Special handling for .pnpm directories
if (entry.name === '.pnpm') {
await this.scanPnpmDirectory(path.join(dirPath, entry.name), nodes, category, search);
await this.scanPnpmDirectory(path.join(dirPath, entry.name), nodes, category, search, seenNodes);
} else if (entry.name !== 'node_modules') {
// Recursively scan subdirectories
await this.scanDirectoryForNodes(path.join(dirPath, entry.name), nodes, category, search);
await this.scanDirectoryForNodes(path.join(dirPath, entry.name), nodes, category, search, seenNodes);
}
}
}
@@ -373,7 +452,8 @@ export class NodeSourceExtractor {
pnpmPath: string,
nodes: any[],
category?: string,
search?: string
search?: string,
seenNodes?: Set<string>
): Promise<void> {
try {
const entries = await fs.readdir(pnpmPath);
@@ -382,7 +462,7 @@ export class NodeSourceExtractor {
const entryPath = path.join(pnpmPath, entry, 'node_modules');
try {
await fs.access(entryPath);
await this.scanDirectoryForNodes(entryPath, nodes, category, search);
await this.scanDirectoryForNodes(entryPath, nodes, category, search, seenNodes);
} catch {
// Skip if node_modules doesn't exist
}