Guardrails on MCP tool calls in LiteLLM

Screen MCP tool inputs before calls and tool results before the model sees them at a self-hosted LiteLLM gateway, using pre, during and post call modes to mask PII and block injected instructions.

On this page

MCP guardrails in LiteLLM: what they check and when teams need them

Picture a team that routes its coding agents and internal assistants through one shared MCP gateway. Every tool the agents call returns text: a CRM record with a customer's phone number, a support ticket with an email address, or a web page carrying a hidden instruction to ignore the system prompt and send data elsewhere. The model reads whatever comes back, so the gateway is the last point where that content can be inspected before it reaches the model. LiteLLM MCP guardrails run pre_mcp_call on tool inputs, during_mcp_call while the tool executes, and post_mcp_call on the tool result before the model sees it, so they can block or mask PII and prompt injections at those points.

Teams that need this control are the ones where tools read data they do not fully control or where results contain personal data that should not enter model context. Typical cases include agents that query customer records, tools that fetch external web content or documents, and shared servers used by many teams with different data rules. Before you enable guardrails, list which tools return personal data, which tools read from sources outside your organization, and which results the model genuinely needs in full. Those answers decide which modes you turn on and which tools they apply to. Confirm in the LiteLLM MCP guardrail documentation how each mode is configured and which guardrail providers it accepts in your release.

Guardrails sit alongside access control and upstream authentication, not in place of them. Access control decides which keys, teams and organizations can reach a server or tool at all. Upstream authentication decides which identity the gateway uses to call the MCP server. A guardrail inspects content that an allowed call has already produced, so a permitted but over-broad tool can still leak data that a filter misses. Test each control on its own: confirm that a blocked key cannot call a tool, that the upstream credential has only the scope it needs, and that a planted injection in a test result is caught by post_mcp_call. Treat the guardrail results as one layer in your review, and record which tools were tested and which were excluded.

How a tool call passes through MCP guardrail checkpoints

The diagram starts with the client or model proposing a tool call to the LiteLLM gateway. pre_mcp_call checks the tool input before anything leaves the gateway. during_mcp_call runs while the external MCP server executes the tool, and that server sits outside the trust boundary. post_mcp_call checks the result before the model sees it. Confirm which guardrail mode applies to each server and team, and test that a blocked or masked result never reaches the model in its raw form.

Guardrail checkpoints on the path between the client, the LiteLLM gateway and an MCP server.

Pre, during and post call modes and what each one catches

LiteLLM's MCP guardrails can run at three points in a tool call: before the input reaches the tool, while the tool runs, and after the result returns. Each point catches a different risk, so the mode you choose decides whether you stop personal data leaving your environment or stop instructions hidden in tool output from reaching the model.

Pre-call mode for tool inputs

pre_mcp_call runs on the arguments a user or agent sends to an MCP tool before the call leaves the gateway. This is where personal data typed into a prompt, such as a customer name, account number or email address, can be masked or blocked before an external tool or SaaS server receives it. Confirm which PII types and actions (mask or block) your LiteLLM version supports for this mode, and test with a sample payload that matches your real data classes.

MCP guardrail docs

During-call mode for execution

during_mcp_call runs while the tool call is in progress, so it covers checks that depend on the execution path rather than on a single input or output. Use it to decide what your team can observe and enforce during a long-running or streaming tool call. Confirm with the LiteLLM release you deploy which checks this mode actually performs, because support for in-flight inspection can differ from the pre and post modes.

Post-call mode for tool results

post_mcp_call runs on the tool result before the model sees it. This is the mode most relevant to injected instructions, where a web page, document or ticket returned by a tool contains text such as "ignore previous instructions" aimed at the model. It also catches personal data that a tool returns but the user should not receive. Test it with planted injection strings in tool output, because a block here protects the model only if the result is actually withheld.

Masking versus blocking per mode

Each mode can be configured to mask matched content or to block the call, and the choice affects the agent's behavior. Masking keeps the workflow running but may leave the model working with altered data, while blocking stops the call and returns an error that the agent must handle. Decide per mode and per data class, and confirm in your version how a blocked call is reported to the client and logged.

Gaps to test before relying on each mode

