Repository navigation
Expand file tree
/
Copy pathstart_cli_http.sh
More file actions
executable file
·212 lines (183 loc) · 6.65 KB
/
Copy pathstart_cli_http.sh
File metadata and controls
executable file
·212 lines (183 loc) · 6.65 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
#--------------------------------------------------------------------------
#
# pgEdge MCP CLI Client Startup Script (HTTP Mode)
#
# Copyright (c) 2025 - 2026, pgEdge, Inc.
# This software is released under The PostgreSQL License
#
# This script starts the MCP server in HTTP mode with authentication and
# LLM proxy enabled, then starts the CLI chat client to connect to it via
# HTTP. When the CLI exits, the MCP server is automatically stopped.
#
# Usage:
# ./start_cli_http.sh
# CONFIG_FILE=custom.yaml ./start_cli_http.sh
#
#--------------------------------------------------------------------------
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Get script directory (should be project root)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$SCRIPT_DIR"
BIN_DIR="$PROJECT_DIR/bin"
# Source directories
CMD_CLI_DIR="$PROJECT_DIR/cmd/pgedge-pg-mcp-cli"
CMD_SERVER_DIR="$PROJECT_DIR/cmd/pgedge-pg-mcp-server"
INTERNAL_DIR="$PROJECT_DIR/internal"
INTERNAL_CHAT_DIR="$PROJECT_DIR/internal/chat"
# Binaries
CLI_BIN="$BIN_DIR/pgedge-nla-cli"
SERVER_BIN="$BIN_DIR/pgedge-postgres-mcp"
# Configuration files
SERVER_CONFIG="$BIN_DIR/pgedge-postgres-mcp-http.yaml"
CLI_CONFIG="${CONFIG_FILE:-$BIN_DIR/pgedge-nla-cli-http.yaml}"
# Log files
SERVER_LOG="/tmp/pgedge-postgres-mcp.log"
# PID file for cleanup
MCP_SERVER_PID=""
# Cleanup function
cleanup() {
echo ""
echo -e "${YELLOW}Shutting down services...${NC}"
if [ ! -z "$MCP_SERVER_PID" ]; then
echo "Stopping MCP server (PID: $MCP_SERVER_PID)..."
kill $MCP_SERVER_PID 2>/dev/null || true
# Wait a moment for graceful shutdown
sleep 1
# Force kill if still running
if kill -0 $MCP_SERVER_PID 2>/dev/null; then
kill -9 $MCP_SERVER_PID 2>/dev/null || true
fi
fi
echo -e "${GREEN}Cleanup complete${NC}"
}
# Set up trap for cleanup
trap cleanup EXIT INT TERM
echo -e "${BLUE}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ pgEdge MCP CLI Client Startup (HTTP Mode) ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""
# Build or rebuild server binary if needed
if [ ! -f "$SERVER_BIN" ]; then
echo -e "${BLUE}Building MCP server binary...${NC}"
cd "$PROJECT_DIR"
make build-server
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to build MCP server${NC}"
exit 1
fi
else
echo -e "${BLUE}Checking if server binary needs rebuilding...${NC}"
# Check if any Go source files are newer than the binary
if [ -n "$(find "$CMD_SERVER_DIR" "$INTERNAL_DIR" -name "*.go" -newer "$SERVER_BIN" 2>/dev/null)" ]; then
echo -e "${BLUE}Source files changed, rebuilding MCP server...${NC}"
cd "$PROJECT_DIR"
make build-server
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to build MCP server${NC}"
exit 1
fi
else
echo -e "${GREEN}Server binary is up to date${NC}"
fi
fi
# Build or rebuild CLI binary if needed
if [ ! -f "$CLI_BIN" ]; then
echo -e "${BLUE}Building CLI client binary...${NC}"
cd "$PROJECT_DIR"
make build-client
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to build CLI client${NC}"
exit 1
fi
else
echo -e "${BLUE}Checking if CLI binary needs rebuilding...${NC}"
# Check if any Go source files are newer than the binary
if [ -n "$(find "$CMD_CLI_DIR" "$INTERNAL_CHAT_DIR" -name "*.go" -newer "$CLI_BIN" 2>/dev/null)" ]; then
echo -e "${BLUE}Source files changed, rebuilding CLI client...${NC}"
cd "$PROJECT_DIR"
make build-client
if [ $? -ne 0 ]; then
echo -e "${RED}Error: Failed to build CLI client${NC}"
exit 1
fi
else
echo -e "${GREEN}CLI binary is up to date${NC}"
fi
fi
# Check if server config file exists
if [ ! -f "$SERVER_CONFIG" ]; then
echo -e "${RED}Error: Server config file not found at $SERVER_CONFIG${NC}"
exit 1
fi
# Check if CLI config file exists
if [ ! -f "$CLI_CONFIG" ]; then
echo -e "${RED}Error: CLI config file not found at $CLI_CONFIG${NC}"
exit 1
fi
# Display configuration
echo ""
echo -e "${BLUE}Configuration:${NC}"
echo " Server config: $SERVER_CONFIG"
echo " CLI config: $CLI_CONFIG"
echo ""
# Start MCP server
echo -e "${GREEN}[1/2] Starting MCP Server (HTTP mode with auth and LLM proxy)...${NC}"
cd "$BIN_DIR"
"$SERVER_BIN" --config "$SERVER_CONFIG" > "$SERVER_LOG" 2>&1 &
MCP_SERVER_PID=$!
cd "$PROJECT_DIR"
echo " PID: $MCP_SERVER_PID"
echo " Config: $SERVER_CONFIG"
echo " Log: $SERVER_LOG"
# Wait a moment for process to stabilize
sleep 1
# Check if MCP server process is still running (catch immediate failures like port conflicts)
if ! kill -0 $MCP_SERVER_PID 2>/dev/null; then
echo -e "${RED}Error: MCP Server process exited immediately${NC}"
echo "This usually means the port is already in use or there's a configuration error."
echo "Check the log file: $SERVER_LOG"
tail -20 "$SERVER_LOG"
exit 1
fi
# Wait for MCP server to be ready
echo -e "${GREEN}Waiting for MCP Server to be ready...${NC}"
MAX_RETRIES=30
RETRY_COUNT=0
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
if curl -s http://localhost:8080/health > /dev/null 2>&1; then
echo " MCP Server is ready!"
break
fi
# Check if process is still running
if ! kill -0 $MCP_SERVER_PID 2>/dev/null; then
echo -e "${RED}Error: MCP Server process died during startup${NC}"
echo "Check the log file: $SERVER_LOG"
tail -20 "$SERVER_LOG"
exit 1
fi
RETRY_COUNT=$((RETRY_COUNT + 1))
sleep 1
done
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
echo -e "${RED}Error: MCP Server failed to start within 30 seconds${NC}"
echo "Check the log file: $SERVER_LOG"
tail -20 "$SERVER_LOG"
exit 1
fi
echo ""
echo -e "${GREEN}[2/2] Starting CLI Chat Client (HTTP mode)...${NC}"
echo " Connecting to: http://localhost:8080"
echo " Config: $CLI_CONFIG"
echo ""
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
echo ""
# Run CLI in foreground - when it exits, cleanup will run
cd "$PROJECT_DIR"
"$CLI_BIN" --config "$CLI_CONFIG"