Skip to main content

OpenAPI to MCP: exposing an existing REST API to AI agents

Serve the operations of an OpenAPI document as MCP tools from a Route, with the Route's API key and request limit applying to every tool call an agent makes.

By Akif F. Güven AI Gateway

What you will have at the end

A Route called orders-mcp that an MCP client connects to. The client lists three tools, listOrders, getOrder and createOrder, and calls them; the G8 engine turns each call into a request to your orders service and returns the answer. The agent signs in with an API key like any other Consumer, its calls are counted by limit-count, every call is a request record in Traffic Explorer with the Consumer’s name on it, and the change that put the tools online is in the audit log.

Prerequisites

  • An OpenAPI 3 document for the API, at a URL the G8 engine can read. Here it is http://orders-svc:8080/openapi.json, served by the orders service itself, with three operations and their descriptions.
  • The base URL the operations are called at, http://orders-svc:8080.
  • An Environment with traffic logging turned on under Traffic Explorer › Settings, so the calls appear as records.
  • A role that may manage Routes and Consumers in that Environment.

What changes for the API

AI agents call tools through the Model Context Protocol (MCP). An MCP client asks a server which tools it offers, then calls them with structured input. Most of the operations an agent needs already exist behind REST APIs that have OpenAPI documents.

The openapi-to-mcp Plugin serves those operations as MCP tools. Each operation in the document becomes a tool, named after its operationId and described from its description. When an agent calls a tool, the engine calls the operation on your service and returns the response. The conversion runs inside the engine, in your infrastructure. There is no separate MCP server to install, and your service still receives ordinary HTTP requests.

Step 1: a Route without a backend

Create the Route orders-mcp with the path /mcp/orders and the methods GET, POST and DELETE. Leave Target at None: the Plugin answers the MCP client itself and calls the service on its own, so the Route needs no Upstream.

Step 2: a Consumer for the agent

Under Consumers, create orders_assistant. Open its Credentials tab, select New credential, give it the ID orders-assistant-key, choose the Plugin key-auth and type the key. The key is shown as •••• from then on, to roles that may manage credentials.

Back on the Route, open Plugins and add key-auth with its defaults. From now on a request without a key is refused with 401 before anything else runs:

{"message":"Missing API key in request"}

Add limit-count next: Count 100, Time window 60 seconds, Key consumer_name. Each Consumer gets its own window of 100 tool calls a minute.

Step 3: add openapi-to-mcp

Select Add plugin once more and search the catalog for openapi-to-mcp. Basics takes the two addresses: the OpenAPI document URL the engine reads the operations from, and the Service base URL it calls them at. Select Add plugin with those two alone; the audit log records the Route with the new block, and the tools are online.

Two settings under Behavior were changed in a second save, and the form below shows the Plugin after it. With Transport left empty the engine serves sse: a client then opens the Route with GET, receives an endpoint event that names a session, and sends its messages to that address. Set to streamable_http, the client posts each message to the Route and reads the answer from the response. Flatten parameters decides the shape of a tool’s input. Off, the engine nests the arguments by kind, as pathParameters, queryParameters and requestBody; on, path and query parameters sit at the top level, so getOrder takes {"orderId": "ord-10421"} instead of {"pathParameters": {"orderId": "ord-10421"}}. A request body stays under requestBody either way.

The openapi-to-mcp form on the orders-mcp Route after the second save: OpenAPI document URL and Service base URL of orders-svc, Transport streamable_http, Flatten parameters on The whole configuration after both saves: where the document is, where the service is, how the client talks to the Route and how tool inputs are shaped.

The Route’s Plugins tab now shows the order the engine runs them in: the global rules first, then key-auth, limit-count and openapi-to-mcp by priority.

The Request path card of the orders-mcp Route: global rules first, then key-auth at priority 2500, limit-count at 1002 and openapi-to-mcp at 540, and no Upstream The order is the point. A tool call is authenticated and counted before the conversion runs, and the Route sends nothing to an Upstream of its own.

Step 4: an MCP client lists and calls the tools

Point an MCP client at http://localhost:9281/mcp/orders with the agent’s key in the apikey header. The same exchange with curl, to see the messages: the client sends initialize, then tools/list.

curl -X POST http://localhost:9281/mcp/orders \
  -H "apikey: $AGENT_KEY" \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'

The answer arrives as a server-sent event whose data line is the JSON-RPC result. The three operations of the document are the three tools, with the document’s descriptions and the input each one takes:

listOrders   Lists orders, newest first. Filter by status.        input: status
getOrder     Reads one order by its id.                           input: orderId (required)
createOrder  Creates an order for a customer with one or more lines.  input: requestBody (required)

A call to getOrder with {"orderId": "ord-10421"} comes back as text content holding the service’s answer: the status, the response headers and the order under data. The orders service, for its part, received a plain GET /orders/ord-10421, and a listOrders call with status: open reached it as GET /orders?status=open. The service did not change.

A call with a wrong key never gets this far. It is refused with 401 and Invalid API key in request, and the tool call is never attempted.

What Traffic Explorer shows

Every MCP message is a request to the Route, so every one is a request record. Filter Traffic Explorer by the path /mcp/orders: the list shows the POSTs, their status and latency, the Route and, after the dot, the Consumer that made them. The Upstream node column reads gateway, because the Route has no Upstream of its own; the Plugin’s call to the service happens inside the engine.

Traffic Explorer filtered by the path /mcp/orders: POST records with status 200 and the Route orders-mcp with the Consumer orders_assistant, one 401 without a Consumer Each tool call is one row. The 401 in the middle is the call without a key: no Consumer, refused by the gateway.

Open a record and switch to the Request tab. The apikey header reads ********, masked by the gateway before the record was stored, while x-consumer-username and x-credential-identifier name the Consumer and the credential that admitted the call.

The Request tab of an MCP call's record: the apikey header masked, x-consumer-username orders_assistant and x-credential-identifier orders-assistant-key The key is never stored. Which agent called, and with which credential, is.

What the audit log keeps

Adding the Plugin was a change to the Route, and the audit log has it: who, when, and the Route before and after, with the openapi-to-mcp block as the difference. Later edits to the Plugin, such as the switch to streamable_http or to flattened parameters, are entries of their own.

The audit record of the Route update that added openapi-to-mcp to orders-mcp, before and after side by side The moment the tools went online, as a diff of the Route. The key-auth and limit-count blocks were already there.

Treat the OpenAPI document as part of the agent’s permissions. A new operation in the document is a new tool, and the engine reads the document from the address in the form, so the audit log records who changed that address, not who changed the document behind it. Serve only the operations agents need, from a document you review.

What to do next

  • Tune the agent’s limit, and move the counter to Redis when the Environment has several gateway nodes: Rate limiting per Consumer with limit-count.
  • Let agents sign in with the company’s identity provider instead of a key: Keycloak and G8 puts openid-connect on a Route; it runs before openapi-to-mcp like any other Plugin of the Route.
  • Copy the Route to the production Environment when it works. G8 shows what the copy will change before anything is written: Change control and audit.
  • Read how the AI gateway features fit together on AI Gateway.
Written by
Akif F. Güven
Engineer, G8
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.