|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Keep MCP Relay Demo |
| 4 | +# |
| 5 | +# Runs the keep-mcp-relay in front of a sqlite MCP server and demonstrates |
| 6 | +# policy enforcement: read-only mode and password redaction. |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# ./examples/mcp-relay-demo/demo.sh |
| 10 | +# |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +# ── Colors ─────────────────────────────────────────────────────── |
| 14 | +BOLD='\033[1m' |
| 15 | +DIM='\033[2m' |
| 16 | +RED='\033[0;31m' |
| 17 | +GREEN='\033[0;32m' |
| 18 | +YELLOW='\033[0;33m' |
| 19 | +BLUE='\033[0;34m' |
| 20 | +CYAN='\033[0;36m' |
| 21 | +RESET='\033[0m' |
| 22 | + |
| 23 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 24 | +DEMO_DIR=$(mktemp -d) |
| 25 | +RELAY_PORT=19090 |
| 26 | +RELAY_PID="" |
| 27 | + |
| 28 | +cleanup() { |
| 29 | + echo "" |
| 30 | + echo -e "${DIM}Cleaning up...${RESET}" |
| 31 | + [ -n "$RELAY_PID" ] && kill "$RELAY_PID" 2>/dev/null || true |
| 32 | + rm -rf "$DEMO_DIR" |
| 33 | +} |
| 34 | +trap cleanup EXIT |
| 35 | + |
| 36 | +echo "" |
| 37 | +echo -e "${BOLD}Keep MCP Relay Demo${RESET}" |
| 38 | +echo -e "${DIM}Policy enforcement for MCP tool calls${RESET}" |
| 39 | +echo "" |
| 40 | + |
| 41 | +# ── Prerequisites ──────────────────────────────────────────────── |
| 42 | +if ! command -v sqlite3 &>/dev/null; then |
| 43 | + echo -e "${RED}Error:${RESET} sqlite3 is required but not found." |
| 44 | + echo " Install it with: brew install sqlite3 (macOS) or apt install sqlite3 (Linux)" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +MCP_RUNNER="" |
| 49 | +if command -v npx &>/dev/null; then |
| 50 | + MCP_RUNNER="npx" |
| 51 | +elif command -v bunx &>/dev/null; then |
| 52 | + MCP_RUNNER="bunx" |
| 53 | +else |
| 54 | + echo -e "${RED}Error:${RESET} npx or bunx is required but neither was found." |
| 55 | + echo " Install Node.js (https://nodejs.org) or Bun (https://bun.sh)" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +echo -e "${DIM}Using ${MCP_RUNNER} to run MCP sqlite server${RESET}" |
| 60 | + |
| 61 | +# ── Build ──────────────────────────────────────────────────────── |
| 62 | +echo -e "${DIM}Building relay...${RESET}" |
| 63 | +go build -o "$DEMO_DIR/keep-mcp-relay" ./cmd/keep-mcp-relay |
| 64 | + |
| 65 | +# ── Seed database ─────────────────────────────────────────────── |
| 66 | +DB_PATH="$DEMO_DIR/demo.db" |
| 67 | +sqlite3 "$DB_PATH" < "$SCRIPT_DIR/seed.sql" |
| 68 | +echo -e "${DIM}Seeded database with 12 users${RESET}" |
| 69 | + |
| 70 | +# ── Generate relay config ─────────────────────────────────────── |
| 71 | +sed \ |
| 72 | + -e "s|RULES_DIR|$SCRIPT_DIR/rules|" \ |
| 73 | + -e "s|LOG_OUTPUT|$DEMO_DIR/audit.jsonl|" \ |
| 74 | + -e "s|COMMAND|$MCP_RUNNER|" \ |
| 75 | + -e "s|ARGS|[\"-y\", \"@modelcontextprotocol/server-sqlite\", \"$DB_PATH\"]|" \ |
| 76 | + "$SCRIPT_DIR/relay.yaml" > "$DEMO_DIR/relay.yaml" |
| 77 | + |
| 78 | +# ── Start relay ───────────────────────────────────────────────── |
| 79 | +export KEEP_DEBUG="$DEMO_DIR/debug.log" |
| 80 | + |
| 81 | +if [ -n "${KEEP_VERBOSE:-}" ]; then |
| 82 | + "$DEMO_DIR/keep-mcp-relay" --config "$DEMO_DIR/relay.yaml" >/dev/null & |
| 83 | +else |
| 84 | + "$DEMO_DIR/keep-mcp-relay" --config "$DEMO_DIR/relay.yaml" >/dev/null 2>&1 & |
| 85 | +fi |
| 86 | +RELAY_PID=$! |
| 87 | +sleep 2 |
| 88 | + |
| 89 | +if ! kill -0 "$RELAY_PID" 2>/dev/null; then |
| 90 | + echo -e "${RED}Error:${RESET} Relay failed to start. Check debug log: $KEEP_DEBUG" |
| 91 | + exit 1 |
| 92 | +fi |
| 93 | + |
| 94 | +echo -e "${GREEN}Relay running${RESET} on :${RELAY_PORT} ${DIM}(PID $RELAY_PID)${RESET}" |
| 95 | +echo "" |
| 96 | + |
| 97 | +# ── Instructions ───────────────────────────────────────────────── |
| 98 | +echo -e "${BOLD}Connect Claude to the relay:${RESET}" |
| 99 | +echo "" |
| 100 | +echo -e " ${CYAN}claude mcp add keep-demo --transport http http://localhost:${RELAY_PORT}${RESET}" |
| 101 | +echo "" |
| 102 | +echo -e "${BOLD}Try these prompts:${RESET}" |
| 103 | +echo "" |
| 104 | +echo -e " ${BLUE}1.${RESET} \"List all users in the database\"" |
| 105 | +echo -e " ${DIM}→ passwords will be redacted from the response${RESET}" |
| 106 | +echo "" |
| 107 | +echo -e " ${BLUE}2.${RESET} \"Add a new user named Test User\"" |
| 108 | +echo -e " ${DIM}→ write operation will be denied${RESET}" |
| 109 | +echo "" |
| 110 | +echo -e "${BOLD}Audit log:${RESET} ${DIM}$DEMO_DIR/audit.jsonl${RESET}" |
| 111 | +echo -e "${BOLD}Debug log:${RESET} ${DIM}$KEEP_DEBUG${RESET}" |
| 112 | +echo "" |
| 113 | +echo -e "${DIM}Press Ctrl+C to stop the relay${RESET}" |
| 114 | + |
| 115 | +# ── Wait ───────────────────────────────────────────────────────── |
| 116 | +wait "$RELAY_PID" |