@chrishdx/n8n-nodes-claude-cli
v1.1.1
Published
n8n Community Node for Claude CLI as LangChain Chat Model
Maintainers
Readme
n8n-nodes-claude-cli
Community node package for n8n that exposes the Claude CLI (Claude Code) as a LangChain Chat Model. It runs claude --print --output-format json using the bundled @anthropic-ai/claude-agent-sdk CLI or a custom Claude CLI install.
Nodes included
- Claude Chat Model (CLI): LangChain chat model for the AI Agent node
- Claude Auth (CLI): browser login + login status for Claude CLI sessions (no API keys)
Features
- Claude CLI integration: non-interactive
--printmode - Login without API keys: browser-based OAuth (PKCE) flow from n8n
- Login status: check Claude CLI session status inside n8n
- Browser login in n8n: start + complete browser login flows for automation
- Automatic token refresh: OAuth tokens are automatically renewed in the background when 75% of their lifetime has elapsed (no workflows needed)
- Manual token refresh: optional manual refresh via workflow (for advanced use cases)
- Model selection: aliases (
sonnet,opus,haiku) or full model IDs - Permissions: permission modes plus tool allow/deny lists
- Session control: stateless or stateful CLI sessions
- Streaming: optional token streaming via
stream-json - Budget controls: optional max budget + fallback model
Requirements
- Self-hosted n8n only (uses
child_process) - n8n >= 1.0.0
- Node.js >= 18
- Claude CLI available via:
- Bundled CLI from
@anthropic-ai/claude-agent-sdk, or - Custom Path to a
claudeCLI executable orcli.js
- Bundled CLI from
Installation
Option 1: Install from npm (once published)
npm install @chrishdx/n8n-nodes-claude-cliOption 2: Install locally for development
git clone <your-repo-url>
cd n8n-nodes-claude-cli
npm install
npm run build
npm linkThen in your n8n installation directory:
npm link @chrishdx/n8n-nodes-claude-cli
n8n startCredentials setup
Create Claude CLI credentials in n8n:
- Claude CLI Source:
BundledorCustom Path - Claude CLI Path: path to
claudeorcli.jsif using Custom Path - Config Directory (CLAUDE_CONFIG_DIR): optional; set to the same directory used by the CLI
No API keys are required.
Login (no API keys)
You must complete Claude CLI login on the same host/container where n8n runs so the CLI can reuse its session.
Browser login from n8n (Start + Complete)
Use Claude Auth (CLI) in two steps so you can send the URL via your workflow:
- Browser Login (Start): returns
loginUrl+loginSessionId+loginConfigDir(defaults to Claude.ai Login) - Send
loginUrlwith your preferred messaging node - After login, copy the full redirect URL from the browser (
https://console.anthropic.com/oauth/code/callback?...) - Browser Login (Complete): pass the callback URL and
loginSessionIdto store the OAuth tokens
If you only have the authorization code, you can paste it into Authorization Code. If it is shown as code#state, you can paste the whole value (the node will extract both).
Notes:
- To force a fresh login (even if you are already logged in), enable Use Temporary Config Directory or set Login Config Directory Override to a new empty folder. Pass the returned
loginConfigDirinto Browser Login (Complete) so it uses the same location. - Enable Use Claude.ai Login if you want the login page hosted on
claude.ai. - Browser Login (Single Node) performs the Start step only; use Complete to finish.
Check login status in n8n
Use Claude Auth (CLI) with Login Status to verify the stored tokens. The node will reuse saved profile data and auto-fetch account info if missing. Enable Check Remote Profile to always validate tokens with Anthropic, and Fail If Not Logged In if you want the workflow to stop when no session is available.
Automatic token refresh
New in v1.1.0: OAuth tokens are now automatically refreshed in the background!
- Tokens are renewed automatically when 75% of their lifetime has elapsed (25% remaining)
- The background service checks every 5 minutes and refreshes when needed
- Works after n8n server restarts (the service resumes on the first node execution)
- No manual workflows required - it just works!
- Uses file locking to prevent concurrent refresh attempts
- Includes automatic retry with exponential backoff on failures
The service starts automatically when:
- You complete a browser login (Browser Login Complete)
- You manually refresh a token (Refresh Token operation)
- You use the Claude Chat Model node
Manual token refresh (optional)
For advanced use cases, you can still manually refresh tokens using Claude Auth (CLI) with Refresh Token. This can be useful for:
- Testing token refresh behavior
- Forcing an immediate refresh outside the automatic schedule
- Custom refresh logic in specific workflows
The node returns tokenExpiresAt so you can monitor token expiration if needed.
Options:
- Use Claude.ai Login is enabled by default to ensure the
user:inferencescope required by the CLI. - Fetch Profile After Refresh (default: true) updates account info after refresh.
- Include Tokens In Output is off by default for safety.
Optional: Login with the CLI
If you prefer to log in from the host:
node node_modules/@anthropic-ai/claude-agent-sdk/cli.jsOr, if you have a claude executable installed:
claudeThe CLI will store its session in ~/.claude (or your CLAUDE_CONFIG_DIR).
Optional: Long-lived OAuth token
If you prefer a long-lived OAuth token (still no API keys), run:
claude setup-tokenThen set the token on the n8n host:
export CLAUDE_CODE_OAUTH_TOKEN=<token>Using the Claude Chat Model
- Add an AI Agent node
- Select Claude Chat Model (CLI) as the chat model
- Choose your Claude CLI credentials
- Configure Model and Options as needed
Options quick reference
- Working Directory and Additional Directories for tool access
- Permission Mode and Dangerously Skip Permissions
- Tools / Allowed Tools / Disallowed Tools to control built-in CLI tools
- Max Budget (USD) and Fallback Model
- Settings (JSON or File Path) for CLI config overrides
- No Session Persistence for stateless runs
- Stream Response for streaming tokens
Example workflow: Telegram chatbot
A simple flow that turns Telegram messages into Claude responses:
- Telegram Trigger (incoming message)
- AI Agent using Claude Chat Model (CLI)
- Telegram node (Send Message)
Typical mappings:
- AI Agent input: use Telegram
message.text(for example,{{$json.message.text}}) - Telegram response: map the AI Agent output text
Troubleshooting
Node does not appear in n8n
- Verify install:
npm list @chrishdx/n8n-nodes-claude-cli - Restart n8n
- Check n8n logs for load errors
CLI execution fails
- Verify the Claude CLI source (Bundled vs Custom Path)
- Check the CLI manually:
claude --versionornode node_modules/@anthropic-ai/claude-agent-sdk/cli.js --version - Ensure the login happened on the same host/container as n8n
Login issues
- Use Browser Login (Start) + Browser Login (Complete) for a multi-step flow (no terminal)
- Ensure the callback URL includes
codeandstate(copy it from the browser address bar) - If you see
code#state, paste the entire value into Authorization Code or provide thestateseparately - If Login Status shows
missingScopeswithuser:inference, re-run Browser Login (Start) with Use Claude.ai Login enabled - Make sure Login Session ID and Login Config Directory match between Start and Complete
- Use Use Temporary Config Directory to force a clean login when testing
- Use Login Status with Check Remote Profile to verify stored tokens
Automatic token refresh issues
The automatic token refresh service logs to the console. Check your n8n logs for messages starting with [TokenRefresh]:
- Service not starting: Ensure at least one Claude node (Auth or Chat Model) executes after n8n starts
- Refresh failures: Check logs for error messages; the service retries automatically with exponential backoff
- Multiple n8n instances: If running multiple n8n servers with shared credentials file, file locking prevents conflicts
- Tokens not refreshing: Verify that
issuedAttimestamp exists in~/.claude/.credentials.json(requires v1.1.0+ login) - Check service status: Look for
[TokenRefresh] Service started for config: <path>in logs
To monitor the service:
# View n8n logs for token refresh events
tail -f ~/.n8n/logs/n8n.log | grep TokenRefreshCommon log messages:
Service started for config: ~/.claude- Service initialized successfullyToken refresh needed (75% lifetime elapsed)- Automatic refresh triggeredToken refreshed successfully- Refresh completedRefresh attempt X failed- Temporary error, will retry automatically
License
MIT
Support
- GitHub Issues: https://github.com/chrishdx/n8n-nodes-claude-cli/issues
- n8n Community Forum: https://community.n8n.io
Acknowledgments
- Built on the n8n community nodes starter
- Powered by LangChain
- Uses the official
@anthropic-ai/claude-agent-sdk
