MCP REST Server

MCP REST Server

Enables interaction with any REST API through token or login authentication, with automatic Swagger/OpenAPI documentation integration for endpoint discovery and comprehensive HTTP request support.

Category
访问服务器

README

MCP REST Server

A Model Context Protocol (MCP) server that provides REST API client functionality with authentication support and Swagger documentation integration.

Features

  • Multiple Authentication Methods: Support for both token-based and login-based authentication
  • Swagger Integration: Automatic endpoint discovery and documentation from OpenAPI/Swagger specs
  • Automatic Token Management: Handles token refresh and re-authentication
  • Comprehensive HTTP Methods: Support for GET, POST, PUT, DELETE, and PATCH requests
  • Error Handling: Robust error handling with retry logic
  • MCP Compatible: Fully compatible with the Model Context Protocol

Installation

npm install
npm run build

Development

npm run dev

Configuration

The server supports two authentication methods:

Token Authentication

{
  "baseUrl": "https://api.example.com",
  "swaggerUrl": "https://api.example.com/swagger.json",
  "auth": {
    "type": "token",
    "token": "your-api-token-here"
  },
  "timeout": 30000,
  "retries": 3
}

Login Authentication

{
  "baseUrl": "https://api.example.com",
  "swaggerUrl": "https://api.example.com/swagger.json",
  "auth": {
    "type": "login",
    "username": "your-username",
    "password": "your-password",
    "loginEndpoint": "/auth/login",
    "tokenField": "access_token"
  },
  "timeout": 30000,
  "retries": 3
}

Available Tools

1. configure_rest_client

Configure the REST client with authentication and API details.

Parameters:

  • baseUrl (required): Base URL for the REST API
  • auth (required): Authentication configuration (token or login)
  • swaggerUrl (optional): URL to Swagger/OpenAPI documentation
  • timeout (optional): Request timeout in milliseconds (default: 30000)
  • retries (optional): Number of retries for failed requests (default: 3)

2. http_request

Make HTTP requests to the configured API.

Parameters:

  • method (required): HTTP method (GET, POST, PUT, DELETE, PATCH)
  • path (required): API endpoint path
  • params (optional): Query parameters or request body parameters
  • body (optional): Request body for POST, PUT, PATCH requests
  • headers (optional): Additional headers

3. get_swagger_documentation

Get the complete list of available endpoints from Swagger documentation.

4. search_endpoints

Search for endpoints in the Swagger documentation.

Parameters:

  • query (required): Search query to find matching endpoints

5. get_endpoint_info

Get detailed information about a specific endpoint.

Parameters:

  • path (required): Endpoint path
  • method (required): HTTP method

6. check_authentication

Check if the client is currently authenticated.

7. logout

Logout and clear authentication state.

Usage Examples

Basic Setup

  1. Configure the client:
{
  "baseUrl": "https://jsonplaceholder.typicode.com",
  "auth": {
    "type": "token",
    "token": "dummy-token"
  }
}
  1. Make a GET request:
{
  "method": "GET",
  "path": "/posts/1"
}
  1. Make a POST request:
{
  "method": "POST",
  "path": "/posts",
  "body": {
    "title": "New Post",
    "body": "Post content",
    "userId": 1
  }
}

With Swagger Documentation

{
  "baseUrl": "https://petstore.swagger.io/v2",
  "swaggerUrl": "https://petstore.swagger.io/v2/swagger.json",
  "auth": {
    "type": "token",
    "token": "your-api-key"
  }
}

Then you can:

  • Search endpoints: search_endpoints with query "pet"
  • Get endpoint info: get_endpoint_info with path "/pet" and method "POST"
  • View all documentation: get_swagger_documentation

Authentication Flow

Token Authentication

  1. Token is stored and used immediately
  2. Added to requests as Authorization: Bearer <token>
  3. If 401 received, no automatic retry (token assumed invalid)

Login Authentication

  1. Makes login request to specified endpoint
  2. Extracts token from response using tokenField
  3. Stores token in memory
  4. Adds token to subsequent requests
  5. If 401 received, automatically re-authenticates and retries

Error Handling

  • Network errors: Automatic retry with exponential backoff
  • Authentication errors: Automatic re-authentication for login-based auth
  • Validation errors: Clear error messages with details
  • API errors: HTTP status and error message forwarding

Development

Project Structure

src/
├── types.ts          # TypeScript type definitions
├── auth.ts           # Authentication manager
├── swagger.ts        # Swagger documentation parser
├── rest-client.ts    # REST client implementation
└── index.ts          # MCP server implementation

Building

npm run build

Running

npm start

MCP Client Configuration

