demo.py•3.31 kB
#!/usr/bin/env python
"""
Demo script to demonstrate the full MCP Server workflow using pipx commands.
"""
import os
import subprocess
import time
import argparse
import sys
def run_command(command, description=None, exit_on_error=True):
"""Run a command and print its output"""
if description:
print(f"\n{'=' * 80}\n{description}\n{'=' * 80}")
print(f"Running: {command}")
try:
process = subprocess.run(command, shell=True, check=True, text=True,
capture_output=True)
print(process.stdout)
return process.stdout
except subprocess.CalledProcessError as e:
print(f"Error running command: {e}")
print(f"Output: {e.stdout}\nError: {e.stderr}")
if exit_on_error:
sys.exit(1)
return None
def main():
"""Run the complete demo"""
parser = argparse.ArgumentParser(description="Run the MCP Server demo")
parser.add_argument("--skip-install", action="store_true",
help="Skip the installation step")
parser.add_argument("--skip-download", action="store_true",
help="Skip the download step")
parser.add_argument("--query", default="What is a module in Sui Move?",
help="Query to run against the vector database")
args = parser.parse_args()
# Set up working directories
data_dir = "demo_data"
docs_dir = os.path.join(data_dir, "docs")
os.makedirs(data_dir, exist_ok=True)
os.makedirs(docs_dir, exist_ok=True)
# Step 1: Install the package with pipx if not already installed
if not args.skip_install:
run_command("pipx install -e .",
"Step 1: Installing MCP Server using pipx")
# Step 2: Download Move files
if not args.skip_download:
run_command(f"pipx run mcp-download --query 'module sui' --max-results 20 --output-dir {docs_dir} --index-file {data_dir}/index.bin",
"Step 2: Downloading Move files from GitHub")
# Step 3: Index the files
run_command(f"pipx run mcp-index --docs-dir {docs_dir} --index-file {data_dir}/index.bin",
"Step 3: Indexing the Move files")
# Step 4: Query the vector database
run_command(f"pipx run mcp-query '{args.query}' -k 3 --index {data_dir}/index.bin -f",
"Step 4: Querying the vector database")
# Step 5: Start the server
print("\n" + "=" * 80)
print("Step 5: Starting the MCP Server (Press Ctrl+C to stop)")
print("=" * 80)
print(f"To query the server, run: curl -X POST 'http://localhost:8000/query' -H 'Content-Type: application/json' -d '{{'query': '{args.query}', 'top_k': 3}}'")
# Run the server in a separate process
server_process = subprocess.Popen(
f"pipx run mcp-server --index-file {data_dir}/index.bin",
shell=True,
text=True
)
try:
# Keep the server running until user interrupts
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nStopping the server...")
server_process.terminate()
server_process.wait()
print("\nDemo completed! You can continue using the mcp-* commands with pipx.")
if __name__ == "__main__":
main()