OpenAPI MCP Server
A Model Context Protocol (MCP) server that exposes OpenAPI endpoints as MCP resources. This server allows Large Language Models to discover and interact with REST APIs defined by OpenAPI specifications through the MCP protocol.
📖 Documentation
- User Guide - For users wanting to use this MCP server with Claude Desktop, Cursor, or other MCP clients
- Library Usage - For developers creating custom MCP servers using this package as a library
- Developer Guide - For contributors and developers working on the codebase
- AuthProvider Guide - Detailed authentication patterns and examples
User Guide
This section covers how to use the MCP server as an end user with Claude Desktop, Cursor, or other MCP-compatible tools.
Overview
This MCP server can be used in two ways:
- CLI Tool: Use
npx @ivotoby/openapi-mcp-server
directly with command-line arguments for quick setup - Library: Import and use the
OpenAPIServer
class in your own Node.js applications for custom implementations
The server supports two transport methods:
- Stdio Transport (default): For direct integration with AI systems like Claude Desktop that manage MCP connections through standard input/output.
- Streamable HTTP Transport: For connecting to the server over HTTP, allowing web clients and other HTTP-capable systems to use the MCP protocol.
Quick Start for Users
Option 1: Using with Claude Desktop (Stdio Transport)
No need to clone this repository. Simply configure Claude Desktop to use this MCP server:
- Locate or create your Claude Desktop configuration file:
- On macOS:
~/Library/Application Support/Claude/claude_desktop_config.json
- On macOS:
- Add the following configuration:
- Replace the environment variables with your actual API configuration:
API_BASE_URL
: The base URL of your APIOPENAPI_SPEC_PATH
: URL or path to your OpenAPI specificationAPI_HEADERS
: Comma-separated key pairs for API authentication headers
Option 2: Using with HTTP Clients (HTTP Transport)
To use the server with HTTP clients:
- No installation required! Use npx to run the package directly:
- Interact with the server using HTTP requests:
Configuration Options
The server can be configured through environment variables or command line arguments:
Environment Variables
API_BASE_URL
- Base URL for the API endpointsOPENAPI_SPEC_PATH
- Path or URL to OpenAPI specificationOPENAPI_SPEC_FROM_STDIN
- Set to "true" to read OpenAPI spec from standard inputOPENAPI_SPEC_INLINE
- Provide OpenAPI spec content directly as a stringAPI_HEADERS
- Comma-separated key pairs for API headersSERVER_NAME
- Name for the MCP server (default: "mcp-openapi-server")SERVER_VERSION
- Version of the server (default: "1.0.0")TRANSPORT_TYPE
- Transport type to use: "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" (load all endpoint-based tools), "dynamic" (load only meta-tools), or "explicit" (load only tools specified in includeTools) (default: "all")DISABLE_ABBREVIATION
- Disable name optimization (this could throw errors when name is > 64 chars)
Command Line Arguments
OpenAPI Specification Loading
The MCP server supports multiple methods for loading OpenAPI specifications, providing flexibility for different deployment scenarios:
1. URL Loading (Default)
Load the OpenAPI spec from a remote URL:
2. Local File Loading
Load the OpenAPI spec from a local file:
3. Standard Input Loading
Read the OpenAPI spec from standard input (useful for piping or containerized environments):
4. Inline Specification
Provide the OpenAPI spec content directly as a command line argument:
Supported Formats
All loading methods support both JSON and YAML formats. The server automatically detects the format and parses accordingly.
Docker and Container Usage
For containerized deployments, you can mount OpenAPI specs or use stdin:
Error Handling
The server provides detailed error messages for spec loading failures:
- URL loading: HTTP status codes and network errors
- File loading: File system errors (not found, permissions, etc.)
- Stdin loading: Empty input or read errors
- Inline loading: Missing content errors
- Parsing errors: Detailed JSON/YAML syntax error messages
Validation
Only one specification source can be used at a time. The server will validate that exactly one of the following is provided:
--openapi-spec
(URL or file path)--spec-from-stdin
--spec-inline
If multiple sources are specified, the server will exit with an error message.
Tool Loading & Filtering Options
Based on the Stainless article "What We Learned Converting Complex OpenAPI Specs to MCP Servers" (https://www.stainless.com/blog/what-we-learned-converting-complex-openapi-specs-to-mcp-servers), the following flags were added to control which API endpoints (tools) are loaded:
--tools <all|dynamic|explicit>
: Choose tool loading mode:all
(default): Load all tools from the OpenAPI spec, applying any specified filtersdynamic
: Load only dynamic meta-tools (list-api-endpoints
,get-api-endpoint-schema
,invoke-api-endpoint
)explicit
: Load only tools explicitly listed in--tool
options, ignoring all other filters
--tool <toolId>
: Import only specified tool IDs or names. Can be used multiple times.--tag <tag>
: Import only tools with the specified OpenAPI tag. Can be used multiple times.--resource <resource>
: Import only tools under the specified resource path prefixes. Can be used multiple times.--operation <method>
: Import only tools for the specified HTTP methods (get, post, etc). Can be used multiple times.
Examples:
Transport Types
Stdio Transport (Default)
The stdio transport is designed for direct integration with AI systems like Claude Desktop that manage MCP connections through standard input/output. This is the simplest setup and requires no network configuration.
When to use: When integrating with Claude Desktop or other systems that support stdio-based MCP communication.
Streamable HTTP Transport
The HTTP transport allows the MCP server to be accessed over HTTP, enabling web applications and other HTTP-capable clients to interact with the MCP protocol. It supports session management, streaming responses, and standard HTTP methods.
Key features:
- Session management with Mcp-Session-Id header
- HTTP responses for
initialize
andtools/list
requests are sent synchronously on the POST. - Other server-to-client messages (e.g.,
tools/execute
results, notifications) are streamed over a GET connection using Server-Sent Events (SSE). - Support for POST/GET/DELETE methods
When to use: When you need to expose the MCP server to web clients or systems that communicate over HTTP rather than stdio.
Security Considerations
- The HTTP transport validates Origin headers to prevent DNS rebinding attacks
- By default, HTTP transport only binds to localhost (127.0.0.1)
- If exposing to other hosts, consider implementing additional authentication
Debugging
To see debug logs:
- When using stdio transport with Claude Desktop:
- Logs appear in the Claude Desktop logs
- When using HTTP transport:
Library Usage
This section is for developers who want to use this package as a library to create custom MCP servers.
🚀 Using as a Library
Create dedicated MCP servers for specific APIs by importing and configuring the OpenAPIServer
class. This approach is ideal for:
- Custom Authentication: Implement complex authentication patterns with the
AuthProvider
interface - API-Specific Optimizations: Filter endpoints, customize error handling, and optimize for specific use cases
- Distribution: Package your server as a standalone npm module for easy sharing
- Integration: Embed the server in larger applications or add custom middleware
Basic Library Usage
Tool Loading Modes
The toolsMode
configuration option controls which tools are loaded from your OpenAPI specification:
Advanced Authentication with AuthProvider
For APIs with token expiration, refresh requirements, or complex authentication:
📁 See the examples/ directory for complete, runnable examples including:
- Basic library usage with static authentication
- AuthProvider implementations for different scenarios
- Real-world Beatport API integration
- Production-ready packaging patterns
🔐 Dynamic Authentication with AuthProvider
The AuthProvider
interface enables sophisticated authentication scenarios that static headers cannot handle:
Key Features
- Dynamic Headers: Fresh authentication headers for each request
- Token Expiration Handling: Automatic detection and handling of expired tokens
- Authentication Error Recovery: Retry logic for recoverable authentication failures
- Custom Error Messages: Provide clear, actionable guidance to users
AuthProvider Interface
Common Patterns
Automatic Token Refresh
Manual Token Management (e.g., Beatport)
API Key Authentication
📖 For detailed AuthProvider documentation and examples, see docs/auth-provider-guide.md
OpenAPI Schema Processing
Reference Resolution
This MCP server implements robust OpenAPI reference ($ref
) resolution to ensure accurate representation of API schemas:
- Parameter References: Fully resolves
$ref
pointers to parameter components in the OpenAPI spec - Schema References: Handles nested schema references within parameters and request bodies
- Recursive References: Prevents infinite loops by detecting and handling circular references
- Nested Properties: Preserves complex nested object and array structures with all their attributes
Input Schema Composition
The server intelligently merges parameters and request bodies into a unified input schema for each tool:
- Parameters + Request Body Merging: Combines path, query, and body parameters into a single schema
- Collision Handling: Resolves naming conflicts by prefixing body properties that conflict with parameter names
- Type Preservation: Maintains the original type information for all schema elements
- Metadata Retention: Preserves descriptions, formats, defaults, enums, and other schema attributes
Complex Schema Support
The MCP server handles various OpenAPI schema complexities:
- Primitive Type Bodies: Wraps non-object request bodies in a "body" property
- Object Bodies: Flattens object properties into the tool's input schema
- Array Bodies: Properly handles array schemas with their nested item definitions
- Required Properties: Tracks and preserves which parameters and properties are required
Developer Information
For Developers
Development Tools
npm run build
- Builds the TypeScript sourcenpm run clean
- Removes build artifactsnpm run typecheck
- Runs TypeScript type checkingnpm run lint
- Runs ESLintnpm run dev
- Watches source files and rebuilds on changesnpm run inspect-watch
- Runs the inspector with auto-reload on changes
Development Workflow
- Clone the repository
- Install dependencies:
npm install
- Start the development environment:
npm run inspect-watch
- Make changes to the TypeScript files in
src/
- The server will automatically rebuild and restart
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting:
npm run typecheck && npm run lint
- Submit a pull request
📖 For comprehensive developer documentation, see docs/developer-guide.md
FAQ
Q: What is a "tool"? A: A tool corresponds to a single API endpoint derived from your OpenAPI specification, exposed as an MCP resource.
Q: How can I use this package in my own project?
A: You can import the OpenAPIServer
class and use it as a library in your Node.js application. This allows you to create dedicated MCP servers for specific APIs with custom authentication, filtering, and error handling. See the examples/ directory for complete implementations.
Q: What's the difference between using the CLI and using it as a library?
A: The CLI is great for quick setup and testing, while the library approach allows you to create dedicated packages for specific APIs, implement custom authentication with AuthProvider
, add custom logic, and distribute your server as a standalone npm module.
Q: How do I handle APIs with expiring tokens?
A: Use the AuthProvider
interface instead of static headers. AuthProvider allows you to implement dynamic authentication with token refresh, expiration handling, and custom error recovery. See the AuthProvider examples for different patterns.
Q: What is AuthProvider and when should I use it?
A: AuthProvider
is an interface for dynamic authentication that gets fresh headers before each request and handles authentication errors. Use it when your API has expiring tokens, requires token refresh, or needs complex authentication logic that static headers can't handle.
Q: How do I filter which tools are loaded?
A: Use the --tool
, --tag
, --resource
, and --operation
flags with --tools all
(default), set --tools dynamic
for meta-tools only, or use --tools explicit
to load only tools specified with --tool
(ignoring other filters).
Q: When should I use dynamic mode?
A: Dynamic mode provides meta-tools (list-api-endpoints
, get-api-endpoint-schema
, invoke-api-endpoint
) to inspect and interact with endpoints without preloading all operations, which is useful for large or changing APIs.
Q: How do I specify custom headers for API requests?
A: Use the --headers
flag or API_HEADERS
environment variable with key:value
pairs separated by commas for CLI usage. For library usage, use the headers
config option or implement an AuthProvider
for dynamic headers.
Q: Which transport methods are supported? A: The server supports stdio transport (default) for integration with AI systems and HTTP transport (with streaming via SSE) for web clients.
Q: How does the server handle complex OpenAPI schemas with references?
A: The server fully resolves $ref
references in parameters and schemas, preserving nested structures, default values, and other attributes. See the "OpenAPI Schema Processing" section for details on reference resolution and schema composition.
Q: What happens when parameter names conflict with request body properties?
A: The server detects naming conflicts and automatically prefixes body property names with body_
to avoid collisions, ensuring all properties are accessible.
Q: Can I package my MCP server for distribution?
A: Yes! When using the library approach, you can create a dedicated npm package for your API. See the Beatport example for a complete implementation that can be packaged and distributed as npx your-api-mcp-server
.
Q: Where can I find development and contribution guidelines? A: See the Developer Guide for comprehensive documentation on architecture, key concepts, development workflow, and contribution guidelines.
License
MIT
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
A server that enables Large Language Models to discover and interact with REST APIs defined by OpenAPI specifications through the Model Context Protocol.
- Overview
- Quick Start for Users
- Transport Types
- Configuration Options
- OpenAPI Specification Loading
- Tool Loading & Filtering Options
- Security Considerations
- Debugging
- For Developers
- FAQ
- License
Related Resources
Related MCP Servers
- -securityAlicense-qualityA high-performance FastAPI server supporting Model Context Protocol (MCP) for seamless integration with Large Language Models, featuring REST, GraphQL, and WebSocket APIs, along with real-time monitoring and vector search capabilities.Last updated -9PythonMIT License
- -securityFlicense-qualityThis server provides an API to query Large Language Models using context from local files, supporting various models and file types for context-aware responses.Last updated -1TypeScript
- AsecurityFlicenseAqualityA Model Context Protocol server that enables large language models to interact with Apache Superset databases through REST API, supporting database queries, table lookups, field information retrieval, and SQL execution.Last updated -45TypeScript
MCP TapData Serverofficial
-securityFlicense-qualityA Model Context Protocol server that enables Large Language Models to access and interact with database connections, including viewing schemas and performing CRUD operations on connected databases.Last updated -