OpenAPI MCP server

Integrates with OpenAPI specifications to expose REST API endpoints as MCP resources, enabling dynamic discovery and utilization of many web services.
Back to servers
Setup instructions
Provider
Ivo Toby
Release date
Dec 05, 2024
Language
TypeScript
Stats
118 stars

This MCP server allows Large Language Models to discover and interact with REST APIs defined by OpenAPI specifications through the Model Context Protocol (MCP). It acts as a bridge between AI systems and REST APIs, making OpenAPI endpoints available as MCP resources.

Installation

Using with Claude Desktop (Stdio Transport)

Configure Claude Desktop to use this MCP server without cloning the repository:

  1. Locate or create your Claude Desktop configuration file:

    • On macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  2. Add the following configuration:

{
  "mcpServers": {
    "openapi": {
      "command": "npx",
      "args": ["-y", "@ivotoby/openapi-mcp-server"],
      "env": {
        "API_BASE_URL": "https://api.example.com",
        "OPENAPI_SPEC_PATH": "https://api.example.com/openapi.json",
        "API_HEADERS": "Authorization:Bearer token123,X-API-Key:your-api-key"
      }
    }
  }
}
  1. Replace the environment variables with your actual API configuration.

Using with HTTP Clients (HTTP Transport)

For use with HTTP clients:

npx @ivotoby/openapi-mcp-server \
  --api-base-url https://api.example.com \
  --openapi-spec https://api.example.com/openapi.json \
  --headers "Authorization:Bearer token123" \
  --transport http \
  --port 3000

Configuration Options

Environment Variables

  • API_BASE_URL - Base URL for the API endpoints
  • OPENAPI_SPEC_PATH - Path or URL to OpenAPI specification
  • OPENAPI_SPEC_FROM_STDIN - Set to "true" to read OpenAPI spec from standard input
  • OPENAPI_SPEC_INLINE - Provide OpenAPI spec content directly as a string
  • API_HEADERS - Comma-separated key:value pairs for API headers
  • SERVER_NAME - Name for the MCP server (default: "mcp-openapi-server")
  • SERVER_VERSION - Version of the server (default: "1.0.0")
  • TRANSPORT_TYPE - Transport type: "stdio" or "http" (default: "stdio")
  • HTTP_PORT - Port for HTTP transport (default: 3000)
  • HTTP_HOST - Host for HTTP transport (default: "127.0.0.1")
  • ENDPOINT_PATH - Endpoint path for HTTP transport (default: "/mcp")
  • TOOLS_MODE - Tools loading mode: "all", "dynamic", or "explicit" (default: "all")
  • DISABLE_ABBREVIATION - Disable name optimization

Command Line Arguments

npx @ivotoby/openapi-mcp-server \
  --api-base-url https://api.example.com \
  --openapi-spec https://api.example.com/openapi.json \
  --headers "Authorization:Bearer token123,X-API-Key:your-api-key" \
  --name "my-mcp-server" \
  --server-version "1.0.0" \
  --transport http \
  --port 3000 \
  --host 127.0.0.1 \
  --path /mcp \
  --disable-abbreviation true

OpenAPI Specification Loading

URL Loading (Default)

npx @ivotoby/openapi-mcp-server \
  --api-base-url https://api.example.com \
  --openapi-spec https://api.example.com/openapi.json

Local File Loading

npx @ivotoby/openapi-mcp-server \
  --api-base-url https://api.example.com \
  --openapi-spec ./path/to/openapi.yaml

Standard Input Loading

# Pipe from file
cat openapi.json | npx @ivotoby/openapi-mcp-server \
  --api-base-url https://api.example.com \
  --spec-from-stdin

# Pipe from curl
curl -s https://api.example.com/openapi.json | npx @ivotoby/openapi-mcp-server \
  --api-base-url https://api.example.com \
  --spec-from-stdin

Inline Specification

npx @ivotoby/openapi-mcp-server \
  --api-base-url https://api.example.com \
  --spec-inline '{"openapi": "3.0.0", "info": {"title": "My API", "version": "1.0.0"}, "paths": {}}'

Tool Loading & Filtering Options

Control which API endpoints (tools) are loaded:

# Load only dynamic meta-tools
npx @ivotoby/openapi-mcp-server --api-base-url https://api.example.com --openapi-spec https://api.example.com/openapi.json --tools dynamic

# Load only explicitly specified tools
npx @ivotoby/openapi-mcp-server --api-base-url https://api.example.com --openapi-spec https://api.example.com/openapi.json --tools explicit --tool GET::users --tool POST::users

# Load only the GET /users endpoint tool
npx @ivotoby/openapi-mcp-server --api-base-url https://api.example.com --openapi-spec https://api.example.com/openapi.json --tool GET-users

# Load tools tagged with "user" under the "/users" resource
npx @ivotoby/openapi-mcp-server --api-base-url https://api.example.com --openapi-spec https://api.example.com/openapi.json --tag user --resource users

# Load only POST operations
npx @ivotoby/openapi-mcp-server --api-base-url https://api.example.com --openapi-spec https://api.example.com/openapi.json --operation post

Using as a Library

For developers who want to use this package as a library:

import { OpenAPIServer } from "@ivotoby/openapi-mcp-server"
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"

const config = {
  name: "my-api-server",
  version: "1.0.0",
  apiBaseUrl: "https://api.example.com",
  openApiSpec: "https://api.example.com/openapi.json",
  specInputMethod: "url" as const,
  headers: {
    Authorization: "Bearer your-token",
    "X-API-Key": "your-api-key",
  },
  transportType: "stdio" as const,
  toolsMode: "all" as const,
}

const server = new OpenAPIServer(config)
const transport = new StdioServerTransport()
await server.start(transport)

Advanced Authentication with AuthProvider

For APIs with token expiration or complex authentication:

import { OpenAPIServer, AuthProvider } from "@ivotoby/openapi-mcp-server"
import { AxiosError } from "axios"

class MyAuthProvider implements AuthProvider {
  async getAuthHeaders(): Promise<Record<string, string>> {
    if (this.isTokenExpired()) {
      await this.refreshToken()
    }
    return { Authorization: `Bearer ${this.token}` }
  }

  async handleAuthError(error: AxiosError): Promise<boolean> {
    if (error.response?.status === 401) {
      await this.refreshToken()
      return true // Retry the request
    }
    return false
  }
}

const authProvider = new MyAuthProvider()
const config = {
  // ... other config
  authProvider: authProvider,
}

HTTP Transport Usage

When using HTTP transport:

# Initialize a session
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"curl-client","version":"1.0.0"}}}'

# Send a request to list tools
curl -X POST http://localhost:3000/mcp \
  -H "Content-Type: application/json" \
  -H "Mcp-Session-Id: your-session-id" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'

# Open a streaming connection for server responses
curl -N http://localhost:3000/mcp -H "Mcp-Session-Id: your-session-id"

# Terminate the session
curl -X DELETE http://localhost:3000/mcp -H "Mcp-Session-Id: your-session-id"

Debugging

To see debug logs:

  1. When using stdio transport with Claude Desktop:

    • Logs appear in the Claude Desktop logs
  2. When using HTTP transport:

    npx @ivotoby/openapi-mcp-server --transport http &2>debug.log
    

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "openapi" '{"command":"npx","args":["-y","@ivotoby/openapi-mcp-server"],"env":{"API_BASE_URL":"https://api.example.com","OPENAPI_SPEC_PATH":"https://api.example.com/openapi.json","API_HEADERS":"Authorization:Bearer token123,X-API-Key:your-api-key"}}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "openapi": {
            "command": "npx",
            "args": [
                "-y",
                "@ivotoby/openapi-mcp-server"
            ],
            "env": {
                "API_BASE_URL": "https://api.example.com",
                "OPENAPI_SPEC_PATH": "https://api.example.com/openapi.json",
                "API_HEADERS": "Authorization:Bearer token123,X-API-Key:your-api-key"
            }
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "openapi": {
            "command": "npx",
            "args": [
                "-y",
                "@ivotoby/openapi-mcp-server"
            ],
            "env": {
                "API_BASE_URL": "https://api.example.com",
                "OPENAPI_SPEC_PATH": "https://api.example.com/openapi.json",
                "API_HEADERS": "Authorization:Bearer token123,X-API-Key:your-api-key"
            }
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later