OpenAPI MCP Server

OpenAPI MCP Server

Transforms OpenAPI 3.x specifications into MCP tools, enabling Large Language Models to interact with REST APIs through standardized MCP protocol. Supports bearer token authentication and all HTTP methods for seamless API integration.

Category
访问服务器

README

OpenAPI MCP Server (JavaScript)

A pure JavaScript implementation of a Model Context Protocol (MCP) server that transforms OpenAPI 3.x specifications into MCP tools, enabling Large Language Models (LLMs) to interact with REST APIs through the standardized MCP protocol.

Features

  • Pure JavaScript: Built with Node.js and ES modules
  • OpenAPI 3.x Support: Automatically converts OpenAPI specifications into MCP tools
  • Bearer Token Authentication: Supports bearer token authentication for API requests
  • All HTTP Methods: Supports GET, POST, PUT, DELETE, PATCH, OPTIONS, and HEAD
  • Configurable Base URL: Accept custom API base URLs
  • Parameter Validation: Validates tool parameters against OpenAPI schemas
  • Error Handling: Comprehensive error handling and reporting
  • CLI Interface: Easy-to-use command-line interface

Installation

npm install

Usage

Quick Start

  1. Validate your OpenAPI specification:
node src/index.js validate -s UserQueueList.json
  1. Start the MCP server:
node src/index.js serve -s UserQueueList.json -b https://api.example.com -t your-bearer-token

Command Line Options

serve - Start the MCP server

node src/index.js serve [options]

Options:
  -s, --spec <path>       Path to OpenAPI specification file (required)
  -b, --base-url <url>    Base URL for API requests
  -t, --token <token>     Bearer token for authentication
  --timeout <ms>          Request timeout in milliseconds (default: 30000)
  --transport <type>      Transport type (stdio or http, default: stdio)
  --http-port <port>      HTTP server port (when using http transport, default: 3000)
  --http-host <host>      HTTP server host (when using http transport, default: localhost)

validate - Validate OpenAPI specification

node src/index.js validate -s <path-to-spec>

info - Show server information

node src/index.js info

Environment Variables

You can use environment variables instead of command-line arguments:

export OPENAPI_BASE_URL=https://api.example.com
export OPENAPI_BEARER_TOKEN=your-bearer-token
node src/index.js serve -s UserQueueList.json

node src/index.js serve -s UserQueueList.json --transport=http --http-port=8020

Configuration

OpenAPI Specification Requirements

  • Must be OpenAPI 3.x format
  • JSON format (.json extension)
  • Must contain at least one path/operation
  • Bearer token authentication should be defined in security schemes

Example OpenAPI Security Configuration

{
  "components": {
    "securitySchemes": {
      "bearer": {
        "type": "http",
        "scheme": "bearer"
      }
    }
  },
  "security": [
    {
      "bearer": []
    }
  ]
}

Tool Generation

The server automatically converts OpenAPI operations into MCP tools:

Tool Naming

  1. Uses operationId if available
  2. Falls back to operation summary (sanitized)
  3. Last resort: {method}_{path} (sanitized)

Parameter Mapping

  • Path parameters: Required string parameters
  • Query parameters: Optional parameters based on OpenAPI schema
  • Header parameters: Prefixed with header_
  • Request body: Object properties or requestBody parameter

Example Tool

For the UserQueueList.json specification:

// Tool: ListUsers (GET /domains/~/users/list)
{
  "name": "ListUsers",
  "description": "List Basic Info on Users in Domain",
  "inputSchema": {
    "type": "object",
    "properties": {},
    "required": []
  }
}

// Tool: ListCallqueues (GET /domains/~/callqueues/list)
{
  "name": "ListCallqueues", 
  "description": "Read Basic info on Call Queues in Domain",
  "inputSchema": {
    "type": "object",
    "properties": {},
    "required": []
  }
}

MCP Integration

Using with Claude Desktop

Stdio Transport (Default)

Add to your Claude Desktop configuration (~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "openapi-server": {
      "command": "node",
      "args": [
        "/path/to/your/project/src/index.js",
        "serve",
        "-s", "/path/to/UserQueueList.json",
        "-b", "https://your-api-domain.com",
        "-t", "your-bearer-token"
      ]
    }
  }
}

HTTP Transport

For HTTP transport, configure the server URL instead:

{
  "mcpServers": {
    "openapi-server": {
      "url": "http://localhost:3000/message"
    }
  }
}

Then start the server separately:

node src/index.js serve -s UserQueueList.json --transport http --http-port 3000

Tool Responses

The server returns structured JSON responses:

Success Response:

{
  "status": 200,
  "statusText": "OK",
  "data": {
    // API response data
  }
}

Error Response:

{
  "error": true,
  "status": 404,
  "statusText": "Not Found",
  "message": "Domain not found",
  "data": {
    "code": 404,
    "message": "Domain example does not exist"
  }
}

Transport Protocols

Stdio Transport

  • Default transport method
  • Uses standard input/output streams for communication
  • Best for integration with Claude Desktop and other MCP clients
  • Automatic process management

HTTP Transport

  • Uses HTTP Server-Sent Events (SSE) for communication
  • Allows remote connections and debugging
  • Useful for development and testing
  • Configurable host and port

API Examples

Based on the UserQueueList.json specification:

List Users

// Tool call
{
  "name": "ListUsers",
  "arguments": {}
}

// Response: Array of user objects with basic information

List Call Queues

// Tool call  
{
  "name": "ListCallqueues",
  "arguments": {}
}

// Response: Array of call queue objects with configuration

Development

Project Structure

src/
├── index.js              # CLI entry point
├── server.js             # MCP server implementation
├── openapi-processor.js  # OpenAPI specification processor
├── http-client.js        # HTTP client for API requests
└── utils.js              # Utility functions

Key Components

  • MCPServer: Main MCP server class handling tool registration and execution
  • OpenAPIProcessor: Parses OpenAPI specs and generates tool definitions
  • HttpClient: Handles HTTP requests with authentication and error handling
  • CLI: Command-line interface with validation and configuration options

Error Handling

The server provides comprehensive error handling:

  • Validation Errors: Parameter validation against OpenAPI schemas
  • HTTP Errors: Proper handling of API error responses
  • Authentication Errors: Clear messages for auth failures
  • Network Errors: Timeout and connectivity error handling

Security Considerations

  • Bearer tokens are handled securely and not logged
  • Request validation prevents injection attacks
  • Error messages don't expose sensitive information
  • HTTPS is recommended for all API communications

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

MIT License - see LICENSE file for details

Troubleshooting

Common Issues

  1. "Tool not found" errors: Check that your OpenAPI spec is valid and contains the expected operations
  2. Authentication failures: Verify your bearer token is correct and has proper permissions
  3. Network timeouts: Increase timeout or check API endpoint availability
  4. Schema validation errors: Ensure your OpenAPI spec follows 3.x standards

Debug Mode

For detailed logging, you can modify the server to enable debug output:

# The server logs to stderr for MCP compatibility
node src/index.js serve -s UserQueueList.json -b https://api.example.com -t token 2>debug.log

Validation

Always validate your OpenAPI specification before use:

node src/index.js validate -s UserQueueList.json

This will show:

  • ✅ Validation status
  • 📄 Specification details
  • 🔧 Available tools
  • 🔐 Security schemes
  • 🌐 Base URL information

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选