forked from IBM/mcp-context-forge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·195 lines (175 loc) · 4.65 KB
/
run.sh
File metadata and controls
executable file
·195 lines (175 loc) · 4.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
#!/bin/bash
# Author: Mihai Criveti
# Exit on any error
set -e
# Default settings
HOST=${HOST:-0.0.0.0}
PORT=${PORT:-4444}
ENV_FILE=${ENV_FILE:-.env}
LOG_LEVEL=${LOG_LEVEL:-info}
LOG_FORMAT=${LOG_FORMAT:-json}
RELOAD=${RELOAD:-false}
WORKERS=${WORKERS:-1}
ACCESS_LOG=${ACCESS_LOG:-true}
APP_MODULE=${APP_MODULE:-mcpgateway.main:app}
# Help message
show_help() {
echo "MCP Gateway Runner"
echo "==================="
echo ""
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -e, --env FILE Use specific env file (default: .env)"
echo " -H, --host HOST Bind to HOST address (default: 0.0.0.0)"
echo " -p, --port PORT Use PORT (default: 4444)"
echo " -l, --log LEVEL Set log level: debug, info, warning, error, critical (default: info)"
echo " -f, --format FORMAT Set log format: json or text (default: json)"
echo " -r, --reload Enable auto-reload for development"
echo " -w, --workers N Number of worker processes (default: 1)"
echo " -n, --no-access-log Disable access log"
echo " -m, --module MODULE Use specified app module (default: mcpgateway.main:app)"
echo ""
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-e|--env)
ENV_FILE="$2"
shift 2
;;
-H|--host)
HOST="$2"
shift 2
;;
-p|--port)
PORT="$2"
shift 2
;;
-l|--log)
LOG_LEVEL="$2"
shift 2
;;
-r|--reload)
RELOAD=true
shift
;;
-w|--workers)
WORKERS="$2"
shift 2
;;
-n|--no-access-log)
ACCESS_LOG=false
shift
;;
-m|--module)
APP_MODULE="$2"
shift 2
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
done
# Validate log level
if ! [[ "$LOG_LEVEL" =~ ^(debug|info|warning|error|critical)$ ]]; then
echo "Invalid log level: $LOG_LEVEL"
echo "Must be one of: debug, info, warning, error, critical"
exit 1
fi
# Validate log format
if ! [[ "$LOG_FORMAT" =~ ^(json|text)$ ]]; then
echo "Invalid log format: $LOG_FORMAT"
echo "Must be one of: json, text"
exit 1
fi
# Create default .env if it doesn't exist
if [ ! -f "$ENV_FILE" ]; then
echo "Creating default $ENV_FILE..."
cat > "$ENV_FILE" << EOL
# Basic Settings
APP_NAME=MCP_Gateway
HOST=${HOST}
PORT=${PORT}
DATABASE_URL=sqlite:///./mcp.db
LOG_LEVEL=${LOG_LEVEL^^}
LOG_FORMAT=${LOG_FORMAT}
# Authentication
BASIC_AUTH_USER=admin
BASIC_AUTH_PASSWORD=changeme
AUTH_REQUIRED=true
AUTH_ENCRYPTION_SECRET=my-test-salt
# Security
SKIP_SSL_VERIFY=false
ALLOWED_ORIGINS='["http://localhost", "http://localhost:${PORT}"]'
CORS_ENABLED=true
# Transport
TRANSPORT_TYPE=all
WEBSOCKET_PING_INTERVAL=30
SSE_RETRY_TIMEOUT=5000
# Federation
FEDERATION_ENABLED=true
FEDERATION_DISCOVERY=false
FEDERATION_TIMEOUT=30
FEDERATION_SYNC_INTERVAL=300
# Resources
RESOURCE_CACHE_SIZE=1000
RESOURCE_CACHE_TTL=3600
MAX_RESOURCE_SIZE=10485760
# Tools
TOOL_TIMEOUT=60
MAX_TOOL_RETRIES=3
TOOL_RATE_LIMIT=100
TOOL_CONCURRENT_LIMIT=10
# Database
DB_POOL_SIZE=200
DB_MAX_OVERFLOW=10
DB_POOL_TIMEOUT=30
DB_POOL_RECYCLE=3600
# Development
DEV_MODE=$([ "$RELOAD" = "true" ] && echo "true" || echo "false")
EOL
fi
# Load environment variables
if [ -f "$ENV_FILE" ]; then
echo "Loading environment from $ENV_FILE..."
set -a
source "$ENV_FILE"
set +a
fi
# Create database directory if needed
if [[ "$DATABASE_URL" == sqlite:* ]]; then
DB_PATH=$(echo "$DATABASE_URL" | sed -e 's/^sqlite:\/\///')
DB_DIR=$(dirname "$DB_PATH")
echo "Ensuring database directory exists: $DB_DIR"
mkdir -p "$DB_DIR"
fi
# Build log configuration
if [ "$ACCESS_LOG" = "false" ]; then
LOG_CONFIG="$LOG_CONFIG --no-access-log"
fi
# Check if running in development mode
if [ "$RELOAD" = "true" ]; then
echo "Starting MCP Gateway in development mode..."
echo "Running: uvicorn $APP_MODULE --host $HOST --port $PORT --reload $LOG_CONFIG"
exec uvicorn $APP_MODULE \
--host "$HOST" \
--port "$PORT" \
--reload \
$LOG_CONFIG
else
echo "Starting MCP Gateway in production mode..."
echo "Running: uvicorn $APP_MODULE --host $HOST --port $PORT --workers $WORKERS $LOG_CONFIG"
exec uvicorn $APP_MODULE \
--host "$HOST" \
--port "$PORT" \
--workers "$WORKERS" \
$LOG_CONFIG
fi