A Model Context Protocol (MCP) server that converts any OpenAPI/Swagger specification into an MCP server, allowing Large Language Models (LLMs) to interact with REST APIs through a standardized interface.
The OpenAPI to MCP server bridges the gap between REST APIs and LLM agents by:
- Automatically converting API specifications into MCP-compatible tools
- Providing type-safe interactions with any REST API
- Eliminating the need for manual tool creation and maintenance
Add the OpenAPI to MCP server to your MCP client's configuration file:
{
"mcpServers": {
"my-api": {
"command": "npx",
"args": [
"-y",
"openapi-to-mcp"
],
"env": {
"OPENAPI_SPEC_URL": "path/to/your/swagger.yml",
"OPENAPI_SPEC_BASE_URL": "http://api.example.com"
}
}
}
}
Configuration file locations for supported clients:
-
Claude Desktop
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json
- Windows:
%APPDATA%\Claude\claude_desktop_config.json
- macOS:
-
Cursor:
~/.cursor/mcp.json
-
Windsurf:
~/.codeium/windsurf/mcp_config.json
-
VS Code:
- Workspace configuration:
.vscode/mcp.json
- Command line:
code --add-mcp "{\"name\":\"openapi-mcp\",\"command\":\"npx\",\"args\":[\"-y\",\"openapi-to-mcp\"],\"env\":{\"OPENAPI_SPEC_URL\":\"path/to/your/swagger.yml\",\"OPENAPI_SPEC_BASE_URL\":\"http://api.example.com\"}}"
Example
.vscode/mcp.json
:{ "servers": { "openapi-mcp": { "type": "stdio", "command": "npx", "args": ["-y", "openapi-to-mcp"], "env": { "OPENAPI_SPEC_URL": "path/to/your/swagger.yml", "OPENAPI_SPEC_BASE_URL": "http://api.example.com" } } } }
- Workspace configuration:
Here's an example of how the server converts OpenAPI operations into MCP tools:
paths:
/pets:
get:
operationId: getPets
summary: List all pets
parameters:
- name: limit
in: query
type: integer
{
name: "getPets",
description: "List all pets",
parameters: {
limit: z.number().int().optional()
}
}
- OpenAPI 2.0 (Swagger)
- OpenAPI 3.0
- Local file paths
- Remote HTTP URLs
- YAML and JSON formats
The server supports various authentication methods:
{
"env": {
"OPENAPI_SPEC_HEADERS": "{\"X-API-Key\": \"your-api-key\"}"
}
}
{
"env": {
"OPENAPI_SPEC_HEADERS": "{\"Authorization\": \"Bearer your-token\"}"
}
}
{
"env": {
"OPENAPI_CERT_PATH": "/path/to/client.crt",
"OPENAPI_KEY_PATH": "/path/to/client.key",
"OPENAPI_CERT_PASSPHRASE": "optional-passphrase"
}
}
{
"env": {
"OPENAPI_OAUTH_CLIENT_ID": "your-client-id",
"OPENAPI_OAUTH_CLIENT_SECRET": "your-client-secret",
"OPENAPI_OAUTH_TOKEN_URL": "https://auth.example.com/oauth/token",
"OPENAPI_OAUTH_SCOPES": "read:data,write:data"
}
}
- Automatic parameter validation
- JSON Schema to Zod conversion
- Content type negotiation
- File uploads
- Binary responses
- Error mapping
- Full TypeScript support
- Automatic type generation
- Runtime type validation
- Type-safe API calls
-
--spec <path>
: Path to OpenAPI specification file -
--base-url <url>
: Base URL for API requests -
--headers <json>
: Additional headers as JSON string -
--verbose
: Enable detailed logging -
--log-level <level>
: Set log level (error, warn, info, debug) -
--cert-path <path>
: Path to client certificate file -
--key-path <path>
: Path to client key file -
--cert-passphrase <string>
: Passphrase for certificate -
--oauth-client-id <id>
: OAuth client ID -
--oauth-client-secret <secret>
: OAuth client secret -
--oauth-token-url <url>
: OAuth token endpoint URL -
--oauth-scopes <scopes>
: Comma-separated list of OAuth scopes -
--version
: Display version information -
--help
: Show help information
To build the Docker image:
# Clone the repository
git clone https://github.com/your-username/openapi-to-mcp
cd openapi-to-mcp
# Build the Docker image
docker build -t openapi-to-mcp .
You can run the server using Docker:
docker run \
-e OPENAPI_SPEC_URL="https://petstore.swagger.io/v2/swagger.json" \
-e OPENAPI_SPEC_BASE_URL="https://petstore.swagger.io/v2" \
openapi-to-mcp
The server uses the following environment variables:
Variable | Description | Requirement | Notes |
---|---|---|---|
OPENAPI_SPEC_URL | Path or URL to OpenAPI spec | Required | Local file path or HTTP(S) URL |
OPENAPI_SPEC_BASE_URL | Base URL for API requests | Required | API server base URL |
OPENAPI_SPEC_HEADERS | Additional headers as JSON | Optional | Used for API key and token auth |
OPENAPI_CERT_PATH | Path to client certificate | Optional* | Required for certificate auth |
OPENAPI_KEY_PATH | Path to client key | Optional* | Required for certificate auth |
OPENAPI_CERT_PASSPHRASE | Passphrase for client key | Optional | Only needed if key is encrypted |
OPENAPI_OAUTH_CLIENT_ID | OAuth client ID | Optional* | Required for OAuth2 auth |
OPENAPI_OAUTH_CLIENT_SECRET | OAuth client secret | Optional* | Required for OAuth2 auth |
OPENAPI_OAUTH_TOKEN_URL | OAuth token URL | Optional* | Required for OAuth2 auth |
OPENAPI_OAUTH_SCOPES | OAuth scopes (comma-separated) | Optional | Used for OAuth2 scope requests |
NODE_EXTRA_CA_CERTS | Custom CA certificates path | Optional | For self-signed certificates |
NODE_TLS_REJECT_UNAUTHORIZED | Disable TLS validation (0 or 1) | Optional | Use with caution for testing |
LOG_LEVEL | Log level (error, warn, info, debug) | Optional | Defaults to "info" |
*These variables are conditionally required depending on the authentication method you're using.
npx openapi-to-mcp --debug
-
Version Conflicts
- Ensure Node.js version ≥ 18
- Check package.json for compatibility
- MacOS / Linux:
tail -f ~/.openapi-mcp/logs/server.log
- Windows:
Get-Content "$env:USERPROFILE\.openapi-mcp\logs\server.log" -Wait
- Spec Parser: Loads and validates the OpenAPI specification
- Operation Generator: Converts API operations to MCP tools
- MCP Tools: Exposes operations as standardized tools
- API Calls: Makes actual HTTP requests to the API
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
git clone https://github.com/your-username/openapi-to-mcp
cd openapi-to-mcp
npm install
npm run build
npm run start
Run the test suite:
npm test
Start the test server:
npm run start:server
MIT License - see LICENSE file for details