OpenAPI转Kong配置生成器

将OpenAPI 3.x规范转换为带有ai-mcp-proxy插件的Kong Deck配置文件,适用于API网关配置自动化。

  • by jbrannst
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 jbrannst/api2mcp

使用说明

OpenAPI到Kong Deck配置生成器

此脚本使用ai mcp代理插件将OpenAPI 3.x规范转换为Kong Deck配置文件。

特性

  • 解析OpenAPI 3.x YAML规范
  • 从OpenAPI服务器部分自动检测服务URL
  • 使用ai-mcp代理插件生成Kong Deck配置
  • 为每个API端点创建MCP工具
  • 处理路径参数(忽略干净工具定义的查询参数)
  • 生成干净、可用于生产的默认值

安装

npm install js-yaml

用法

命令行

# Print configuration to stdout
node openapi-to-kong.js <input-file>

# Save configuration to file
node openapi-to-kong.js <input-file> <output-file>

# Show help
node openapi-to-kong.js --help

例子

# Generate Kong config from OpenAPI spec
node openapi-to-kong.js api.yaml

# Save to specific file
node openapi-to-kong.js api.yaml kong-config.yaml

输入要求

  • YAML格式的OpenAPI 3.x规范
  • 必须包括 servers 至少包含一个服务器URL的部分
  • 必须包括 info.title 用于服务命名

输出

该脚本生成一个Kong Deck配置,其中包含:

  • 服务:以API标题命名(已冻结)
  • 路线:单MCP代理路由位于 /mcp/{service-name}
  • 插件:具有转换侦听器模式的ai-mcp代理
  • 工具:每个API终结点一个工具,具有:
    • HTTP方法和路径
    • 仅需要路径参数
    • 操作总结作为标题和描述

示例

输入OpenAPI(api.yaml)

openapi: 3.1.0
info:
  title: Marketplace API
  version: 1.0.0
servers:
  - url: http://host.docker.internal:3000
paths:
  /users/{id}:
    get:
      summary: Get user by ID
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string

生成Kong配置

_format_version: '3.0'
services:
  - name: marketplace-api
    url: http://host.docker.internal:3000
    routes:
      - name: mcp-marketplace-api
        paths:
          - /mcp/marketplace-api
        strip_path: true
        plugins:
          - name: ai-mcp-proxy
            instance_name: mcp-proxy-marketplace-api
            config:
              mode: conversion-listener
              logging:
                log_payloads: true
                log_statistics: true
              server:
                tag: null
                timeout: 60000
              tools:
                - description: Get user by ID
                  annotations:
                    title: Get user by ID
                  method: GET
                  path: users/{id}
                  parameters:
                    - in: path
                      name: id
                      required: true
                      schema:
                        type: string
            tags:
              - ai-gateway-mcp

配置详情

服务命名

  • API标题转换为URL-安全段塞
  • 示例:“市场API”→ “市场api”

路由配置

  • 路线名称: mcp-{service-name}
  • 路线路径: /mcp/{service-name}
  • strip_path: true 转发时删除路由前缀

工具生成

  • 仅包括所需的路径参数
  • 对于更清晰的工具定义,查询参数被忽略
  • 路径上的每个HTTP方法都成为一个单独的工具
  • 操作摘要用于描述和标题

插件配置

  • 模式: conversion-listener
  • 为有效载荷和统计数据启用日志记录
  • 60秒超时
  • 标记为 ai-gateway-mcp

错误处理

该脚本验证:

  • 输入文件存在
  • OpenAPI YAML解析
  • 必填字段(服务器、info.title)

程序化使用

const OpenAPIToKongConverter = require('./openapi-to-kong');

const converter = new OpenAPIToKongConverter();
const yamlOutput = converter.convert('api.yaml', 'output.yaml');