No single mode covers every risk, and pattern-based checks miss encoded, translated or split text. Build a test set with clean inputs, PII samples, injection strings in tool output and multi-step calls, then run it against each mode in your own environment. Record which checks fire, which do not and what the logs show, and treat any gap as an open risk until a retest passes.

Guardrail configuration options and when each fits

This table maps each MCP guardrail mode and action to the risk it addresses in a tool call. Read each row as a question: where does the risky content enter, and should the gateway stop the call or remove the sensitive part. The main takeaway is that tool inputs and tool outputs need different checks, and tools with side effects need screening before they run.

RiskMode to considerAction to considerWhy it fits and what to verify
PII in tool inputspre_mcp_callMaskRuns on arguments before the MCP server receives them, so personal data sent by the caller is masked before it leaves the gateway. Verify whether masked values are restored in results.
Injected instructions in tool outputspost_mcp_callBlockChecks the tool result before the model sees it, so instructions hidden in a fetched page or ticket can be stopped. Verify what the caller receives when a result is blocked.
PII in tool outputspost_mcp_callMaskTool responses can echo customer or employee records into model context and logs. Confirm which entity types the built-in PII detection covers for your data.
Tools with side effects (send, write, delete)pre_mcp_callBlockScreening arguments before execution is the point where a harmful request can be stopped before the action happens. Post-call checks only see the result after the effect.
Long-running or streaming toolsduring_mcp_callConfirm before relying on itThe docs describe checks during execution. Confirm which tools, transports and actions apply during execution before depending on it for controls.
Read-only lookups with sensitive resultspost_mcp_callMaskLookup tools often return more fields than the task needs. Masking the result keeps unneeded personal data out of model context. Test with representative records.
Mixed risk on one serverpre_mcp_call and post_mcp_callBlock inputs, mask outputsCombining modes lets one server screen requests and responses separately. Confirm that guardrails can be scoped per server or tool before you rely on it.

Setting up MCP guardrails in LiteLLM step by step

This sequence takes a team from an inventory of MCP servers that return external or user content to a tested configuration in which masked tool output, rather than raw output, reaches the model.

  1. Inventory MCP servers that return untrusted content

    The platform owner lists every MCP server registered or planned in the gateway and marks each one that returns content a person or outside party could influence, such as web pages, tickets, email, documents or chat messages. Servers that only return fixed system data are noted separately. The output is a short table of server names, the tools each exposes, and the reason each is in or out of scope for screening. Check: every server in the gateway appears in the table with a decision, and the security reviewer has signed off on the list.

  2. Define the guardrail settings in the LiteLLM config

    The gateway engineer adds guardrail definitions to the proxy configuration file, choosing the check type for each (PII masking, prompt injection blocking, or both) and the mode that runs it: pre_mcp_call for tool inputs, during_mcp_call during execution, or post_mcp_call on the tool result before the model receives it. Configuration is kept in version control and secrets for any external guardrail service are referenced from the secret store, not written into the file. Check: the proxy starts with the new configuration, and the guardrail names appear in the admin view with their modes.

  3. Attach guardrails to specific servers and teams

    The platform owner binds each guardrail to the MCP servers identified in the inventory, and narrows it to teams or keys where the risk differs. A server that returns customer email might carry a masking and injection check on post_mcp_call, while an internal lookup server might carry only input checks. Attachments are reviewed against the access model so that no high-risk server runs unscreened for any caller. Check: a test key from each team shows the expected guardrails applied to each server it can reach, and none is missing.

  4. Prepare seeded test strings

    The security team builds a small test set of fictional records containing PII-shaped values such as names, email addresses, account numbers and phone numbers, along with tool-result text that carries injected instructions, such as a line telling the model to ignore prior rules or to send data elsewhere. The test data is synthetic and stored with the test plan. Expected results are written down for each string before any call is made, so the outcome can be judged objectively. Check: each test case has an input, a tool that will return it, and an expected outcome of masked, blocked or passed.

  5. Run test calls through the gateway

    A tester calls the configured MCP tools through the gateway with the seeded strings, once for each guardrail mode under test, and records what the gateway returns to the caller and what it logs. Input-side tests confirm that PII in tool arguments is handled as configured before the upstream server receives it. Output-side tests confirm the treatment of seeded results. Check: every test case has a recorded result that matches the expected outcome, and any mismatch is logged as a defect against the configuration rather than adjusted in the test.

  6. Confirm masked output reaches the model

    The tester runs an end-to-end call from an LLM client, so the model receives the tool result through the normal path. The team verifies that the model's input contains the masked form of the seeded PII and that injected instructions are blocked or neutralized as configured, using the gateway logs and the client transcript. This step is run against the same configuration that will be used in production. Check: the model input, captured in logs or the client transcript, shows no raw seeded PII and no unblocked injected instruction, and the test record is signed off by the owner of the affected server.

  7. Record the baseline and repeat tests after changes

    The team saves the configuration, the test set, and the results as the baseline for this gateway. Any change to guardrail definitions, attachments, the MCP server or the upstream content source triggers the same test set again before the change is released. Operators also confirm that guardrail decisions appear in the logs they already review. Check: the baseline is stored under version control with a dated record, and the rerun procedure is written into the change process for the gateway.

