self-healing-mcp-service
Skill1 starsChanged 5 months agoMITView on GitHub
Build a robust host-side FastMCP (Model Context Protocol) service that survives restarts and reboots, reconnects cleanly to a Dockerized MCP gateway/agent, and auto-recovers its own dependency. Use when standing up an MCP server (tool server) for an agent — especially on macOS with launchd and a containerized gateway. Trigger on "build an MCP server", "MCP keeps dropping / Session not found", "MCP service won't stay up", "register MCP with the gateway", or "host.docker.internal MCP".
In this repository2 other files it gives its agents.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 4 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈4
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 26
Don't run it on nohup — it dies with the terminal and orphans.
- Distinctive: line 29
Put the absolute interpreter path in the launcher (e.g.
- Distinctive: line 36
After restarting the host service, re-probe / restart the gateway once so it re-handshakes (stateless makes future restarts transparent).
- Distinctive: line 40
If your service depends on something that can disappear (a browser over CDP, a model server, a DB), detect it and relaunch it on demand …
- Distinctive: line 52
If the agent must *display* a file, save it inside the agent's workspace (mounted + allowed by the gateway) and return a path relative to …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Self-Healing MCP Service
A checklist + patterns for a host-side FastMCP service that doesn't fall over. Learned the hard way wiring several MCP services into a Dockerized agent gateway.
1. Make the server stateless (restart resilience)
Streamable-HTTP MCP ties sessions to the process. If the server restarts, the gateway keeps a stale
session and every call fails with Session not found (-32600). Fix:
mcp.run(transport="http", host="0.0.0.0", port=PORT, stateless_http=True)⚠️ Pass stateless_http to run(), NOT the FastMCP(...) constructor — constructor kwargs are
deprecated (2.3.4+) and crash older versions.
2. Supervise it (survive crashes + reboot)
Don't run it on nohup — it dies with the terminal and orphans. Use a process supervisor with restart:
- macOS: a launchd LaunchAgent with
<key>KeepAlive</key><true/>+<key>RunAtLoad</key><true/>. - Linux: a systemd unit with
Restart=always. Put the absolute interpreter path in the launcher (e.g. the conda/venv python that has your deps), and set secrets/config via the launcher's environment block, never in code or git.
3. Wire it to a Dockerized gateway correctly
- The gateway (in a container) reaches a host service via
http://host.docker.internal:PORT/mcp/. - Register the exact path that returns HTTP 200/307 — many gateways mishandle redirects; a bare
/mcpmay 404 while/mcp/works. Verify with the gateway'smcp probe, not raw curl (curl misses the MCP handshake). - After restarting the host service, re-probe / restart the gateway once so it re-handshakes (stateless makes future restarts transparent).
4. Self-heal your own dependency
If your service depends on something that can disappear (a browser over CDP, a model server, a DB), detect it and relaunch it on demand rather than failing:
def _ensure_dep():
if reachable(): return True
subprocess.Popen([BIN, *ARGS], start_new_session=True) # detached: survives a service restart
# then poll until reachable, with a timeoutGate every tool on this so a quit dependency recovers without human action.
5. Return small results
Tools feed the agent's context. Return file paths/IDs, not big blobs (base64 images, full documents). If the agent must display a file, save it inside the agent's workspace (mounted + allowed by the gateway) and return a path relative to that workspace — absolute host paths are usually rejected as "outside allowed folders".
Gotchas cheat-sheet
Session not found→ you forgotstateless_http=True, or the gateway holds a stale session (restart it once).- Service dies overnight → it was on
nohup; move to launchd/systemd KeepAlive. - "Outside allowed folders" → return a workspace-relative path; save into the agent's workspace dir.
- Two instances double-process work → enforce a single instance or claim work atomically (
FOR UPDATE SKIP LOCKED).
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

