# REST API to MCP Server — AI Agent Tools (`wsgcjj/mcp-server-adapter`) Actor

Convert any REST API into a Model Context Protocol (MCP) server configuration. Automatically generates tools and resources from OpenAPI specs or API documentation.

- **URL**: https://apify.com/wsgcjj/mcp-server-adapter.md
- **Developed by:** [陈俊杰](https://apify.com/wsgcjj) (community)
- **Categories:** AI, Developer tools, Agents
- **Stats:** 2 total users, 1 monthly users, 100.0% runs succeeded, NaN bookmarks
- **User rating**: No ratings yet

## Pricing

Pay per usage

This Actor is paid per platform usage. The Actor is free to use, and you only pay for the Apify platform usage, which gets cheaper the higher subscription plan you have.

Learn more: https://docs.apify.com/platform/actors/running/actors-in-store#pay-per-usage

## What's an Apify Actor?

Actors are a software tools running on the Apify platform, for all kinds of web data extraction and automation use cases.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.
Actors are written with capital "A".

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.
The best way to integrate Actors is as follows.

In JavaScript/TypeScript projects, use official [JavaScript/TypeScript client](https://docs.apify.com/api/client/js.md):

```bash
npm install apify-client
```

In Python projects, use official [Python client library](https://docs.apify.com/api/client/python.md):

```bash
pip install apify-client
```

In shell scripts, use [Apify CLI](https://docs.apify.com/cli/docs.md):

````bash
# MacOS / Linux
curl -fsSL https://apify.com/install-cli.sh | bash
# Windows
irm https://apify.com/install-cli.ps1 | iex
```bash

In AI frameworks, you might use the [Apify MCP server](https://docs.apify.com/platform/integrations/mcp.md).

If your project is in a different language, use the [REST API](https://docs.apify.com/api/v2.md).

For usage examples, see the [API](#api) section below.

For more details, see Apify documentation as [Markdown index](https://docs.apify.com/llms.txt) and [Markdown full-text](https://docs.apify.com/llms-full.txt).


# README

## MCP Server Adapter — REST API 转 MCP Server 配置

> 把任意 REST API 快速转换成 Model Context Protocol (MCP) Server 配置，让开发者无缝对接 MCP 生态。

### 🚀 功能

- **OpenAPI / Swagger 解析** — 自动抓取并解析 OpenAPI 规范，为每个 API 操作生成 MCP Tool 定义
- **URL 分析** — 分析 API 基 URL 的路径结构，生成通用的 HTTP Tool 配置（GET/POST/PUT/DELETE）
- **模板生成** — 输出 MCP Server 配置模板，由用户手动补充具体端点信息
- **标准 MCP 格式** — 输出包含 `name`、`version`、`tools`、`resources` 的标准 MCP 配置 JSON

### 📋 输入参数

| 参数 | 类型 | 必填 | 描述 |
|------|------|------|------|
| `api_url` | string | ✅ | OpenAPI 规范 URL、API 文档 URL 或 API 基 URL |
| `api_type` | enum | ✅ | 解析方式：`openapi` / `url` / `description` |
| `name` | string | ❌ | 自定义 MCP Server 名称（默认自动生成） |

#### api_type 说明

| 类型 | 说明 |
|------|------|
| `openapi` | 抓取 OpenAPI 3.x 或 Swagger 2.0 规范，自动解析 paths/operations 生成 MCP tools |
| `url` | 分析 URL 路径结构，生成通用 HTTP 工具（GET/POST/PUT/DELETE） |
| `description` | 输出一个 MCP Server 配置模板，用户手动填写具体端点 |

### 📤 输出格式

```json
{
  "name": "My API MCP Server",
  "version": "1.0.0",
  "tools": [
    {
      "name": "getPetById",
      "description": "Returns a single pet by ID",
      "input_schema": {
        "type": "object",
        "properties": {
          "petId": {
            "type": "integer",
            "description": "ID of pet to return"
          }
        },
        "required": ["petId"]
      },
      "url": "https://petstore.swagger.io/v2/pet/{petId}",
      "method": "GET"
    }
  ],
  "resources": [
    {
      "uri": "https://petstore.swagger.io/v2/pet/{petId}",
      "name": "Returns a single pet by ID"
    }
  ]
}
````

### 🧪 使用示例

#### 示例 1: 解析 OpenAPI 规范

```
输入:
  api_url: https://petstore.swagger.io/v2/swagger.json
  api_type: openapi
  name: Petstore MCP

输出: 完整的 MCP 配置，包含所有 Petstore API 端点的 tool 定义
```

#### 示例 2: URL 分析

```
输入:
  api_url: https://jsonplaceholder.typicode.com
  api_type: url

输出: 包含 GET/POST/PUT/DELETE 通用 HTTP 工具的 MCP 配置
```

#### 示例 3: 模板生成

```
输入:
  api_url: https://api.example.com/v1
  api_type: description

输出: MCP 配置模板，包含示例 tool 和 resource 定义
```

### 🔧 本地开发

```bash
## 克隆项目
cd apify-actors/mcp-server-adapter

## 安装依赖
pip install -r requirements.txt

## 本地运行（需要 Apify 令牌）
python -m src
```

### 📦 技术栈

- **Python 3.11+** — 异步运行时
- **Apify SDK** — Actor 框架，提供输入输出、日志、事件系统
- **httpx** — 异步 HTTP 客户端，用于抓取 OpenAPI 规范

### ⚠️ 注意事项

- 解析 OpenAPI 规范时，依赖目标 API 的规范 URL **可公开访问**
- URL 分析模式仅基于路径结构猜测资源，建议配合 OpenAPI 模式获得更精确结果
- 生成的 MCP 配置是**静态定义**，实际调用需要配合 MCP 客户端运行

### 📄 许可证

MIT

# Actor input Schema

## `api_url` (type: `string`):

URL to the OpenAPI specification (JSON/YAML), API documentation, or API base URL.

## `api_type` (type: `string`):

How to interpret the API URL: openapi = parse OpenAPI spec, url = generate generic HTTP tools, description = output template

## `name` (type: `string`):

Custom name for the generated MCP server.

## Actor input object example

```json
{
  "api_url": "https://petstore.swagger.io/v2/swagger.json",
  "api_type": "openapi"
}
```

# Actor output Schema

## `result` (type: `string`):

The generated MCP server configuration in JSON format

## `tool_count` (type: `string`):

Number of MCP tools generated

## `resource_count` (type: `string`):

Number of MCP resources generated

## `error` (type: `string`):

Error message if something went wrong

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {
    "api_url": "https://petstore.swagger.io/v2/swagger.json"
};

// Run the Actor and wait for it to finish
const run = await client.actor("wsgcjj/mcp-server-adapter").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = { "api_url": "https://petstore.swagger.io/v2/swagger.json" }

# Run the Actor and wait for it to finish
run = client.actor("wsgcjj/mcp-server-adapter").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print("💾 Check your data here: https://console.apify.com/storage/datasets/" + run["defaultDatasetId"])
for item in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{
  "api_url": "https://petstore.swagger.io/v2/swagger.json"
}' |
apify call wsgcjj/mcp-server-adapter --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "command": "npx",
            "args": [
                "mcp-remote",
                "https://mcp.apify.com/?tools=wsgcjj/mcp-server-adapter",
                "--header",
                "Authorization: Bearer <YOUR_API_TOKEN>"
            ]
        }
    }
}

```

## OpenAPI specification

```json
{
    "openapi": "3.0.1",
    "info": {
        "title": "REST API to MCP Server — AI Agent Tools",
        "description": "Convert any REST API into a Model Context Protocol (MCP) server configuration. Automatically generates tools and resources from OpenAPI specs or API documentation.",
        "version": "0.0",
        "x-build-id": "hoYh1xAEQDUUJc4YI"
    },
    "servers": [
        {
            "url": "https://api.apify.com/v2"
        }
    ],
    "paths": {
        "/acts/wsgcjj~mcp-server-adapter/run-sync-get-dataset-items": {
            "post": {
                "operationId": "run-sync-get-dataset-items-wsgcjj-mcp-server-adapter",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor, waits for its completion, and returns Actor's dataset items in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        },
        "/acts/wsgcjj~mcp-server-adapter/runs": {
            "post": {
                "operationId": "runs-sync-wsgcjj-mcp-server-adapter",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor and returns information about the initiated run in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/runsResponseSchema"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/acts/wsgcjj~mcp-server-adapter/run-sync": {
            "post": {
                "operationId": "run-sync-wsgcjj-mcp-server-adapter",
                "x-openai-isConsequential": false,
                "summary": "Executes an Actor, waits for completion, and returns the OUTPUT from Key-value store in response.",
                "tags": [
                    "Run Actor"
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/inputSchema"
                            }
                        }
                    }
                },
                "parameters": [
                    {
                        "name": "token",
                        "in": "query",
                        "required": true,
                        "schema": {
                            "type": "string"
                        },
                        "description": "Enter your Apify token here"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK"
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "inputSchema": {
                "type": "object",
                "required": [
                    "api_url",
                    "api_type"
                ],
                "properties": {
                    "api_url": {
                        "title": "API URL",
                        "type": "string",
                        "description": "URL to the OpenAPI specification (JSON/YAML), API documentation, or API base URL."
                    },
                    "api_type": {
                        "title": "API Type",
                        "enum": [
                            "openapi",
                            "url",
                            "description"
                        ],
                        "type": "string",
                        "description": "How to interpret the API URL: openapi = parse OpenAPI spec, url = generate generic HTTP tools, description = output template",
                        "default": "openapi"
                    },
                    "name": {
                        "title": "MCP Server Name",
                        "type": "string",
                        "description": "Custom name for the generated MCP server."
                    }
                }
            },
            "runsResponseSchema": {
                "type": "object",
                "properties": {
                    "data": {
                        "type": "object",
                        "properties": {
                            "id": {
                                "type": "string"
                            },
                            "actId": {
                                "type": "string"
                            },
                            "userId": {
                                "type": "string"
                            },
                            "startedAt": {
                                "type": "string",
                                "format": "date-time",
                                "example": "2025-01-08T00:00:00.000Z"
                            },
                            "finishedAt": {
                                "type": "string",
                                "format": "date-time",
                                "example": "2025-01-08T00:00:00.000Z"
                            },
                            "status": {
                                "type": "string",
                                "example": "READY"
                            },
                            "meta": {
                                "type": "object",
                                "properties": {
                                    "origin": {
                                        "type": "string",
                                        "example": "API"
                                    },
                                    "userAgent": {
                                        "type": "string"
                                    }
                                }
                            },
                            "stats": {
                                "type": "object",
                                "properties": {
                                    "inputBodyLen": {
                                        "type": "integer",
                                        "example": 2000
                                    },
                                    "rebootCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "restartCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "resurrectCount": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "computeUnits": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            },
                            "options": {
                                "type": "object",
                                "properties": {
                                    "build": {
                                        "type": "string",
                                        "example": "latest"
                                    },
                                    "timeoutSecs": {
                                        "type": "integer",
                                        "example": 300
                                    },
                                    "memoryMbytes": {
                                        "type": "integer",
                                        "example": 1024
                                    },
                                    "diskMbytes": {
                                        "type": "integer",
                                        "example": 2048
                                    }
                                }
                            },
                            "buildId": {
                                "type": "string"
                            },
                            "defaultKeyValueStoreId": {
                                "type": "string"
                            },
                            "defaultDatasetId": {
                                "type": "string"
                            },
                            "defaultRequestQueueId": {
                                "type": "string"
                            },
                            "buildNumber": {
                                "type": "string",
                                "example": "1.0.0"
                            },
                            "containerUrl": {
                                "type": "string"
                            },
                            "usage": {
                                "type": "object",
                                "properties": {
                                    "ACTOR_COMPUTE_UNITS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_WRITES": {
                                        "type": "integer",
                                        "example": 1
                                    },
                                    "KEY_VALUE_STORE_LISTS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_INTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_EXTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_RESIDENTIAL_TRANSFER_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_SERPS": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            },
                            "usageTotalUsd": {
                                "type": "number",
                                "example": 0.00005
                            },
                            "usageUsd": {
                                "type": "object",
                                "properties": {
                                    "ACTOR_COMPUTE_UNITS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATASET_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "KEY_VALUE_STORE_WRITES": {
                                        "type": "number",
                                        "example": 0.00005
                                    },
                                    "KEY_VALUE_STORE_LISTS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_READS": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "REQUEST_QUEUE_WRITES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_INTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "DATA_TRANSFER_EXTERNAL_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_RESIDENTIAL_TRANSFER_GBYTES": {
                                        "type": "integer",
                                        "example": 0
                                    },
                                    "PROXY_SERPS": {
                                        "type": "integer",
                                        "example": 0
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
```
