# Securing MCP servers for unattended coding agents

URL: https://codepanel.ai/guides/securing-mcp-for-unattended-agents

Connect MCP servers to autonomous coding agents safely with SHA-256 tool pinning, explicit read-only marks, scrubbed env, and data boundaries.

Giving an unattended coding agent access to Model Context Protocol (MCP) servers creates four concrete attack surfaces: a server rewriting a tool description after approval (a rug pull), untrusted servers claiming a mutating tool is read-only, stdio servers inheriting parent environment secrets, and production data flowing into non-compliant models. Securing MCP requires pinning tool schemas by cryptographic hash, enforcing read-only boundaries on the host side, scrubbing child environments, and gating production reads by provider.

## Pin approved tools by SHA-256 hash

An MCP tool's description is injected directly into the agent's model context. If a remote or third-party server updates a tool description after you approve it, it can insert prompt-injection instructions telling the agent to exfiltrate files or call another tool.

CodePanel hashes every approved tool's canonical JSON definition (name, description, and input schema) with SHA-256 at approval time. On every subsequent run, the runner re-hashes the server's live tool list before exposing any tool to the agent. If a tool's hash changes, the tool is dropped immediately until the account owner reviews and approves the new definition.

## Never trust server annotations for read-only access

MCP tool definitions include optional hint fields such as `readOnlyHint`. Those annotations are self-reported by the server and must not be used as an authorization boundary.

- **Owner-marked read-only:** Connections that reach production systems only expose tools the owner explicitly marked read-only in account settings.
- **Repository `.mcp.json` as proposal only:** An MCP configuration committed inside a repository can be edited by any contributor or pull request. Never auto-start stdio or remote MCP servers straight from a repository file without explicit owner approval.

## Scrub stdio server environments

Local stdio MCP servers run as child processes on your machine. Some SDK transports merge `HOME`, `PATH`, `USER`, and other parent environment variables into the child process even when you pass a custom `env` object.

To keep credentials out of third-party MCP servers:

1. Use a custom stdio transport that passes only the exact allowlisted environment variables the server requires.
2. Run stdio MCP servers inside the same OS sandbox (`sandbox-exec` or `bwrap`) as the coding agent's shell commands so they cannot read `~/.ssh`, `~/.codepanel`, or unrelated repositories.
3. Keep provider API keys in a backend [model proxy](/glossary/model-proxy) rather than in the local environment.

## Enforce data boundaries for production reads

When a coding agent holds MCP tools that read production logs, databases, or support tickets, every byte it reads enters the model's prompt context. In multi-agent teams, one subagent's findings can also be passed to another subagent.

Enforce the data boundary before the run starts:

| Control | What it prevents |
| --- | --- |
| Zero-retention provider allowlist | Refuses to start a production-read run (or any subagent in a team run) on providers outside your approved zero-retention list, or on external CLIs. |
| Fail-closed LLM gateway | If the account routes through an LLM gateway with payload logging disabled and spend limits enabled, a production-read run stops (`gateway_unreachable`) instead of falling back to direct provider calls. |
| Owner-confirmed OAuth scopes | Scopes that widen unattended permissions (`rules:write`, `agents:write`, `budget:write`, `interrupts:answer`) stay unticked by default during agent token approval. |

Read the full architecture in [Security](/security) and see how CodePanel runs unattended workflows safely in [Autopilot](/agent-autopilot).
