Currently Available: Need a skilled Software Developer for your next project?
Categories
Coding Agents

How to Keep Sensitive Data Out of an MCP Server’s Model Context

When you build a server with the Model Context Protocol (MCP), your tools let a model pull data from services like databases and APIs. The model reads each tool's result and uses it to answer the user's request. If your server returns an access token, personal details, or records the user has no right to see, the model already has that sensitive data.

You can cut that exposure at the source by returning only the records and fields the task requires. When you filter fields, your server chooses which specific fields a tool sends back. When you redact values, your server strips out or masks sensitive details that still need to be represented in some form. You also keep each result narrow so the model receives less data and less detail overall. These steps lower your risk, though your server still needs to enforce authorization, protect its logs, and verify any follow-up tool calls.

Return only fields the task needs

Design each tool for one clear job, and build its response from an allowlist - a written list of the exact fields the tool is permitted to return. A case-status tool that answers whether a case is open and when it was last updated only needs those two facts plus a case reference. It has no reason to return the full case record with contact details and internal notes.

Do not pass a database row or an API response straight through to the model. If the upstream database or API adds a new field later, a pass-through server exposes that field automatically. When you maintain an allowlist in your server code, anyone reviewing a change can see exactly what the tool discloses.

Your server must check authorization before a tool retrieves protected data. In a multi-tenant service, where different customers or organizations must see separate records, scope the database query to records the authenticated user is allowed to access. Do not fetch a broad set of rows and count on the model or a display layer to hide the unauthorized ones later. OpenAI’s MCP server guidance says the server should enforce authorization on every request. For more on keeping credentials separate across users, see per-user API credentials.

Redact values only when the task needs them

Leave a sensitive field out completely when the model does not need it. If the task does require some information from that field, send back the least revealing version that still answers the question. A payment tool can report whether a payment method is on file instead of returning the full card number. A support tool can return the last few digits of an account identifier when a user needs those digits to tell two accounts apart.

You can also replace a direct identifier with a stable substitute, often called a pseudonym, so the model can refer to the same person or record across turns without seeing the real value. Keep in mind that a pseudonym can still be sensitive if someone can link it back to the original record using other fields the model can read.

You cannot rely on pattern matching alone to redact data. A regular expression, which is a rule that matches a specific text pattern, might catch one account-number format while missing another format, a nested field, or sensitive details written in free text. In its sensitive-information guidance, OWASP recommends minimizing model context and using more than pattern matching to sanitize sensitive data. Check non-text outputs as well. If a tool returns images, audio, embedded resources, or links, text-based filters will not inspect them.

Keep tool results narrow

Limit how many records a tool returns, not only which fields those records contain. A search tool should return only the records relevant to its task, with hard caps on both the number of results and their text length. When a user asks for a short status, return that status instead of an entire mailbox, document, or customer profile.

The MCP specification allows a tool to return text in content and structured JSON in structuredContent. Because a tool can return both at the same time, stripping a sensitive value from one representation while leaving it in the other still exposes it to the model. The MCP tools specification also covers other result types, including images, audio, and resource links. Review every representation your tool sends.

An output schema defines how structured data should be shaped so the server can check that the response matches that format. It does not check whether a field is safe to disclose or whether the user has permission to see its value. If a tool sends back an access token inside a response that passes schema validation, your server has still leaked that token. Build a dedicated result object for the task first, then validate it against the schema before you return it.

Treat returned content as untrusted

Text inside a webpage, support ticket, or file can instruct the model to reveal data or call another tool. Treat content from external sources as untrusted even when it sits inside a structured field. A structured format lets your server verify a result's shape, but it does not make the text inside it safe.

OWASP’s MCP security guidance advises treating tool responses as untrusted and enforcing authorization for any follow-up tool calls in trusted server code. When a model decides to make another call, that decision does not grant permission. Your server has to check the caller's authorization for the new call on its own.

Keep confidential values on the server instead of placing them in a field the model is supposed to ignore. For example, OpenAI describes _meta as client-specific metadata that stays hidden from the model in its MCP guidance, yet hidden metadata does not replace access control or secure storage.

Test every path that carries data

Seed your tests with fake sensitive values and verify that none of them appear in any part of a tool result. Cover nested fields, null or empty values, unexpected upstream fields, and changes to source schemas. Run those tests across different user and tenant identities so you can confirm that one caller never receives another caller's records.

Check what your server writes to its logs as well. Tool results, arguments, prompts, and retrieved content can all leak sensitive data outside the model's final answer. OWASP’s RAG security guidance recommends keeping raw content out of logs by default and using restricted, redacted evidence only when an investigation requires it. For audit practices that track tool calls without storing raw results by default, see keeping an audit log.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *