create_mcp_server.pyā¢4.63 kB
#!/usr/bin/env python3
"""
CLI tool to create a new MCP server from the example template.
Usage:
python create_mcp_server.py <name> <path_to_sitemap.xml>
Example:
python create_mcp_server.py my-mcp-server ./sitemap.xml
"""
import os
import sys
import shutil
import subprocess
import argparse
from pathlib import Path
from dotenv import load_dotenv
from github import Github, GithubException
load_dotenv()
def run_command(cmd, cwd=None, check=True):
"""Run a shell command and return the result."""
print(f"Running: {' '.join(cmd)}")
result = subprocess.run(
cmd,
cwd=cwd,
check=check,
capture_output=True,
text=True
)
if result.stdout:
print(result.stdout)
if result.stderr:
print(result.stderr, file=sys.stderr)
return result
def clone_example_repo(target_dir):
"""Clone the MCP server example repository."""
repo_url = "https://github.com/rhit-bhuwalk/mcp-dedalus"
print(f"\nCloning example repository from {repo_url}...")
if target_dir.exists():
print(f"Error: Directory {target_dir} already exists!")
sys.exit(1)
run_command(["git", "clone", repo_url, str(target_dir)])
# Remove the .git directory to start fresh
git_dir = target_dir / ".git"
if git_dir.exists():
shutil.rmtree(git_dir)
print("Removed original .git directory")
def replace_sitemap(target_dir, sitemap_path):
"""Replace the sitemap.xml in the cloned repository."""
sitemap_source = Path(sitemap_path)
if not sitemap_source.exists():
print(f"Error: Sitemap file not found at {sitemap_path}")
sys.exit(1)
sitemap_dest = target_dir / "assets" / "sitemap.xml"
# Create assets directory if it doesn't exist
sitemap_dest.parent.mkdir(parents=True, exist_ok=True)
print(f"\nReplacing sitemap.xml with {sitemap_path}...")
shutil.copy2(sitemap_source, sitemap_dest)
print(f"Copied sitemap to {sitemap_dest}")
def init_git_repo(target_dir):
"""Initialize a new git repository."""
print("\nInitializing git repository...")
run_command(["git", "init"], cwd=target_dir)
run_command(["git", "add", "."], cwd=target_dir)
run_command(
["git", "commit", "-m", "Initial commit from MCP server template"],
cwd=target_dir
)
def create_github_repo(name, target_dir):
"""Create a GitHub repository and push the code."""
github_token = os.getenv("GITHUB_TOKEN")
if not github_token:
print("Error: GITHUB_TOKEN not found in .env file")
sys.exit(1)
print(f"\nCreating GitHub repository: {name}...")
try:
g = Github(github_token)
user = g.get_user()
# Create the repository
repo = user.create_repo(
name=name,
description=f"MCP server for {name}",
private=False,
auto_init=False
)
print(f"Created repository: {repo.html_url}")
# Add remote and push
print("\nPushing to GitHub...")
run_command(
["git", "remote", "add", "origin", repo.clone_url],
cwd=target_dir
)
run_command(
["git", "branch", "-M", "main"],
cwd=target_dir
)
run_command(
["git", "push", "-u", "origin", "main"],
cwd=target_dir
)
print(f"\nā
Success! Repository created and pushed to {repo.html_url}")
except GithubException as e:
print(f"Error creating GitHub repository: {e}")
sys.exit(1)
def main():
parser = argparse.ArgumentParser(
description="Create a new MCP server from the example template"
)
parser.add_argument("name", help="Name for the new MCP server repository")
parser.add_argument("sitemap_path", help="Path to sitemap.xml file")
parser.add_argument("local_dir", help="Local directory to clone the repository to")
args = parser.parse_args()
# Validate inputs
name = args.name
sitemap_path = args.sitemap_path
# Create target directory in current working directory
target_dir = Path(args.local_dir) / name
print(f"Creating MCP server: {name}")
print(f"Target directory: {target_dir}")
print(f"Sitemap path: {sitemap_path}")
# Execute the workflow
clone_example_repo(target_dir)
replace_sitemap(target_dir, sitemap_path)
init_git_repo(target_dir)
create_github_repo(name, target_dir)
print(f"\nš MCP server '{name}' has been created successfully!")
print(f" Location: {target_dir}")
if __name__ == "__main__":
main()