Skip to main content

OpenAPI to MCP

The openapi-to-mcp plugin turns an API described by an OpenAPI document into an MCP (Model Context Protocol) server without requiring changes to the upstream application. It derives tools from the API operations, handles tool discovery and invocation over Streamable HTTP or Server-Sent Events (SSE), and translates tool calls into upstream HTTP requests.

The plugin is available in both APISIX and API7 Enterprise. APISIX generates tools and invokes APIs inside the gateway. API7 Enterprise forwards MCP requests to a separate OpenAPI-to-MCP service. The two implementations share the basic route configuration but differ in deployment, caching, header forwarding, and supported options.

Before exposing an MCP route to clients, review the OpenAPI-to-MCP security boundaries and resource limits.

The following sequence shows the native APISIX request flow:

Examples​

In the tabbed examples, the Admin API commands target APISIX, ADC can target APISIX or API7 Gateway, and the Ingress Controller resources apply to either gateway. Complete Prepare API7 Gateway before using an API7 Gateway workflow.

The examples run the official swaggerapi/petstore3:1.0.27 image with a pinned manifest digest. Petstore provides both the OpenAPI document and the sample API, keeping tool discovery and invocation reproducible.

Start Petstore​

Use the tab that matches the gateway deployment:

Set GATEWAY_CONTAINER to the running APISIX or API7 Gateway container, then place the gateway and Petstore on a dedicated network. In an API7 Gateway deployment, the OpenAPI-to-MCP service must share the gateway's network namespace as described in Prepare API7 Gateway.

export GATEWAY_CONTAINER=replace-with-gateway-container-name

docker network inspect gateway-petstore-net > /dev/null 2>&1 || \
docker network create gateway-petstore-net

if ! docker inspect "$GATEWAY_CONTAINER" \
--format '{{json .NetworkSettings.Networks}}' \
| grep -q '"gateway-petstore-net"'; then
docker network connect gateway-petstore-net "$GATEWAY_CONTAINER"
fi

Create a Compose file for Petstore on the gateway's shared network:

docker-compose.petstore.yaml
services:
petstore:
image: swaggerapi/petstore3:1.0.27@sha256:749432676c31cd05256f0d9e878302164debb94daa1c1223e193747e1b34e7d1
platform: linux/amd64
networks:
- gateway-petstore-net
ports:
- "18080:8080"

networks:
gateway-petstore-net:
external: true

Start or update Petstore with a fixed Compose project name. You can rerun the same command from any of these guides:

docker compose -p api7-docs-petstore \
-f docker-compose.petstore.yaml up -d

Once the container has started, confirm that the OpenAPI document is available:

curl -fsS "http://127.0.0.1:18080/api/v3/openapi.json" > /dev/null

Prepare API7 Gateway​

API7 Gateway forwards MCP requests to a separate OpenAPI-to-MCP service. Native APISIX runs the MCP implementation inside the gateway and does not require this step.

Starting with API7 Enterprise 3.9.10, the service is no longer bundled in the gateway image. Deploy it in the same network namespace as the gateway:

  • Kubernetes (Helm): Set openapiToMcp.enabled: true in the gateway chart values to run the compatible service image as a sidecar.
  • Docker or bare metal: Run api7/openapi-to-mcp:1.0.6 and share the gateway's network namespace, for example with --network=container:<gateway> or host networking. A shared Docker bridge network is insufficient because the plugin connects to 127.0.0.1:3000 by default.

A refused connection to the service normally returns 502. This also applies to mcp-tools-acl. To use another port, see Static Configurations.

Add OpenAPI-to-MCP to Docker Compose​

When you add a gateway instance in the Dashboard, it generates a ready-to-use docker-compose.yaml. Keep the generated gateway service unchanged and add the OpenAPI-to-MCP service:

docker-compose.yaml
services:
openapi-to-mcp:
image: api7/openapi-to-mcp:1.0.6
network_mode: "service:gateway"
restart: always

The network_mode setting makes the service share the gateway service's network stack so the plugin can reach it at 127.0.0.1:3000. If the generated gateway service has another name, replace gateway with that name. A shared Docker bridge network is not sufficient.

Start the services:

docker compose up -d

Expose Petstore as MCP Tools​

This example exposes Petstore through a stateless MCP endpoint. The Admin API tab configures native APISIX; ADC and Ingress Controller can configure APISIX or API7 Gateway.

Create a route with the openapi-to-mcp plugin:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "openapi-to-mcp-route",
"uri": "/mcp",
"methods": ["POST"],
"plugins": {
"openapi-to-mcp": {
"transport": "streamable_http",
"base_url": "http://petstore:8080/api/v3",
"headers": {
"Authorization": "special-key"
},
"openapi_url": "http://petstore:8080/api/v3/openapi.json"
}
}
}'