Where guardrails help, where they add latency and where they miss

MCP guardrails catch some problems well and miss others, so plan them as one layer rather than a complete defense. Pattern-based checks (regular expressions for card numbers, national ID formats or email addresses) are fast and predictable, but they produce false positives on strings that merely look like sensitive data, such as order numbers or test fixtures, and false negatives on personal data written in free text. Classifier-based checks catch more paraphrased content and injected instructions, but they miss novel phrasing and add their own error rate in both directions. Before go-live, build a test set from real tool outputs in your own environment (with sensitive values replaced) and measure how often each guardrail mode blocks or masks content that should pass, and how often it lets through content that should be caught. Confirm with your guardrail vendor or the LiteLLM documentation which detectors are built in and which need an external service.

Masking has a cost that blocking does not. If post_mcp_call replaces a customer identifier in a search result with a placeholder, the model may no longer be able to match that record to a follow-up request, and the agent may retry, call more tools or give a wrong answer. Blocking is simpler to reason about but stops the workflow entirely, so the user sees a refusal with no explanation of what was removed. A common pattern is to mask fields that the model never needs (for example, full account numbers) and to block only on clear injection signals, but this split must be designed per tool and tested with the workflows people actually run. Checkpoints also add processing time to every call: pre_mcp_call, during_mcp_call and post_mcp_call each run a check on the request path, so measure the added latency per mode under realistic load before enabling all three on high-volume servers.

The choice between blocking and masking also determines what your audit evidence shows. A block event records that a call was refused and why, which is clear evidence of control operation but gives reviewers no view of what the model would have seen. A mask event records what was changed, and it can confirm that personal data did not reach the model, but only if the original and masked values are handled under your logging and retention rules. Decide early which events are written to logs, who may read them and how long they are kept, because these settings affect privacy obligations as well as incident response. Ask your security and compliance teams to review sample events from a pilot before choosing a mode for each server.

Security and operational considerations for MCP guardrails

Guardrails on MCP tool calls only protect you if the guardrail settings themselves are controlled, logged and kept available. The cards below cover who can change them, what the logs should and should not hold, the infrastructure they depend on and how to keep their configuration reviewable.

Treat guardrail config as controlled change

A guardrail that masks PII or blocks injected instructions is only as strong as its current configuration. A change that disables a post_mcp_call check or narrows its scope removes protection for every tool result that passes through the gateway. Ask for a written change process for guardrail edits, including a reviewer who is not the person making the change, and a record of what was altered and when. Confirm which guardrail changes need that review and which the admin UI allows without it.

Limit who can edit guardrails

Editing guardrails is an administrative action, so the people who can reach the admin UI or the configuration API effectively control what the model sees from every MCP server. Map which admin roles can create, change or remove guardrails, and check whether those roles are granted through SSO group membership or set by hand. Restricting admin access to a small group, and reviewing that group on a schedule, is the control to verify before production use.

Log blocks and masks without raw PII

Your audit trail needs to show that a guardrail fired, which tool and key were involved, and what action was taken. It should not copy the personal data that was masked or the injected text that was blocked, because the log then becomes a second store of sensitive data with its own retention. Test a masked and a blocked call, then search the request logs and database for the original values. Confirm the logging setting that controls whether payloads are stored, and set your retention period before enabling it.

