Secure Internal MCP Gateway

Minimum Version
AI Gateway - 2.0
Incompatible with
on-prem

Overview

Turn internal REST APIs into MCP tools behind one MCP endpoint in Kong AI Gateway, so that users sign in with your identity provider and their team decides which tools they can call. You’ll set up:

  • Eight MCP tools from three mock REST APIs (orders, inventory, customers), with no MCP server to run
  • One aggregated MCP endpoint that MCP clients discover and sign in to with OAuth 2.1
  • Tool ACLs that give the warehouse-ops and customer-support teams different tools

For third-party MCP servers, for example GitHub, see the Secure External MCP Gateway cookbook.

Prerequisites

This tutorial uses Kong Konnect. The AI Gateway quickstart script provisions a recipe-scoped AI Gateway and local data plane.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  2. Export your token. You reuse the same token later for kongctl commands:

    export KONNECT_TOKEN='YOUR_KONNECT_PAT'
  3. Set the recipe-scoped AI Gateway name and run the quickstart script:

    export KONNECT_CONTROL_PLANE_NAME='secure-internal-mcp-gateway-recipe'
    curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN

    This provisions an AI Gateway named secure-internal-mcp-gateway-recipe, a local data plane connected to it, and prints export lines for the rest of the session vars, including AI_GATEWAY_ID. Paste those into your shell when prompted.

This tutorial uses kongctl to manage AI Gateway configuration.

  1. Install kongctl from the kongctl documentation.
  2. Verify it’s installed:

    kongctl version

The aggregated MCP endpoint authenticates users against your identity provider (IdP) with token introspection, and reads their teams from a groups claim. This cookbook covers Okta and Keycloak. Each setup creates two applications: a confidential client that Kong uses to call the introspection endpoint, and a public client for the MCP client.

This tutorial uses Insomnia as the MCP client. Insomnia connects to MCP servers and runs the OAuth flow that MCP servers advertise (MCP clients in Insomnia).

Install the latest version of Insomnia from insomnia.rest/download.

What this cookbook solves

Most teams expose internal APIs to AI agents through separate MCP servers, each with its own authentication and its own tool catalog, which causes the following problems:

Problem

Solution

Each team runs and secures its own MCP server, so agents connect to many endpoints and each one has its own credentials. One listener AI MCP Server aggregates the tools of each team’s conversion-only AI MCP Server behind /ecommerce-mcp.
Building an MCP server for each REST API takes code that someone has to maintain. A conversion-only AI MCP Server turns REST endpoints into MCP tools from configuration.
Each agent sees every tool, so a warehouse agent can call customer-data tools. Tool ACLs on each tool allow AI Consumer Groups that match IdP groups. Kong removes other tools from tools/list and rejects calls to them.
Offboarding a user means touching every MCP server they had access to. An OpenID Connect AI Auth Strategy validates each token with your IdP, so disabling the user or changing their group in the IdP takes effect at Kong.

Try it out

Apply the configuration, connect Insomnia to the aggregated MCP endpoint, then compare the tools that each team gets.

Apply the Kong configuration

The configuration creates three conversion-only AI MCP Servers (orders, inventory, and customers) with eight tools, one listener AI MCP Server that aggregates them, an OpenID Connect AI Auth Strategy for your IdP, two AI Consumer Groups (warehouse-ops and customer-support), and a CORS Policy. The tools call https://httpbin.konghq.com/anything, which echoes each request. A kongctl namespace scopes every resource, so you can remove this configuration without affecting anything else on the same AI Gateway. See the kongctl documentation for more on namespaces.

Adopt the quickstart AI Gateway into a kongctl namespace so that kongctl can manage it. Adoption adds the KONGCTL-namespace label to the AI Gateway:

kongctl adopt ai-gateway "${KONNECT_CONTROL_PLANE_NAME}" \
  --namespace "${KONNECT_CONTROL_PLANE_NAME}" \
  --pat "${KONNECT_TOKEN}"

Export the URL that MCP clients use for the aggregated endpoint. Kong advertises it as the protected resource in its OAuth metadata:

export MCP_RESOURCE_URL='http://localhost:8000/ecommerce-mcp'

The IdP values come from the prerequisites. The same configuration works with Okta and Keycloak. Apply the Kong configuration:

cat <<'EOF' > kong-recipe.yaml
_defaults:
  kongctl:
    namespace: secure-internal-mcp-gateway-recipe

ai_gateway_auth_strategies:
- ref: secure-internal-mcp-gateway-auth
  ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
  name: secure-internal-mcp-gateway-auth
  display_name: secure-internal-mcp-gateway OIDC auth
  type: openid-connect
  config:
    issuer: !env 'OAUTH_AUTH_SERVER'
    introspection_endpoint: !env 'OAUTH_INTROSPECTION_URL'
    client_id:
    - !env 'OAUTH_CLIENT_ID'
    client_secret:
    - !secret {source: !env 'OAUTH_CLIENT_SECRET'}
    auth_methods:
    - introspection
    cache_introspection: true
    scopes:
    - openid
    consumer_optional: true
    consumer_groups_claim:
    - groups
    consumer_groups_optional: false
    credential_claim:
    - sub
    cache_tokens_salt: secure-internal-mcp-gateway-oidc-cache
    ssl_verify: true
    hide_credentials: true

ai_gateway_consumer_groups:
- ref: warehouse-ops
  ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
  name: warehouse-ops
  display_name: Warehouse Operations
- ref: customer-support
  ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
  name: customer-support
  display_name: Customer Support

ai_gateway_mcp_servers:
- ref: orders-mcp
  ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
  name: orders-mcp
  display_name: Orders API MCP tools
  type: conversion-only
  config:
    url: https://httpbin.konghq.com/anything
    route:
      paths:
      - /orders-api
    max_request_body_size: 1048576
    logging:
      payloads: true
  tools:
  - name: list-orders
    description: >-
      List recent orders with optional filters for status and date.
      Returns order summaries including order ID, status, and total.
    method: GET
    path: orders
    annotations:
      read_only_hint: true
      title: List orders
    access:
      acls:
        allow:
        - warehouse-ops
        - customer-support
    parameters:
    - name: status
      in: query
      required: false
      description: Filter by order status (pending, shipped, delivered, cancelled)
      schema:
        type: string
    - name: date
      in: query
      required: false
      description: Filter by order date (YYYY-MM-DD)
      schema:
        type: string
  - name: get-order
    description: >-
      Get full details for a specific order including line items,
      shipping address, and payment status.
    method: GET
    path: orders/{orderId}
    annotations:
      read_only_hint: true
      title: Get order details
    access:
      acls:
        allow:
        - warehouse-ops
        - customer-support
    parameters:
    - name: orderId
      in: path
      required: true
      description: The order ID
      schema:
        type: string
  - name: cancel-order
    description: >-
      Cancel a pending order. This action cannot be undone.
      Only customer support agents can perform cancellations.
    method: POST
    path: orders/{orderId}/cancel
    annotations:
      destructive_hint: true
      title: Cancel order
    access:
      acls:
        allow:
        - customer-support
    parameters:
    - name: orderId
      in: path
      required: true
      description: The order ID to cancel
      schema:
        type: string

- ref: inventory-mcp
  ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
  name: inventory-mcp
  display_name: Inventory API MCP tools
  type: conversion-only
  config:
    url: https://httpbin.konghq.com/anything
    route:
      paths:
      - /inventory-api
    max_request_body_size: 1048576
    logging:
      payloads: true
  tools:
  - name: list-inventory
    description: >-
      List inventory items with optional filters for category
      or low stock alerts.
    method: GET
    path: inventory
    annotations:
      read_only_hint: true
      title: List inventory
    access:
      acls:
        allow:
        - warehouse-ops
        - customer-support
    parameters:
    - name: category
      in: query
      required: false
      description: Filter by product category
      schema:
        type: string
    - name: low_stock
      in: query
      required: false
      description: Set to true to show only items below restock threshold
      schema:
        type: string
  - name: check-inventory
    description: >-
      Check current stock level, reorder threshold, and product
      details for a specific SKU.
    method: GET
    path: inventory/{sku}
    annotations:
      read_only_hint: true
      title: Check inventory for SKU
    access:
      acls:
        allow:
        - warehouse-ops
        - customer-support
    parameters:
    - name: sku
      in: path
      required: true
      description: The product SKU
      schema:
        type: string
  - name: restock-item
    description: >-
      Add units to a product's inventory. Only warehouse
      operations staff can perform restocking.
    method: POST
    path: inventory/{sku}/restock
    annotations:
      destructive_hint: true
      title: Restock inventory item
    access:
      acls:
        allow:
        - warehouse-ops
    parameters:
    - name: sku
      in: path
      required: true
      description: The product SKU to restock
      schema:
        type: string
    - name: quantity
      in: query
      required: true
      description: Number of units to add to inventory
      schema:
        type: integer

