# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --working_dir)
            WORKING_DIR="$2"
            shift 2
            ;;
        --mcp_name)
            MCP_NAME="$2"
            shift 2
            ;;
        --mcp_url)
            REMOTE_MCP_URL="$2"
            shift 2
            ;;
        -h|--help)
            echo "Usage: $0 [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  --working_dir DIR     Working directory (default: $WORKING_DIR)"
            echo "  --mcp_name NAME       MCP name (default: $MCP_NAME)"
            echo "  --mcp_url URL         Remote MCP URL (default: $REMOTE_MCP_URL)"
            echo "  -h, --help            Show this help message"
            echo ""
            echo "Example:"
            echo "  $0 --working_dir /path/to/analysis --mcp_name mymcp --mcp_url https://example.com"
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use -h or --help for usage information"
            exit 1
            ;;
    esac
done

# 3) Validate inputs
if [[ -z "$WORKING_DIR" ]]; then
    echo "Error: Working directory cannot be empty"
    exit 1
fi

if [[ -z "$MCP_NAME" ]]; then
    echo "Error: MCP name cannot be empty"
    exit 1
fi

if [[ -z "$REMOTE_MCP_URL" ]]; then
    echo "Error: Remote MCP URL cannot be empty"
    exit 1
fi

# 4) Normalize the URL and derive the Claude endpoint
REMOTE_MCP_BASE="${REMOTE_MCP_URL%/}"
REMOTE_MCP_HTTP="${REMOTE_MCP_BASE}/mcp"

# 5) Create CLAUDE.md from the template
mkdir -p "$WORKING_DIR"
cd "$WORKING_DIR"

cat > CLAUDE.md << 'EOF'
# Scientific Data Analysis Assistant

You are a scientific data analyst. You will use MCP tools and Python code to solve user tasks.

## Workflow

Always follow this structured workflow:

1. **Start with MCP tools** to generate or process data
2. **Inspect the data** using simple and precise Python code—always examine the head of the data before writing analysis code
3. **Write Python code** to analyze the MCP output and complete the task

## File Handling Rules

### Uploading Local Files
If an MCP tool requires a file path but the file is only on the local computer, upload it to the remote MCP server:

```bash
curl -F "file=@/<absolute_path_to_local_file>" \
${REMOTE_MCP_URL}/upload
```

### File Persistence
- When using multiple tools from the same MCP server that require files generated by MCP tools, you do not need to re-download and re-upload files
- The server keeps generated files available for subsequent operations

### Downloading Output Files
Output files generated by the MCP server are stored remotely, not locally. To download them:

```bash
wget ${REMOTE_MCP_URL}/outputs/<output_filename>
```

**Important**: Only download files the user explicitly needs, not every intermediate file.

## Data Integrity

- **Never** fake, mock, or hallucinate data
- If data is not available, state clearly that you do not have it
- Always work with actual data from the MCP tools
EOF


# 6) Register the remote MCP with Claude Code
claude mcp add --transport http "$MCP_NAME" "$REMOTE_MCP_HTTP"

# 7) Launch Claude Code
claude
