I’d like to ask for some help please. After hours of playing and google/chatbot/forums, I’ve coome here for help
I have a working workflow using an AI agent with OpenAI/Gemini Chat model and a Playwright MCP Tool. I am hosting my own n8n and Playwright MCP servers.
I am using it for web crawling/automation and can type requests into the chat window and the AI/Playwright MCP server can crawl / scrape web pages successfully.
I can type “Login to www.domain.com with username:MrBlobby and password:dontshare and the AI uses this just fine to login to the website.
However, I dont want to pass the credentials to the AI Chat as this feels very insecure. I have been trying and failing to find a way to save n8n credentials that I can pass on more securely.
Does anyone have any tips or code I can use to make this happen please?
Hi. Anyone have any idea how I can achieve this please?
I have looked into the getcredentials method but it seems to not be available for me to use:
async function main() {
try {
// Replace 'genericCredentials' with your credential type and 'WebsiteLogin' with your credential name
const credentials = await $.getCredentials('stored-creds');
return [{ json: { email: credentials.email, password: credentials.password } }];
} catch (error) {
throw new Error(`Failed to fetch credentials: ${error.message}`);
}
}
return main();
Hey can you tell me how did you get the MCP working. I am trying but unable to call as playwright mcp needs proxy for http calling. Adding MCP only supporting http streamable
I run self-hosted non-docker, test environ. my n8n does NOT have the new fancy “variables” section.
I grab env variables at n8n startup, via a bat file. I start n8n, a couple other things.
Bat file has code section like this:
set ENV_FILE=C:\Users\YOURPATH.n8n.env
:: 1. LOAD .ENV
if not exist “%ENV_FILE%” (
echo ERROR: .env not found at %ENV_FILE%
pause & exit /b 1
)
echo Loading %ENV_FILE%…
for /f “usebackq eol=# delims=” %%L in (“%ENV_FILE%”) do (
set “%%L”
)
AND i have an env var set: N8N_BLOCK_ENV_ACCESS_IN_NODE=false.
see vid - i put env var wherever i may need in a flow, and the UI complains** it says not accessible! Then it goes ahead and runs, resolves fine. Here i demonstrate a Slack message firing off to url
Your $.getCredentials idea was actually right, like you were so close.
Drop the dot — it’s $getCredentials() not $.getCredentials(), I’ve done this myself tbh. And um, you need to create a Generic Credential in n8n’s credential store first, the name you pass in has to match it exactly.
Stick that in a Code node before your AI Agent, then in the system prompt just reference it:
When logging into domain.com use username: {{ $('Code').item.json.username }}
and password: {{ $('Code').item.json.password }}
Credentials stay encrypted in n8n, never touch the chat. Does exactly what you wanted.
Oh and if you’re on a recent version of n8n there’s an even easier way — Settings > Variables, add them there and just use {{ $vars.SITE_USERNAME }} anywhere in the workflow, no code node needed at all.
The env variable thing mentioned above works but it’s a bit of a faff honestly.
Ich würde bei dem letzten Schritt vorsichtig sein. Selbst wenn das Geheimnis in n8n gespeichert ist, wird durch die Interpolation von Benutzername/Passwort in den Agent-Prompt das Geheimnis trotzdem in den Modellkontext eingefügt.
Sicherer Ansatz: Ein kleines Login-Tool besitzt die Anmeldedaten, führt die Anmeldung durch und gibt nur Status, Domain, Kontobezeichnung und Sitzungsstatus zurück. Der Agent erhält eine Bestätigung, nicht das Passwort.
Der Schlüssel ist, Anmeldedaten vor der KI-Schicht aufzulösen, nicht innerhalb dieser. Fügen Sie einen Code-Node vor Ihrem KI-Agent hinzu, der $getCredentials('your-cred-name') aufruft – das funktioniert im Code-Node-Kontext einwandfrei, auch wenn es in Tool-Sub-Workflows nicht verfügbar ist. Übergeben Sie dann die aufgelösten Werte in das System-Message-Feld des KI-Agents, indem Sie einen Ausdruck wie {{ $('Code').item.json.username }} verwenden. Das LLM erhält bereits aufgelöste Werte im Kontext und sieht niemals rohe Anmeldedaten im Chat-Verlauf.
Wenn $getCredentials in Ihrem Setup blockiert ist, ist eine zuverlässige Alternative, Anmeldedaten als n8n-Umgebungsvariablen zu speichern und sie mit {{ $env.MY_PASSWORD }} in der System-Eingabeaufforderung zu referenzieren. Das Playwright-MCP-Tool erhält sie dann als Teil der Task-Anweisungen, ohne sie im Konversationsverlauf offenzulegen.