Skip to main content

Agent CLI - AI Integration Documentation

Note: This package is used by AI assistants to access n8n node documentation and generate AI context files. It can be used via NPX commands or programmatically.

๐ŸŽฏ Purposeโ€‹

The Agent CLI (@n8n-as-code/agent-cli) provides tools for AI assistants to understand and work with n8n workflows. It offers:

  1. Node Schema Access: Search and retrieve n8n node schemas
  2. AI Context Generation: Create AGENTS.md and AI rule files
  3. Code Snippets: Generate VS Code snippets for common n8n patterns

๐Ÿ—๏ธ Architectureโ€‹

Component Diagramโ€‹

graph TD
A[Agent CLI] --> B[NodeSchemaProvider]
A --> C[AIContextGenerator]
A --> D[SnippetGenerator]

B --> E[Search/Get/List Commands]
C --> F[AGENTS.md]
C --> G[.cursorrules/.clinerules/.windsurfrules]
D --> H[.vscode/n8n.code-snippets]

style A fill:#ff6b35
style B fill:#3498db
style C fill:#2ecc71
style D fill:#9b59b6

Core Componentsโ€‹

1. NodeSchemaProviderโ€‹

Provides access to n8n node schemas for AI assistants.

Key Features:

  • Loads n8n node index from generated JSON
  • Supports fuzzy search for nodes
  • Returns full JSON schemas for specific nodes
  • Lists all available nodes

2. AIContextGeneratorโ€‹

Generates AI context files with instructions for AI assistants.

Key Features:

  • Creates AGENTS.md with n8n-as-code context and rules
  • Generates specialized rule files (.cursorrules, .clinerules, .windsurfrules)
  • Creates .ai-rules.md for general AI assistants
  • Injects/updates existing files without overwriting user content

3. SnippetGeneratorโ€‹

Generates VS Code snippets for common n8n patterns.

Key Features:

  • Creates code snippets for common n8n nodes
  • Supports tab stops and placeholders
  • Falls back to hardcoded snippets when node index is unavailable
  • Organized by node category

๐Ÿ“ Generated Filesโ€‹

AGENTS.mdโ€‹

# ๐Ÿค– AI Agents Guidelines

<!-- n8n-as-code-start -->
## ๐ŸŽญ Role: Expert n8n Engineer
You manage n8n workflows as **clean, version-controlled JSON**.

### ๐ŸŒ Context
- **n8n Version**: 2.2.6
- **Source of Truth**: Use `@n8n-as-code/agent-cli` tools to get accurate node schemas from n8n-nodes-index.json.

### ๐Ÿ›  Coding Standards
1. **Expressions**: Use `{{ $json.field }}` (modern) instead of `{{ $node["Name"].json.field }}` when possible.
2. **Nodes**: Always prefer the `Code` node for custom logic.
3. **Credentials**: NEVER hardcode API keys. Mention needed credentials by name.

### ๐Ÿ”ฌ Research Protocol (MANDATORY)
Do NOT hallucinate node parameters. Use these tools via `npx @n8n-as-code/agent-cli`:
- `search "<term>"`: Find the correct node named (camelCase).
- `get "<nodeName>"`: Get the EXACT property definitions for a node.
- `list`: See all available nodes.

Apply the Knowledge: Use the `get` tool's output as the absolute source of truth for JSON parameter names.
<!-- n8n-as-code-end -->

AI Rule Filesโ€‹

  • .cursorrules: Rules for Cursor AI
  • .clinerules: Rules for Cline AI
  • .windsurfrules: Rules for Windsurf AI
  • .ai-rules.md: General rules for all AI assistants

.vscode/n8n.code-snippetsโ€‹

{
"n8n-webhook": {
"prefix": "n8n-webhook",
"body": [
"{",
" \"parameters\": {\"path\": \"webhook\", \"httpMethod\": \"POST\"},",
" \"name\": \"Webhook\",",
" \"type\": \"n8n-nodes-base.webhook\",",
" \"typeVersion\": 1,",
" \"position\": [0, 0]",
"}"
],
"description": "โšก Insert a Webhook node"
}
}

