Skip to main content
Glama
qckfx

Tree-Hugger-JS MCP Server

by qckfx

find_pattern

Locate specific JavaScript/TypeScript code patterns using intuitive syntax for targeted searches in code analysis.

Instructions

Find first node matching the specified pattern using tree-hugger-js intuitive syntax. Use for targeted searches when you need one specific match.

Examples: • Find main function: find_pattern('function[name="main"]') • Find React component: find_pattern('function[name="UserProfile"]') • Find async functions: find_pattern('function[async]') • Find specific class: find_pattern('class[name="UserManager"]') • Find error handling: find_pattern('call[text*="catch"]') • Find JSX with props: find_pattern('jsx:has(jsx-attribute[name="className"])') • Debug specific calls: find_pattern('call[text*="console.log"]')

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternYesPattern using intuitive syntax: 'function', 'class[name="MyClass"]', 'function[async]', 'call[text*="fetch"]'

Implementation Reference

  • The handler function that executes the 'find_pattern' tool. It checks if AST is loaded, uses tree.find(pattern) to locate the first matching node, extracts metadata, and returns formatted node information or error.
    private async findPattern(args: { pattern: string }) {
      if (!this.currentAST) {
        return {
          content: [{
            type: "text",
            text: "No AST loaded. Please use parse_code first.",
          }],
          isError: true,
        };
      }
    
      try {
        const result = this.currentAST.tree.find(args.pattern);
        
        if (!result) {
          return {
            content: [{
              type: "text",
              text: `No match found for pattern: ${args.pattern}`,
            }],
          };
        }
    
        const nodeInfo = {
          type: result.type,
          text: result.text.length > 200 ? result.text.slice(0, 200) + '...' : result.text,
          line: result.line,
          column: result.column,
          name: result.name,
          startPosition: result.startPosition,
          endPosition: result.endPosition,
          childrenCount: result.children.length,
        };
    
        return {
          content: [{
            type: "text", 
            text: `Found match for pattern "${args.pattern}":\n${JSON.stringify(nodeInfo, null, 2)}`,
          }],
        };
      } catch (error) {
        return {
          content: [{
            type: "text",
            text: `Error finding pattern: ${error instanceof Error ? error.message : String(error)}`,
          }],
          isError: true,
        };
      }
    }
  • Input schema definition for the 'find_pattern' tool, specifying a required 'pattern' string parameter.
    inputSchema: {
      type: "object",
      properties: {
        pattern: {
          type: "string",
          description: "Pattern using intuitive syntax: 'function', 'class[name=\"MyClass\"]', 'function[async]', 'call[text*=\"fetch\"]'"
        }
      },
      required: ["pattern"],
    },
  • src/index.ts:416-417 (registration)
    Registration in the CallToolRequestSchema switch statement that dispatches to the findPattern handler.
    case "find_pattern":
      return await this.findPattern(args as { pattern: string });
  • src/index.ts:202-215 (registration)
    Tool registration in the ListToolsRequestSchema handler, including name, description, and input schema.
    {
      name: "find_pattern",
      description: "Find first node matching the specified pattern using tree-hugger-js intuitive syntax. Use for targeted searches when you need one specific match.\n\nExamples:\n• Find main function: find_pattern('function[name=\"main\"]')\n• Find React component: find_pattern('function[name=\"UserProfile\"]')\n• Find async functions: find_pattern('function[async]')\n• Find specific class: find_pattern('class[name=\"UserManager\"]')\n• Find error handling: find_pattern('call[text*=\"catch\"]')\n• Find JSX with props: find_pattern('jsx:has(jsx-attribute[name=\"className\"])')\n• Debug specific calls: find_pattern('call[text*=\"console.log\"]')",
      inputSchema: {
        type: "object",
        properties: {
          pattern: {
            type: "string",
            description: "Pattern using intuitive syntax: 'function', 'class[name=\"MyClass\"]', 'function[async]', 'call[text*=\"fetch\"]'"
          }
        },
        required: ["pattern"],
      },
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/qckfx/tree-hugger-js-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server