Skip to content

Commit e30e98a

Browse files
authored
openai-swagger-to-mcp (#49)
* openai-swagger-to-mcp * openai-swagger-to-mcp * openai-swagger-to-mcp * openai-swagger-to-mcp * fix
1 parent 488c0f5 commit e30e98a

55 files changed

Lines changed: 1491 additions & 19 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "npm"
4+
directory: "/"
5+
schedule:
6+
interval: "monthly"

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ test*
1919
openapiSchemaExample.json
2020
package-lock.json
2121
.gitconfig
22+
tasks.md
23+
plan.md
24+
code-review.md
25+
.playwright-mcp

ReadMe.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,71 @@
11
# openapi-toolkit
22
openapi-toolkit is an open-source tool designed to streamline the integration of OpenAPI (formerly known as Swagger) specifications into your development workflow. By taking an OpenAPI/Swagger file as input, the OpenAPI Toolkit automatically generates server and client code, enabling seamless integration of APIs. This automation accelerates development processes, ensures consistency across different platforms, and reduces the risk of manual errors. Whether you're building a new service or integrating with existing APIs, OpenAPI Toolkit simplifies the process by providing ready-to-use code tailored to your OpenAPI specifications.
33

4+
# Generate MCP Server
5+
6+
openapi-toolkit generates a fully functional [MCP SDK FastMCP](https://github.com/modelcontextprotocol/python-sdk) server from any OpenAPI/Swagger spec — one MCP tool per API endpoint, grouped by controller, with the async Pydantic v2 client bundled inline.
7+
8+
## Generate
9+
10+
```bash
11+
# no install required
12+
npx openapi-toolkit -i https://petstore3.swagger.io/api/v3/openapi.json -g python-mcp-server -t server -o ./my-api-mcp
13+
14+
# or with global install
15+
npm i -g openapi-toolkit
16+
openapi-toolkit -i https://petstore3.swagger.io/api/v3/openapi.json -g python-mcp-server -t server -o ./my-api-mcp
17+
```
18+
19+
Generated layout:
20+
```
21+
my-api-mcp/
22+
pyproject.toml # uv project: mcp, httpx, pydantic
23+
README.md
24+
src/
25+
mcp_server.py # FastMCP entry point, tool registration, transport CLI
26+
client/ # generated async httpx + Pydantic v2 client (inline)
27+
__init__.py
28+
client.py
29+
models/
30+
controllers/
31+
server/
32+
tools/
33+
<controller>.py # one file per API controller tag
34+
```
35+
36+
## Install and run
37+
38+
```bash
39+
cd my-api-mcp
40+
uv sync
41+
BASE_URL=https://api.example.com uv run python src/mcp_server.py
42+
```
43+
44+
## Transport options
45+
46+
```bash
47+
# stdio (default — for Claude Desktop and most MCP clients)
48+
BASE_URL=https://api.example.com uv run python src/mcp_server.py --transport stdio
49+
50+
# SSE
51+
BASE_URL=https://api.example.com uv run python src/mcp_server.py --transport sse --host 0.0.0.0 --port 8000
52+
53+
# Streamable HTTP
54+
BASE_URL=https://api.example.com uv run python src/mcp_server.py --transport streamable-http --host 0.0.0.0 --port 8000 --path /mcp
55+
```
56+
57+
A fully generated example (Petstore API) is available in this repo: [examples/pet-store-mcp](https://github.com/barnuri/openapi-toolkit/tree/master/examples/pet-store-mcp)
58+
59+
![pet-store-mcp folder structure](/docs/pet-store-mcp-vscode.png?raw=true)
60+
61+
## Environment variables
62+
63+
| Variable | Description |
64+
|---|---|
65+
| `BASE_URL` | Base URL of the target API (required at runtime) |
66+
| `TOOL_FILTER_ROUTES` | Comma-separated controller names to load (e.g. `pet,store`). Loads all if unset. |
67+
| `TOOL_FILTER_METHODS` | Comma-separated tool function names to register (e.g. `getPetById,addPet`). Registers all if unset. |
68+
469
# Install
570

671
[![Run Tests](https://github.com/barnuri/openapi-toolkit/actions/workflows/runTests.yaml/badge.svg)](https://github.com/barnuri/openapi-toolkit/actions/workflows/runTests.yaml) [![Create Tag And Release And Publish To NPM](https://github.com/barnuri/openapi-toolkit/actions/workflows/createTagAndReleaseAndPublish.yaml/badge.svg)](https://github.com/barnuri/openapi-toolkit/actions/workflows/createTagAndReleaseAndPublish.yaml)
File renamed without changes.

docs/pet-store-mcp-vscode.png

199 KB
Loading

examples/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ const ex = async () = {
3838

3939
### Result
4040

41-
![Example](https://github.com/barnuri/openapi-toolkit/blob/master/ex.png?raw=true)
41+
![Example](https://github.com/barnuri/openapi-toolkit/blob/master/docs/ex.png?raw=true)

examples/pet-store-mcp/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# pet-store-mcp
2+
3+
MCP server generated by [openapi-toolkit](https://github.com/barnuri/openapi-toolkit).
4+
5+
## Install
6+
7+
```bash
8+
uv sync
9+
```
10+
11+
## Run
12+
13+
```bash
14+
# stdio (default — for Claude Desktop and most MCP clients)
15+
BASE_URL=https://api.example.com uv run python src/mcp_server.py
16+
17+
# SSE
18+
BASE_URL=https://api.example.com uv run python src/mcp_server.py --transport sse --host 0.0.0.0 --port 8000
19+
20+
# Streamable HTTP
21+
BASE_URL=https://api.example.com uv run python src/mcp_server.py --transport streamable-http --host 0.0.0.0 --port 8000 --path /mcp
22+
```
23+
24+
## Environment variables
25+
26+
| Variable | Description |
27+
|---|---|
28+
| `BASE_URL` | Base URL of the target API (required) |
29+
| `TOOL_FILTER_ROUTES` | Comma-separated controller names to load (e.g. `pet,store`). Loads all if unset. |
30+
| `TOOL_FILTER_METHODS` | Comma-separated tool function names to register (e.g. `getPetById,addPet`). Registers all if unset. |
31+
32+
## CLI options
33+
34+
```
35+
--transport stdio | sse | streamable-http (default: stdio)
36+
--host host for HTTP transports (default: 0.0.0.0)
37+
--port port for HTTP transports (default: 8000)
38+
--path path for streamable-http (default: /mcp)
39+
```
40+
41+
## Project layout
42+
43+
```
44+
src/
45+
mcp_server.py # entry point — tool registration and transport CLI
46+
client/ # async httpx + Pydantic v2 client (generated inline)
47+
server/tools/ # one file per API controller
48+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[project]
2+
name = "pet-store-mcp"
3+
version = "0.1.0"
4+
requires-python = ">=3.11"
5+
dependencies = [
6+
"mcp>=1.0",
7+
"httpx>=0.27",
8+
"pydantic>=2.0",
9+
]
10+
11+
[build-system]
12+
requires = ["hatchling"]
13+
build-backend = "hatchling.build"
14+
15+
[tool.hatch.build.targets.wheel]
16+
packages = ["src/client", "src/server"]
17+
18+
[project.scripts]
19+
pet-store-mcp = "mcp_server:main"

examples/pet-store-mcp/src/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .client import Client

0 commit comments

Comments
 (0)