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.
- 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
- Clone the repository:
git clone <repository-url>
cd "API Testing Automata - Basic Backend"
- Install dependencies:
pip install -r requirements.txt
- Generate from OpenAPI URL:
python main.py generate https://petstore.swagger.io/v2/swagger.json --output petstore_server.py
- 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
- Validate API specification:
python main.py validate examples/open_meteo_api/open_meteo_api.json
- See examples:
python main.py example
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
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"}
}
}
]
}
The MCP Generator is the core component that transforms API specifications into functional MCP servers. Here's what it does in detail:
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
- 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
- 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
- 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
- Helper Tools: Generates utility tools like
list_available_endpoints
andget_endpoint_schema
- Resources: Creates MCP resources for API documentation
- Validation: Includes input validation and error handling
- Documentation: Auto-generates comprehensive API documentation
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}
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
- APIParser: Handles parsing of various input formats
- MCPGenerator: Core generator that creates MCP servers
- Models: Pydantic models for validation and type safety
- CLI: Rich command-line interface for easy usage
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.
The generated MCP servers include:
- One tool per API endpoint
list_available_endpoints
: List all available toolsget_endpoint_schema
: Get detailed schema for any endpoint
api_documentation
: Complete API documentation in markdown format
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.