AI Agent based Playwright MCP using credentials

I’d like to ask for some help please. After hours of playing and google/chatbot/forums, I’ve coome here for help :blush:

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?

This is my simple workflow:

1 « J'aime »

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();
1 « J'aime »

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

Hey does this help at all:

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


And heres the old thread:
http://community.n8n.io/t/no-access-to-env/20665/10

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.

So like this:

const creds = await $getCredentials('MyWebsiteLogin');
return [{ json: { username: creds.username, password: creds.password } }];

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.

Je serais prudent avec la dernière étape. Même si le secret est stocké dans n8n, l’interpolation du nom d’utilisateur/mot de passe dans l’invite de l’agent place toujours le secret dans le contexte du modèle.

Approche plus sûre : un petit outil de connexion possède l’identifiant, effectue la connexion et retourne uniquement le statut, le domaine, l’étiquette du compte et l’état de la session. L’agent reçoit un reçu, pas le mot de passe.

La clé est de résoudre les identifiants avant la couche IA, et non à l’intérieur. Ajoutez un nœud Code avant votre AI Agent qui appelle $getCredentials('your-cred-name') - cela fonctionne bien dans le contexte du nœud Code même s’il n’est pas disponible dans les sous-workflows d’outils. Ensuite, transmettez les valeurs résolues dans le champ System Message de l’AI Agent en utilisant une expression comme {{ $('Code').item.json.username }}. Le LLM reçoit des valeurs déjà résolues en contexte et ne voit jamais d’identifiants bruts passant par le chat.

Si $getCredentials est bloqué dans votre configuration, une solution de secours fiable consiste à stocker les identifiants en tant que variables d’environnement n8n et à les référencer avec {{ $env.MY_PASSWORD }} dans le prompt système. L’outil Playwright MCP les reçoit alors comme faisant partie des instructions de tâche sans les exposer à l’historique de conversation.