watchakorn-18k/template-swagger-to-mcp
If you are the rightful owner of template-swagger-to-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
This template allows you to convert an OpenAPI/Swagger specification into a Model Context Protocol (MCP) server using FastMCP.
Swagger-to-MCP Template
Turn any existing OpenAPI/Swagger specification into a Model Context Protocol (MCP) server powered by FastMCP. This repository is a minimal starting point for exposing an HTTP API to MCP-compatible clients such as Claude Code and Codex.
What you get
main.pythat loadsswagger.yaml, builds a reusablehttpx.AsyncClient, and serves it through FastMCP.- Dependency management via
pyproject.toml(withuv.lockfor reproducible installs). - A place to drop your own OpenAPI 3.x / Swagger 2.0 spec (
swagger.yaml).
Local setup after cloning
- Clone & enter the project
git clone <repo-url> cd template-swagger-to-mcp - Place your OpenAPI document – copy or generate the file as
swagger.yamlalongsidemain.py. - Install dependencies
# Using uv (recommended) uv sync # or using pip python -m venv .venv source .venv/bin/activate pip install -e . - Check the HTTP client configuration – update
client = httpx.AsyncClient(base_url=...)inmain.pyif your API lives somewhere other thanhttp://localhost:1323, or add your own environment-variable handling.
Running the MCP server locally
uv run python main.py
FastMCP serves over STDIO. Any MCP-aware client that invokes this command will proxy every operation defined in your OpenAPI document.
Working straight from GitHub
MCP clients still need the code on your machine, but you can automate pulling the latest version from GitHub before launch.
# Run once to place the repo where your MCP clients expect it
mkdir -p ~/mcp-servers
cd ~/mcp-servers
if [ -d template-swagger-to-mcp ]; then
cd template-swagger-to-mcp
git pull
else
git clone https://github.com/watchakorn-18k/template-swagger-to-mcp template-swagger-to-mcp
cd template-swagger-to-mcp
fi
uv sync
You can reuse the same folder for both Claude Code and Codex configurations (see below). Whenever you want the newest version, run the git pull snippet above. If you create your own fork, replace <owner>/<repo> with the GitHub namespace for that fork.
Connecting to Claude Code (Claude Desktop)
Claude Code reads MCP definitions from a JSON config file. After you have the project available locally (cloned or auto-pulled as shown above):
- Locate the config file:
- macOS:
~/Library/Application Support/Anthropic/claude_code_config.json - Windows:
%APPDATA%/Anthropic/claude_code_config.json - Linux:
~/.config/Anthropic/claude_code_config.json
- macOS:
- Add an entry under
"mcpServers"that launches the script from your local path:{ "mcpServers": { "tempate-api": { "command": "uv", "args": [ "run", "python", "main.py" ], "cwd": "/Users/<you>/mcp-servers/template-swagger-to-mcp", "env": { "PYTHONPATH": "." } } } }- Swap in the real absolute path you cloned to. If you prefer to activate a virtual environment before running, point the command to a wrapper script that does so.
- Restart Claude Code. The new MCP server appears in the tools list; enable it and start issuing API calls from chats or the command palette.
Claude MCP CLI:
claude mcp add pinto-api-mcp -- uv --directory /Users/<ชื่อผู้ใช้>/pinto-api-mcp run main.py
Tip: For authenticated APIs, add secrets via Claude Code's UI (Settings → Tools) or inject environment variables in the
envblock.
Connecting to Codex CLI
Codex also supports MCP servers. Configure it by editing ~/.config/codex/config.json (create it if necessary) and adding a matching entry:
{
"mcpServers": {
"tempate-api": {
"command": "uv",
"args": [
"run",
"python",
"main.py"
],
"cwd": "/Users/<you>/mcp-servers/template-swagger-to-mcp"
}
}
}
Then start a Codex session:
codex chat --with tempate-api
Codex spawns the MCP server using the configured command and exposes each OpenAPI operation as a callable tool. If you want Codex to keep the repository fresh automatically, wrap the command in a shell script that performs git pull before launching uv run python main.py.
Maintaining your bridge
- Update
swagger.yamlwhenever backend endpoints change; FastMCP regenerates tool definitions on the next launch. - Introduce auth helpers, interceptors, or custom tools by expanding
main.pybefore instantiatingFastMCP. - Keep dependencies locked with
uv lock --update(or manage with standardpipworkflows).
Troubleshooting
- Spec fails to load: validate it with
npx @redocly/cli lint swagger.yamlor another OpenAPI validator. - Connection errors: ensure
base_urlpoints to a reachable server and that any required auth headers are configured. - MCP client cannot find the server: double-check config file paths,
cwdvalues, and restart the client after edits.
Happy building!
