| 1 | # Netdata MCP Bridge - Python Implementation |
| 2 | |
| 3 | This Python bridge converts MCP stdio communication to Netdata's MCP over WebSocket. |
| 4 | |
| 5 | ## Requirements |
| 6 | |
| 7 | - Python 3.7+ |
| 8 | - websockets library |
| 9 | |
| 10 | ## Installation |
| 11 | |
| 12 | The easiest way to set up the Python bridge is to use the included build script, which creates a virtual environment (recommended for most Linux distributions where pip is restricted to virtual environments): |
| 13 | |
| 14 | ```bash |
| 15 | # Make the build script executable (if needed) |
| 16 | chmod +x build.sh |
| 17 | |
| 18 | # Run the build script |
| 19 | ./build.sh |
| 20 | ``` |
| 21 | |
| 22 | The script will: |
| 23 | 1. Check if Python 3 is installed |
| 24 | 2. Create a virtual environment in the `venv` directory if it doesn't exist |
| 25 | 3. Install the required websockets dependency in the virtual environment |
| 26 | 4. Generate a requirements.txt file for reproducibility |
| 27 | |
| 28 | ### Running with Virtual Environment |
| 29 | |
| 30 | After running the build script: |
| 31 | |
| 32 | ```bash |
| 33 | # Activate the virtual environment |
| 34 | source venv/bin/activate |
| 35 | |
| 36 | # Run the bridge |
| 37 | python nd-mcp.py ws://localhost:19999/mcp |
| 38 | |
| 39 | # When finished, deactivate the virtual environment |
| 40 | deactivate |
| 41 | ``` |
| 42 | |
| 43 | Or run it directly with the virtual environment's Python: |
| 44 | |
| 45 | ```bash |
| 46 | ./venv/bin/python nd-mcp.py ws://localhost:19999/mcp |
| 47 | ``` |
| 48 | |
| 49 | ### Manual Installation (without virtual environment) |
| 50 | |
| 51 | If your system allows global pip installations: |
| 52 | |
| 53 | ```bash |
| 54 | pip install websockets |
| 55 | ``` |
| 56 | |
| 57 | ## Usage |
| 58 | |
| 59 | The script can be run directly as an executable (the shebang line will use the system's Python): |
| 60 | |
| 61 | ```bash |
| 62 | ./nd-mcp.py ws://<ip>:19999/mcp |
| 63 | ``` |
| 64 | |
| 65 | Or explicitly with Python: |
| 66 | |
| 67 | ```bash |
| 68 | python nd-mcp.py ws://<ip>:19999/mcp |
| 69 | ``` |
| 70 | |
| 71 | When using a virtual environment, run it with the environment's Python: |
| 72 | |
| 73 | ```bash |
| 74 | ./venv/bin/python nd-mcp.py ws://<ip>:19999/mcp |
| 75 | ``` |
| 76 | |
| 77 | Where `<ip>` is either `localhost` or the IP address where a Netdata instance is listening. |
| 78 | |
| 79 | ## Example with Claude Desktop |
| 80 | |
| 81 | To use this bridge with Claude Desktop: |
| 82 | |
| 83 | 1. In Claude Desktop settings, configure the Custom Command option: |
| 84 | |
| 85 | ```bash |
| 86 | python /path/to/stdio-python/nd-mcp.py ws://localhost:19999/mcp |
| 87 | ``` |
| 88 | |
| 89 | 2. If your Netdata instance is running on a different machine, replace `localhost` with the appropriate IP address. |
| 90 | |
| 91 | ## How It Works |
| 92 | |
| 93 | The bridge: |
| 94 | 1. Establishes a WebSocket connection to the specified Netdata MCP endpoint |
| 95 | 2. Reads from standard input and sends to the WebSocket |
| 96 | 3. Receives messages from the WebSocket and writes to standard output |
| 97 | 4. Handles both directions simultaneously |
| 98 | 5. Automatically reconnects if the connection is lost, with exponential backoff |
| 99 | |
| 100 | ## Connection Reliability |
| 101 | |
| 102 | This bridge implements robust connection handling: |
| 103 | |
| 104 | - **Automatic Reconnection**: If the WebSocket connection is lost, the bridge will automatically attempt to reconnect |
| 105 | - **Exponential Backoff**: Reconnection attempts use exponential backoff with jitter to avoid overwhelming the server |
| 106 | - **Message Queuing**: Messages sent while disconnected are queued and delivered once reconnected |
| 107 | - **Connection Status Logging**: The bridge logs connection status to stderr for monitoring |
| 108 | |
| 109 | The reconnection algorithm starts with a 1-second delay and doubles the wait time with each attempt, up to a maximum of 60 seconds. Random jitter is added to prevent connection storms. |
| 110 | |
| 111 | ## Protocol Compatibility |
| 112 | |
| 113 | - Netdata MCP implements the JSON-RPC 2.0 protocol |
| 114 | - Messages that don't conform to the JSON-RPC 2.0 format are silently ignored |
| 115 | - The bridge passes messages directly without any modification |