Skip to content

aruncs78/API2MCP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API2MCP: OpenAPI to FastMCP Generator

API2MCP (formerly Jule) is a powerful Streamlit application that automatically converts OpenAPI (Swagger) specifications into ready-to-use Model Context Protocol (MCP) servers. This tool bridges the gap between existing REST APIs and AI agents, allowing you to quickly expose your API endpoints as tools for LLMs.

Features

  • Universal Parsing: Supports both JSON and YAML OpenAPI specifications.
  • Smart Reference Resolution: Automatically handles $ref in OpenAPI specs to ensure all schemas are correctly resolved.
  • Interactive Selection: specific endpoints to expose as tools via a user-friendly interface.
  • Automated Code Generation: Produces a complete, standalone Python server file (server.py) using the fastmcp library.
  • Type Safety: Maps OpenAPI types to Python types for robust tool definitions.

Installation

  1. Clone the repository:

    git clone https://github.com/aruncs78/API2MCP.git
    cd API2MCP
  2. Install dependencies:

    pip install -r requirements.txt

Usage

  1. Run the Streamlit app:

    streamlit run app.py
  2. Upload OpenAPI Spec: Upload your openapi.json or openapi.yaml file.

  3. Configure: Set the Base URL for your API and the environment variable name for your API Key.

  4. Select Tools: Choose which API endpoints you want to expose as MCP tools.

  5. Generate & Download: Click "Generate Server Code" to preview the code, then download server.py and requirements.txt.

Project Structure & API Documentation

app.py

The main entry point for the Streamlit application.

  • Functionality: Handles file upload, UI rendering, user input collection (Base URL, API Key Env), and triggers the parsing and generation logic.

src/parser.py

Handles the parsing and normalization of OpenAPI specifications.

Functions:

  • clean_identifier(name)

    • Description: Sanitizes a string to ensure it is a valid Python identifier. Replaces invalid characters with underscores and handles Python keywords.
    • Args: name (str) - The string to sanitize.
    • Returns: str - A valid Python identifier.
  • parse_openapi(spec_content)

    • Description: The core parsing logic. Takes raw OpenAPI content (JSON string or dict), resolves references using jsonref, and extracts a list of valid tool definitions.
    • Args: spec_content (str | dict) - The OpenAPI content.
    • Returns: tuple(list, str) - A list of parsed tool dictionaries and the default base URL found in the spec.
  • map_type(openapi_type)

    • Description: Maps OpenAPI data types (e.g., 'integer', 'string') to Python types (e.g., 'int', 'str').
    • Args: openapi_type (str) - The OpenAPI type string.
    • Returns: str - The corresponding Python type.

src/generator.py

Generates the Python code for the FastMCP server.

Functions:

  • generate_mcp_server(parsed_spec, selected_tool_names, base_url, api_key_env_var="API_KEY")
    • Description: Uses jinja2 templates to construct the final server.py file. It iterates through selected tools, creates function signatures with type hints, and generates the implementation logic to call the actual API.
    • Args:
      • parsed_spec: List of tools from parser.py.
      • selected_tool_names: List of tool names selected by the user.
      • base_url: The base URL for the API calls.
      • api_key_env_var: Name of the environment variable for the API key.
    • Returns: str - The complete source code for the generated MCP server.

Generated Server (server.py)

The output file is a standalone Python script that:

  1. Initializes a FastMCP server.
  2. Reads the API Key from the specified environment variable.
  3. Defines tool functions decorated with @mcp.tool().
  4. Uses httpx to make authenticated HTTP requests to the target API.

Requirements

  • Python 3.10+
  • streamlit
  • fastmcp
  • httpx
  • pyyaml
  • jinja2
  • jsonref

License

MIT

About

API to MCP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages