Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions actions/setup/sh/check_mcp_servers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@

set -e

# Timing helper functions
print_timing() {
local start_time=$1
local label=$2
local end_time=$(date +%s%3N)
local duration=$((end_time - start_time))
echo "⏱️ TIMING: $label took ${duration}ms"
}

# Usage: check_mcp_servers.sh GATEWAY_CONFIG_PATH GATEWAY_URL GATEWAY_API_KEY
#
# Arguments:
Expand All @@ -31,10 +40,14 @@ GATEWAY_CONFIG_PATH="$1"
GATEWAY_URL="$2"
GATEWAY_API_KEY="$3"

# Start overall timing
SCRIPT_START_TIME=$(date +%s%3N)

echo "Checking MCP servers..."
echo ""

# Validate configuration file exists
CONFIG_VALIDATION_START=$(date +%s%3N)
if [ ! -f "$GATEWAY_CONFIG_PATH" ]; then
echo "ERROR: Gateway configuration file not found: $GATEWAY_CONFIG_PATH" >&2
exit 1
Expand All @@ -60,6 +73,9 @@ if [ -z "$SERVER_NAMES" ]; then
exit 0
fi

print_timing $CONFIG_VALIDATION_START "Configuration validation"
echo ""

# Track overall results
SERVERS_CHECKED=0
SERVERS_SUCCEEDED=0
Expand All @@ -73,6 +89,7 @@ MAX_RETRIES=3
# Iterate through each server
while IFS= read -r SERVER_NAME; do
SERVERS_CHECKED=$((SERVERS_CHECKED + 1))
SERVER_START_TIME=$(date +%s%3N)

# Extract server configuration
SERVER_CONFIG=$(echo "$MCP_SERVERS" | jq -r ".\"$SERVER_NAME\"" 2>/dev/null)
Expand Down Expand Up @@ -173,9 +190,13 @@ while IFS= read -r SERVER_NAME; do
SERVERS_FAILED=$((SERVERS_FAILED + 1))
fi

print_timing $SERVER_START_TIME "Server check for $SERVER_NAME"
echo ""

done <<< "$SERVER_NAMES"

# Print summary
print_timing $SCRIPT_START_TIME "Overall MCP server checks"
echo ""
if [ $SERVERS_FAILED -gt 0 ]; then
echo "ERROR: $SERVERS_FAILED of $SERVERS_CHECKED server(s) failed connectivity check"
Expand Down
31 changes: 31 additions & 0 deletions actions/setup/sh/start_mcp_gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

set -e

# Timing helper functions
print_timing() {
local start_time=$1
local label=$2
local end_time=$(date +%s%3N)
local duration=$((end_time - start_time))
echo "⏱️ TIMING: $label took ${duration}ms"
}

# Required environment variables:
# - MCP_GATEWAY_DOCKER_COMMAND: Container image to run (required)
# - MCP_GATEWAY_API_KEY: API key for gateway authentication (required for converter scripts)
Expand Down Expand Up @@ -47,9 +56,15 @@ if ! echo "$MCP_GATEWAY_DOCKER_COMMAND" | grep -qE -- '--network'; then
exit 1
fi

# Start overall timing
SCRIPT_START_TIME=$(date +%s%3N)

# Read MCP configuration from stdin
echo "Reading MCP configuration from stdin..."
CONFIG_READ_START=$(date +%s%3N)
MCP_CONFIG=$(cat)
print_timing $CONFIG_READ_START "Configuration read from stdin"
echo ""

# Log the configuration for debugging
echo "-------START MCP CONFIG-----------"
Expand All @@ -58,6 +73,7 @@ echo "-------END MCP CONFIG-----------"
echo ""

# Validate configuration is valid JSON
CONFIG_VALIDATION_START=$(date +%s%3N)
if ! echo "$MCP_CONFIG" | jq empty 2>/tmp/gh-aw/mcp-config/jq-error.log; then
echo "ERROR: Configuration is not valid JSON"
echo ""
Expand Down Expand Up @@ -98,6 +114,7 @@ if ! echo "$MCP_CONFIG" | jq -e '.gateway.apiKey' >/dev/null 2>&1; then
fi

