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:

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 Like

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.