Laravel Devtoolbox

repository·main·Indexed 41 days ago

https://github.com/grazulex/laravel-devtoolbox

A Swiss-army artisan CLI tool for scanning, inspecting, and debugging Laravel applications. It provides insights into models, routes, service containers, SQL performance, and environment configurations. It includes an MCP server to expose scanners as tools for AI coding agents (such as Claude Code or Cursor) via the laravel/mcp package. Supports PHP 8.3+ and Laravel 12.0+ or 13.0+.

Tokens
26.6K
Snippets
85
Records
98
Agent score
99%

What's inside Laravel Devtoolbox

  1. How DevToolbox MCP tools work

    main

    DevToolbox exposes its scanners as MCP tools. Each scanner is mapped to a tool with the prefix devtoolbox-<name> (e.g., devtoolbox-routes, devtoolbox-sql-trace).

    Tool Types

    Tools are categorized into two types based on their side effects:

    1. Active Scanners: These tools execute actual code or queries. They are not marked as read-only or idempotent.
      • Examples: sql-trace, sql-analysis, performance.
      • Warning: Never run devtoolbox-sql-trace in a production environment.
    2. Read-Only Scanners: These are idempotent and safe for introspection. They are marked with #[IsReadOnly] and #[IsIdempotent].

    Tool Discovery and Execution

    • Naming: Tools use the scanner name as a slug (e.g., database-schema) and a human-readable title.
    • Arguments: Each tool has a typed schema defined by getOptionSchema(). Arguments are validated before execution.
    • Responses: Results are returned as structured JSON. If a response exceeds devtoolbox.mcp.max_response_bytes (default 262144), the ResponseTruncator will truncate large lists and add a _truncated metadata key to help you refine your query.
  2. Use DevToolbox MCP tools

    main

    When the MCP server is enabled, every DevToolbox scanner is exposed as a tool with a specific naming convention. Tools are prefixed with devtoolbox- followed by the scanner name.

    Tool Naming Pattern: devtoolbox-<scanner-name>

    Example Tools:

    • devtoolbox-routes
    • devtoolbox-sql-trace (Active/Write-capable)
    • devtoolbox-sql-analysis (Active/Write-capable)
    • devtoolbox-performance (Active/Write-capable)

    Active scanners (those that can perform non-read-only operations) include sql-trace, sql-analysis, and performance.

  3. How scanner option schemas are inferred

    main

    DevToolbox uses a tiered approach to determine the available options and their types for its scanners. This allows both modern scanners (with explicit schemas) and legacy scanners (with simple text descriptions) to work with the MCP server.

    1. Explicit Schema: If a scanner implements getOptionSchema(), that schema is used directly.
    2. Inference Fallback: If a scanner only provides legacy options via getAvailableOptions() (a name => description array), the OptionSchemaInferrer attempts to derive types from the description text:
      • (array) or array of $\rightarrow$ array type.
      • JSON object $\rightarrow$ object type.
      • Well-known numeric keys (e.g., limit, count, threshold) $\rightarrow$ integer type.
      • Well-known string keys (e.g., route, model, table, path) $\rightarrow$ string type.
      • Default $\rightarrow$ boolean type.
    3. Empty State: If nothing is declared, the schema and options are empty.
  4. Understand the DevToolbox MCP Server architecture

    main

    The DevToolbox MCP (Model Context Protocol) server allows AI agents to introspect your Laravel application. It works by wrapping existing DevToolbox scanners into MCP tools.

    There are two types of tools based on their side effects:

    1. ReadOnlyScannerTool: Used for scanners that only read application metadata (e.g., routes, models, middleware). These are annotated with #[IsReadOnly] and #[IsIdempotent].
    2. ActiveScannerTool: Used for scanners that execute code, such as internal HTTP requests or database queries (e.g., sql-trace, sql-analysis, and performance). These do not carry read-only annotations.

    The ToolFactory automatically prefixes all tool names with devtoolbox- and converts scanner names into human-readable headlines.

  5. Set up the DevToolbox MCP Server

    main

    The DevToolbox MCP (Model Context Protocol) server allows AI coding agents (like Claude Code, Cursor, or Gemini CLI) to interact directly with your Laravel application by querying its scanners (e.g., detecting unused routes, tracing SQL, or analyzing performance).

    Prerequisites

    • Laravel 12 or 13
    • PHP ≥ 8.3
    • laravel/mcp package installed

    Installation

    Add laravel/mcp to your development dependencies:

    composer require laravel/mcp --dev

    Configuration

    Ensure the MCP server is enabled and restricted to your local/testing environments in config/devtoolbox.php:

    'mcp' => [
        'enabled' => env('DEVTOOLBOX_MCP_ENABLED', true),
        'environments' => ['local', 'testing'],
        'max_response_bytes' => 262_144,
    ],

    Registration (Claude/Cursor)

    To use the server with an MCP client, register it using the following command (example for Claude):

    claude mcp add devtoolbox php artisan mcp:start devtoolbox

    Note: The server is only registered automatically if laravel/mcp is present and the current environment is allowed by your configuration.

    composer require laravel/mcp --dev
    claude mcp add devtoolbox php artisan mcp:start devtoolbox
  6. Identify unprotected or unused routes

    main

    You can audit your application's routing security and usage using a three-step process:

    1. Find unprotected routes: Use devtoolbox-security with the check_unprotected_routes: true option to identify routes missing authentication middleware.
    2. Trace controller usage: For any suspicious route, use devtoolbox-route-where-lookup with the target set to that route's controller. This reveals every route that hits that specific piece of code.
    3. Detect unused routes: Use devtoolbox-routes with detect_unused: true. Note that unused results should be treated as hints rather than absolute proof before deleting code.

    All these tools are available via MCP or as Artisan commands using the --format=json flag (e.g., php artisan dev:routes --detect_unused=true --format=json).

    # Example Artisan command for detecting unused routes
    php artisan dev:routes --detect_unused=true --format=json
  7. Analyze Laravel applications with the devtoolbox-analysis skill

    main

    The devtoolbox-analysis skill (installed via php artisan boost:install) provides recipes for common Laravel maintenance tasks. All tools mentioned have Artisan twins using the --format=json flag.

    1. Unprotected or Unused Routes

    • Find unprotected routes: Use devtoolbox-security with check_unprotected_routes: true to list routes missing auth middleware.
    • Trace controller usage: For a specific controller, use devtoolbox-route-where-lookup with the target parameter to see all routes hitting that code.
    • Detect unused routes: Use devtoolbox-routes with detect_unused: true before deleting routes. Note: unused is a hint, not absolute proof.

    2. N+1 and Duplicate Queries

    • Analyze queries: Use devtoolbox-sql-analysis with the route (or url) and the HTTP method to find duplicate query groups.
    • Trace exact queries: Use devtoolbox-sql-trace on the same route to see the ordered list of queries and bindings. Look for repeated statements with different IDs.
    • Verification: Fix with eager loading (with()) and re-run the analysis to compare counts.
    • Warning: These tools execute requests inside the app; use only in local environments.

    3. Model or Column Impact

    • Model usage: Use devtoolbox-model-usage with the model (class name) to find controllers, views, routes, jobs, and observers using it.
    • Column usage: Use devtoolbox-db-column-usage with tables: ["<table>"]. Setting unused_only: true identifies candidates for removal.

    4. Slow Boot Performance

    • Identify slow providers: Use devtoolbox-provider-timeline with slow_threshold: 20 (in ms).
    • Inspect bindings: Use devtoolbox-container-bindings with filter set to the slow provider's namespace to see what it registers.
    # Example: Finding unused routes via Artisan
    php artisan dev:routes --detect-unused --format=json
  8. Use DevToolbox via Artisan (Without MCP)

    main

    If you are not using an MCP server, every DevToolbox tool has an Artisan equivalent that outputs JSON. This is useful for manual inspection or scripting.

    Examples:

    • List routes in JSON: php artisan dev:routes --format=json
    • Check model usage in JSON: php artisan dev:model:where-used User --format=json

    To start the MCP server manually via Artisan:

    php artisan mcp:start devtoolbox
    php artisan dev:routes --format=json
  9. Diagnose slow application boot times

    main

    If your Laravel application is booting slowly, use these tools to pinpoint the bottleneck:

    1. Identify slow providers: Use devtoolbox-provider-timeline with a slow_threshold (e.g., slow_threshold: 20) to list Service Providers that take longer than the specified milliseconds to boot.
    2. Inspect provider bindings: Once a slow provider is identified, use devtoolbox-container-bindings with the filter option set to that provider's namespace to see exactly what it registers in the container.
    3. Optimization: Based on the findings, consider deferring the provider or moving heavy logic out of the boot() method.
    // Example: Finding providers slower than 20ms
    {
      "slow_threshold": 20
    }
  10. Set up the MCP Server for AI Agents

    main

    You can expose Devtoolbox scanners as tools for AI coding agents (like Claude Code or Cursor) using the Model Context Protocol (MCP). This requires the laravel/mcp package.

    1. Install MCP dependency: composer require laravel/mcp --dev (Requires Laravel 12.41.1+ or 13.x)

    2. Register with Claude Code: claude mcp add devtoolbox php artisan mcp:start devtoolbox

    3. Generic MCP Configuration (for other clients): Create a .mcp.json file with the following configuration:

      {
        "mcpServers": {
          "devtoolbox": {
            "command": "php",
            "args": ["artisan", "mcp:start", "devtoolbox"]
          }
        }
      }
    composer require laravel/mcp --dev
  11. Use DevToolbox via MCP Server

    main

    The DevToolbox MCP server exposes all scanners as tools for AI agents. It requires the laravel/mcp package.

    Installation

    Install the required dependency:

    composer require laravel/mcp --dev

    Registration

    To register the server with Claude Code, use:

    claude mcp add devtoolbox php artisan mcp:start devtoolbox

    Alternatively, you can add it to your .mcp.json configuration:

    {
      "mcpServers": {
        "devtoolbox": { "command": "php", "args": ["artisan", "mcp:start", "devtoolbox"] }
      }
    }

    Environment Safety

    By default, the MCP server is only registered in local or testing environments. This behavior is configurable via the devtoolbox.mcp configuration key or the DEVTOOLBOX_MCP_ENABLED environment variable.