-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-proxy.sh
More file actions
executable file
·139 lines (124 loc) · 4.41 KB
/
Copy pathmcp-proxy.sh
File metadata and controls
executable file
·139 lines (124 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
set -euo pipefail
# mcp-proxy.sh — Start the MCP proxy server
#
# Exposes stdio-based MCP servers (defined in mcp-servers.json) over HTTP
# so that Claude Code (in WSL2) can reach them via http://localhost:MCP_PROXY_PORT
# and Docker containers can reach them via http://host.docker.internal:MCP_PROXY_PORT
#
# Usage:
# ./mcp-proxy.sh Start the proxy (foreground)
# ./mcp-proxy.sh --bg Start the proxy (background, logs to file)
# ./mcp-proxy.sh --stop Stop a running background proxy
# ./mcp-proxy.sh --status Check if the proxy is running
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG="$SCRIPT_DIR/mcp-servers.json"
MCP_PROXY_PORT="${MCP_PROXY_PORT:-8808}"
MCP_PROXY_HOST="0.0.0.0"
PIDFILE="$SCRIPT_DIR/.mcp-proxy.pid"
LOGFILE="$SCRIPT_DIR/.mcp-proxy.log"
# ── Helpers ──────────────────────────────────────────────────────────
die() { echo "Error: $*" >&2; exit 1; }
info() { echo "==> $*"; }
ensure_uv() {
if command -v uv &>/dev/null; then
return
fi
info "uv not found, installing..."
curl -LsSf https://astral.sh/uv/install.sh | sh
# uv installs to ~/.local/bin; add to PATH for this session
export PATH="$HOME/.local/bin:$PATH"
command -v uv &>/dev/null || die "uv installed but not on PATH. Check your PATH."
}
ensure_mcp_proxy() {
if command -v mcp-proxy &>/dev/null; then
return
fi
info "mcp-proxy not found, installing..."
ensure_uv
uv tool install mcp-proxy
# uv tools are in ~/.local/bin
export PATH="$HOME/.local/bin:$PATH"
command -v mcp-proxy &>/dev/null || die "mcp-proxy installed but not on PATH. Check your PATH."
}
is_running() {
[[ -f "$PIDFILE" ]] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null
}
# ── Commands ─────────────────────────────────────────────────────────
do_stop() {
if is_running; then
local pid
pid=$(cat "$PIDFILE")
info "Stopping mcp-proxy (PID $pid)..."
kill "$pid" 2>/dev/null || true
rm -f "$PIDFILE"
info "Stopped."
else
info "mcp-proxy is not running."
rm -f "$PIDFILE"
fi
}
do_status() {
if is_running; then
local pid
pid=$(cat "$PIDFILE")
info "mcp-proxy is running (PID $pid) on port $MCP_PROXY_PORT"
echo " Named servers config: $CONFIG"
echo " Endpoints:"
python3 -c "
import json, sys
with open('$CONFIG') as f:
cfg = json.load(f)
for name in cfg.get('mcpServers', {}):
print(f' http://host.docker.internal:$MCP_PROXY_PORT/servers/{name}/sse')
" 2>/dev/null || true
else
info "mcp-proxy is not running."
fi
}
do_start_fg() {
if is_running; then
die "mcp-proxy already running (PID $(cat "$PIDFILE")). Use --stop first."
fi
[[ -f "$CONFIG" ]] || die "Config file not found: $CONFIG"
ensure_mcp_proxy
info "Starting mcp-proxy on $MCP_PROXY_HOST:$MCP_PROXY_PORT ..."
info "Config: $CONFIG"
exec mcp-proxy \
--host="$MCP_PROXY_HOST" \
--port="$MCP_PROXY_PORT" \
--pass-environment \
--named-server-config "$CONFIG"
}
do_start_bg() {
if is_running; then
die "mcp-proxy already running (PID $(cat "$PIDFILE")). Use --stop first."
fi
[[ -f "$CONFIG" ]] || die "Config file not found: $CONFIG"
ensure_mcp_proxy
info "Starting mcp-proxy in background on $MCP_PROXY_HOST:$MCP_PROXY_PORT ..."
info "Config: $CONFIG"
info "Log: $LOGFILE"
nohup mcp-proxy \
--host="$MCP_PROXY_HOST" \
--port="$MCP_PROXY_PORT" \
--pass-environment \
--named-server-config "$CONFIG" \
> "$LOGFILE" 2>&1 &
echo $! > "$PIDFILE"
sleep 1
if is_running; then
info "mcp-proxy started (PID $(cat "$PIDFILE"))"
do_status
else
rm -f "$PIDFILE"
die "mcp-proxy failed to start. Check $LOGFILE"
fi
}
# ── Main ─────────────────────────────────────────────────────────────
case "${1:-}" in
--stop) do_stop ;;
--status) do_status ;;
--bg) do_start_bg ;;
*) do_start_fg ;;
esac