ORDS MCP with Deep Data Security for Row and Column Access

MCP standardizes service access for AI Agents. But, are you adequately protecting the APIs you expose over MCP? Think TLS, role-based access, and for NL2SQL: row-and-column level security.

Agents should connect securely, authenticate, and have access only to the resources they need. When you use the Oracle REST Data Services (ORDS) MCP server, this means using Oracle Deep Data Security to restrict agent access (using data grants) to only the data they should see.

Diagram showing SALES_US and SALES_CA agents querying an ORDS MCP Oracle AI Database with data grants; US returns four rows with masked email, while CA returns two rows with visible email.
Agent queries are filtered server side using Deep Data Security data grants.

This local lab demonstrates how to use ORDS with an identity provider (Keycloak) to provide row-and-column level security to agents. ORDS exposes SQL tools over MCP so clients like Codex and Claude Code can run queries. Keycloak supplies the caller’s roles, and Oracle Deep Data Security controls the data each query returns.

The lab runs locally with Docker Compose, Oracle REST Data Services (ORDS), Keycloak, and Oracle AI Database Free.  You can download the lab from GitHub here, which builds on the ORDS with Docker Compose walkthrough.

Start the lab (README)

To run the lab locally, you’ll need Docker Compose and about 8 GB of free memory to run Oracle AI Database Free, ORDS, and Keycloak as containers.

From the repository checkout, run these commands.

cd ords-docker-compose
docker compose --env-file deep-sec/compose.env up -d --wait

Once the containers are ready, the ORDS MCP endpoint will be http://localhost:8888/mcp. The lab imports one Keycloak OAuth client and three demo users. A get-token.sh helper script is provided to retrieve JWTs from Keycloak.

MCP client entryToken helper argumentsKeycloak sales roleORDS pool
ords-sales-us--headers sales-usSALES_USsales-us
ords-sales-ca--headers sales-caSALES_CAsales-ca

Use the MCP client of your choice, or connect to Codex/Claude Code as shown below.

Connect Codex desktop or CLI

Add or update an ords MCP server in your ~/.codex/config.toml, replacing the script path with the absolute path to the repository checkout:

[mcp_servers.ords-sales-us]
url = "http://localhost:8888/mcp"
http_headers_helper = "/bin/bash \"/absolute/path/to/oracle-database-java-samples/ords-docker-compose/scripts/get-token.sh\" --headers sales-us"

[mcp_servers.ords-sales-ca]
url = "http://localhost:8888/mcp"
http_headers_helper = "/bin/bash \"/absolute/path/to/oracle-database-java-samples/ords-docker-compose/scripts/get-token.sh\" --headers sales-ca"

The token script returns a raw token by default or authorization-header JSON with --headers.

Restart Codex once to load the configuration. Quote the script path inside the command string if it contains spaces.

Connect Claude Code

Merge this entry into .mcp.json in this lab directory, replacing the script path with the absolute path to the repository checkout:

{
  "mcpServers": {
    "ords-sales-us": {
      "type": "http",
      "url": "http://localhost:8888/mcp",
      "headersHelper": "/bin/bash \"/absolute/path/to/oracle-database-java-samples/ords-docker-compose/scripts/get-token.sh\" --headers sales-us"
    },
    "ords-sales-ca": {
      "type": "http",
      "url": "http://localhost:8888/mcp",
      "headersHelper": "/bin/bash \"/absolute/path/to/oracle-database-java-samples/ords-docker-compose/scripts/get-token.sh\" --headers sales-ca"
    }
  }
}

Start claude from this directory, accept the directory trust and project MCP prompts, and use /mcp to check the connection. The token helper supplies fresh authorization headers at connection time.

See the official Claude Code dynamic-header documentation.

Run the same query as both users

In your MCP client, ask each entry to call database_list to find its pool, then use sql_run to execute the following query.

select id, name, country, email
from testuser.customers
order by id;
Table titled “One query, two results” showing TESTUSER.CUSTOMERS records: US rows have masked NULL US pool emails and excluded Canada emails, while Canadian rows show taylor@example.com and riley@example.com in the Canada pool.

Deep Data Security applies the row filter server-side and masks unauthorized column values to NULL as Oracle AI Database executes the SQL. About data grants

To test unauthorized access, try asking the ords-sales-us client to run the query on the sales-ca pool: you should receive an unauthorized response.

Try your own queries to experiment with each pool.

How access is enforced

Diagram showing Keycloak JWT roles controlling ORDS MCP access to sales-us and sales-ca pools with corresponding reader roles.

ORDS validates the Keycloak token and reads its top-level roles claim. All demo users have ORDS_MCP_SERVER for access to the MCP endpoint. Each sales pool has an mcp.role setting that requires its corresponding sales role.

The sales-us pool connects as SALES_US_READER, and the sales-ca pool connects as SALES_CA_READER.  See ORDS role mode docs and Lab configuration script for additional info.

Each database end user has a Deep Data Security data role, and each data role has a read-only grant on the same table. Here are the grants from the lab. Lab grants SQL

create or replace data grant testuser.ords_sales_us_customers
  as select (id, name, city, country)
  on testuser.customers
  where country = 'US'
  to ords_sales_us;

create or replace data grant testuser.ords_sales_ca_customers
  as select (id, name, email, city, country)
  on testuser.customers
  where country = 'CA'
  to ords_sales_ca;

The column list specifies which values are visible, and the WHERE clause limits the rows the role can read. Oracle AI Database enforces both parts of the grant even when the MCP client supplies its own SQL. See Creating data grants for information on writing your own data grants.

What about passing the user’s token to the database?

ORDS also supports the APP_TOKEN_WITH_END_USER_TOKEN option. In this mode, ORDS obtains its own database-access token and attaches the caller’s token as the database end-user context. With an appropriate identity provider configuration, one pool can serve multiple roles and Oracle AI Database can map the caller’s Keycloak realm roles to data roles. See ORDS application token authentication and Keycloak role mapping for more information.

Stop the lab

When you’re done, shut down the lab from the ords-docker-compose/ directory:

docker compose --env-file deep-sec/compose.env down

Response

  1. Harries Steele Avatar

    Putting the row-and-column filters on the ORDS side, as Deep Data Security data grants, is the right place for natural-language SQL agents: the model can still call the SQL tools, but it never sees rows or columns it was not granted. Wiring Keycloak roles into separate ORDS pools, as in the sales-us lab pattern, also keeps the identity mapping explicit instead of hoping a prompt will stay inside its scope. The enforce-on-the-server design matters more than transport encryption and a bearer token alone, because a chatty agent will otherwise explore the schema until it finds data nobody intended to expose.

Leave a Reply