crates-docs 0.9.0

High-performance Rust crate documentation query MCP server, supports Stdio/HTTP/SSE transport and OAuth authentication
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
name: CI

on:
  push:
    branches: [ main, master ]
    tags:
      - 'v*.*.*'
  pull_request:
    branches: [ main, master ]

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  # 代码质量检查
  check:
    name: Code Quality Checks
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        components: rustfmt, clippy
      
    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
        
    - name: Check formatting
      run: cargo fmt -- --check
      
    - name: Run clippy
      run: cargo clippy --all-targets -- -D warnings
      
    - name: Check with all features
      run: cargo clippy --all-features --all-targets -- -D warnings

  # 安全审计
  security:
    name: Security Audit
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      
    - name: Run cargo audit
      run: |
        cargo install cargo-audit
        cargo audit

  # 测试
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        rust: [stable, beta, nightly]
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        toolchain: ${{ matrix.rust }}
      
    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-${{ matrix.rust }}-cargo-${{ hashFiles('**/Cargo.toml') }}
        
    - name: Build
      run: cargo build --verbose
      
    - name: Test
      run: cargo test --verbose
      
    - name: Test with all features
      run: cargo test --all-features --verbose


  # Windows 原生编译测试
  build-windows:
    name: Build Windows
    runs-on: windows-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      
    - name: Build
      run: |
        cargo build --release --all-features
      
    - name: Verify binary
      run: |
        if (Test-Path target\release\crates-docs.exe) {
          Write-Host "Binary compiled successfully for Windows"
        } else {
          Write-Error "Binary not found for Windows"
          exit 1
        }

  # 测试 Redis 缓存功能
  test-redis:
    name: Test with Redis
    runs-on: ubuntu-latest
    
    services:
      redis:
        image: redis:7-alpine
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      
    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-redis-cargo-${{ hashFiles('**/Cargo.toml') }}
        
    - name: Build with Redis feature
      run: cargo build --features cache-redis --verbose
      
    - name: Test with Redis feature
      run: cargo test --features cache-redis --verbose

  # 文档构建检查
  docs:
    name: Documentation
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      
    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-docs-cargo-${{ hashFiles('**/Cargo.toml') }}
        
    - name: Build documentation
      run: cargo doc --no-deps --all-features
      
    - name: Check documentation links
      run: cargo doc --no-deps --all-features --document-private-items

  # 代码覆盖率测试
  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    
    services:
      redis:
        image: redis:7-alpine
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      
    - name: Install tarpaulin
      run: cargo install cargo-tarpaulin
      
    - name: Generate coverage
      run: cargo tarpaulin --out Xml --all-features --verbose
      
    - name: Install Codecov CLI
      run: python -m pip install --user codecov-cli

    - name: Upload to codecov.io
      env:
        CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
      run: |
        export PATH="$(python -m site --user-base)/bin:$PATH"
        codecovcli upload-process \
          --disable-search \
          --file ./cobertura.xml \
          --flag unittests \
          --name codecov-umbrella \
          --sha "$GITHUB_SHA"
      continue-on-error: true

  # Docker 镜像构建测试
  docker:
    name: Docker Build
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v4
      
    - name: Build Docker image
      run: docker build -t crates-docs:test .
      
    - name: Test Docker image
      run: |
        docker run -d --name test-container -p 8080:8080 crates-docs:test
        trap 'docker logs test-container || true; docker stop test-container || true; docker rm test-container || true' EXIT

        for i in $(seq 1 30); do
          if curl -fsSI http://127.0.0.1:8080/mcp > /tmp/docker-mcp-headers.txt; then
            break
          fi
          sleep 2
        done

        curl -fsSI http://127.0.0.1:8080/mcp | tee /tmp/docker-mcp-headers.txt
        grep -Eq '^HTTP/[0-9.]+ (200|204|400|405|406)' /tmp/docker-mcp-headers.txt
        docker logs test-container

  # 发布预检查 - 在 push 到 main/master 分支时运行
  # 确保所有检查通过后,push tag 就一定会发布成功
  publish-check:
    name: Publish Check
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      
    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-publish-cargo-${{ hashFiles('**/Cargo.toml') }}
        
    - name: Check publish (dry-run)
      run: cargo publish --dry-run

  # Docker Hub 凭证验证 - 在 push 到 main/master 分支时运行
  docker-credential-check:
    name: Docker Credential Check
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
    
    steps:
    - name: Log in to Docker Hub
      uses: docker/login-action@v4
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
      
    - name: Verify Docker credentials
      run: |
        # 验证登录成功
        docker info | grep -q "Username" || {
          echo "Error: Docker Hub login failed"
          exit 1
        }
        echo "Docker Hub credentials are valid!"

  # ==================== 以下是发布 jobs(只在 push tag 时触发)====================
  
  # 实际发布 - 只在推送版本标签时运行
  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    needs: [check, security, test, build-windows, test-redis, docs, docker]
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      
    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-publish-cargo-${{ hashFiles('**/Cargo.toml') }}
        
    - name: Verify version matches tag
      run: |
        CARGO_VERSION=$(grep '^version = ' Cargo.toml | head -n 1 | sed 's/version = "\(.*\)"/\1/')
        TAG_VERSION=${GITHUB_REF#refs/tags/v}
        
        echo "Cargo.toml version: $CARGO_VERSION"
        echo "Tag version: $TAG_VERSION"
        
        if [ "$CARGO_VERSION" != "$TAG_VERSION" ]; then
          echo "Error: Version mismatch!"
          exit 1
        fi
        
    - name: Publish to crates.io
      env:
        CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
      run: |
        if [ -z "$CARGO_REGISTRY_TOKEN" ]; then
          echo "Error: CARGO_REGISTRY_TOKEN is not set"
          exit 1
        fi

        cargo publish

  # Docker 二进制预编译 - 只在推送版本标签时运行
  build-docker-binaries:
    name: Build Docker Binaries
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    needs: [check, docker]
    
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            arch: amd64
            use_cross: false
          - target: aarch64-unknown-linux-musl
            arch: arm64
            use_cross: true
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        targets: ${{ matrix.target }}
      
    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-${{ matrix.target }}-docker-cargo-${{ hashFiles('**/Cargo.toml') }}
      
    - name: Install musl tools
      run: |
        sudo apt-get update
        sudo apt-get install -y musl-tools
      
    - name: Install cross
      if: matrix.use_cross
      run: cargo install cross --git https://github.com/cross-rs/cross
      
    - name: Build binary (native)
      if: ${{ !matrix.use_cross }}
      run: cargo build --release --target ${{ matrix.target }}
      
    - name: Build binary (cross)
      if: matrix.use_cross
      run: cross build --release --target ${{ matrix.target }}
      
    - name: Prepare binary
      run: |
        BINARY_NAME="crates-docs-${{ matrix.arch }}"
        cp target/${{ matrix.target }}/release/crates-docs "$BINARY_NAME"
        chmod +x "$BINARY_NAME"
        echo "binary_name=$BINARY_NAME" >> $GITHUB_OUTPUT
      id: prepare
      
    - name: Upload binary artifact
      uses: actions/upload-artifact@v4
      with:
        name: binary-${{ matrix.arch }}
        path: ${{ steps.prepare.outputs.binary_name }}
        retention-days: 1

  # Docker 镜像发布 - 只在推送版本标签时运行
  publish-docker:
    name: Publish Docker Images
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    needs: [build-docker-binaries]
    
    permissions:
      contents: read
      packages: write
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
      
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v4
      
    - name: Log in to Docker Hub
      uses: docker/login-action@v4
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
      
    - name: Extract version
      id: version
      run: |
        VERSION=${GITHUB_REF#refs/tags/v}
        echo "version=$VERSION" >> $GITHUB_OUTPUT
        echo "Tag version: $VERSION"
        
    - name: Download amd64 binary
      uses: actions/download-artifact@v4
      with:
        name: binary-amd64
        path: ./binaries/amd64
      
    - name: Download arm64 binary
      uses: actions/download-artifact@v4
      with:
        name: binary-arm64
        path: ./binaries/arm64
      
    - name: Verify and prepare binaries
      run: |
        echo "Downloaded binaries structure:"
        find ./binaries -type f -ls
        
        # Move binaries to expected locations for Docker
        # download-artifact@v4 creates subdirectories with artifact names
        mv ./binaries/amd64/crates-docs-amd64 ./binaries/crates-docs-amd64 || true
        mv ./binaries/arm64/crates-docs-arm64 ./binaries/crates-docs-arm64 || true
        
        echo "Final binaries:"
        ls -la ./binaries/
        
        # Verify files exist and are executable
        test -f ./binaries/crates-docs-amd64 || { echo "ERROR: amd64 binary not found"; exit 1; }
        test -f ./binaries/crates-docs-arm64 || { echo "ERROR: arm64 binary not found"; exit 1; }
        chmod +x ./binaries/crates-docs-amd64 ./binaries/crates-docs-arm64
      
    - name: Build and push Docker image
      uses: docker/build-push-action@v7
      with:
        context: .
        file: ./Dockerfile.release
        push: true
        platforms: linux/amd64,linux/arm64
        tags: |
          ${{ secrets.DOCKER_USERNAME }}/crates-docs:latest
          ${{ secrets.DOCKER_USERNAME }}/crates-docs:${{ steps.version.outputs.version }}
        cache-from: type=gha
        cache-to: type=gha,mode=max

  # 二进制文件发布到 GitHub Release
  release:
    name: Build and Release Binaries
    runs-on: ${{ matrix.os }}
    if: startsWith(github.ref, 'refs/tags/v')
    needs: [check, security, test, build-windows, test-redis, docs, docker]
    
    permissions:
      contents: write
    
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            cross: false
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            cross: false
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            cross: true
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
          - target: x86_64-apple-darwin
            os: macos-latest
            cross: false
          - target: aarch64-apple-darwin
            os: macos-latest
            cross: false
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            cross: false
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v6
    
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        targets: ${{ matrix.target }}
    
    - name: Cache cargo
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.toml') }}
    
    - name: Install cross
      if: matrix.cross
      run: cargo install cross --git https://github.com/cross-rs/cross
    
    - name: Install musl tools (Linux musl)
      if: endsWith(matrix.target, '-musl')
      run: |
        sudo apt-get update
        sudo apt-get install -y musl-tools
    
    - name: Build binary (native)
      if: ${{ !matrix.cross }}
      run: cargo build --release --target ${{ matrix.target }}
    
    - name: Build binary (cross)
      if: matrix.cross
      run: cross build --release --target ${{ matrix.target }}
    
    - name: Extract version
      id: version
      shell: bash
      run: |
        VERSION=${GITHUB_REF#refs/tags/v}
        echo "version=$VERSION" >> $GITHUB_OUTPUT
        echo "Tag version: $VERSION"
    
    - name: Prepare binary (Unix)
      if: runner.os != 'Windows'
      run: |
        BINARY_NAME="crates-docs-v${{ steps.version.outputs.version }}-${{ matrix.target }}"
        mv target/${{ matrix.target }}/release/crates-docs "$BINARY_NAME"
        chmod +x "$BINARY_NAME"
        echo "binary_name=$BINARY_NAME" >> $GITHUB_OUTPUT
    
    - name: Prepare binary (Windows)
      if: runner.os == 'Windows'
      shell: bash
      run: |
        BINARY_NAME="crates-docs-v${{ steps.version.outputs.version }}-${{ matrix.target }}.exe"
        mv target/${{ matrix.target }}/release/crates-docs.exe "$BINARY_NAME"
        echo "binary_name=$BINARY_NAME" >> $GITHUB_OUTPUT
    
    - name: Upload release asset
      uses: softprops/action-gh-release@v2
      with:
        files: crates-docs-v${{ steps.version.outputs.version }}-${{ matrix.target }}*
        fail_on_unmatched_files: false
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}