echo "Configuration validated successfully"
print_timing $CONFIG_VALIDATION_START "Configuration validation"
echo ""

# Set MCP_GATEWAY_LOG_DIR environment variable for use by the gateway
Expand All @@ -107,13 +124,15 @@ export MCP_GATEWAY_LOG_DIR="/tmp/gh-aw/mcp-logs/"
echo "Starting gateway with container: $MCP_GATEWAY_DOCKER_COMMAND"
echo "Full docker command: $MCP_GATEWAY_DOCKER_COMMAND"
echo ""
GATEWAY_START_TIME=$(date +%s%3N)
# Note: MCP_GATEWAY_DOCKER_COMMAND is the full docker command with all flags, mounts, and image
# Pass MCP_GATEWAY_LOG_DIR to the container via -e flag
echo "$MCP_CONFIG" | MCP_GATEWAY_LOG_DIR="$MCP_GATEWAY_LOG_DIR" $MCP_GATEWAY_DOCKER_COMMAND \
> /tmp/gh-aw/mcp-config/gateway-output.json 2> /tmp/gh-aw/mcp-logs/stderr.log &

GATEWAY_PID=$!
echo "Gateway started with PID: $GATEWAY_PID"
print_timing $GATEWAY_START_TIME "Gateway container launch"
echo "Verifying gateway process is running..."
if ps -p $GATEWAY_PID > /dev/null 2>&1; then
echo "Gateway process confirmed running (PID: $GATEWAY_PID)"
Expand Down Expand Up @@ -153,6 +172,7 @@ echo ""
# Note: Gateway may take 40-50 seconds when starting multiple MCP servers
# (e.g., serena alone takes ~22 seconds to start)
echo "Waiting for gateway to be ready..."
HEALTH_CHECK_START=$(date +%s%3N)
# Use localhost for health check since:
# 1. This script runs on the host (not in a container)
# 2. The gateway uses --network host, so it's accessible on localhost
Expand Down Expand Up @@ -185,6 +205,7 @@ fi

if [ "$HTTP_CODE" = "200" ] && [ -n "$HEALTH_RESPONSE" ]; then
echo "Gateway is ready!"
print_timing $HEALTH_CHECK_START "Health check wait"
else
echo ""
echo "ERROR: Gateway failed to become ready"
Expand Down Expand Up @@ -218,6 +239,7 @@ echo ""

# Wait for gateway output (rewritten configuration)
echo "Reading gateway output configuration..."
OUTPUT_WAIT_START=$(date +%s%3N)
WAIT_ATTEMPTS=10
WAIT_ATTEMPT=0
while [ $WAIT_ATTEMPT -lt $WAIT_ATTEMPTS ]; do
Expand All @@ -230,6 +252,8 @@ while [ $WAIT_ATTEMPT -lt $WAIT_ATTEMPTS ]; do
sleep 1
fi
done
print_timing $OUTPUT_WAIT_START "Gateway output wait"
echo ""

# Verify output was written
if [ ! -s /tmp/gh-aw/mcp-config/gateway-output.json ]; then
Expand Down Expand Up @@ -260,6 +284,7 @@ fi

# Convert gateway output to agent-specific format
echo "Converting gateway configuration to agent format..."
CONFIG_CONVERT_START=$(date +%s%3N)
export MCP_GATEWAY_OUTPUT=/tmp/gh-aw/mcp-config/gateway-output.json

# Validate MCP_GATEWAY_API_KEY is set (required by converter scripts)
Expand Down Expand Up @@ -307,10 +332,12 @@ case "$ENGINE_TYPE" in
cat /home/runner/.copilot/mcp-config.json
;;
esac
print_timing $CONFIG_CONVERT_START "Configuration conversion"
echo ""

# Check MCP server functionality
echo "Checking MCP server functionality..."
MCP_CHECK_START=$(date +%s%3N)
if [ -f /opt/gh-aw/actions/check_mcp_servers.sh ]; then
echo "Running MCP server checks..."
# Store check diagnostic logs in /tmp/gh-aw/mcp-logs/start-gateway.log for artifact upload
Expand All @@ -327,6 +354,7 @@ if [ -f /opt/gh-aw/actions/check_mcp_servers.sh ]; then
exit 1
fi
set +o pipefail
print_timing $MCP_CHECK_START "MCP server connectivity checks"
else
echo "WARNING: MCP server check script not found at /opt/gh-aw/actions/check_mcp_servers.sh"
echo "Skipping MCP server functionality checks"
Expand All @@ -348,6 +376,9 @@ echo " - From host: http://localhost:${MCP_GATEWAY_PORT}"
echo " - From containers: http://${MCP_GATEWAY_DOMAIN}:${MCP_GATEWAY_PORT}"
echo "Gateway PID: $GATEWAY_PID"

print_timing $SCRIPT_START_TIME "Overall gateway startup"
echo ""

# Output PID as GitHub Actions step output for use in cleanup
echo "gateway-pid=$GATEWAY_PID" >> $GITHUB_OUTPUT
# Output port and API key for use in stop script (per MCP Gateway Specification v1.1.0)
Expand Down
21 changes: 21 additions & 0 deletions actions/setup/sh/verify_mcp_gateway_health.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

set -e

# Timing helper functions
print_timing() {
local start_time=$1
local label=$2
local end_time=$(date +%s%3N)
local duration=$((end_time - start_time))
echo "⏱️ TIMING: $label took ${duration}ms"
}

# Usage: verify_mcp_gateway_health.sh GATEWAY_URL MCP_CONFIG_PATH LOGS_FOLDER
#
# Arguments:
Expand All @@ -24,6 +33,9 @@ gateway_url="$1"
mcp_config_path="$2"
logs_folder="$3"

# Start overall timing
SCRIPT_START_TIME=$(date +%s%3N)

echo 'Waiting for MCP Gateway to be ready...'
echo ''
echo '=== File Locations ==='
Expand All @@ -47,6 +59,7 @@ echo ''

# Wait for gateway to be ready FIRST before checking config
echo '=== Testing Gateway Health ==='
HEALTH_CHECK_START=$(date +%s%3N)

# Capture both response body and HTTP code in a single curl call
# Use curl retry: 120 attempts with 1 second delay = 120s total
Expand All @@ -66,6 +79,7 @@ fi

if [ "$http_code" = "200" ]; then
echo "✓ MCP Gateway is ready!"
print_timing $HEALTH_CHECK_START "Health endpoint polling"
else
echo ''
echo "✗ Error: MCP Gateway failed to start"
Expand Down Expand Up @@ -95,6 +109,7 @@ echo ''

# Now that gateway is ready, check the config file
echo '=== MCP Configuration File ==='
CONFIG_CHECK_START=$(date +%s%3N)
if [ -f "$mcp_config_path" ]; then
echo "✓ Config file exists at: $mcp_config_path"
echo "File size: $(stat -f%z "$mcp_config_path" 2>/dev/null || stat -c%s "$mcp_config_path" 2>/dev/null || echo 'unknown') bytes"
Expand Down Expand Up @@ -123,6 +138,7 @@ if ! grep -q '"safeoutputs"' "$mcp_config_path"; then
exit 1
fi
echo '✓ safeoutputs server found in configuration'
print_timing $CONFIG_CHECK_START "Configuration file verification"
echo ''

# Fetch and display gateway servers list
Expand All @@ -133,6 +149,7 @@ echo ''

# Test MCP server connectivity through gateway
echo '=== Testing MCP Server Connectivity ==='
CONNECTIVITY_TEST_START=$(date +%s%3N)

# Extract first external MCP server name from config (excluding safeinputs/safeoutputs)
mcp_server=$(jq -r '.mcpServers | to_entries[] | select(.key != "safeinputs" and .key != "safeoutputs") | .key' "$mcp_config_path" | head -n 1)
Expand Down Expand Up @@ -176,8 +193,12 @@ if [ -n "$mcp_server" ]; then
echo "Gateway logs (last 20 lines):"
tail -20 "${logs_folder}/gateway.log" 2>/dev/null || echo "Could not read gateway logs"
fi
print_timing $CONNECTIVITY_TEST_START "MCP server connectivity test"
else
echo "No external MCP servers configured for testing"
fi

print_timing $SCRIPT_START_TIME "Overall gateway health verification"
echo ""

exit 0
Loading