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.
Ten cuidado con el último paso. Incluso si el secreto se almacena en n8n, interpolar el nombre de usuario/contraseña en el aviso del agente sigue poniendo el secreto en el contexto del modelo.
Forma más segura: una pequeña herramienta de inicio de sesión es propietaria de la credencial, realiza el inicio de sesión y devuelve solo el estado, el dominio, la etiqueta de cuenta y el estado de la sesión. El agente obtiene un comprobante, no la contraseña.
La clave es resolver las credenciales antes de la capa de IA, no dentro de ella. Agrega un nodo Code antes de tu Agente de IA que llame a $getCredentials('your-cred-name') — esto funciona bien en el contexto del nodo Code incluso si no está disponible en subworkflows de herramientas. Luego pasa los valores resueltos al campo System Message del Agente de IA usando una expresión como {{ $('Code').item.json.username }}. El LLM recibe valores ya resueltos en el contexto y nunca ve credenciales sin procesar pasando por el chat.
Si $getCredentials está bloqueado en tu configuración, una alternativa confiable es almacenar las credenciales como variables de entorno de n8n y referenciarlas con {{ $env.MY_PASSWORD }} en el prompt del sistema. La herramienta Playwright MCP entonces las recibe como parte de las instrucciones de tarea sin exponerlas al historial de conversación.