The MCP REST server now supports automatic configuration through multiple methods, eliminating the need to configure APIs manually for each project.

Configuration Methods (in order of priority)

  1. Command Line Arguments (highest priority)
  2. Environment Variables
  3. Configuration File
  4. Manual Configuration (via MCP tools - lowest priority)

Cursor Configuration

Option 1: Auto-Configuration with Environment Variables (Recommended)

{
  "mcpServers": {
    "mcp-rest-github": {
      "command": "node",
      "args": ["/path/to/your/mcp-rest/dist/index.js"],
      "env": {
        "MCP_REST_BASE_URL": "https://api.github.com",
        "MCP_REST_AUTH_TYPE": "token",
        "MCP_REST_TOKEN": "your-github-token-here",
        "MCP_REST_SWAGGER_URL": "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json"
      }
    },
    "mcp-rest-petstore": {
      "command": "node",
      "args": ["/path/to/your/mcp-rest/dist/index.js"],
      "env": {
        "MCP_REST_BASE_URL": "https://petstore.swagger.io/v2",
        "MCP_REST_AUTH_TYPE": "token",
        "MCP_REST_TOKEN": "your-api-key",
        "MCP_REST_SWAGGER_URL": "https://petstore.swagger.io/v2/swagger.json"
      }
    }
  }
}

Option 2: Auto-Configuration with Config Files

{
  "mcpServers": {
    "mcp-rest-github": {
      "command": "node",
      "args": ["/path/to/your/mcp-rest/dist/index.js", "--config", "/path/to/your/mcp-rest/examples/github-api.json"]
    },
    "mcp-rest-petstore": {
      "command": "node",
      "args": ["/path/to/your/mcp-rest/dist/index.js", "--config", "/path/to/your/mcp-rest/examples/petstore.json"]
    }
  }
}

Option 3: Auto-Configuration with Command Line Arguments

{
  "mcpServers": {
    "mcp-rest-github": {
      "command": "node",
      "args": [
        "/path/to/your/mcp-rest/dist/index.js",
        "--base-url", "https://api.github.com",
        "--auth-type", "token",
        "--token", "your-github-token-here",
        "--swagger-url", "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json"
      ]
    }
  }
}

Claude Desktop Configuration

Location:

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

Use the same configuration options as Cursor above.

Configuration Examples

The project includes several example configurations in the examples/ directory:

  • examples/github-api.json - GitHub API configuration
  • examples/petstore.json - Swagger Petstore API configuration
  • examples/jsonplaceholder.json - JSONPlaceholder API configuration

Environment Variables

Variable Description
MCP_REST_BASE_URL Base URL for the REST API (required)
MCP_REST_AUTH_TYPE Authentication type: 'token' or 'login' (required)
MCP_REST_TOKEN API token (required for token auth)
MCP_REST_USERNAME Username (required for login auth)
MCP_REST_PASSWORD Password (required for login auth)
MCP_REST_LOGIN_ENDPOINT Login endpoint path (required for login auth)
MCP_REST_TOKEN_FIELD Token field name in login response (default: access_token)
MCP_REST_SWAGGER_URL URL to Swagger/OpenAPI documentation
MCP_REST_TIMEOUT Request timeout in milliseconds (default: 30000)
MCP_REST_RETRIES Number of retries for failed requests (default: 3)
MCP_REST_CONFIG_FILE Path to JSON configuration file

Note: Replace /path/to/your/mcp-rest/ with the actual path to your MCP REST server directory.

Usage in Claude/Cursor

With Auto-Configuration (Recommended)

If you've configured the server with auto-configuration (environment variables, CLI args, or config file), the server will be ready to use immediately:

Make a GET request to /posts/1
Show me all available endpoints
Search for endpoints related to "user"

With Manual Configuration

If you haven't provided auto-configuration, you can still configure the client manually:

  1. Configure the client first:
Please configure the REST client with:
- Base URL: https://api.example.com
- Authentication: token
- Token: your-api-token-here
- Swagger URL: https://api.example.com/swagger.json
  1. Then make API requests:
Make a GET request to /users/123

Testing the Configuration

You can test your configuration before using it in Claude/Cursor:

# Test with config file
node dist/index.js --config examples/jsonplaceholder.json

# Test with CLI arguments
node dist/index.js --base-url https://api.github.com --auth-type token --token your-token

# Test with environment variables
MCP_REST_BASE_URL=https://httpbin.org MCP_REST_AUTH_TYPE=token MCP_REST_TOKEN=test node dist/index.js

If you see "✅ Auto-configured REST client for [URL]", the configuration is working correctly.

License

MIT

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选