---
name: rust-crate-explorer
description: Automatically scan Rust crate source code when user asks about Rust libraries, analyzes dependencies, or needs to discover APIs. Use proactively for questions like "how does serde work?" or "show me tokio's Runtime API".
---

# Rust Crate Explorer

Use the `scan_crate` MCP tool to search through Rust crate source code when the user needs to understand how Rust libraries work.

## When to Use (Auto-Trigger)

Proactively use scan_crate when:

1. **Rust Library Questions**
   - User asks "how does [crate] work?"
   - User mentions specific Rust crates by name (serde, tokio, actix, etc.)
   - User asks about implementing traits or using types from known crates
   - Examples: "how does serde::Serialize work?", "explain tokio's runtime"

2. **Dependency Analysis**
   - User wants to understand dependencies in Cargo.toml
   - User asks "what does this dependency do?"
   - User is exploring a new Rust project's dependencies
   - Examples: "what's in our cargo dependencies?", "analyze this crate's deps"

3. **API Discovery**
   - User asks "how do I use X in crate Y?"
   - User searches for function signatures or trait implementations
   - User wants examples of how to call specific APIs
   - Examples: "find examples of Client::new in reqwest", "show me hyper server API"

## How to Use

### Basic Search Pattern

```
mcp__scan_crate__scan_crate({
  crate_spec: "crate-name",           // or "crate@version" or "git-url@tag"
  query: "regex pattern",              // ripgrep regex syntax
  case_sensitive: false                // usually false for broader results
})
```

### Common Query Patterns

**Finding trait implementations:**
```
query: "impl.*Serialize.*for"
```

**Finding function definitions:**
```
query: "pub fn new\\("
```

**Finding struct/enum definitions:**
```
query: "pub (struct|enum) Runtime"
```

**Finding macro definitions:**
```
query: "#\\[derive\\(.*Serialize"
```

### Crate Spec Formats

- `"serde"` - Latest version from crates.io
- `"serde@1.0.130"` - Specific version
- `"https://github.com/rust-lang/cargo.git@0.70.0"` - Git repo with tag
- `"./"` - Local directory (for workspace crates)

## Workflow

1. **Identify the crate**: Extract crate name from user's question
2. **Formulate regex**: Convert user's question to appropriate regex pattern
3. **Search**: Use scan_crate MCP tool
4. **Analyze results**: Read the matching code snippets
5. **Explain**: Provide clear explanation based on actual source code
6. **Follow-up**: Offer to search for related patterns if needed

## Important Notes

- **Requires ripgrep**: The tool needs `rg` (ripgrep) installed
- **Regex syntax**: Uses ripgrep's regex engine (similar to Rust's `regex` crate)
- **Escaping**: Literal characters like `(` need escaping: `\\(`
- **Case sensitivity**: Default to case-insensitive unless user specifies otherwise
- **Multiple results**: Tool returns file paths and line numbers; read relevant files if needed

## Examples

### User: "How does serde's Serialize trait work?"

```
Step 1: Search for Serialize trait definition
mcp__scan_crate__scan_crate({
  crate_spec: "serde",
  query: "pub trait Serialize",
  case_sensitive: false
})

Step 2: Search for example implementations
mcp__scan_crate__scan_crate({
  crate_spec: "serde",
  query: "impl.*Serialize.*for",
  case_sensitive: false
})
```

### User: "Show me tokio Runtime API"

```
mcp__scan_crate__scan_crate({
  crate_spec: "tokio",
  query: "impl Runtime",
  case_sensitive: false
})
```

### User: "How do I create a hyper Client?"

```
mcp__scan_crate__scan_crate({
  crate_spec: "hyper",
  query: "impl.*Client.*new",
  case_sensitive: false
})
```

## Do NOT Use When

- User asks about codebase structure (use Glob/Grep instead)
- User asks about local project files (use Read/Grep instead)
- User asks about non-Rust topics
- User asks about general Rust syntax (no specific crate mentioned)

## Best Practices

1. **Start broad**: Use simple queries first, then narrow down
2. **Multiple searches**: Often need 2-3 searches to fully understand
3. **Read context**: After finding matches, read surrounding code with Read tool
4. **Cite sources**: Always mention file paths and line numbers from results
5. **Version awareness**: Mention which version of the crate you're examining