❶ Configure the route to accept POST requests for MCP messages such as tools/list and tools/call.

❷ Configure the transport method to be streamable_http (recommended for production).

❸ Configure the Petstore API address.

❹ Add an illustrative header to requests sent to the Petstore API.

❺ Configure the Petstore OpenAPI document URL.

List the generated tools. Streamable HTTP clients must accept both JSON and event-stream responses:

curl -N "http://127.0.0.1:9080/mcp" \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}'

The response should contain a tools list that includes getPetById.

After applying the Admin API, ADC, or Ingress Controller configuration, update your MCP settings with the gateway address and append the previously created route path. For instance:

mcp.json
{
"mcpServers": {
"api7-petstore-mcp": {
"url": "http://123.123.123.123:9080/mcp"
}
}
}

If the configuration is successful, you should see the available tools (external functions or services exposed to AI clients through MCP).

You can now interact with the Petstore service directly from the chat window of your AI client. For example, try asking: "Show me pet 1 from the petstore."

AI client interaction with Petstore

Configure Authentication for MCP Routes​

The following example demonstrates how to expose Petstore APIs through the MCP protocol when the route is protected by an authentication method such as key-auth.

Create a consumer johndoe:

curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"username": "johndoe"
}'

Configure the key-auth credential for johndoe:

curl "http://127.0.0.1:9180/apisix/admin/consumers/johndoe/credentials" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "cred-john-key-auth",
"plugins": {
"key-auth": {
"key": "john-key"
}
}
}'

Create a route with the openapi-to-mcp and key-auth plugins:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "openapi-to-mcp-route",
"uri": "/mcp",
"methods": ["POST"],
"plugins": {
"openapi-to-mcp": {
"transport": "streamable_http",
"base_url": "http://petstore:8080/api/v3",
"headers": {
"Authorization": "special-key"
},
"openapi_url": "http://petstore:8080/api/v3/openapi.json"
},
"key-auth": {
"header": "apikey"
}
}
}'

When an MCP server requires authentication, you can specify headers in the mcp.json configuration. Refer to the documentation of your AI client to verify whether headers are supported.

If Headers Are Supported​

After applying the Admin API, ADC, or Ingress Controller configuration, update Cursor with the gateway address and the route path created above. Include the header required by key-auth:

mcp.json
{
"mcpServers": {
"api7-petstore-mcp": {
"url": "http://123.123.123.123:9080/mcp",
"headers": {
"apikey": "john-key"
}
}
}
}

The configured headers will be added to every POST request.

If the configuration is successful, you should see the available tools (external functions or services exposed to AI clients through MCP). You can then interact with Petstore directly from the chat window of your AI client.

If the authentication header is not configured in mcp.json, the AI client will be unable to load tools from the MCP server.

If Headers Are Not Supported​

If your AI client does not support configuring headers in mcp.json, you can include the authentication credential in the MCP URL query, since key-auth supports obtaining credential from the URL query.

Update the key-auth configuration on the route as such:

curl "http://127.0.0.1:9180/apisix/admin/routes/openapi-to-mcp-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"plugins": {
"key-auth": {
"query": "apikey"
}
}
}'

Authenticate every MCP request. Streamable HTTP uses POST for initialization, discovery, and tool calls. Use a client that sends the credential on every request; if it does not retain query credentials, configure a supported authentication header instead.

The example reads apikey from the query. The client must keep that credential on subsequent requests as well.

After applying the Admin API, ADC, or Ingress Controller configuration, include the credential in the gateway address query parameter:

mcp.json
{
"mcpServers": {
"api7-petstore-mcp": {
"url": "http://123.123.123.123:9080/mcp?apikey=john-key"
}
}
}

If the configuration is successful, you should see the available tools (external functions or services exposed to AI clients through MCP). You can then interact with Petstore directly from the chat window of your AI client.

If the authentication credential is not configured in the MCP server URL query, the AI client will be unable to load tools from the MCP server.

Flatten Tool Schema Parameters​

The following example demonstrates how flatten_parameters affects the structure of query and path parameters in the generated MCP tool input schema.

Complete the previous example using Admin API, ADC, or Ingress Controller to set up MCP access to the Petstore APIs. Although the configuration does not explicitly set flatten_parameters, the parameter defaults to false.

In your AI client, such as Cursor, inspect the tool input schema. You should see parameters nested under pathParameters and queryParameters:

{
"operations": {
...,
"getPetById": {
"method": "GET",
"path": "/pet/{petId}",
"pathParameters": {
"type": "object",
"required": ["petId"],
"properties": {
"petId": {
"type": "integer",
"description": "ID of pet to return"
}
},
"additionalProperties": false
}
},
"findPetsByStatus": {
"method": "GET",
"path": "/pet/findByStatus",
"queryParameters": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["available", "pending", "sold"],
"description": "Status values that need to be considered for filter",
"default": "available"
}
},
"additionalProperties": false
}
}
}
}

Update the plugin to flatten query and path parameters:

curl "http://127.0.0.1:9180/apisix/admin/routes/openapi-to-mcp-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"plugins": {
"openapi-to-mcp": {
"flatten_parameters": true
}
}
}'

In your AI client, such as Cursor, inspect the tool input schema. You should see that parameters like status are no longer nested under pathParameters or queryParameters:

{
"operations": {
...,
"getPetById": {
"parameters": {
"type": "object",
"required": ["petId"],
"properties": {
"petId": {
"type": "integer",
"description": "ID of pet to return"
}
},
"additionalProperties": false
}
},
"findPetsByStatus": {
"parameters": {
"type": "object",
"properties": {
"status": {
"type": "string",
"enum": ["available", "pending", "sold"],
"description": "Status values that need to be considered for filter",
"default": "available"
}
},
"additionalProperties": false
}
}
}
}

Customize MCP Tool Annotations​

Availability

MCP tool annotations were introduced in API7 Enterprise 3.9.7 and APISIX 3.19.0.

The following example demonstrates how to add MCP tool annotations to OpenAPI operations exposed by the openapi-to-mcp plugin.

Without these annotations, AI clients only receive the generated tool name, description, and input schema. They cannot reliably tell whether a tool is read-only, destructive, or idempotent, which makes it harder to rank tools correctly and use them safely.

This is implemented in the bundled OpenAPI-to-MCP converter in two ways:

  1. It infers default tool behavior from the HTTP method.
  2. It reads explicit operation-level configuration from the OpenAPI vendor extension x-mcp-annotations.

When both are present, explicit x-mcp-annotations values override the inferred defaults.

Complete the previous example using Admin API, ADC, or Ingress Controller to expose an OpenAPI document through the openapi-to-mcp plugin, then add annotations to the OpenAPI operations:

The Petstore instance serves a fixed OpenAPI document. To apply x-mcp-annotations, host a modified copy of the document and update openapi_url to point to it.

openapi.yaml
paths:
/users/{id}:
get:
operationId: getUser
summary: Get user information
x-mcp-annotations:
title: Get User
readOnlyHint: true
openWorldHint: false
delete:
operationId: deleteUser
summary: Delete a user
x-mcp-annotations:
title: Delete User
destructiveHint: true

Supported annotation fields:

  • title
  • readOnlyHint
  • destructiveHint
  • idempotentHint
  • openWorldHint

If x-mcp-annotations is not configured, the converter still applies default inference rules:

  • GET, HEAD, and OPTIONS map to readOnlyHint: true
  • DELETE maps to destructiveHint: true and idempotentHint: true
  • PUT maps to idempotentHint: true

After updating the hosted OpenAPI document, ask your MCP client to list tools. The following snippet shows the result.tools portion of the tools/list response:

{
"tools": [
{
"name": "getUser",
"annotations": {
"title": "Get User",
"readOnlyHint": true,
"openWorldHint": false
}
},
{
"name": "deleteUser",
"annotations": {
"title": "Delete User",
"destructiveHint": true,
"idempotentHint": true
}
}
]
}

Notes:

  • Only operation-level x-mcp-annotations is supported.
  • Invalid values and unsupported fields are ignored.
  • summary and description still control the generated tool description.
  • title is only read from x-mcp-annotations.title.

Forward Client Headers to an Upstream API​

API7 Gateway can forward per-client context, such as a tenant identifier or a user token, through the OpenAPI-to-MCP service. Native APISIX handles tool-call headers inside the gateway and does not use this client-header convention.

This example exposes HTTPBin's header inspection operation, protects the MCP route with key-auth, and verifies that a tenant header from the MCP client reaches the upstream API. It uses an immutable OpenAPI 3 document so the generated tool remains reproducible. Complete Prepare API7 Gateway before continuing.

Create the consumer and service configuration:

adc.yaml
consumers:
- username: dynamic-header-client
labels:
docs-example: openapi-to-mcp-client-headers
credentials:
- name: primary-key
type: key-auth
config:
key: john-key
services:
- name: openapi-to-mcp-client-headers
labels:
docs-example: openapi-to-mcp-client-headers
upstream:
type: roundrobin
scheme: https
pass_host: node
nodes:
- host: httpbin.org
port: 443
weight: 1
routes:
- name: openapi-to-mcp-client-headers
uris:
- /mcp/headers
methods:
- POST
plugins:
key-auth:
header: apikey
openapi-to-mcp:
transport: streamable_http
base_url: "https://httpbin.org"
allowed_hosts:
- httpbin.org
openapi_url: "https://raw.githubusercontent.com/Kong/swagger-ui-kong-theme/d24578902104603582a145d2cb394a44c0520c61/demo/public/specs/httpbin.yaml"

Preview changes owned by this example and confirm that the diff contains no unintended updates or deletions:

adc diff -f adc.yaml \
--include-resource-type service \
--include-resource-type consumer \
--label-selector docs-example=openapi-to-mcp-client-headers

Synchronize the reviewed configuration:

adc sync -f adc.yaml \
--include-resource-type service \
--include-resource-type consumer \
--label-selector docs-example=openapi-to-mcp-client-headers

Configure the MCP client with the gateway credential and a tenant header:

mcp.json
{
"mcpServers": {
"httpbin-headers": {
"url": "http://123.123.123.123:9080/mcp/headers",
"headers": {
"apikey": "john-key",
"x-openapi2mcp-header-x-tenant-id": "tenant-42"
}
}
}
}

Ask the client to call the GetHeaders tool. The tool result should include the header received by HTTPBin:

{
"headers": {
"X-Tenant-Id": "tenant-42"
}
}

The OpenAPI-to-MCP service removes the x-openapi2mcp-header- prefix and forwards the remaining header name in lowercase. The prefixed header itself is not sent upstream. Streamable HTTP reads these headers on every MCP request; SSE captures them when the client opens the connection and keeps them for that session.

caution

An authenticated MCP client can use this prefix to send arbitrary upstream headers. Configure sensitive authentication or authorization headers in the plugin's headers field, which takes precedence over a client value with the same name. Expose only the operations that clients may call with those credentials.

Enable MCP Access to API7 Enterprise APIs​

The following example demonstrates how to expose API7 Enterprise APIs through the MCP protocol, enabling AI models and clients to interact with your API7 Enterprise configuration.

Create a route with the openapi-to-mcp plugin:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "openapi-to-mcp-route",
"uri": "/mcp",
"methods": ["POST"],
"plugins": {
"openapi-to-mcp": {
"transport": "streamable_http",
"base_url": "https://your-dashboard.com",
"headers": {
"X-API-KEY": "<API7_ENTERPRISE_API_KEY>"
},
"openapi_url": "https://run.api7.ai/api7-ee/openapi-latest.json"
}
}
}'

❶ Configure the route to accept POST requests for MCP messages such as tools/list and tools/call.

❷ Configure the transport method to be streamable_http (recommended for production).

❸ Replace with your API7 Enterprise address, where requests will be forwarded.

❹ Replace with your credential in the X-API-KEY header for API7 Enterprise authentication.

❺ Configure the URL of the API7 Enterprise OpenAPI document.

In your AI client, such as Cursor, update the MCP settings with your gateway address and append the previously created route path. For instance:

mcp.json
{
"mcpServers": {
"api7-enterprise-mcp": {
"url": "http://123.123.123.123:9080/mcp"
}
}
}

If the configuration is successful, you should see the available tools (external functions or services exposed to AI clients through MCP).

You can now interact with API7 Enterprise directly from the chat window of your AI client. For example, try asking: "How many gateway groups are there in API7 Enterprise?"

AI client interaction with API7 Enterprise

Troubleshooting​

For native APISIX, inspect the gateway error log. A specification-fetch or parsing failure prevents the plugin from building the tools list. Verify that the URL returns an OpenAPI document containing API paths, rather than an HTML error page or a document containing only components.

The following logging guidance and known issues apply to the API7 Enterprise OpenAPI-to-MCP service.

The separate service writes errors to standard error. For the Docker Compose example, inspect its logs with docker compose logs openapi-to-mcp. For a Helm deployment, inspect the openapi-to-mcp sidecar with kubectl logs <gateway-pod> -c openapi-to-mcp. These logs are separate from the gateway error log.

Known Issues​

  1. The error Cannot use 'in' operator to search for '$ref' in undefined typically occurs when an OpenAPI v2 document is used in openapi_url. The plugin only supports OpenAPI v3 document in openapi_url.

  2. The plugin has a known parsing issue when handling oneOf schemas in OpenAPI v3 document retrieved from openapi_url. In this case, the MCP client will be stuck at tool loading.