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 API. 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 APISIX and API7 Gateway, but the products process MCP traffic differently. APISIX runs the MCP implementation inside the gateway. API7 Gateway forwards MCP traffic to a separate OpenAPI-to-MCP service deployed alongside the gateway.

MCP Request Flow​

APISIX fetches the OpenAPI document, generates tools, and sends tool calls to the described API inside the gateway process.

Demo (API7 Dashboard)​

The interactive demo follows the Expose Petstore as MCP Tools workflow in the API7 Dashboard. It shows how to configure a service and route, connect an MCP client, and test the generated Petstore tools.

Examples​

The following examples configure openapi-to-mcp for common MCP workflows.

Prerequisites​

Start Petstore before configuring the plugin. API7 Gateway also requires the separate OpenAPI-to-MCP service.

Start the Example Upstream​

The examples use Petstore as the upstream API. Petstore serves both the OpenAPI document and the API operations exposed as MCP tools. Its container image is available for linux/amd64, which the deployment examples select explicitly.

Select the tab for your deployment method:

Set GATEWAY_CONTAINER to the running APISIX or API7 Gateway container. Create a network for the example and connect the gateway to it. Skip a command if the network or connection already exists.

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

docker network create gateway-petstore-net
docker network connect gateway-petstore-net "$GATEWAY_CONTAINER"

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

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

networks:
gateway-petstore-net:
external: true

Start Petstore:

docker compose -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

Deploy the OpenAPI-to-MCP Service for API7 Gateway​

This step applies only to API7 Gateway. APISIX processes MCP requests inside the gateway and does not require a separate service. API7 Gateway forwards MCP requests to the OpenAPI-to-MCP service, which must run in the same network namespace as the gateway.

Add the following block to the values file used to deploy API7 Gateway. Preserve the existing settings in that file.

dp-values.yaml
openapiToMcp:
enabled: true

Apply the updated values. This example uses the release name and namespace from the API7 Gateway Kubernetes deployment guide:

helm upgrade api7-ee-3-gateway api7/gateway \
--namespace api7 \
--version "~3.10.0" \
-f dp-values.yaml

For API7 Gateway 3.9, change the chart constraint to --version "~3.9.0". Replace the release name, namespace, or values filename if your deployment uses different ones.

Expose Petstore as MCP Tools​

Configure a Streamable HTTP endpoint on your gateway. For Kubernetes, see Configure with Ingress Controller.

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",
"openapi_url": "http://petstore:8080/api/v3/openapi.json"
}
}
}'

Configure with Ingress Controller​

The manifests can be applied with Apache APISIX Ingress Controller or API7 Ingress Controller. Connect the controller to the gateway it manages. For API7 Gateway, deploy the OpenAPI-to-MCP service first because Ingress Controller configures the route but does not deploy that service.

If your installation uses a namespace, Gateway name, or IngressClass name other than those shown, update the manifests to match.

Create a route with the openapi-to-mcp plugin configured as follows:

openapi-to-mcp-ic.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: openapi-to-mcp-plugin-config
spec:
plugins:
- name: openapi-to-mcp
config:
transport: streamable_http
base_url: "http://petstore:8080/api/v3"
openapi_url: "http://petstore:8080/api/v3/openapi.json"
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: openapi-to-mcp-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /mcp
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: openapi-to-mcp-plugin-config
backendRefs:
- name: petstore-external-domain
port: 8080
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: petstore-external-domain
spec:
type: ExternalName
externalName: petstore.aic.svc.cluster.local
ports:
- port: 8080
---
apiVersion: apisix.apache.org/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: aic
name: petstore-external-domain
spec:
targetRefs:
- group: ""
kind: Service
name: petstore-external-domain
passHost: node
scheme: http

Apply the configuration to your cluster:

kubectl apply -f openapi-to-mcp-ic.yaml

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. Call that tool to confirm that the gateway can reach Petstore:

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": 2,
"method": "tools/call",
"params": {
"name": "getPetById",
"arguments": {
"pathParameters": {
"petId": 1
}
}
}
}'

The tool result should contain the Petstore resource with ID 1.

Set your MCP client's server URL to the gateway address followed by /mcp. The following example uses the local gateway address.

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

After the client connects, confirm that it lists getPetById, then ask it to retrieve pet 1.

AI client interaction with Petstore

Connect over SSE​

Use SSE when your MCP client expects a long-lived event stream and a separate message endpoint. For multiple gateway instances, use session affinity so the stream and message requests reach the same instance. See SSE Session Isolation.

Configure the Petstore route for SSE:

Patch the existing route to accept the SSE stream and message requests:

curl "http://127.0.0.1:9180/apisix/admin/routes/openapi-to-mcp-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"methods": ["GET", "POST"],
"plugins": {
"openapi-to-mcp": {
"transport": "sse"
}
}
}'

Configure SSE with Ingress Controller​

Update the manifest from Expose Petstore as MCP Tools and preserve its other resources:

openapi-to-mcp-ic.yaml (updated resources)
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: openapi-to-mcp-plugin-config
spec:
plugins:
- name: openapi-to-mcp
config:
transport: sse
base_url: "http://petstore:8080/api/v3"
openapi_url: "http://petstore:8080/api/v3/openapi.json"
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: openapi-to-mcp-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /mcp
method: GET
- path:
type: Exact
value: /mcp
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: openapi-to-mcp-plugin-config
backendRefs:
- name: petstore-external-domain
port: 8080

Reapply the manifest:

kubectl apply -f openapi-to-mcp-ic.yaml

Open the stream and leave it running:

curl -N "http://127.0.0.1:9080/mcp"

The MCP server sends an endpoint event. Copy the path and query string from its data line, including the session ID. In a second terminal, append that path and query string to the same gateway address and send a discovery request:

export MCP_MESSAGE_PATH="<ENDPOINT_EVENT_DATA>"

curl -i "http://127.0.0.1:9080${MCP_MESSAGE_PATH}" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}'

The POST returns 202 Accepted. Read the tool list in a message event on the original stream. For the rest of the Petstore examples, restore transport: streamable_http and the original route methods.

Configure Authentication for MCP Routes​

Protect the Petstore MCP route with key-auth. The client sends the gateway credential with every MCP request. Upstream API credentials remain in the openapi-to-mcp plugin's headers setting.

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"
}
}
}'

Add key-auth to the existing MCP route:

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": {
"header": "apikey"
}
}
}'

Configure Authentication with Ingress Controller​

Select the Kubernetes API used by your Ingress Controller deployment:

openapi-to-mcp-ic.yaml
apiVersion: apisix.apache.org/v1alpha1
kind: Consumer
metadata:
namespace: aic
name: johndoe
spec:
gatewayRef:
name: apisix
credentials:
- type: key-auth
name: primary-key
config:
key: john-key
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: openapi-to-mcp-plugin-config
spec:
plugins:
- name: key-auth
config:
header: apikey
- name: openapi-to-mcp
config:
transport: streamable_http
base_url: "http://petstore:8080/api/v3"
openapi_url: "http://petstore:8080/api/v3/openapi.json"
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: openapi-to-mcp-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /mcp
method: POST
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: openapi-to-mcp-plugin-config
backendRefs:
- name: petstore-external-domain
port: 8080
---
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: petstore-external-domain
spec:
type: ExternalName
externalName: petstore.aic.svc.cluster.local
ports:
- port: 8080
---
apiVersion: apisix.apache.org/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: aic
name: petstore-external-domain
spec:
targetRefs:
- group: ""
kind: Service
name: petstore-external-domain
passHost: node
scheme: http

Apply the updated manifest:

kubectl apply -f openapi-to-mcp-ic.yaml

Configure the client to send the credential in a request header or, if headers are unsupported, in the MCP server URL query string.

If Headers Are Supported​

For a client that supports request headers, add the apikey header to the MCP server configuration:

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

Connect to the MCP server and confirm that the client can list and call the Petstore tools. A missing or invalid key prevents the client from loading tools.

If Headers Are Not Supported​

For clients that cannot send headers, key-auth reads the apikey query parameter by default when the header is absent. Use this option only where URLs containing credentials are protected from logs and other records. The client must include the credential with every MCP request.

Set the MCP server URL to http://127.0.0.1:9080/mcp?apikey=john-key for the local example. Verify that the client can list and call the Petstore tools with the credential.

Flatten Tool Schema Parameters​

By default, tool arguments are grouped by where the API expects them. The Petstore example sends petId inside pathParameters, for example. Set flatten_parameters to true to put those arguments directly in the tool input. Keep the default when parameters in different locations have the same name.

Enable parameter flattening on the Petstore route:

Patch the existing route:

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
}
}
}'

Flatten Parameters with Ingress Controller​

Update the plugin configuration in openapi-to-mcp-ic.yaml and preserve the other resources:

openapi-to-mcp-ic.yaml (updated PluginConfig)
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: openapi-to-mcp-plugin-config
spec:
plugins:
- name: key-auth
config:
header: apikey
- name: openapi-to-mcp
config:
transport: streamable_http
base_url: "http://petstore:8080/api/v3"
openapi_url: "http://petstore:8080/api/v3/openapi.json"
flatten_parameters: true

Reapply the manifest:

kubectl apply -f openapi-to-mcp-ic.yaml

List the tools again and inspect the getPetById input schema. Its properties should now contain petId directly. Call the tool using the same request as before, with "arguments": {"petId": 1}.

Customize MCP Tool Annotations​

Availability

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

Add MCP annotations to OpenAPI operations so clients can identify read-only, destructive, and idempotent tools. The converter determines annotations 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.

Annotations are stored in the OpenAPI document rather than the gateway configuration. The document changes below work with APISIX and API7 Gateway.

Complete Expose Petstore as MCP Tools, then host a modified copy of the OpenAPI document. Add annotations to the operations in that copy:

openapi.yaml
paths:
/pet/{petId}:
get:
operationId: getPetById
summary: Find pet by ID.
x-mcp-annotations:
title: Get Pet by ID
readOnlyHint: true
openWorldHint: false
delete:
operationId: deletePet
summary: Deletes a pet.
x-mcp-annotations:
title: Delete Pet
destructiveHint: true

Point the Petstore route to the modified document:

Patch the existing route and replace <ANNOTATED_OPENAPI_URL> with the URL of the modified document:

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": {
"openapi_url": "<ANNOTATED_OPENAPI_URL>"
}
}
}'

Update Annotations with Ingress Controller​

Replace <ANNOTATED_OPENAPI_URL> in the resource used by your Ingress Controller deployment and preserve the other resources:

openapi-to-mcp-ic.yaml (updated PluginConfig)
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: openapi-to-mcp-plugin-config
spec:
plugins:
- name: key-auth
config:
header: apikey
- name: openapi-to-mcp
config:
transport: streamable_http
base_url: "http://petstore:8080/api/v3"
openapi_url: "<ANNOTATED_OPENAPI_URL>"
flatten_parameters: true

Reapply the manifest:

kubectl apply -f openapi-to-mcp-ic.yaml

The extension accepts the following fields. Configure only the fields that describe the operation:

x-mcp-annotations:
title: Tool title
readOnlyHint: true
destructiveHint: false
idempotentHint: true
openWorldHint: false

If x-mcp-annotations is absent, the converter infers these defaults from the HTTP method:

HTTP methodInferred annotations
GET, HEAD, OPTIONSreadOnlyHint: true
DELETEdestructiveHint: true, idempotentHint: true
PUTidempotentHint: 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": "getPetById",
"annotations": {
"title": "Get Pet by ID",
"readOnlyHint": true,
"openWorldHint": false
}
},
{
"name": "deletePet",
"annotations": {
"title": "Delete Pet",
"destructiveHint": true,
"idempotentHint": true
}
}
]
}

The converter reads x-mcp-annotations only at the operation level and ignores invalid values and unsupported fields. The operation's summary and description control the generated tool description. The tool title comes only from x-mcp-annotations.title.

Forward Client Headers to an Upstream API​

Client-specific headers, such as tenant IDs or user tokens, can be forwarded to the upstream API. APISIX maps selected headers through request variables, while API7 Gateway uses the OpenAPI-to-MCP service's prefix convention.

Complete Configure Authentication for MCP Routes first. The following examples reuse the johndoe consumer and john-key credential, expose HTTPBin's header inspection operation, and verify that a tenant header reaches the upstream API.

Configure the route to map the client's X-Tenant-Id header to the upstream request:

curl "http://127.0.0.1:9180/apisix/admin/routes/openapi-to-mcp-client-headers" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"uri": "/mcp/headers",
"methods": ["POST"],
"plugins": {
"key-auth": {
"header": "apikey"
},
"openapi-to-mcp": {
"transport": "streamable_http",
"base_url": "https://httpbin.org",
"headers": {
"X-Tenant-Id": "${http_x_tenant_id}"
},
"openapi_url": "https://docs.api7.ai/openapi-to-mcp/httpbin-headers.yaml"
}
}
}'

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

mcp.json
{
"mcpServers": {
"httpbin-headers": {
"url": "http://127.0.0.1:9080/mcp/headers",
"headers": {
"apikey": "john-key",
"X-Tenant-Id": "tenant-42"
}
}
}
}

APISIX forwards client-provided header values only for headers explicitly mapped in the plugin configuration. The value ${http_x_tenant_id} resolves from the current MCP request.

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

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

Troubleshooting​

Start with the component that processes MCP traffic: the gateway for APISIX or the OpenAPI-to-MCP service for API7 Gateway.

APISIX and the API7 OpenAPI-to-MCP service fetch openapi_url separately from API operation calls, so the plugin's headers field does not authenticate the document request. If the document requires authentication, publish a reviewed copy at a location accessible to the gateway or service within a restricted network. Keep credentials out of the document URL.

APISIX​

Inspect the APISIX error log first. If the client cannot list tools, test openapi_url from the gateway's network environment and confirm that it returns HTTP 200 with an OpenAPI document containing API paths. An HTML login page, redirect, authentication error, or document containing only components cannot produce tools.

If the document uses a private CA, add that CA to the gateway runtime trust store.

API7 Gateway​

Conversion failures may not appear in the gateway error log. Inspect the OpenAPI-to-MCP service logs with docker compose logs openapi-to-mcp or kubectl logs <gateway-pod> -c openapi-to-mcp.

If the client cannot load tools, verify that openapi_url returns an OpenAPI document from the service's network environment. The service follows redirects, so check the final response body for an HTML login page or another non-OpenAPI response.

The service's VERIFY_SSL setting applies to API calls rather than document fetching. Install a required private CA in the service runtime trust store.

If the MCP route returns 502, confirm that the service is running and listening on the port configured for the gateway plugin. A timeout before the backend responds returns status: 0 with timeout of <TIMEOUT>ms exceeded; if a response starts and then stalls, the service logs stream has been aborted.