๐Ÿ”ง Usageโ€‹

Command Line Interfaceโ€‹

The Agent CLI provides three main commands for AI assistants:

# Search for nodes by name, display name, or description
npx @n8n-as-code/agent-cli search "google sheets"

# Get full JSON schema for a specific node
npx @n8n-as-code/agent-cli get "httpRequest"

# List all available nodes
npx @n8n-as-code/agent-cli list

Programmatic APIโ€‹

import { NodeSchemaProvider, AiContextGenerator, SnippetGenerator } from '@n8n-as-code/agent-cli';

// Access node schemas
const provider = new NodeSchemaProvider();
const schema = provider.getNodeSchema('httpRequest');
const results = provider.searchNodes('google');
const allNodes = provider.listAllNodes();

// Generate AI context files
const aiGenerator = new AiContextGenerator();
await aiGenerator.generate('./project-root', '2.2.6');

// Generate VS Code snippets
const snippetGenerator = new SnippetGenerator();
await snippetGenerator.generate('./project-root');

๐Ÿง  How AI Assistants Use Thisโ€‹

1. Node Schema Researchโ€‹

AI assistants use the CLI commands to:

  • Search for the correct node names using search
  • Get exact parameter definitions using get
  • Avoid hallucinations by using real n8n node schemas
  • Ensure workflow JSON follows n8n's actual structure

2. Context Understandingโ€‹

AI assistants read generated files to understand:

  • Their role as n8n Automation Engineer
  • n8n version and environment context
  • Coding standards and syntax rules
  • Research protocol for avoiding hallucinations

3. Code Generationโ€‹

AI assistants use:

  • Generated snippets for common node patterns
  • Real node schemas for accurate parameter configuration
  • AGENTS.md guidelines for workflow structure

๐Ÿ”„ Integration with Other Packagesโ€‹

VS Code Extensionโ€‹

The VS Code extension can use the Agent CLI programmatically to:

  1. Generate AI context files when initializing projects
  2. Provide node schema access for AI features
  3. Update rule files when n8n version changes

Main CLIโ€‹

The main CLI's init-ai command uses the Agent CLI internally to:

n8n-as-code init-ai

This generates all AI context files in the current project.

๐Ÿงช Testingโ€‹

Test Structureโ€‹

packages/agent-cli/tests/
โ”œโ”€โ”€ ai-context-generator.test.ts
โ”œโ”€โ”€ node-schema-provider.test.ts
โ””โ”€โ”€ snippet-generator.test.ts

Test Coverageโ€‹

  • Unit Tests: Individual component testing
  • Integration Tests: End-to-end generation testing
  • Snapshot Tests: Ensure generated files match expected format

Running Testsโ€‹

cd packages/agent-cli
npm test

๐Ÿ“Š Performance Considerationsโ€‹

Cachingโ€‹

  • Node schemas are cached to avoid repeated API calls
  • Generated files are cached with hash comparison
  • Only regenerate when source data changes

Optimizationโ€‹

  • Parallel generation of different file types
  • Incremental updates for large schemas
  • Memory-efficient processing for large node libraries

๐Ÿ” Securityโ€‹

Data Handlingโ€‹

  • No sensitive data in generated files
  • Public n8n documentation only
  • No API keys or credentials

Validationโ€‹

  • Generated files are validated before writing
  • Schema validation ensures correctness
  • File permissions are set appropriately

๐Ÿš€ Developmentโ€‹

Buildingโ€‹

cd packages/agent-cli
npm run build

Development Modeโ€‹

cd packages/agent-cli
npm run dev

Adding New Node Typesโ€‹

  1. Update the node schema extraction logic
  2. Add new snippet templates
  3. Update test snapshots
  4. Regenerate context files

The Agent CLI enables AI assistants to work effectively with n8n workflows by providing comprehensive context, validation, and code generation capabilities.