- ref: customers-mcp
  ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
  name: customers-mcp
  display_name: Customers API MCP tools
  type: conversion-only
  config:
    url: https://httpbin.konghq.com/anything
    route:
      paths:
      - /customers-api
    max_request_body_size: 1048576
    logging:
      payloads: true
  tools:
  - name: get-customer
    description: >-
      Retrieve customer profile including name, contact info,
      and account status. Only customer support can access
      customer data.
    method: GET
    path: customers/{customerId}
    annotations:
      read_only_hint: true
      title: Get customer details
    access:
      acls:
        allow:
        - customer-support
    parameters:
    - name: customerId
      in: path
      required: true
      description: The customer ID
      schema:
        type: string
  - name: update-customer-contact
    description: >-
      Update a customer's contact information. Only customer
      support agents can modify customer records.
    method: PUT
    path: customers/{customerId}/contact
    annotations:
      destructive_hint: true
      title: Update customer contact info
    access:
      acls:
        allow:
        - customer-support
    parameters:
    - name: customerId
      in: path
      required: true
      description: The customer ID to update
      schema:
        type: string
    - name: email
      in: query
      required: false
      description: New email address
      schema:
        type: string
    - name: phone
      in: query
      required: false
      description: New phone number
      schema:
        type: string

- ref: ecommerce-mcp
  ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
  name: ecommerce-mcp
  display_name: Aggregated Ecommerce MCP Server
  type: listener
  sources:
  - orders-mcp
  - inventory-mcp
  - customers-mcp
  access:
    acl_attribute_type: consumer
    auth_strategies:
    - secure-internal-mcp-gateway-auth
    metadata:
      resource: !env 'MCP_RESOURCE_URL'
      endpoint: /.well-known/oauth-protected-resource/ecommerce-mcp
  config:
    max_request_body_size: 1048576
    logging:
      payloads: true
      audits: true
    route:
      paths:
      - /ecommerce-mcp
      protocols:
      - http
      - https
    server:
      forward_client_headers: true
  policies:
  - secure-internal-mcp-gateway-cors

ai_gateway_policies:
- ref: secure-internal-mcp-gateway-cors
  ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
  name: secure-internal-mcp-gateway-cors
  display_name: secure-internal-mcp-gateway CORS
  type: cors
  config:
    origins:
    - '*'
    methods:
    - GET
    - HEAD
    - PUT
    - PATCH
    - POST
    - DELETE
    - OPTIONS
    credentials: false
    preflight_continue: false
EOF

kongctl apply -f kong-recipe.yaml -o text --auto-approve --pat "${KONNECT_TOKEN}"

rm -f kong-recipe.yaml

Connect Insomnia to Kong

Create an MCP client in Insomnia. When Kong rejects the first request, Insomnia reads Kong’s protected resource metadata, finds your IdP, and runs the sign-in.

  1. In Insomnia, click MCP Clients in the left pane, name the client Ecommerce MCP, and click Create.
  2. In MCP Server URL, enter http://localhost:8000/ecommerce-mcp.
  3. Click the Auth tab, select OAuth 2.0, set Grant Type to MCP Auth Flow, and enter the Client ID of the MCP Client application (Okta) or mcp-client (Keycloak). Leave Client Secret empty.
  4. Click Connect. Kong answers with 401 and a link to its protected resource metadata. Insomnia opens your IdP in the browser.
  5. Sign in as your test user. Insomnia connects with the IdP token. The Events tab shows each step of the OAuth flow.

Call tools as warehouse-ops

The left pane lists the tools that Kong returns for the warehouse-ops group: list-orders, get-order, list-inventory, check-inventory, and restock-item. Kong removed cancel-order, get-customer, and update-customer-contact.

Call check-inventory with sku set to WIDGET-42. The response is httpbin’s echo of the request, with the URL https://httpbin.konghq.com/anything/inventory/WIDGET-42.

If a client calls a tool that its AI Consumer Group can’t use, for example cancel-order, Kong rejects the tools/call request with 403 Forbidden before it reaches the API (ACL evaluation logic).

Switch to customer-support

The groups claim decides the AI Consumer Group, so you change a user’s tools in the IdP, not in Kong.

  1. In your IdP, remove the test user from warehouse-ops and add the user to customer-support.
  2. In Insomnia, disconnect, delete the access token on the Auth tab, and connect again. Sign in when the browser opens.

The tool list now has seven tools: each tool except restock-item.

Check the results

The Insomnia sessions confirm the following:

Behavior

Evidence

OAuth discovery and sign-in Insomnia gets a 401 from Kong, then opens your IdP’s sign-in page without any OAuth endpoint in its configuration.
One endpoint for three APIs Tools from the orders, inventory, and customers AI MCP Servers appear in one tool list.
REST-to-MCP conversion check-inventory returns httpbin’s echo of GET /anything/inventory/WIDGET-42.
Tool ACLs from IdP groups warehouse-ops gets five tools, and customer-support gets seven. Changing the IdP group changes the list after the next sign-in.

To review the created entities and the MCP traffic, open the secure-internal-mcp-gateway-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.

How it works

The MCP client connects to the listener AI MCP Server on /ecommerce-mcp. Kong validates the IdP token, maps the groups claim to an AI Consumer Group, filters the aggregated tool list with each tool’s ACL, and converts each allowed tool call into an HTTP request to the team’s API.

sequenceDiagram
    participant C as MCP Client
    participant K as Kong AI Gateway
    participant I as Identity Provider
    participant B as Backend APIs

    C->>K: MCP request, no token
    activate K
    K-->>C: 401 and protected resource metadata
    deactivate K
    C->>I: Authorization Code with PKCE
    activate I
    I-->>C: Access token
    deactivate I
    C->>K: tools/list with the IdP token
    activate K
    K->>I: Introspect the token (else 401)
    activate I
    I-->>K: Claims with sub and groups
    deactivate I
    K->>K: Map groups to an AI Consumer Group (else rejected)
    K-->>C: Tools that the group can call
    deactivate K
    C->>K: tools/call
    activate K
    K->>K: Tool ACL for the AI Consumer Group (else 403)
    K->>B: HTTP request for the tool
    activate B
    B-->>K: Response
    deactivate B
    K-->>C: Tool result
    deactivate K

AI MCP Servers

Each team’s API has a conversion-only AI MCP Server, which defines MCP tools from REST endpoints and their ACLs. A conversion-only AI MCP Server accepts no MCP traffic itself. The listener AI MCP Server lists them by name in sources, exposes all their tools on one route, and applies the ACL that travels with each tool. When a client calls a tool, the listener sends the HTTP request through the conversion-only AI MCP Server’s route plus the tool’s path (Tool paths in conversion-only servers).

The following settings configure the AI MCP Servers:

Setting

Purpose

type: conversion-only Defines tools for aggregation. The orders, inventory, and customers AI MCP Servers use it.
config.url The API base URL. All three APIs use httpbin in this cookbook.
config.route.paths (conversion-only) The route that the listener sends tool calls through, for example /inventory-api. Without a route, the data plane can’t compile the configuration.
tools[].method, path, and parameters The HTTP request that the tool sends, and the tool’s input. The path has no leading /, so Kong adds it to the route. Path parameters such as {sku} fill the path.
tools[].annotations MCP hints for agents, for example read_only_hint and destructive_hint.
tools[].access.acls.allow The AI Consumer Groups that can call the tool. The ACL travels with the tool to the listener.
type: listener and sources Aggregates the tools of the three conversion-only AI MCP Servers on one route.
access.auth_strategies Each request needs a valid IdP token.
access.acl_attribute_type: consumer Kong evaluates the ACLs against the caller’s AI Consumer Groups.
access.metadata.resource and endpoint Kong serves protected resource metadata for MCP_RESOURCE_URL at /.well-known/oauth-protected-resource/ecommerce-mcp and adds the path to the route. The authorization server and scopes come from the AI Auth Strategy’s issuer and scopes.
config.route.paths: [/ecommerce-mcp] (listener) The endpoint that MCP clients call.
config.server.forward_client_headers: true Kong forwards the client’s request headers to the APIs when it calls a tool. The OpenID Connect AI Auth Strategy already removed the IdP token.
config.logging.payloads and audits: true Kong logs MCP payloads and an audit entry for each ACL decision.
config.max_request_body_size: 1048576 Limits request bodies to 1 MB, which is enough for these tool calls.

The tools have the following access:

Tool

warehouse-ops

customer-support

list-orders, get-order, list-inventory, check-inventory Allowed Allowed
restock-item Allowed Denied
cancel-order, get-customer, update-customer-contact Denied Allowed

OpenID Connect AI Auth Strategy and AI Consumer Groups

Authentication is an AI Auth Strategy, not an AI Policy. The OpenID Connect AI Auth Strategy validates each token with your IdP’s introspection endpoint and maps the values in the groups claim to the AI Consumer Groups with the same names. The AI Consumer Groups have no members in Kong, because the IdP decides group membership. Because the listener sets access.metadata, AI Gateway adds the OAuth resource server behavior that MCP clients expect (Protected resource metadata).

The following settings configure the OpenID Connect AI Auth Strategy:

Setting

Purpose

issuer Your IdP’s authorization server, from OAUTH_AUTH_SERVER. The protected resource metadata advertises it.
auth_methods: [introspection] Kong validates the token with the introspection endpoint, so the IdP can issue opaque tokens.
introspection_endpoint, client_id, and client_secret The introspection endpoint and the Kong MCP Gateway client’s credentials. client_id and client_secret are lists.
cache_introspection: true Kong caches introspection results, so it doesn’t call the IdP for each request.
scopes: [openid] The scope that the protected resource metadata advertises, so the MCP client requests it.
consumer_optional: true Kong accepts the request without an AI Consumer. The AI Auth Strategy sets consumer_groups_claim and no consumer_claims: Konnect accepts only one of the two.
consumer_groups_claim: [groups] Kong maps each value in the groups claim to the AI Consumer Group with the same name.
consumer_groups_optional: false Kong rejects a user whose groups match no AI Consumer Group, so a user without a team can’t connect.
credential_claim: [sub] Kong identifies a caller without an AI Consumer by the sub claim.
cache_tokens_salt A stable, non-secret value for the token cache keys.
ssl_verify: true Kong verifies the IdP’s TLS certificate.
hide_credentials: true Kong removes the IdP token before it calls the APIs.

The AI Auth Strategy has no audience_required, so Kong doesn’t check the token’s aud claim against MCP_RESOURCE_URL. Okta and Keycloak don’t issue tokens for that audience by default. Group mapping works without an AI Consumer: the groups claim from introspection selects the AI Consumer Group, and the tool ACLs use it.

Production credentials: This cookbook stores the IdP client secret in an environment variable for simplicity. In production, use AI Vaults to reference it from a secret manager, for example AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager, or Azure Key Vault.

CORS Policy

The CORS Policy on the listener answers browser preflight requests, so MCP clients that run in a browser can call the endpoint. Insomnia doesn’t need it.

The following settings configure the CORS Policy:

Setting

Purpose

origins: ['*'] The value of the Access-Control-Allow-Origin header: any origin. In production, list your clients’ origins.
methods The value of the Access-Control-Allow-Methods header.
credentials: false Kong doesn’t send Access-Control-Allow-Credentials: true. MCP clients authenticate with the Authorization header, not cookies.
preflight_continue: false Kong answers OPTIONS preflight requests instead of proxying them to the APIs.

Variations and next steps

You can change or extend this configuration in the following ways:

Variation

How

Point the tools at real APIs Change config.url on each conversion-only AI MCP Server to the team’s API, and adjust each tool’s path and parameters.
Add another team’s tools Add a conversion-only AI MCP Server with its tools and ACLs, and add its name to the listener’s sources. The AI Auth Strategy and the AI Consumer Groups don’t change.
Aggregate an existing MCP server Add an upstream-server AI MCP Server for the MCP server and add it to sources. See Tool aggregation with upstream-server.
Set a default tool ACL Set access.default_tool_acls on the listener for tools that have no ACL of their own. A tool’s own ACL replaces the default. See ACL tool control.
Validate JWTs locally For an IdP without an introspection endpoint, set auth_methods: [bearer] and leave out introspection_endpoint. Kong validates the JWT with the IdP’s signing keys. See OIDC token authentication.
Check the token audience If your IdP issues tokens for the MCP resource URL, set audience_required on the AI Auth Strategy to that URL.
Limit calls for each team Attach a Rate Limiting Advanced Policy to the listener to limit requests for each AI Consumer or AI Consumer Group. See Rate limiting.
Connect another MCP client Add the client’s OAuth redirect URL to your IdP’s MCP client application. Token validation and the tool ACLs don’t change.

Cleanup

The recipe’s kongctl namespace scoped all resources, so this teardown removes only this recipe’s configuration. Tear down the local data plane and delete the AI Gateway from Konnect:

export KONNECT_CONTROL_PLANE_NAME='secure-internal-mcp-gateway-recipe' && curl -Ls https://get.konghq.com/ai | bash -s -- -d -k $KONNECT_TOKEN

In Insomnia, delete the Ecommerce MCP MCP client. In your IdP, delete the two applications and the two groups that you created.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!