generate_code
Create API call code from captured HTTP requests to simplify client-side implementation and testing.
Instructions
Generate API call code from a captured request.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| request_id | Yes | ||
| language | No | python | |
| framework | No | requests |
Implementation Reference
- src/proxypin_mcp/server.py:186-212 (handler)The handler function that implements the logic to generate code for a given request_id, supporting various languages and frameworks.
@mcp.tool() def generate_code( request_id: str, language: str = "python", framework: str = "requests", ) -> str: """Generate API call code from a captured request.""" normalized_language = language.lower().strip() if normalized_language not in VALID_LANGUAGES: return f"# Error: unsupported language '{language}'. Use one of {sorted(VALID_LANGUAGES)}" request = reader.get_request_by_id(request_id, DetailLevel.FULL) if request is None: return f"# Error: Request {request_id} not found" if normalized_language == "curl": return request.to_curl() if normalized_language == "python": normalized_framework = framework.lower().strip() if normalized_framework not in VALID_PYTHON_FRAMEWORKS: normalized_framework = "requests" return _gen_python(request, normalized_framework) normalized_framework = framework.lower().strip() if normalized_framework not in VALID_JS_FRAMEWORKS: normalized_framework = "fetch" return _gen_js(request, normalized_framework) - src/proxypin_mcp/server.py:223-269 (helper)Helper function to generate Python code for the request.
def _gen_python(req: RequestFull, framework: str) -> str: """Generate Python code.""" lines = [] headers = _safe_headers(req) method = req.method.upper() if framework == "httpx": lines.extend(["import asyncio", "import httpx", "", "async def main():"]) indent = " " else: lines.extend(["import requests", ""]) indent = "" lines.append(f"{indent}method = {json.dumps(method)}") lines.append(f"{indent}url = {json.dumps(req.url, ensure_ascii=False)}") if headers: lines.append(f"{indent}headers = {json.dumps(headers, ensure_ascii=False)}") payload_param = "" if req.request_body_json is not None: lines.append(f"{indent}payload = {json.dumps(req.request_body_json, ensure_ascii=False)}") payload_param = ", json=payload" elif req.request_body: lines.append(f"{indent}payload = {json.dumps(req.request_body, ensure_ascii=False)}") payload_param = ", data=payload" headers_param = ", headers=headers" if headers else "" lines.append("") if framework == "httpx": lines.append(f"{indent}async with httpx.AsyncClient(timeout=30) as client:") lines.append( f"{indent} response = await client.request(" f"method, url{headers_param}{payload_param})" ) lines.append(f"{indent} print(response.status_code)") lines.append(f"{indent} print(response.text)") lines.append("") lines.append("asyncio.run(main())") else: lines.append( f"response = requests.request(method, url{headers_param}{payload_param}, timeout=30)" ) lines.append("print(response.status_code)") lines.append("print(response.text)") return "\n".join(lines) - src/proxypin_mcp/server.py:272-315 (helper)Helper function to generate JavaScript/TypeScript code for the request.
def _gen_js(req: RequestFull, framework: str) -> str: """Generate JavaScript/TypeScript code.""" lines = [] headers = _safe_headers(req) method = req.method.upper() if framework == "axios": lines.append("import axios from 'axios';") lines.append("") config: dict[str, Any] = {"method": method, "url": req.url} if headers: config["headers"] = headers if req.request_body_json is not None: config["data"] = req.request_body_json elif req.request_body: config["data"] = req.request_body lines.append(f"const config = {json.dumps(config, indent=2, ensure_ascii=False)};") lines.append("") lines.append("axios(config)") lines.append(" .then((response) => console.log(response.data))") lines.append(" .catch((error) => console.error(error));") return "\n".join(lines) lines.append(f"const url = {json.dumps(req.url, ensure_ascii=False)};") options: dict[str, Any] = {"method": method} if headers: options["headers"] = headers lines.append(f"const options = {json.dumps(options, indent=2, ensure_ascii=False)};") if req.request_body_json is not None: lines.append( "options.body = JSON.stringify(" f"{json.dumps(req.request_body_json, ensure_ascii=False)});" ) elif req.request_body: lines.append(f"options.body = {json.dumps(req.request_body, ensure_ascii=False)};") lines.append("") lines.append("fetch(url, options)") lines.append(" .then((response) => response.text())") lines.append(" .then((text) => console.log(text))") lines.append(" .catch((error) => console.error('Error:', error));") return "\n".join(lines)