LS

lostinbrittany/swagger-to-mcp-generator

API 开发

本页由 AI 翻译,技术细节请以原文为准。 查看原文

21 stars 3 forks 质量 55 趋势 0MIT

一个从 Open API Swagger 文件生成 Quarkus MCP 服务器的 Java 程序

概览

该项目提供了一个强大的工具,可以自动将 OpenAPI/Swagger 规范转换为模型上下文协议(MCP)服务器,使大型语言模型(LLM)能够通过标准化工具与任何 REST API 交互。 一个综合工具,可将任何 OpenAPI/Swagger 规范转换为功能齐全的 MCP 服务器:- 解析 OpenAPI 规范文件 - 将 API 端点转换为 MCP 工具 - 处理路径参数、查询参数和请求体 - 支持多种 HTTP 方法(GET、POST、PUT、DELETE、PATCH)- 提供认证支持(API 密钥、承载令牌、基本认证) - 格式化 JSON 响应以提升可读性 - 生成强大的错误处理 - 包含参数文档,包含有效值和默认值 要从任意 OpenAPI 规范生成 MCP 服务器: 参数:- path/to/swagger.json:通往OpenAPI/Swagger规范文件的路径- GeneratedMcpServer:输出Java文件名称(不含 .

概述(AI 翻译)

该项目提供了一个强大的工具,可以自动将 OpenAPI/Swagger 规范转换为模型上下文协议(MCP)服务器,使大型语言模型(LLM)能够通过标准化工具与任何 REST API 交互。

组成部分

SwaggerToMcpGenerator.java

一个综合工具,将任何 OpenAPI/Swagger 规范转换为功能齐全的 MCP 服务器:

  • 解析OpenAPI规范文件
  • 将API端点转换为MCP工具
  • 处理路径参数、查询参数和请求体
  • 支持多种HTTP方法(GET、POST、PUT、DELETE、PATCH)
  • 提供认证支持(API 密钥、持有令牌、基本认证)
  • 格式化JSON响应以提升可读性
  • 生成稳健的错误处理
  • 包含参数文档,包含有效值和默认值

从 OpenAPI 生成 MCP 服务器

要从任意 OpenAPI 规范生成 MCP 服务器:

参数:

  • path/to/swagger.json:通往 OpenAPI/Swagger 规范文件的路径
  • GeneratedMcpServer:输出 Java 文件名称(无.java扩展名)

选项:

  • --server-index :OpenAPI规范中用于服务器的索引(基于0)

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

OpenAPI to MCP Generator

This project provides a powerful tool for automatically converting OpenAPI/Swagger specifications into Model Context Protocol (MCP) servers, allowing LLMs to interact with any REST API through standardized tools.

Components

SwaggerToMcpGenerator.java

A comprehensive utility that converts any OpenAPI/Swagger specification into a fully functional MCP server:

  • Parses OpenAPI specification files
  • Converts API endpoints to MCP tools
  • Handles path parameters, query parameters, and request bodies
  • Supports multiple HTTP methods (GET, POST, PUT, DELETE, PATCH)
  • Provides authentication support (API keys, Bearer tokens, Basic auth)
  • Formats JSON responses for readability
  • Generates robust error handling
  • Includes parameter documentation with valid values and defaults

Generating an MCP Server from OpenAPI

To generate an MCP server from any OpenAPI specification:

jbang SwaggerToMcpGenerator.java path/to/swagger.json GeneratedMcpServer [options]

Parameters:

  • path/to/swagger.json: Path to the OpenAPI/Swagger specification file
  • GeneratedMcpServer: Name of the output Java file (without .java extension)

Options:

  • --server-index : Index of the server to use from the OpenAPI specification (0-based)
  • --server-url : URL of the server to use (overrides server-index)

This will create a new file GeneratedMcpServer.java that implements an MCP server with tools for each API endpoint defined in the swagger file. The generator will emit a warning if multiple servers are defined in the OpenAPI specification and none is explicitly selected.

Running the Generated MCP Server

To run the generated MCP server:

jbang GeneratedMcpServer.java

Server Selection

The generated MCP server includes constants for all servers defined in the OpenAPI specification, allowing you to choose which server to use at runtime. By default, the first server in the list is used, but you can select a specific server using environment variables:

# Select server by index (0-based)
export SERVER_INDEX=1

# Or select server by URL
export SERVER_URL="https://api-example.com/v2"

jbang GeneratedMcpServer.java

Authentication

The generated MCP server supports multiple authentication methods through environment variables:

  • API Key: Set API_KEY and API_KEY_HEADER environment variables
  • Bearer Token: Set BEARER_TOKEN environment variable
  • Basic Auth: Set API_USERNAME and API_PASSWORD environment variables

Example:

export API_KEY="your-api-key"
export API_KEY_HEADER="X-API-Key"
jbang GeneratedMcpServer.java

Examples

Open-Meteo Weather API

The project includes an example OpenAPI specification for the Open-Meteo Weather API in the examples/open-meteo directory.

Generating the Open-Meteo MCP Server

cd examples/open-meteo
jbang ../../SwaggerToMcpGenerator.java open-meteo-openapi.yml OpenMeteoMcpServer
在 GitHub 查看完整 README

安装

This server does not publish a one-line install command.

Open the repository installation guide 新手?先看 mcpskill.org 教程 →