跳到主要内容

APISIX MCP 协议(APISIX-MCP)

APISIX-MCP 是一个 Model Context Protocol (MCP) 服务器,旨在连接大型语言模型 (LLM) 和 APISIX Admin API。这种集成使得能够通过兼容 MCP 的 AI 客户端进行自然语言交互,以管理和查看 APISIX 中的资源,无论 APISIX 如何部署。

通过利用 APISIX-MCP,用户可以执行诸如创建、检索、更新、删除资源以及发送请求等操作。这种方法通过允许对话式命令来处理传统上需要手动配置的任务,从而简化了 API 管理。

APISIX-MCP 是开源的,可在 npmGitHub 上找到。它可以配置为通过任何兼容 MCP 的 AI 客户端(如 Claude Desktop、Cursor 或 VS Code 中的 Cline 扩展)使用。

安装和配置 APISIX-MCP

以下是不同的安装方式。

npm

如果你通过 npm 安装,请使用以下详细信息配置 MCP 服务器,并在 AI 客户端中根据你的环境更新 APISIX 服务器地址、端口、Admin API 端口、前缀和身份验证密钥:

{
"mcpServers": {
"apisix-mcp": {
"command": "npx",
"args": ["-y","apisix-mcp"],
"env": {
"APISIX_SERVER_HOST": "http://127.0.0.1",
"APISIX_SERVER_PORT": "9080",
"APISIX_ADMIN_API_PORT": "9180",
"APISIX_ADMIN_API_PREFIX": "/apisix/admin",
"APISIX_ADMIN_KEY": "edd1c9f034335f136f87ad84b625c8f1"
}
}
}
}

Smithery

要为 Claude Desktop 安装 APISIX-MCP,请运行:

npx -y @smithery/cli install @api7/apisix-mcp --client claude

源代码

要从源代码安装,首先克隆 apisix-mcp 仓库:

git clone https://github.com/api7/apisix-mcp.git
cd apisix-mcp

安装依赖项并构建项目:

pnpm install
pnpm build

最后,使用以下详细信息配置 MCP 服务器,并在 AI 客户端中根据你的环境更新 APISIX 服务器地址、端口、Admin API 端口、前缀和身份验证密钥:

{
"mcpServers": {
"apisix-mcp": {
"command": "npx",
"args": [
"your-apisix-mcp-path/dist/index.js"
],
"env": {
"APISIX_SERVER_HOST": "http://127.0.0.1",
"APISIX_SERVER_PORT": "9080",
"APISIX_ADMIN_API_PORT": "9180",
"APISIX_ADMIN_API_PREFIX": "/apisix/admin",
"APISIX_ADMIN_KEY": "edd1c9f034335f136f87ad84b625c8f1"
}
}
}
}
提示

上面的 APISIX_SERVER_HOSTAPISIX_SERVER_PORTAPISIX_ADMIN_API_PORTAPISIX_ADMIN_API_PREFIXAPISIX_ADMIN_KEY 配置为其默认值。如果你的 APISIX 安装使用这些默认值,你可以选择省略 env 配置。

保存配置后,你应该会看到 MCP 服务器已成功安装在你的 AI 客户端中。

支持的操作

APISIX-MCP 支持以下操作。当你将 AI 客户端与 APISIX-MCP 一起使用时,你的自然语言输入将被转换为这些操作。

常见操作

  • get_resource:按类型检索资源,例如路由、服务和上游。
  • delete_resource:按 ID 删除资源。
  • send_request_to_gateway:向网关发送请求。

API 资源操作

  • create_route / update_route / delete_route:管理路由。
  • create_service / update_service / delete_service:管理服务。
  • create_upstream / update_upstream / delete_upstream:管理上游。
  • create_or_update_proto:管理 protobuf 定义。
  • create_or_update_stream_route:管理流路由。

插件操作

  • get_all_plugin_names:获取所有可用的插件名称。
  • get_plugin_info / get_plugins_by_type / get_plugin_schema:检索插件配置。
  • create_plugin_config / update_plugin_config:管理插件配置。
  • create_global_rule / update_global_rule:管理全局插件。
  • get_plugin_metadata / create_or_update_plugin_metadata / delete_plugin_metadata:管理插件元数据。

安全配置

  • get_secret_by_id / create_secret / update_secret:管理密钥。
  • create_or_update_consumer / delete_consumer:管理消费者。
  • get_credential / create_or_update_credential / delete_credential:管理消费者凭证。
  • create_consumer_group / delete_consumer_group:管理消费者组。
  • create_ssl / update_ssl / delete_ssl:管理 SSL 证书。

示例

要创建路由,你可以在 AI 客户端中输入此提示:

创建一个名为 httpbin-get-route 的路由,路由 URI 为 /get,用于访问 https://httpbin.org 上游,将请求限制为每 30 秒 2 个请求。配置后打印路由详细信息。

你应该看到路由已由你的 AI 客户端成功创建:

Successfully created route "httpbin-get-route" with the following configuration:

URI: /get
Upstream: https://httpbin.org (roundrobin load balancing)
Rate limiting: 2 requests per 30 seconds per IP address
Route ID: httpbin-get-route
Status: Active (1)
The route is now ready to accept requests at the /get path, which will be forwarded to httpbin.org with rate limiting applied.

你还可以从 AI 客户端向路由发送请求进行验证:

向此路由发送请求。

你应该在 AI 客户端中看到类似以下的响应:

Successfully tested the route "httpbin-get-route":

Received HTTP 200 response from httpbin.org
Rate limiting headers show:
Limit: 2 requests per 30 seconds (x-ratelimit-limit: 2)
Remaining: 1 request available (x-ratelimit-remaining: 1)
Reset in: 30 seconds (x-ratelimit-reset: 30)
The route is properly configured and forwarding requests to httpbin.org
The rate limiting plugin is working as expected

有关更多示例,请遵循 快速入门教程