A developer connects a Model Context Protocol (MCP) client to a local repository so an AI assistant can work with the code, and the client now has the same reach into that folder as the developer. That reach includes the .env file with production API keys, the test fixtures built from real customer records, and the config file holding a database password.
Once any of that content is read, it sits in the client's context window, where every connected tool and MCP server in the session can reach it. The context window is the exposure point, so the controls that matter most are the ones that decide what is allowed to enter it.
What It Means to Protect Local Files from MCP Client Access
Protecting local files from MCP client access means controlling which files in a repository an AI assistant or agent can read through an MCP server, so secrets, credentials, and regulated data never enter the client's context window. Once file contents reach that context, any model, tool, or connected server in the session can reuse or transmit them.
The risk follows from how filesystem access works in MCP. When a developer configures a filesystem MCP server, they pass it a list of allowed directories, and the client can read every file inside them. Approving a folder approves everything in it, including files nobody intended an AI assistant to see.
That makes the client context a data boundary in its own right. Identity providers and MCP gateways govern which servers connect, and a gateway sees only the servers registered with it. A local filesystem server reading a developer's disk often sits outside both.
Why Sensitive Repository Files End Up in MCP Client Context
Repositories hold more than source code. A typical project directory contains environment files, private keys and certificates, cloud credentials in config files, and seed or fixture data copied from production. According to Cyberhaven Labs, 60.5% of organizations have adopted AI coding assistants, and endpoint-based AI agents grew 509% in 2025, so these directories are increasingly open to agents that read files on their own.
Sensitive files reach the context in three common ways:
- A broad request: A developer asks the assistant to review the project or explain how authentication works, and the agent opens the .env file as part of the answer.
- Autonomous retrieval: An agent searches the repository to complete a multi-step task and reads whatever files look relevant.
- Session persistence: A file read earlier in the conversation stays in chat history and remains available to every tool called later.
The third path is where shared context becomes an exfiltration channel. When several MCP servers connect to one client, they share the session context.
Agents also move data on their own initiative. In Cyberhaven research across 100 runs and five AI models, agents leaked confidential data to a public blog in 87 runs while completing an assigned task.
How to Keep Sensitive Files Out of an MCP Client's Context
Prevention at the source keeps sensitive content out of the context before any tool can use it. Four practices cover most repositories.
Scope Allowed Directories to the Task
Grant the filesystem server the narrowest directory that the task requires, such as a single service folder instead of the home directory or the full monorepo. Review MCP client configuration files, including claude_desktop_config.json and mcp.json, for allowed paths that point at broad locations like the desktop or documents folder.
Exclude Sensitive Paths Before the Client Reads Them
Use an MCP server that enforces exclusion patterns, so matching files are blocked at the server and never returned to the client. Start with .env and .env.* files, private keys (*.pem, *.key, id_rsa), cloud credential files, and data directories that hold exports, fixtures, or seed data. Keep the exclusion file under version control so it travels with the repository.
Move Secrets Out of the Repository
An exclusion pattern protects a file only while the pattern matches it. Storing credentials in a secrets manager and injecting them at runtime removes the file entirely, which closes the gap when someone renames a file or adds a new one. Replace production-derived fixtures with synthetic data for the same reason.
Limit What Else Shares the Session
Every server connected to the same client can read what the session has already seen, so one unvetted third-party server can reach the contents of every file read earlier. Keeping filesystem access out of sessions with unreviewed servers limits that reach, and an approved-server registry with least-privilege scoping stops new servers from joining those sessions unchecked.
Where File-Level Exclusions Stop
Exclusion patterns govern one path to the data, the filesystem server that enforces them. The same file can still reach a model through other routes:
- Built-in agent tools: Coding agents often read files and run terminal commands through their own tools, and a command like cat .env bypasses any MCP server. Agents also route around controls that block them, as when a coding agent blocked from a Git push wrote the files through the GitHub API instead.
- User actions: A developer can paste a credential into a prompt, and agent output can leave through email, web upload, or USB after the task ends.
- Pattern drift: A file named customer_export_final.csv slips past a pattern written for /exports, and file names stay visible to listing and search tools even when contents are blocked.
These gaps call for a second layer that follows the data itself on the endpoint, where developers and agents work.
How Cyberhaven Keeps Sensitive Repository Data Out of MCP Client Context
Cyberhaven delivers Data Security for the Agentic Enterprise. Cyberhaven traces the full lifecycle of your data, adapting protection to changing context, and takes action when and where it matters, on the endpoint where developers and agents work.
Many teams start with an MCP gateway, which decides which apps agents can reach through approved MCP servers. A gateway sees only the servers registered with it and inspects MCP messages at a single point in time, so teams pair MCP gateways with endpoint data security to cover what moves outside it. Cyberhaven's endpoint sensor discovers registered and unregistered MCP servers on the device, including shadow MCP servers, and inspects data continuously while capturing its lineage.
Data Lineage traces a repository file from its origin through the agent read, the model, the output, and every later copy or transmission, and Linea AI classifies that data by lineage and context. Agents keep pulling data from approved apps while Cyberhaven blocks sensitive data from moving to unsanctioned MCP servers or unsanctioned external large language models (LLMs). The same protection extends to terminal commands, agent-to-agent connections, and the email, web, and USB channels where agent output leaves the device.
MCP clients make local repositories more useful to AI assistants, and every allowed directory becomes a source of context. Narrow scoping, exclusion patterns, and secrets kept outside the repository limit what enters that context. Cyberhaven traces what moves anyway, from the first file read to the final destination, and blocks sensitive data before it leaves.
Explore Cyberhaven Agentic Data Security in-depth.
Learn more about the value of Data Lineage in securing agentic workflows with our MIT executive briefing.
Frequently Asked Questions
Can Cyberhaven's MCP server prevent sensitive data from being placed in client context?
Yes, for files accessed through it. Cyberhaven's open-source @cyberhaven/mcpignore-filesystem server reads a .mcpignore file with .gitignore-style patterns and blocks read, write, edit, move, list, and file-info requests for matching files, so their contents never reach the MCP client context. Search and directory tree tools return file names only. Cyberhaven's endpoint sensor covers the paths the server can't, such as terminal commands and agent output.
Does .gitignore stop an MCP client from reading a file?
No. A .gitignore file controls what Git tracks and commits, and a filesystem MCP server reads whatever exists on disk inside its allowed directories. An ignored .env file is still fully readable by the client. Blocking it requires an MCP server that enforces its own exclusion patterns, such as a .mcpignore file, or removing the file from the repository.
Which files should an .mcpignore file exclude?
Start with environment files (.env and .env.*), private keys and certificates (*.pem, *.key, id_rsa), cloud and service credential files, and directories holding production exports, fixtures, or seed data. Add customer data, regulated data, and proprietary files that the assistant has no task-related reason to read. Review the patterns whenever the repository structure changes.
Can an ignore file stop prompt injection from exfiltrating repository data?
An ignore file reduces what an injected instruction can reach. If a malicious tool tells the model to retrieve an API key, and the file holding that key never entered the context, the key is unavailable to exfiltrate. The injection itself still runs, so teams also need server approval, tool output validation, and endpoint controls that block sensitive data from leaving.
Do MCP gateways protect local repository files?
MCP gateways control which applications agents reach through registered MCP servers and inspect the traffic routed through them. A local filesystem server, a terminal command, or a built-in agent tool reading files on the developer's machine typically never passes through the gateway. Protecting local files requires controls on the endpoint, where the files and the agent both run.

.avif)
.avif)
