Categories
AI Best Practices Patterns

MCP Auth Gateway

Who May the LLM Ask?

In March 2026 a security firm pointed an autonomous agent at McKinsey’s internal AI platform, Lilli. According to the researchers, it needed about two hours. It found unauthenticated endpoints and a classic SQL injection, and ended up with read and write access to the production database. McKinsey says it fixed the issue within hours and found no unauthorised access to client data. See the artile at CodeWall.

I wrote about the incident in What About Security in AI Development?, mostly about the prompts that lived in that same database. This article is about a different question the case left me with. Lilli was the door to the company’s knowledge, and the door was far too wide. What would a narrower door look like? Not for Lilli itself, but for any assistant that reaches company data through MCP.

A narrower door

Take a company assistant that can answer questions about employees, customers and prices. Alice works in HR and may see employee data. Bob works in sales and may see customers and prices. Neither should see the other’s data, and the model should not be the one deciding.

That last point is the argument of this article. The model is not a security boundary. It reads text, and text can be manipulated. What a user may see has to be decided somewhere that does not depend on what the model says or believes.

MCP in five sentences

The Model Context Protocol lets an AI application discover and call tools that an MCP server offers. The application, the “client”, asks the server which tools exist and then calls them with arguments the model has derived from its input prompt. The server answers with data, which goes back into the model’s context. Authentication follows OAuth: the user logs in at an identity provider, and the MCP server acts as a resource server that checks the token. That is all you need for what follows.

The gateway idea

The project I want to show is called McpAuthGateway. It is an MCP server that holds no data itself. It sits between the AI application and an existing API, and for every request it asks a separate rights system whether the user is allowed to do this. Only if the answer is yes, it calls the API.

In security vocabulary, the gateway is the policy enforcement point (PEP) and the rights system is the policy decision point (PDP). The identity provider (IDP), here Keycloak or Auth0, tells the gateway who the user is. The three roles are separate on purpose: one asks, one decides, one vouches for identity.

Two details connect back to Lilli. Every call needs a valid token, and a call without one is refused. And whatever the model sends as input is never passed on as it is. I will come back to that.

What flows where

Three kinds of data travel through this picture, and they are worth keeping apart.

The prompt (blue). The gateway never sees what the user typed. It only sees the tool arguments the model derived from it. Those arguments are untrusted input, however friendly the prompt looked.

The claims (orange). The user logs in at the identity provider and the application receives an access token. The gateway validates it and turns its claims into a subject (see the side note). The rights system gets subject attributes, never the raw token.

Side note: claims and subject

Claims are the statements inside the access token: who issued it, which user it is about, which scopes are bound to it and which roles it belongs to. Their shape depends on the identity provider. Keycloak and Auth0, for example, put roles in different places.

The subject is what the gateway makes of them: one fixed, provider-independent record with issuer, user ID, client, scopes and roles. Everything after the token check, including the rights system, works with the subject only. It never sees the token itself.

The issuer is part of the subject because a user ID alone is not unique across identity providers.

The data (green). This is the part that matters most. Whatever comes back from the API ends up in the model’s context, and from there it can end up in an answer, a log or a summary. Once a row has been delivered, it is gone. So the restriction must be applied before the query runs, not afterwards. That is also where the Lilli parallel is closest: the model produces query expressions, and the gateway parses them and rebuilds the query from the parsed structure, instead of handing the raw string to the database. I will come back to that in a later article.

McpAuthGateway: What is in the repository

You can find the full code in the McpAuthGateway GitHub repository.

The code is split along the same lines as the picture. ApiConnectors talks to downstream APIs, in this demo an OData service based on Northwind. AuthAdapters talks to the rights system. IdentityAdapters handles Keycloak and Auth0. None of the three is meant to depend on another, and architecture tests are meant to enforce that.

The demo has four users: alice (HR) sees employees, bob (sales) sees customers, dave sees only the UK employees, and carol can see articles but no prices.

An honest state about the current version

This is a skeleton, not a product. It is read-only and uses an in-memory rights system for the demo. It also contains docker definitions for OPA and OpenFGA.

Two things should be said out loud:

First, in this code the enforcement happens in the gateway, not in the downstream API. The downstream API does not know what the user may see. It receives a query that the gateway has already restricted. That is a deliberate choice, and it has a cost: if the gateway is compromised, everything it can reach is exposed. A gateway like this is itself a worthwhile target. A second, independent check inside the downstream API would be better, and the design prepares for it. The next article is about exactly this question.

Second, the gateway would not have stopped the attack on Lilli. That attack went against the platform’s own database, not through tools. And it does not solve everything: if prompts can be rewritten in a database, a permission model on the tool side does not help. What it does is keep the path from the model to the data narrow, and makes sure the rights never depend on what the model says.

Where to go from here

The repository is publicly available at github.com.

You can checkout the code and run it directly with the provided docker images. Just copy the .env.example and adjust the values.

cp docker/.env.example docker/.env # << adjust values here
docker compose -f docker/docker-compose.yml up -d --build
Bash

Then register the gateway as a local MCP server:

claude mcp add --transport http northwind http://localhost:8080/mcp
Bash

This will add an MCP server to your Clause harness with the name northwind. The local MCP gateway comes with the users alice, bob and dave predefined. They all share the same password set in your .env file with MCPGW_DEMO_PASSWORD. The gateway connects to the publiv Northwind OData service available at https://services.odata.org/V4/Northwind/Northwind.svc/. Inside claude you can access the registeeed MCP server with the /mcp command:

The “northwind” MCP server is marked with a yellow triangle, because the gateway requires an authenticated user. Note that the connected Nothwind OData service itself does not need authentication. Selecting the “northwind” MCP gives you the theoption to log onto the gateway / keycloak.

Logged on as “alice”, which is in role “HR” is able to see all employees.

But “alice” is not allowed to see the prices of the products. So asking the Claude to get the products with prices results in a deny from the gateway:

Upcoming: how the downstream API learns what the user may see, and why filtering afterwards does not work.

By marcus

Experienced technology leader and software architect with more than 30 years of experience in software delivery, architecture and leadership. Focus on technical strategy, sustainable architectures and team leadership.