Implementation Guide | Healthcare

    Limiting a Postgres MCP Agent to Patients on the Requesting Clinician's Care Team

    Use three layers. First, carry the requesting clinician's identity into every database transaction and enforce row-level security against a care-team table, so the database is the authoritative boundary. Second, expose narrow, parameterized MCP tools instead of open-ended SQL. Third, apply runtime policy to each tool call to allow, redact or block it and to record the decision. None of these layers depends on the model following instructions.

    The layered pattern

    1. 1

      Database layer (authoritative)

      Row-level security on every PHI table, keyed to the requesting clinician's identity in the session. Holds even if the SQL is arbitrary.

    2. 2

      MCP tool layer (narrowing)

      Purpose-built, parameterized, read-only tools. Reduces the query shapes available and the volume of PHI per call.

    3. 3

      Runtime policy layer (decision and evidence)

      Allow, redact or block on each tool call using the clinician's identity, with a durable record of the outcome.

    Test the boundary before relying on it

    • As clinician A, request a patient on the team and a patient off the team; the second must return no rows.
    • Submit SQL that tries to change the session variable or switch roles, and confirm the result does not change.
    • Alternate requests from two clinicians over one pooled connection and confirm no identity carries over.
    • End a clinician's care-team membership and confirm the next request fails without a restart or token refresh.
    • Plant instructions in a clinical note telling the agent to fetch other patients and confirm both layers refuse.
    • Check that the agent role has no superuser or BYPASSRLS attribute and review every security-definer function.

    Why a shared service account breaks the boundary

    A typical Postgres MCP server connects with one credential, and that credential can read every patient record. Whatever the clinician is allowed to see, the agent can see more. An instruction in the system prompt such as "only query the user's patients" is a request to the model, not a control. Text inside a clinical note or an uploaded document can also alter the SQL the model writes. The boundary has to hold when the generated query is wrong or hostile.

    Start by defining what care team means in your data. The healthit.gov Care Team Members data class is broad: it covers licensed and unlicensed staff, family, and the patient. Your relationship table should state which of those roles grant access to the agent's data. That decision is a policy choice, and it should be written down.

    HHS describes access control as role-based, user-based, attribute-based or another mechanism the organization chooses, and says it applies to software programs as well as people. Care-team membership is a relationship attribute, so it fits as an access decision input. The agent and the MCP server belong in your access-control inventory. The minimum necessary standard, as of November 2026, requires reasonable efforts to limit PHI use, disclosure and requests to what the purpose requires.

    Layer one: identity in the session and row-level security

    The MCP server must receive the clinician's identity as a validated, delegated credential, such as an OAuth 2.0-style token, and not as a tool argument the model can fill in. NCCoE frames agent authorization around both the agent's identity and the identity of the user it acts for. The server verifies the token, then binds that identity to the database transaction.

    Session variables are the simplest binding, but they have a weakness. Any statement running under the same role can usually change them, so an open SQL tool lets the model overwrite the clinician ID. Either remove open SQL, or switch to a per-clinician database role entered for the transaction. Both approaches have tradeoffs: variables are easy to operate, while per-user roles give stronger separation at the cost of role management.

    • Model the relationship: A care_team table with patient, clinician, role, start and end dates. End dates let membership expire without manual cleanup.
    • Use a restricted role: The agent connects as a non-owner role that is not a superuser and lacks BYPASSRLS. Superusers and BYPASSRLS roles skip policies entirely.
    • Enable and force RLS: Enable row-level security on each PHI table and force it so table owners are also subject to policies.
    • Write the policy: Allow a row only when an active care_team row links the clinician in the session to that row's patient.
    • Scope identity to the transaction: Set the identity with transaction-local settings inside an explicit transaction. Session-level settings can persist on a pooled connection and leak to the next request.
    • Audit definer objects: Security-definer functions run with their owner's privileges and views may run as their owner. Either can bypass the policy unless reviewed.

    Layer two: tools that narrow what the agent can ask

    Open-ended SQL execution gives the model the most room to produce unexpected queries. Replace it with purpose-built tools such as a list of the clinician's patients, a medication summary for one patient, or recent lab results for a date range. Each tool takes typed parameters, runs a fixed parameterized statement, and never accepts the clinician identity as input.

    Run those statements through a read-only role. Add statement timeouts and hard row limits so a single call cannot enumerate a table. Return only the columns the task needs, which is also how you document minimum necessary criteria for recurring agent query patterns. Free-text notes and other high-risk fields can be excluded or handled by the policy layer.

    Treat tool results as untrusted input. A record can contain text that tells the model to request other patients. With layers one and two in place, that request returns nothing or is never a valid tool call.

    Layer three: runtime policy and audit evidence

    The database and tool layers cannot reason about context such as which tool is being called with which parameters for which user, and they do not decide what is redacted before data reaches the model. Policy enforced in the execution path of the tool call can. Trussed enforces policy on tool calls to MCP servers at runtime, not after the fact, with allow, redact and block outcomes. Examples: block a call with no valid clinician identity, block a patient identifier outside the clinician's care team before the query runs, and redact free-text fields from a result. Its scope is the individual interaction and tool call, and it complements row-level security rather than replacing it. Trussed runs in production with healthcare organizations and produces audit-ready evidence mapped to HIPAA.

    Each layer produces different evidence, and you want all of it correlated by one request identifier to support 164.312(b) audit controls. The database holds the role and session identity. The MCP server holds the tool invocation and parameters. The policy layer holds the decision. A useful record per call includes the requesting clinician, the agent, the tool and parameters, the care-team determination, the policy outcome, the number of records returned, and a timestamp.

    Care-team scoping in three layers

    Database

    Clinician identity in the session, row-level security keyed to care-team membership.

    MCP tool design

    Parameterized, read-only tools with column allowlists and result limits.

    Runtime policy

    Identity-aware allow, redact or block on each tool call, with an audit record.

    Enforce care-team scoping where the tool call happens

    See how policy enforcement in the execution path and audit-ready evidence fit alongside row-level security for MCP agents handling PHI.

    Talk to an Expert