How to secure an assistant that loads tools from MCP servers

Keep one reviewed config file in which every server is pinned to an exact version, declares its own environment, and receives only the secrets it needs by reference. Launch each local server with that environment and nothing else, and put servers that run code you did not write in a sandbox. In the runs below, a launcher that passed the whole parent environment gave every server every secret and a readable copy of the others' token files; a scoped environment closed the first and a bubblewrap sandbox closed the second.

Updated 29 Nov 2026

What this assistant can reach

The host application starts or connects to MCP servers, loads their tool descriptions into the model's context, and passes tool results back. In the stdio transport, "the client launches the MCP server as a subprocess".1 A local server therefore runs as you, with whatever environment the launcher gives it and whatever files you can read. A remote server gets whatever token its configuration carries. Tool descriptions and tool results are both text the model reads, so a poisoned or silently changed tool can steer every other tool in the session. OWASP's third-party MCP guide names tool poisoning and rug pulls, prompt injection and others, and recommends pinning, containers and least-privilege policy at the server boundary.2

Threats and controls for this shape

Capabilities: Reads content it does not control; Calls tools, APIs or MCP servers; Holds credentials or sessions.

Threats that apply (12)

Repudiation and untraceable actions

OWASP Agentic v1.1: T8

Overwhelming the human in the loop

OWASP Agentic v1.1: T10

Controls in order (14)

2. Scoped, short-lived credentials

Why here: An agent acts with some credential: one given for its tools, its host's or the user's when it has none of its own, or the authority it passes on when it talks to other agents. Scoping that credential and giving it a short life caps the damage from every other failure: a hijacked agent can only do what the credential allows, for as long as it lives.

3. Secrets kept out of the model's context

Why here: Anything in the prompt, memory or tool results can be repeated back by the model or read by an attacker who finds a way to ask. Keep secrets where the model cannot see them before anything else is exposed.

4. Egress allowlist

Why here: It removes the exit. An injected instruction that cannot send data anywhere has little to steal for, even when the earlier controls fail.

5. Tool allowlist and argument validation

Why here: After credentials and network are bounded, the next question is which actions the agent may take at all (tool calls, browser actions, code it runs) and with which arguments.

8. Untrusted-content isolation

Why here: The earlier controls limit what a hijacked agent can do; this one reduces how easily it is hijacked and keeps a hijack from spreading.

9. Output handling

Why here: Model output goes into other systems; once inputs and actions are bounded, the last gap is what those systems do with it.

10. Human approval gates

Why here: After the actions are bounded and checked, a person decides on what is left that is irreversible or high-impact.

11. Rate, cost and loop limits

Why here: Limits on volume protect availability and budget and slow an attacker, but they do not stop a single harmful action, so they come after the controls that do.

12. Action logging and reconstruction

Why here: Detection and investigation come after containment. Logs do not stop an attack but make the other controls testable and the incident explainable.

13. Kill switch and rollback

Why here: Response needs logging to know what to undo and controls in place to make stopping safe. It is the last control to build and the first one used in an incident.

This page covers the client configuration that ties those controls together and tests the one thing those pages do not: whether one server can see another server's secrets.

The config file

mcp.json uses the mcpServers shape with command, args and env that MCP clients commonly read. A value can reference a variable as ${VAR}, so the file holds no secret and can be reviewed and committed.

{
  "mcpServers": {
    "alpha": { "command": "node", "args": ["server.mjs", "alpha"], "env": { "ALPHA_TOKEN": "${ALPHA_TOKEN}" } },
    "beta": { "command": "node", "args": ["server.mjs", "beta"], "env": { "BETA_TOKEN": "${BETA_TOKEN}" } },
    "everything": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-everything@2.4.0"],
      "env": {}
    }
  }
}

alpha and beta are two small servers written for the test; everything is the published reference server from the MCP project, pinned to the exact version 2.4.0. Three habits show in the file: an exact version in the package argument, an env block on every server (an empty one for a server that needs nothing), and each secret named in one server's block only.

Lint the file

check_config.py fails a config on five rules: R1 an npx, uvx, bunx or pipx package without an exact version; R2 a server with no env block; R3 a secret-named variable with a literal value; R4 one variable passed to more than one server; R5 a shell as the launcher. It ran on a deliberately bad file and on mcp.json:

$ ./check_config.py mcp.bad.json
FAIL R1 alpha: package '@modelcontextprotocol/server-everything@latest' is not pinned to an exact version
FAIL R3 alpha: ALPHA_TOKEN holds a literal value in the config file
FAIL R1 beta: package '@modelcontextprotocol/server-memory' is not pinned to an exact version
FAIL R1 gamma: package 'mcp-server-fetch>=0.5' is not pinned to an exact version
FAIL R2 gamma: no env block (inheritance is then up to the client)
FAIL R5 delta: launcher is a shell (bash); the command is not reviewable
FAIL R4 beta, delta: ALPHA_TOKEN is passed to more than one server
7 problem(s)
exit=1
$ ./check_config.py mcp.json
config OK
exit=0

An exact version fixes what you approved; it does not prove the package is unchanged under that version. For that, record the package hash and the hash of each tool definition and compare at every start, as in tool supply chain pinning. OWASP's guide says the same in two parts: pin the server and tool version at first approval, and "use checksums to verify integrity and prevent silent, malicious upgrades".2

Test: can one server see another's secrets?

The test starts each server over stdio from the same config, calls its probe tool, and reports what it can see. The parent process holds three fake secrets (ALPHA_TOKEN, BETA_TOKEN, HOST_SECRET) and the home directory holds ~/.config/alpha/token and ~/.config/beta/token. The probe reports three things for each server: another server's token in its environment, HOST_SECRET in its environment, and whether it can read the other server's token file. The client is the MCP TypeScript SDK 1.32.2. Its getDefaultEnvironment() passes only HOME, LOGNAME, PATH, SHELL, TERM and USER on Linux and macOS ("Environment variables to inherit by default, if an environment is not explicitly given"),3 and client.mjs merges that list with the server's own declared variables in the scoped mode. Three launch policies ran:

$ node client.mjs inherit-all
server alpha
  ATTACK SUCCEEDED  sees the other server's token in its environment (BETA_TOKEN)
  ATTACK SUCCEEDED  sees the host's HOST_SECRET in its environment
  ATTACK SUCCEEDED  reads the other server's token file (READ)
  env: 171 variables
server beta     (the same three, ALPHA_TOKEN)
$ node client.mjs scoped
server alpha
  blocked  sees the other server's token in its environment (BETA_TOKEN)
  blocked  sees the host's HOST_SECRET in its environment
  ATTACK SUCCEEDED  reads the other server's token file (READ)
  env names it has: ALPHA_TOKEN HOME LOGNAME NODE_DIR PATH SHELL USER
server beta     (the same three, ALPHA_TOKEN)
$ node client.mjs scoped+bwrap
server alpha
  blocked  sees the other server's token in its environment (BETA_TOKEN)
  blocked  sees the host's HOST_SECRET in its environment
  blocked  reads the other server's token file (ENOENT)
  env names it has: ALPHA_TOKEN HOME LOGNAME NODE_DIR PATH PWD SHELL USER
server beta     (the same three, ALPHA_TOKEN)

(NODE_DIR is a test variable that tells the servers where to find the SDK; PWD is added by bubblewrap, which unsets all variables except PWD and those set by --setenv.4)

The pattern is the point. Scoping the environment removes the secrets the server was never given, which is most of the exposure. It does not remove files: both local servers run as the same user, so alpha reads beta's token file until something stops it. bubblewrap does. Its README says it "is not a complete, ready-made sandbox with a specific security policy", so the mounts above are a policy this test chose, and a container with the same effect (sandboxed code execution) is the more usual form; OWASP's third-party MCP guide says to "Use Containers (e.g. Docker) to run third-party MCP servers".25

A published server you did not write, under the same two policies

everything.mjs starts the pinned @modelcontextprotocol/server-everything@2.4.0 exactly as mcp.json names it and calls its get-env tool, which returns the server process's environment (pinned-server-*.log):

$ node everything.mjs inherit-all
server: mcp-servers/everything 2.4.0
environment printed by the server: 195 variables
  ATTACK SUCCEEDED  ALPHA_TOKEN visible to the server
  ATTACK SUCCEEDED  BETA_TOKEN visible to the server
  ATTACK SUCCEEDED  HOST_SECRET visible to the server
$ node everything.mjs scoped
environment printed by the server: 30 variables
  blocked  ALPHA_TOKEN visible to the server
  blocked  BETA_TOKEN visible to the server
  blocked  HOST_SECRET visible to the server

The 30 variables under scoped are mostly npm_* variables (by inspection; the log prints only the count) plus the default list; none is one of the three secrets. The same pattern holds for a server you did not write: whatever is in its environment, the server (and anything an attacker gets it to run) can read and return to the model.

Remote servers

A remote server has no environment to scope: its credential is a header or token the client sends. The same rule applies in other words: one token per server, with an audience of that server only, never the same token for two servers, and never forwarded onward. A headers value that references a variable (headers: { "Authorization": "Bearer ${API_KEY}" }) keeps the token out of the file, and MCP server and client hardening tests the audience check. This page's runs cover local stdio servers only.

Evidence to keep

Where this stops

Files

In /runs/shape-mcp-connected-assistant/: mcp.json, mcp.bad.json, check_config.py, server.mjs, client.mjs, everything.mjs, run_all.sh, package.json and package-lock.json (SDK 1.32.2 and server-everything 2.4.0), and the logs lint-bad.log, lint-good.log, env-scope-inherit-all.log, env-scope-scoped.log, env-scope-scoped+bwrap.log, pinned-server-inherit-all.log, pinned-server-scoped.log. Environment: Node 22.23.5, npm 10.9.9, bubblewrap 0.11.1, Python 3.14.4, run 29 Nov 2026.

Sources

  1. Transports (stdio), Model Context Protocol specification 2025-06-18, https://modelcontextprotocol.io/specification/2025-06-18/basic/transports, read 29 Nov 2026
  2. A Practical Guide for Securely Using Third-Party MCP Servers v1.0 (23 Oct 2025), OWASP GenAI Security Project, https://genai.owasp.org/download/51928/?tmstv=1762283701, read 29 Nov 2026
  3. @modelcontextprotocol/sdk 1.32.2, file dist/esm/client/stdio.js (DEFAULT_INHERITED_ENV_VARS, getDefaultEnvironment), MCP TypeScript SDK, https://github.com/modelcontextprotocol/typescript-sdk, read from the installed package 29 Nov 2026
  4. bwrap manual (bwrap.xml), bubblewrap, https://github.com/containers/bubblewrap/blob/main/bwrap.xml, read 29 Nov 2026
  5. bubblewrap README, https://github.com/containers/bubblewrap/blob/main/README.md, read 29 Nov 2026