mcp_cli.shโข11.3 kB
#!/bin/bash
# Fast MCP CLI Tool
# This script provides a command-line interface for interacting with the Fast MCP Server
# and Google Gemini integration.
# Configuration
SERVER_URL="http://localhost:5001"
SERVER_PID_FILE="mcp_server.pid"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Helper functions
print_usage() {
echo -e "${BLUE}Fast MCP CLI Tool${NC}"
echo ""
echo "Usage: $0 <command> [arguments]"
echo ""
echo "Commands:"
echo " start Start the MCP server"
echo " stop Stop the MCP server"
echo " status Check server status"
echo " list List all available MCP tools"
echo " run <tool> [args...] Execute an MCP tool"
echo " gemini \"<prompt>\" Send a prompt to Gemini via MCP server"
echo " logs Show recent MCP operation logs"
echo " health Check server health"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 start"
echo " $0 list"
echo " $0 run list_tasks"
echo " $0 run create_task \"New Task\" \"Task Description\" \"high\""
echo " $0 gemini \"What are the benefits of using FastAPI?\""
echo " $0 logs"
}
print_error() {
echo -e "${RED}Error: $1${NC}" >&2
}
print_success() {
echo -e "${GREEN}Success: $1${NC}"
}
print_info() {
echo -e "${BLUE}Info: $1${NC}"
}
print_warning() {
echo -e "${YELLOW}Warning: $1${NC}"
}
# Check if server is running
check_server_running() {
if [ -f "$SERVER_PID_FILE" ]; then
local pid=$(cat "$SERVER_PID_FILE")
if ps -p "$pid" > /dev/null 2>&1; then
return 0
else
rm -f "$SERVER_PID_FILE"
return 1
fi
else
return 1
fi
}
# Start the MCP server
start_server() {
if check_server_running; then
print_warning "MCP server is already running (PID: $(cat $SERVER_PID_FILE))"
return 0
fi
print_info "Starting MCP server..."
# Check if virtual environment exists
if [ ! -d "venv" ]; then
print_error "Virtual environment not found. Please run setup first."
return 1
fi
# Activate virtual environment and start server
source venv/bin/activate
# Install dependencies if needed
if [ ! -f "venv/.deps_installed" ]; then
print_info "Installing dependencies..."
pip install -r requirements.txt
touch venv/.deps_installed
fi
# Start server in background
nohup python server.py > server.log 2>&1 &
local server_pid=$!
echo $server_pid > "$SERVER_PID_FILE"
# Wait a moment for server to start
sleep 3
if check_server_running; then
print_success "MCP server started successfully (PID: $server_pid)"
print_info "Server running on $SERVER_URL"
print_info "API documentation available at $SERVER_URL/docs"
else
print_error "Failed to start MCP server. Check server.log for details."
rm -f "$SERVER_PID_FILE"
return 1
fi
}
# Stop the MCP server
stop_server() {
if ! check_server_running; then
print_warning "MCP server is not running"
return 0
fi
local pid=$(cat "$SERVER_PID_FILE")
print_info "Stopping MCP server (PID: $pid)..."
kill "$pid"
rm -f "$SERVER_PID_FILE"
# Wait for process to stop
sleep 2
if ! ps -p "$pid" > /dev/null 2>&1; then
print_success "MCP server stopped successfully"
else
print_warning "Server may still be running. You may need to kill it manually."
fi
}
# Check server status
check_status() {
if check_server_running; then
local pid=$(cat "$SERVER_PID_FILE")
print_success "MCP server is running (PID: $pid)"
# Test server health
if command -v curl > /dev/null 2>&1; then
local health_response=$(curl -s "$SERVER_URL/health" 2>/dev/null)
if [ $? -eq 0 ]; then
print_info "Server health check: OK"
echo "$health_response" | python -m json.tool 2>/dev/null || echo "$health_response"
else
print_warning "Server health check failed"
fi
fi
else
print_error "MCP server is not running"
return 1
fi
}
# List available MCP tools
list_tools() {
if ! check_server_running; then
print_error "MCP server is not running. Start it first with: $0 start"
return 1
fi
print_info "Available MCP tools:"
echo ""
if command -v curl > /dev/null 2>&1; then
local tools_response=$(curl -s "$SERVER_URL/tools" 2>/dev/null)
if [ $? -eq 0 ]; then
echo "$tools_response" | python -m json.tool 2>/dev/null || echo "$tools_response"
else
print_error "Failed to fetch tools from server"
return 1
fi
else
print_error "curl is required but not installed"
return 1
fi
}
# Execute an MCP tool
run_tool() {
if [ $# -lt 1 ]; then
print_error "Tool name is required"
echo "Usage: $0 run <tool_name> [arguments...]"
echo "Use '$0 list' to see available tools"
return 1
fi
if ! check_server_running; then
print_error "MCP server is not running. Start it first with: $0 start"
return 1
fi
local tool_name="$1"
shift
# Build arguments JSON based on tool name
local args_json="{}"
case "$tool_name" in
"list_tasks")
if [ $# -gt 0 ]; then
args_json="{\"status_filter\": \"$1\"}"
fi
;;
"create_task")
if [ $# -lt 2 ]; then
print_error "create_task requires at least title and description"
echo "Usage: $0 run create_task \"<title>\" \"<description>\" [priority] [assigned_to]"
return 1
fi
args_json="{\"title\": \"$1\", \"description\": \"$2\""
if [ $# -gt 2 ]; then
args_json="$args_json, \"priority\": \"$3\""
fi
if [ $# -gt 3 ]; then
args_json="$args_json, \"assigned_to\": \"$4\""
fi
args_json="$args_json}"
;;
"update_task_status")
if [ $# -lt 2 ]; then
print_error "update_task_status requires task_id and new_status"
echo "Usage: $0 run update_task_status <task_id> <new_status>"
return 1
fi
args_json="{\"task_id\": $1, \"new_status\": \"$2\"}"
;;
"search_tasks")
if [ $# -lt 1 ]; then
print_error "search_tasks requires a query"
echo "Usage: $0 run search_tasks \"<query>\""
return 1
fi
args_json="{\"query\": \"$1\"}"
;;
"get_task_statistics"|"export_tasks_to_csv")
# These tools don't require arguments
;;
*)
print_error "Unknown tool: $tool_name"
echo "Use '$0 list' to see available tools"
return 1
;;
esac
# Make API call
local request_json="{\"tool_name\": \"$tool_name\", \"arguments\": $args_json}"
print_info "Executing tool: $tool_name"
if command -v curl > /dev/null 2>&1; then
local response=$(curl -s -X POST \
-H "Content-Type: application/json" \
-d "$request_json" \
"$SERVER_URL/mcp/tool" 2>/dev/null)
if [ $? -eq 0 ]; then
echo ""
echo "$response" | python -m json.tool 2>/dev/null || echo "$response"
else
print_error "Failed to execute tool"
return 1
fi
else
print_error "curl is required but not installed"
return 1
fi
}
# Send prompt to Gemini
call_gemini() {
if [ $# -lt 1 ]; then
print_error "Prompt is required"
echo "Usage: $0 gemini \"<prompt>\""
return 1
fi
if ! check_server_running; then
print_error "MCP server is not running. Start it first with: $0 start"
return 1
fi
local prompt="$1"
print_info "Sending prompt to Gemini..."
print_info "Prompt: $prompt"
echo ""
local request_json="{\"prompt\": \"$prompt\"}"
if command -v curl > /dev/null 2>&1; then
local response=$(curl -s -X POST \
-H "Content-Type: application/json" \
-d "$request_json" \
"$SERVER_URL/gemini" 2>/dev/null)
if [ $? -eq 0 ]; then
echo "$response" | python -m json.tool 2>/dev/null || echo "$response"
else
print_error "Failed to call Gemini"
return 1
fi
else
print_error "curl is required but not installed"
return 1
fi
}
# Show logs
show_logs() {
if ! check_server_running; then
print_error "MCP server is not running. Start it first with: $0 start"
return 1
fi
print_info "Recent MCP operation logs:"
echo ""
if command -v curl > /dev/null 2>&1; then
local logs_response=$(curl -s "$SERVER_URL/logs" 2>/dev/null)
if [ $? -eq 0 ]; then
echo "$logs_response" | python -m json.tool 2>/dev/null || echo "$logs_response"
else
print_error "Failed to fetch logs from server"
return 1
fi
else
print_error "curl is required but not installed"
return 1
fi
}
# Check server health
check_health() {
if ! check_server_running; then
print_error "MCP server is not running. Start it first with: $0 start"
return 1
fi
print_info "Checking server health..."
echo ""
if command -v curl > /dev/null 2>&1; then
local health_response=$(curl -s "$SERVER_URL/health" 2>/dev/null)
if [ $? -eq 0 ]; then
echo "$health_response" | python -m json.tool 2>/dev/null || echo "$health_response"
else
print_error "Failed to check server health"
return 1
fi
else
print_error "curl is required but not installed"
return 1
fi
}
# Main script logic
main() {
case "${1:-help}" in
"start")
start_server
;;
"stop")
stop_server
;;
"status")
check_status
;;
"list")
list_tools
;;
"run")
shift
run_tool "$@"
;;
"gemini")
shift
call_gemini "$@"
;;
"logs")
show_logs
;;
"health")
check_health
;;
"help"|"-h"|"--help")
print_usage
;;
*)
print_error "Unknown command: $1"
echo ""
print_usage
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"