Skip to content

A tool to accept any generic API details from the user, create MCP servers and use them to test the APIs

Notifications You must be signed in to change notification settings

SohhamS/API-Testing-Automata

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Testing Automata

A powerful tool for automatically generating Model Context Protocol (MCP) servers from API specifications. This system allows you to quickly convert any API into an MCP-compliant server that can be used with AI agents and LLM applications.

🌟 Features

  • Multi-format Support: Parse OpenAPI/Swagger files, URLs, or custom JSON format
  • Automatic Tool Generation: Convert API endpoints into MCP tools with proper schemas
  • Authentication Handling: Support for API keys, Bearer tokens, and Basic auth
  • Code Export: Generate standalone Python MCP servers
  • Validation: Validate API specifications before generation
  • Rich CLI: Beautiful command-line interface with progress indicators
  • Documentation: Auto-generate comprehensive API documentation

🚀 Quick Start

Installation

  1. Clone the repository:
git clone <repository-url>
cd "API Testing Automata - Basic Backend"
  1. Install dependencies:
pip install -r requirements.txt

Basic Usage

  1. Generate from OpenAPI URL:
python main.py generate https://petstore.swagger.io/v2/swagger.json --output petstore_server.py
  1. Generate from custom JSON:
python main.py generate examples/open_meteo_api/open_meteo_api.json --output examples/open_meteo_api/open_meteo_api_mcp_server.py
  1. Validate API specification:
python main.py validate examples/open_meteo_api/open_meteo_api.json
  1. See examples:
python main.py example

📋 Input Formats

1. OpenAPI/Swagger Format

Standard OpenAPI 2.0/3.0 JSON or YAML files:

python main.py generate openapi.json
python main.py generate https://api.example.com/swagger.json

2. Custom JSON Format

Use the simplified custom format for quick API definitions:

{
  "name": "My API",
  "version": "1.0.0",
  "description": "Example API",
  "base_url": "https://api.example.com",
  "auth": {
    "type": "api_key",
    "api_key_header": "X-API-Key"
  },
  "api": [
    {
      "name": "get_users",
      "endpoint": "/users",
      "method": "GET",
      "description": "Get all users",
      "input_schema": {
        "type": "object",
        "properties": {
          "limit": {
            "type": "integer",
            "description": "Maximum number of users"
          }
        }
      },
      "output_schema": {
        "type": "array",
        "items": {"type": "object"}
      }
    }
  ]
}

🛠️ What the MCP Generator Does

The MCP Generator is the core component that transforms API specifications into functional MCP servers. Here's what it does in detail:

1. Tool Definition Generation

For each API endpoint, it creates an MCP tool with:

  • Name: Derived from the endpoint operation ID or path
  • Description: Uses the API's description for LLM understanding
  • Input Schema: Converts API parameters to JSON Schema format
  • Handler Function: Generates Python code to execute the API call

2. Schema Conversion

  • Converts OpenAPI parameter schemas to MCP-compatible JSON schemas
  • Handles path parameters, query parameters, and request bodies
  • Ensures proper type validation and descriptions for LLM use
  • Maps API response schemas to structured output formats

3. Authentication Integration

  • Detects authentication requirements from API specs
  • Generates code for API key, Bearer token, and Basic authentication
  • Handles header injection and credential management
  • Supports both global and per-endpoint authentication

4. Request/Response Handling

  • Generates HTTP client code for each endpoint
  • Handles URL construction with path parameter substitution
  • Manages query parameters and request bodies
  • Processes responses and structures them for LLM consumption
  • Includes comprehensive error handling

5. Additional Features

  • Helper Tools: Generates utility tools like list_available_endpoints and get_endpoint_schema
  • Resources: Creates MCP resources for API documentation
  • Validation: Includes input validation and error handling
  • Documentation: Auto-generates comprehensive API documentation

6. Code Generation Process

The generator creates Python functions like this for each endpoint:

async def call_get_users(**kwargs):
    """Get all users - Auto-generated tool handler for GET /users"""
    import requests
    import json
    
    try:
        # Extract and validate parameters
        limit = kwargs.get('limit')
        query_params = {}
        if limit is not None:
            query_params['limit'] = limit
        
        # Setup authentication
        headers = {}
        api_key = kwargs.get('api_key') or 'YOUR_API_KEY'
        headers['X-API-Key'] = api_key
        
        # Make the API request
        base_url = 'https://api.example.com'
        url = base_url.rstrip('/') + '/users'
        response = requests.get(url, params=query_params, headers=headers, timeout=30)
        response.raise_for_status()
        
        # Process response
        result_data = response.json()
        result = {
            'success': True,
            'status_code': response.status_code,
            'data': result_data,
            'endpoint': 'get_users',
            'method': 'GET',
            'url': url
        }
        return result
        
    except requests.RequestException as e:
        return {"error": f"API request failed: {str(e)}", "success": False}
    except Exception as e:
        return {"error": f"Tool execution failed: {str(e)}", "success": False}

Architecture

API Testing Automata - Basic Backend/
├── api_to_mcp/          # Core generator package
│   ├── __init__.py      # Package initialization
│   ├── models.py        # Data models (Pydantic)
│   ├── parser.py        # Input parsing module
│   └── generator.py     # MCP server generation
├── main.py              # CLI interface
├── executor.py          # Executes generated MCP servers
├── orchestrator.py      # Orchestrates test generation and execution
├── examples/
│   └── open_meteo_api.json # Example custom format
├── requirements.txt     # Dependencies
└── README.md            # This file

Key Components:

  1. APIParser: Handles parsing of various input formats
  2. MCPGenerator: Core generator that creates MCP servers
  3. Models: Pydantic models for validation and type safety
  4. CLI: Rich command-line interface for easy usage

🔐 Authentication Support

The system supports multiple authentication methods:

  • API Key: Header-based API key authentication
  • Bearer Token: JWT or token-based authentication
  • Basic Auth: Username/password authentication
  • No Auth: For public APIs

Authentication can be specified globally or per-endpoint.

📊 Generated MCP Server Features

The generated MCP servers include:

Tools

  • One tool per API endpoint
  • list_available_endpoints: List all available tools
  • get_endpoint_schema: Get detailed schema for any endpoint

Resources

  • api_documentation: Complete API documentation in markdown format

DISCLAIMER:

This is a work in progress and is not ready for production use. Moreover, the readme has been generated using AI, so it may not be accurate.

About

A tool to accept any generic API details from the user, create MCP servers and use them to test the APIs

Topics

Resources

Stars

Watchers

Forks

Languages