Plan the database and Redis dependencies

The production guide names PostgreSQL as the primary supported database for LiteLLM, and it requires Redis 7.0 or newer once more than one proxy instance runs. Guardrail enforcement and the state behind it depend on these services, so an outage in either can stop or degrade tool calls. Decide how each service is backed up, how failover is handled and what the gateway does when Redis is unavailable, then test that behavior before go-live.

Production guide

Keep the master key out of config files

The LITELLM_MASTER_KEY grants administrative access to the gateway, including the settings that define guardrails. The production guide says it must start with sk- and should live in a secret manager rather than in a config file or a container image. Check where the key is stored, who can read it from the secret store, and whether it is rotated after staff changes. A guardrail policy that anyone with the master key can silently remove gives little assurance.

Version guardrail settings in git

Keeping guardrail settings in version control gives you a diff for every change, an author, a review step and a way to restore a known-good state after a mistake. Store the configuration files that define each pre_mcp_call, during_mcp_call and post_mcp_call guardrail, and deploy them through the same pipeline as the rest of the gateway. Confirm that the deployed configuration matches the repository, because settings changed directly in the admin UI will not appear in the diff.

Questions buyers ask about MCP guardrails in LiteLLM

What drives the cost of MCP guardrails in LiteLLM?

The cost is driven by how many MCP servers and tools need screening, how many guardrail modes you configure, and how much testing the rules need before they go live. Pre-call, during-call and post-call checks each add design and test work, and the number of distinct tool outputs matters more than the number of servers. Ask any partner for a quote that lists the servers in scope, the modes per server, the detection targets (PII, injected instructions or both), and the retest effort. Software licensing for LiteLLM is separate: the core project is MIT licensed, with content under the enterprise directory governed by its own license, so confirm which features your configuration needs before you budget.

How long does it take to get a first working MCP guardrail?

Duration depends on how many MCP servers are registered, whether a gateway with a working MCP setup already exists, and how quickly your security team can approve test cases. A first guardrail on one low-risk server is usually a smaller piece of work than a full rollout across every tool. The fastest path is to start with one server, one mode and one detection target, prove it blocks or masks what you expected, then widen scope. Any timeline should come from a written scope, not from a general estimate. Ask for milestones that separate configuration, test validation and sign-off.

What does the client need to provide before guardrail work starts?

Three inputs matter most. First, an inventory of MCP servers and the tools each exposes, including which ones return data from internal systems or the public internet. Second, test data: sample tool inputs and results that contain realistic PII patterns and known injection attempts, prepared so that no production personal data is copied into test records. Third, security sign-off: a named owner who decides what gets masked, what gets blocked, and what happens when a guardrail fires. Without these, the rules are guesses and the test results cannot be accepted by your compliance team.

Can an internal team set up MCP guardrails in LiteLLM alone?

An experienced platform or security team can configure the guardrail settings in the gateway without outside help, provided it already runs LiteLLM with MCP servers registered and has the skills to write and test detection rules. The harder part is validating that masking and blocking work on real tool results without breaking agent workflows, and documenting that evidence for reviewers. Teams without that testing capacity often use an implementation partner for scope, test design and retest. Whichever route you take, assign an owner for ongoing rule changes, since new tools and new data sources will need the same review.

Which parts of MCP guardrails require LiteLLM Enterprise?

Check the Enterprise documentation for each feature before you assume it is included in the open-source build. The LiteLLM Enterprise docs list team-level guardrails and logging, audit logs, organization and team RBAC, and secret manager integrations as Enterprise features. Team-level guardrail scoping is the one most relevant here, because it lets different teams apply different tool screening. Ask your vendor or partner to map each guardrail requirement to a documented edition before you commit to a design, and keep that mapping in your architecture record.

How do MCP guardrail checks affect what the model sees?

Guardrails sit at three points. Pre-call checks inspect tool inputs before the call goes out, during-call checks run while the tool executes, and post-call checks inspect the tool result before the model receives it. Post-call screening is the main defense against injected instructions returned by a tool, and it is also where PII masking usually matters most. Each check can block or mask content, so you should decide per tool whether a blocked result should fail the call, return a safe placeholder, or alert a reviewer. Test these outcomes explicitly, because agents often retry failed tool calls in ways that multiply cost and noise.

