|
| 1 | +# PRD & Technical Plan: context-sherpa - AI-Powered Code Analysis Server |
| 2 | + |
| 3 | +**Version: 1.2** |
| 4 | +**Date: 2025-10-01** |
| 5 | +Author: Gemini |
| 6 | + |
| 7 | +## 1. Overview |
| 8 | + |
| 9 | +This document outlines the requirements for an MCP (Model-as-a-Tool Protocol) server, written in Go, that provides an AI coding agent with tools to interact with `ast-grep`. The primary objective is to create a system where an AI agent can not only lint and validate code using a predefined set of rules but also dynamically create, update, and remove those rules based on natural language feedback from a developer. |
| 10 | + |
| 11 | +The final product will be a single, portable, cross-platform binary with no external runtime dependencies, making setup trivial for the end-user. |
| 12 | + |
| 13 | +## 2. Core Objective & User Story |
| 14 | + |
| 15 | +As a developer using an AI coding agent, I want to: |
| 16 | + |
| 17 | +- Have my agent automatically validate the code it generates against my project's specific coding patterns. |
| 18 | +- Be able to provide natural language feedback (e.g., "From now on, all async functions must have a try/catch block") to my agent. |
| 19 | +- Have the agent intelligently convert my feedback into a permanent, machine-readable linting rule using `ast-grep`. |
| 20 | +- Be able to easily remove rules that are no longer needed. |
| 21 | +- Ensure this system is self-contained in a single executable that I can easily run without managing servers, dependencies, or security risks. |
| 22 | + |
| 23 | +## 3. Key Features & Tool Definitions for the AI Agent |
| 24 | + |
| 25 | +The MCP server will expose the following **four** tools to the AI agent. The agent will use the tool descriptions to decide which tool to call based on the user's request. |
| 26 | + |
| 27 | +### Tool 1: `initialize_ast_grep` |
| 28 | + |
| 29 | +- **Description**: "Initializes an ast-grep project if one is not already present. It creates the `sgconfig.yml` file and a `rules` directory. This tool should be suggested if another tool fails due to a missing configuration file." |
| 30 | +- **Input Schema**: (None) |
| 31 | +- **Output Schema**: |
| 32 | + - `success` (boolean): `true` if the project was initialized successfully. |
| 33 | + - `message` (string): A confirmation message (e.g., "ast-grep project initialized successfully. Created sgconfig.yml and rules/ directory."). |
| 34 | + |
| 35 | +### Tool 2: `scan_code` |
| 36 | + |
| 37 | +- **Description**: "Scans a given code snippet using the project's central `ast-grep` ruleset (`sgconfig.yml`). Use this to validate code, check for rule violations, or before committing changes." |
| 38 | +- **Input Schema**: |
| 39 | + - `code` (string, required): The raw source code to scan. |
| 40 | + - `language` (string, required): The programming language of the code (e.g., `javascript`, `python`, `go`). |
| 41 | +- **Output Schema**: |
| 42 | + - `success` (boolean): `true` if no issues were found, `false` otherwise. |
| 43 | + - `issues` (array of objects): A list of violations found. Each object contains: |
| 44 | + - `ruleId` (string): The ID of the rule that was violated. |
| 45 | + - `message` (string): The error message for the violation. |
| 46 | + - `line` (integer): The line number where the violation occurred. |
| 47 | + |
| 48 | +### Tool 3: `add_or_update_rule` |
| 49 | + |
| 50 | +- **Description**: "Adds a new rule or updates an existing rule in the project's central `sgconfig.yml` file. Use this after a rule has been generated and confirmed by the user." |
| 51 | +- **Input Schema**: |
| 52 | + - `rule_id` (string, required): A unique identifier for the rule (e.g., `no-console-log`). |
| 53 | + - `rule_yaml` (string, required): The complete YAML definition for the rule. |
| 54 | +- **Output Schema**: |
| 55 | + - `success` (boolean): `true` if the file was written successfully. |
| 56 | + - `message` (string): A confirmation message (e.g., "Rule 'no-console-log' was added successfully."). |
| 57 | + |
| 58 | +### Tool 4: `remove_rule` |
| 59 | + |
| 60 | +- **Description**: "Removes a rule from the project's central `sgconfig.yml` file by its unique ID. Use this when a coding standard is no longer desired." |
| 61 | +- **Input Schema**: |
| 62 | + - `rule_id` (string, required): The unique identifier of the rule to remove. |
| 63 | +- **Output Schema**: |
| 64 | + - `success` (boolean): `true` if the rule was found and removed successfully. |
| 65 | + - `message` (string): A confirmation message (e.g., "Rule 'no-console-log' was removed successfully."). |
| 66 | + |
| 67 | +## 4. Technical Implementation Plan |
| 68 | + |
| 69 | +This plan details the steps to build the server using Go, embedding the `ast-grep` binary directly. |
| 70 | + |
| 71 | +### Step A: Project Setup |
| 72 | +(No changes) |
| 73 | + |
| 74 | +### Step B: Bundle the ast-grep Binary |
| 75 | +(No changes) |
| 76 | + |
| 77 | +### Step C: Implement the MCP Server |
| 78 | + |
| 79 | +- In `main()`, create a new MCP server instance. |
| 80 | +- Register a handler for each of the three tools defined above. |
| 81 | + - `server.RegisterTool("scan_code", scanCodeHandler)` |
| 82 | + - `server.RegisterTool("add_or_update_rule", addOrUpdateRuleHandler)` |
| 83 | + - `server.RegisterTool("remove_rule", removeRuleHandler)` |
| 84 | +- Start the server to listen for requests from the AI agent. |
| 85 | + |
| 86 | +### Step D: Implement Tool Handler Functions |
| 87 | + |
| 88 | +- `scanCodeHandler(req mcp.Request) mcp.Response`: (No changes) |
| 89 | +- `addOrUpdateRuleHandler(req mcp.Request) mcp.Response`: (No changes) |
| 90 | +- `removeRuleHandler(req mcp.Request) mcp.Response`: (No changes) |
| 91 | + |
| 92 | +## 5. Non-Functional Requirements |
| 93 | +(No changes) |
| 94 | + |
| 95 | +## 6. Testing Strategy & Usage Example |
| 96 | + |
| 97 | +### Testing |
| 98 | + |
| 99 | +- **Unit Tests**: Each handler function (`scanCodeHandler`, `addOrUpdateRuleHandler`, etc.) will have corresponding unit tests. External dependencies will be mocked. |
| 100 | +- **Integration Tests**: An end-to-end test script will be created to compile the binary, start it, and simulate an MCP client making a sequence of calls to add, scan, and remove rules, verifying the `sgconfig.yml` content at each stage. |
| 101 | + |
| 102 | +### Example Usage Workflow |
| 103 | + |
| 104 | +This scenario illustrates the updated interaction between a developer, the AI agent, and the MCP server. |
| 105 | + |
| 106 | +1. **Agent handles a missing configuration file:** |
| 107 | + - **Developer**: "Hey agent, please add a rule to disallow `fmt.Println` in our Go code." |
| 108 | + - **AI Agent**: Calls the `add_or_update_rule` tool. |
| 109 | + - **MCP Server**: Fails because `sgconfig.yml` does not exist and returns an error: "Error: sgconfig.yml not found. Please run the 'initialize_ast_grep' tool first to set up the project." |
| 110 | + - **AI Agent**: "It looks like this project hasn't been set up for ast-grep yet. Would you like me to initialize it for you?" |
| 111 | + - **Developer**: "Yes, please." |
| 112 | + - **AI Agent**: Calls the `initialize_ast_grep` tool. |
| 113 | + - **MCP Server**: Creates `sgconfig.yml` and the `rules/` directory. |
| 114 | + - **AI Agent**: "The project has been initialized. I will now add the rule." |
| 115 | + - **AI Agent**: Calls `add_or_update_rule` again, which now succeeds. |
| 116 | + |
| 117 | +2. **Developer sets a new rule:** |
| 118 | + - **Developer**: "Hey agent, we should never use the `eval()` function in our JavaScript. It's a security risk. Can you create a rule to block it?" |
| 119 | + - **AI Agent**: Understands the intent. It uses its internal knowledge to generate a valid `ast-grep` YAML rule. |
| 120 | + - **AI Agent**: "I've drafted this rule: [shows YAML to user]. Does this look correct?" |
| 121 | + - **Developer**: "Yes, that's perfect." |
| 122 | + - **AI Agent**: Calls the `add_or_update_rule` tool with the YAML to save it to `sgconfig.yml`. |
| 123 | + |
| 124 | +2. **Agent self-corrects its own code:** |
| 125 | + - **Developer**: "Now, please refactor this old utility file to be more modern." |
| 126 | + - **AI Agent**: Generates a refactored version of the code, mistakenly using `eval()`. |
| 127 | + - **Agent (Internal Step)**: Before showing the code, it calls the `scan_code` tool on its own output. |
| 128 | + - **MCP Server**: Runs `ast-grep scan`, finds the `eval()` call, and returns a violation. |
| 129 | + - **AI Agent**: Sees the violation, corrects its code to use a safer alternative, and re-scans to confirm compliance. |
| 130 | + - **AI Agent**: "Here is the refactored code. I've also updated a part that was using `eval()` to be more secure." |
| 131 | + |
| 132 | +3. **Developer removes a rule:** |
| 133 | + - **Developer**: "You know what, the `no-console-log` rule is too annoying for our debug builds. Please remove it." |
| 134 | + - **AI Agent**: Calls the `remove_rule` tool with `rule_id: "no-console-log"`. |
| 135 | + - **MCP Server**: Removes the rule from `sgconfig.yml`. |
| 136 | + - **AI Agent**: "Done. The `no-console-log` rule has been removed." |
0 commit comments