Code Reasoning MCP Server

repository·main·Indexed 42 days ago

https://github.com/mettamatt/code-reasoning

An MCP server (v0.8.1) that enhances AI models like Claude in solving complex programming tasks using a sequential thinking methodology. It provides structured reasoning capabilities, including thought branching and revision, to improve problem-solving accuracy. The server can be integrated into Claude Desktop and VS Code, featuring a JSON-based data format for tracking thought steps and a prompt system with value persistence.

Tokens
9.4K
Snippets
28
Records
40
Agent score
54%

What's inside Code Reasoning

  1. Use Thought Revision to correct reasoning errors

    main

    Thought Revision is used when a previous reasoning step is found to be incorrect or suboptimal. To perform a revision, set is_revision to true and specify the revises_thought field with the thought_number of the error being corrected.

    {
      "thought": "On second thought, I overlooked the fact that the data might be already partially sorted... Mergesort would be a better choice.",
      "thought_number": 3,
      "total_thoughts": 5,
      "is_revision": true,
      "revises_thought": 1,
      "next_thought_needed": true
    }
  2. Understand the Sequential Thinking data format

    main

    The Code-Reasoning MCP Server uses a structured JSON format to represent a sequence of thoughts. Each thought object tracks its position in the reasoning process and determines if the process should continue.

    Key fields in a thought object include:

    • thought: The actual reasoning text.
    • thought_number: The current step index.
    • total_thoughts: The total number of steps planned.
    • next_thought_needed: A boolean indicating if the reasoning process should continue.
    • is_revision: (Optional) Boolean indicating if this thought corrects a previous one.
    • revises_thought: (Optional) The thought_number being corrected.
    • branch_from_thought: (Optional) The thought_number from which a new reasoning path starts.
    • branch_id: (Optional) A unique identifier for the new branch.
    {
      "thought": "First, let's understand the problem: we need to implement a function that finds duplicate elements in an array.",
      "thought_number": 1,
      "total_thoughts": 4,
      "next_thought_needed": true
    }
  3. How Prompt Configuration and Persistence Works

    main

    The server includes a prompt system that can be enabled via configuration. To improve user experience, the server persists prompt argument values to reduce repetitive data entry.

    Enabling Prompts

    Prompts are enabled by default (promptsEnabled: true). If using the internal buildConfig helper, the system checks this flag to initialize the PromptManager.

    Value Persistence

    Values are stored in a JSON file located at [config_dir]/prompt_values.json. The system manages two types of values:

    1. Global Values: Shared across all prompts (e.g., working_directory).
    2. Prompt-Specific Values: Stored for individual prompt templates (e.g., decision_context for an architecture-decision prompt).

    Precedence: When applying a prompt, the system merges stored values with user-provided values. User input always takes precedence over stored values.

    // Example structure of [config_dir]/prompt_values.json
    {
      "global": {
        "working_directory": "/path/to/project"
      },
      "prompts": {
        "architecture-decision": {
          "decision_context": "Previous value",
          "constraints": "Previous constraints",
          "options": "Previous options"
        }
      }
    }
  4. Configure the working_directory parameter

    main

    All prompts support a working_directory parameter. Setting this allows Claude to have clear context about your project location, reference project files easily, and simplify file access via filesystem tools.

    Example values:

    • macOS/Linux: ~/projects/my-app or /Users/username/projects/my-app
    • Windows: C:\Users\username\projects\my-app
  5. How Logging Works in Code-Reasoning

    main

    The server uses a streamlined logging approach via console.error() to ensure compatibility with the MCP protocol (which expects logs on stderr).

    • Output Destination: All logs are written to stderr.
    • Debug Mode: Verbose debug logs are only emitted when the --debug flag is provided at startup.
    • Limitations: There is no support for custom log directories or log file rotation.
  6. Use Thought Branching to explore multiple approaches

    main

    Thought Branching allows the reasoning process to diverge from a previous thought to explore alternative solutions or architectures. To branch, set branch_from_thought to the index of the thought you are diverging from, and provide a unique branch_id to identify this new path.

    {
      "thought": "Alternative Approach: We could also consider batch processing with scheduled ETL jobs instead of real-time processing.",
      "thought_number": 3,
      "total_thoughts": 6,
      "branch_from_thought": 1,
      "branch_id": "BatchETL",
      "next_thought_needed": true
    }
  7. Understand the MCP regression suite coverage

    main

    The MCP regression harness (located in test/mcp-regression.test.ts) verifies the server's MCP contract end-to-end by exercising the published stdio entry point (dist/index.js) using the official SDK client.

    Currently, the suite covers:

    • Tool discovery: Verifying tools/list works correctly.
    • Tool execution: Testing both happy-path calls and validation-error scenarios for the code-reasoning tool.
    • Prompt management: Testing prompt discovery, prompt rendering, and completion persistence for the bug-analysis prompt.

    You can extend this suite by adding additional request flows within the existing test runner.

  8. How prompt value persistence works

    main

    The Code Reasoning MCP server persists argument values between sessions to reduce repetitive data entry.

    Key Behaviors

    • Value Storage: When you use a prompt, argument values are saved to a JSON file in the ~/.code-reasoning/ directory, organized by prompt name.
    • Global Values: Certain parameters, such as working_directory, are stored globally and shared across all prompts. You only need to set these once.
    • Automatic Retrieval: When re-using a prompt, the dialog fields will be pre-filled with your previously saved values. You can override these at any time by entering new data.

    Managing Stored Values

    Stored values are located at: ~/.code-reasoning/prompt_values.json

    To reset all stored prompt values, delete this file or modify it directly.

  9. Configure VS Code integration

    main

    To integrate the Code-Reasoning MCP server into VS Code, update your user settings (settings.json) to include the server configuration under the mcp.servers key. Alternatively, you can configure it for a specific project by creating a .vscode/mcp.json file in your workspace root.

    // VS Code User Settings (settings.json)
    {
      "editor.fontFamily": "Fira Code, monospace",
      "editor.fontSize": 14,
      "mcp": {
        "servers": {
          "code-reasoning": {
            "command": "code-reasoning",
            "args": ["--debug"]
          }
        }
      }
    }
    // Workspace-specific configuration (.vscode/mcp.json)
    {
      "servers": {
        "code-reasoning": {
          "command": "code-reasoning",
          "args": ["--debug"]
        }
      }
    }
  10. Configure Claude Desktop with Code-Reasoning

    main

    To integrate the Code-Reasoning MCP Server into Claude Desktop, add it to your mcpServers configuration file using the following JSON structure:

    {
      "mcpServers": {
        "code-reasoning": {
          "command": "npx",
          "args": ["-y", "@mettamatt/code-reasoning"]
        }
      }
    }
  11. How to use Code Reasoning in chat

    main

    The Code Reasoning MCP server enhances Claude's ability to solve complex programming tasks through structured, step-by-step thinking. You can trigger its capabilities in two ways:

    1. Manual Trigger

    Append the following instruction to your chat messages: Use sequential thinking to reason about this.

    2. Using Prompt Templates

    You can use pre-defined templates to trigger reasoning:

    • Claude Desktop: Click the "+" icon in the chat window and select "Add from Code Reasoning" from the available tools. Choose a template, fill in the required info, and submit.
    • Claude Code: Type /help to see available commands and prompt options.

    Key Reasoning Features

    • Structured Thinking: Breaks problems into manageable steps.
    • Thought Branching: Explores multiple solution paths in parallel.
    • Thought Revision: Refines reasoning as understanding improves.
    • Safety Limits: The server automatically stops after 20 thought steps to prevent infinite loops.
  12. Integrate Code-Reasoning with Claude Desktop

    main

    To use the Code-Reasoning MCP Server within Claude Desktop, add it to your claude_desktop_config.json file.

    Config File Locations:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • Windows: %APPDATA%\Claude\claude_desktop_config.json
    • Linux: ~/.config/Claude/claude_desktop_config.json

    Configuration Format: Ensure the command is set to code-reasoning. You can pass arguments like --debug via the args array.

    {
      "mcpServers": {
        "code-reasoning": {
          "command": "code-reasoning",
          "args": ["--debug"]
        }
      }
    }