What is the main risk of relying only on built-in MCP guardrails?

Built-in detection is one control, not a complete one. Pattern-based masking can miss PII in unfamiliar formats, and injection detection can miss new phrasing, so you should test with examples drawn from your own tools and data. Guardrails also do not replace tool permissions: a server that can write to a system should still be restricted by key, team and organization, and its outputs should be logged. Plan a retest schedule whenever a server, tool or model changes, and record which findings were accepted as residual risk.

Limits to confirm in your environment

MCP guardrails in LiteLLM sit on the path between tools and the model, so their gaps become your gaps. The cards below list what to confirm in your release and your environment before you treat tool-result screening as a control.

Guardrail providers and built-in checks

Confirm which guardrail providers and built-in checks your LiteLLM release supports for MCP hooks, not just for model requests. Support for the pre_mcp_call, during_mcp_call and post_mcp_call modes can differ by provider and by version. Test each mode against a sample tool result that contains a planted instruction and a sample email address, and record which ones are blocked, masked or passed.

MCP guardrail docs

Coverage across MCP transports

Check whether guardrails apply to every transport you register, including Streamable HTTP, SSE and stdio servers. A hook that fires for one transport may not fire for another. Run a test tool call over each transport and confirm the post-call hook sees the full result before the model receives it.

Latency under real load

Measure the added latency of pre-call and post-call checks with your real tool payload sizes and concurrency, not a single test request. Large tool results and external guardrail services can dominate response time. Compare results with and without the hooks, and set a timeout and a failure behavior (fail open or fail closed) that your security team has approved.

Language coverage of PII detection

Ask the detector's maintainers which languages, identifier formats and document types it handles. Detection that works for English names and US phone numbers may miss local national identifiers, addresses written in other scripts or PII split across fields. Build a test set from your own data types, with synthetic values, and measure misses before you choose a masking policy.

Team-level guardrails and Enterprise licensing

Confirm whether applying guardrails per team, key or model requires the Enterprise license in your deployment. The enterprise documentation lists team-level guardrails and logging as an Enterprise feature. Ask for the written license scope before you design per-team policy, so the open-source tier is not assumed to cover it.

Enterprise docs

Guardrail changes across upgrades

Guardrail behavior and provider support can change between minor releases. Record the release you tested against, rerun your MCP guardrail test set after each upgrade, and keep the release line within the versions LiteLLM supports. Treat a passing test on an older release as evidence for that release only.

How DeployOpen scopes and tests MCP guardrails

DeployOpen approaches guardrails on MCP tool calls in LiteLLM as a scoped engagement inside your own infrastructure. We agree in writing which MCP servers, tools and guardrail modes are covered, build and test the configuration against your own tool outputs, and hand over documentation your team can run and review.

How we scope

  • Inventory the MCP servers and tools whose inputs and results need pre, during or post call checks
  • Agree which PII types and injection patterns each guardrail must mask or block
  • Confirm the LiteLLM edition and any Enterprise features the chosen guardrail setup depends on
  • Set fixed deliverables and timelines in writing before any build work starts

How we build

  • Configure pre_mcp_call, during_mcp_call and post_mcp_call modes per server and tool
  • Test masking of personal data in tool inputs and in results returned to the model
  • Run injected-instruction samples from tool outputs and record which ones reach the model
  • Confirm how guardrail decisions appear in gateway logs for each blocked or masked call

How we hand over

  • Deliver a guardrail configuration record covering each server, mode, action and owner
  • Document the test cases, expected results and retest steps your team can rerun
  • Provide a findings report with fixes and a retest after changes are applied
  • Walk your security and platform engineers through the configuration and runbook

How we support

  • Review guardrail changes when new MCP servers or tools are registered in the gateway
  • Retest after LiteLLM upgrades that touch guardrail behavior or configuration
  • Continue testing as an ongoing retainer when your MCP tool set keeps changing
  • Confirm support terms, response expectations and escalation paths in the written scope

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.