name: Publish to MCP Registry

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to publish (e.g., 3.67.0)'
        required: true

permissions:
  id-token: write
  contents: read

jobs:
  publish-crates-io:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Build and test
        run: |
          cargo build --features mcp
          cargo test --features mcp

      - name: Publish to crates.io
        run: cargo publish --features mcp --token ${{ secrets.CARGO_TOKEN }}
        continue-on-error: true  # May already be published

  publish-mcp-registry:
    name: Publish to MCP Registry
    runs-on: ubuntu-latest
    needs: publish-crates-io
    steps:
      - uses: actions/checkout@v4

      - name: Setup Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.21'

      - name: Build MCP Publisher
        run: |
          git clone https://github.com/modelcontextprotocol/registry /tmp/mcp-registry
          cd /tmp/mcp-registry
          make publisher

      - name: Verify server.json exists
        run: |
          if [ ! -f server.json ]; then
            echo "Error: server.json not found"
            exit 1
          fi
          cat server.json

      - name: Login to MCP Registry (GitHub OIDC)
        run: /tmp/mcp-registry/bin/mcp-publisher login github-oidc

      - name: Publish to MCP Registry
        run: /tmp/mcp-registry/bin/mcp-publisher publish

      - name: Verify publication
        run: |
          sleep 10  # Wait for registry to update
          curl -s "https://registry.modelcontextprotocol.io/v0/servers?search=ruchy-mcp" | jq '.'
