# RAG Retrieve

An example autoload skill that demonstrates how to integrate `rag-retrieve` and `table-rag-retrieve` through Claude Plugins hooks and an MCP server.

## Overview

This skill uses a `PrePrompt` hook to inject retrieval guidance into the prompt, and starts an MCP server that exposes retrieval capabilities for the current bot.

### PrePrompt Hook
Runs when the system prompt is loaded and injects retrieval policy content.
- File: `hooks/pre_prompt.py`
- Purpose: load retrieval instructions and add them to the prompt context

### MCP Server
Provides retrieval tools over stdio for the current `bot_id`.
- File: `rag_retrieve_server.py`
- Purpose: expose `rag-retrieve` and related retrieval tools to the agent

## Directory Structure

```text
rag-retrieve/
├── README.md                      # Skill documentation
├── .claude-plugin/
│   └── plugin.json                # Hook and MCP server configuration
├── hooks/
│   ├── pre_prompt.py              # PrePrompt hook script
│   └── retrieval-policy.md        # Retrieval policy injected into the prompt
├── mcp_common.py                  # Shared MCP utilities
├── rag_retrieve_server.py         # MCP server entrypoint
└── rag_retrieve_tools.json        # Tool definitions
```

## `plugin.json` Format

```json
{
  "name": "rag-retrieve",
  "description": "rag-retrieve and table-rag-retrieve",
  "hooks": {
    "PrePrompt": [
      {
        "type": "command",
        "command": "python hooks/pre_prompt.py"
      }
    ]
  },
  "mcpServers": {
    "rag_retrieve": {
      "transport": "stdio",
      "command": "python",
      "args": [
        "./skills_autoload/rag-retrieve/rag_retrieve_server.py",
        "{bot_id}"
      ]
    }
  }
}
```

## Hook Script Behavior

The hook script runs as a subprocess, receives input through environment variables, and writes the injected content to stdout.

### Available Environment Variables

| Environment Variable | Description | Applies To |
|----------------------|-------------|------------|
| `ASSISTANT_ID` | Bot ID | All hooks |
| `USER_IDENTIFIER` | User identifier | All hooks |
| `SESSION_ID` | Session ID | All hooks |
| `LANGUAGE` | Language code | All hooks |
| `HOOK_TYPE` | Hook type | All hooks |

### PrePrompt Example

```python
#!/usr/bin/env python3
import os
import sys


def main():
    user_identifier = os.environ.get('USER_IDENTIFIER', '')
    bot_id = os.environ.get('ASSISTANT_ID', '')

    print(f"## Retrieval Context\n\nUser: {user_identifier}\nBot: {bot_id}")
    return 0


if __name__ == '__main__':
    sys.exit(main())
```

## Example Use Cases

1. **Prompt-time retrieval guidance**: inject retrieval rules before the model starts reasoning
2. **Bot-specific retrieval setup**: start the MCP server with the current `bot_id`
3. **Unified retrieval access**: expose RAG and table RAG tools through a single skill
