Last active
September 30, 2025 04:21
-
-
Save daryltucker/2e6c2b20149ddf4f310efbea60fd88ad to your computer and use it in GitHub Desktop.
openapi_mcp_confgen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # Neo-Retro Group | |
| # @daryltucker | |
| # /// script | |
| # dependencies = [ | |
| # "requests" | |
| # ] | |
| # /// | |
| # uv run https://tinyurl.com/openapi-mcp-confgen localhost:8000 | |
| import os | |
| import re | |
| import json | |
| import argparse | |
| import requests | |
| from urllib.parse import urlparse | |
| parser = argparse.ArgumentParser( | |
| description='Generate mcpServers configuration from an OpenAPI description.' | |
| ) | |
| parser.add_argument( | |
| 'mcp_server_url', | |
| type=str, | |
| help='The base URL of the MCP server (ie: http://localhost:8000)' | |
| ) | |
| parser.add_argument( | |
| 'mcp_server_auth_token', | |
| type=str, | |
| help='Authorization token for the MCP server (default: $MCP_AUTH_TOKEN)', | |
| default=os.getenv("MCP_AUTH_TOKEN"), | |
| nargs='?' | |
| ) | |
| parser.add_argument( | |
| '-r', '--remote', | |
| action='store_true', | |
| help='Enable remote wrapper for all tools') | |
| args = parser.parse_args() | |
| url_parsed = urlparse(args.mcp_server_url) | |
| url = args.mcp_server_url | |
| if not url_parsed.scheme: | |
| url = f"http://{url}" | |
| url_parsed = urlparse(url) | |
| try: | |
| url = url + '/openapi.json' | |
| response = requests.get(url) | |
| data = response.json() | |
| description = data['info']['description'] | |
| tools = [] | |
| lines = description.split('\n') | |
| except Exception as e: | |
| print(e) | |
| print("\n\nUnable to reach {url}/openapi.json!") | |
| for line in lines: | |
| line = line.strip() | |
| if line.startswith('- ['): | |
| # Use regex to extract [tool name] and (link) | |
| match = re.search(r'\[([^\]]+)\]\(([^)]+)\)', line) | |
| if match: | |
| tool_name = match.group(1) | |
| link = match.group(2) | |
| # Process the link to remove '/docs' and prepend the base URL | |
| url_path = link.replace('/docs', '', 1) | |
| full_url = f'{url}{url_path}' | |
| tools.append({ | |
| 'name': tool_name, | |
| 'url': full_url | |
| }) | |
| # Construct the mcpServers JSON structure | |
| auth_token = args.mcp_server_auth_token or '$__ACCESS_TOKEN__' | |
| mcp_servers = {} | |
| if args.remote: | |
| for tool in tools: | |
| mcp_servers[tool['name']] = { | |
| 'command': 'npx', | |
| 'args': [ | |
| 'mcp-remote', | |
| tool['url'], | |
| '--header', | |
| auth_token, | |
| '--allow-http' | |
| ], | |
| 'env': { | |
| 'AUTH_TOKEN': '$__ACCESS_TOKEN__' | |
| } | |
| } | |
| else: | |
| for tool in tools: | |
| mcp_servers[tool['name']] = { | |
| 'type': 'http', | |
| 'url': tool['url'], | |
| 'headers': { | |
| 'Authorization': f'Bearer {auth_token}' | |
| } | |
| } | |
| config = { | |
| "mcpServers": mcp_servers | |
| } | |
| fname = f"mcpServers-{url_parsed.hostname}_{url_parsed.port}.json" | |
| with open(fname, 'w') as f: | |
| json.dump(config, f, indent=2) | |
| print(f"Successfully Generated: {fname}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment