name: Publish to MCP Registry

on:
  push:
    tags:
      - 'v*'

permissions:
  id-token: write
  contents: read

concurrency:
  group: publish-mcp-${{ github.ref }}
  cancel-in-progress: false

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install uv
          uv pip install --system -e .

      - name: Run tests
        run: |
          uv pip install --system pytest pytest-asyncio
          pytest tests/ || echo "No tests found - skipping"

      - name: Build package
        run: |
          uv pip install --system build
          python -m build

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true

      - name: Install MCP Publisher
        run: |
          # Get latest release version dynamically
          VERSION=$(curl -s https://api.github.com/repos/modelcontextprotocol/registry/releases/latest | jq -r '.tag_name')
          echo "Installing mcp-publisher version: $VERSION"
          OS=$(uname -s | tr '[:upper:]' '[:lower:]')
          ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
          # New naming format: mcp-publisher_OS_ARCH.tar.gz (no version in filename)
          curl -L "https://github.com/modelcontextprotocol/registry/releases/download/${VERSION}/mcp-publisher_${OS}_${ARCH}.tar.gz" | tar xz
          sudo mv mcp-publisher /usr/local/bin/
          mcp-publisher --version

      - name: Login to MCP Registry
        run: mcp-publisher login github-oidc

      - name: Publish to MCP Registry
        run: mcp-publisher publish
