Home › Recipes
All recipes
9 recipes tagged python.
Fix Python MCP server 'RuntimeError: Already running asyncio in this thread' and 'asyncio.run() cannot be called from a
Tested on Python mcp 1.30.0 (FastMCP) and 2.3.0 (MCPServer): mcp.run() inside asyncio.run(main()) crashes, sync tools that call asyncio.run fail on 1.x, blocking calls in async tools serialize parallel requests. Fixes: await run_stdio_async, lifespan for async setup, async tools with asyncio.to_thre
Fix MCP stdio 'Unexpected token ... is not valid JSON' and timeouts caused by console.log / print() on stdout
Tested (TS SDK 1.32.1, Python mcp 2.3.0 and 1.30.0): what console.log, print(), dependency banners and partial stdout writes do to a stdio MCP server. Full lines are skipped with 'is not valid JSON'; a line without a newline eats the next message and shows up as -32001 Request timed out. Verified fi
Fix MCP server not seeing environment variables (API key undefined, .env not loaded, env block ignored)
Tested (TS SDK 1.32.1, Python mcp 2.3.0/1.30.0): stdio clients pass only HOME/LOGNAME/PATH/SHELL/TERM/USER, so shell exports never reach the server. env merges with defaults, PATH override breaks spawn, ${VAR} stays literal, dotenv reads the client's cwd, null becomes the string 'null', bash -c skip
Fix ModuleNotFoundError: No module named 'mcp.server.fastmcp' after upgrading to mcp 2.x (FastMCP renamed to MCPServer)
Tested on Python mcp 2.3.0 vs 1.30.0: FastMCP is now mcp.server.mcpserver.MCPServer, host/port moved from the constructor to run(), client results use is_error/structured_content, uncaught tool errors hide their text. Pin mcp<2 to unblock, then a tested migration and a 1.x/2.x import shim.
Fix uvx MCP servers: spawn uvx ENOENT, -32000 Connection closed, 'No solution found' and ImportError McpError
Tested with uv 0.13.5, Python mcp 1.30.0 and TS SDK 1.32.2 clients: what a missing PATH, a package typo, a wrong executable name, Python 3.9, --offline and an old pinned server that pulls mcp 2.x print; why the client only says Connection closed; and the configs that work (absolute uvx path, --from,
Fix MCP SSL errors: CERTIFICATE_VERIFY_FAILED, unable to get local issuer certificate, fetch failed behind a corporate p
Tested with Python mcp 1.30.0 (httpx) and the TS SDK 1.32.2 / mcp-remote on Node 20, 22 and 24 against six bad-certificate setups: what each TLS error means, how to trust an internal or proxy CA (NODE_EXTRA_CA_CERTS, --use-system-ca, SSL_CERT_FILE bundle, httpx_client_factory, truststore), and the s
Fix MCP stdio server Error: write EPIPE and orphaned server processes after the client disconnects
Tested with Node 20 + TS SDK 1.32.2 and Python mcp 1.30.0: when a stdio MCP server crashes with write EPIPE, when it stays running as an orphan (any setInterval in Node, closed stdout in Python), and the few lines that make it exit cleanly.
Build your first MCP server in Python (free) and connect it to Claude, Cursor or any AI agent
Step-by-step: create a minimal Model Context Protocol (MCP) server with the official Python SDK, expose one useful tool, test it locally, and add it to an MCP client such as Claude Code or Cursor. No paid tools needed.
Fix MCP Streamable HTTP errors: fetch failed, ECONNRESET, Connection reset by peer, other side closed
Tested with the TS SDK 1.32.2 (undici fetch), Node http, curl, Python urllib and mcp 1.30.0/httpx against servers that RST after accept, after initialize, mid-body, or after kill -9: how ECONNRESET, UND_ERR_SOCKET 'other side closed', RemoteProtocolError and ECONNREFUSED differ, how to curl-probe th
* Earnings are self-reported and unverified.