Skip to main content
Glama
pdogra1299
by pdogra1299

search_code

Find code snippets across Bitbucket repositories using context-aware search patterns. Filter by workspace, repository, file type, or specific code contexts like assignments, declarations, or usages.

Instructions

Search for code across Bitbucket repositories with enhanced context-aware search patterns (currently only supported for Bitbucket Server)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_patternNoFile path pattern to filter results (e.g., "*.java", "src/**/*.ts") (optional)
include_patternsNoAdditional custom search patterns to include (e.g., ["variable =", ".variable"]) (optional)
limitNoMaximum number of results to return (default: 25)
repositoryNoRepository slug to search in (optional, searches all repos if not specified)
search_contextNoContext to search for: assignment (term=value), declaration (defining term), usage (calling/accessing term), exact (quoted match), or any (all patterns)
search_queryYesThe search term or phrase to look for in code (e.g., "variable")
startNoStart index for pagination (default: 0)
workspaceYesBitbucket workspace/project key (e.g., "PROJ")

Implementation Reference

  • Main handler function executing the search_code tool: parses args, builds contextual search query, calls Bitbucket Server search API, formats and returns results.
    async handleSearchCode(args: any) {
      try {
        const { 
          workspace, 
          repository, 
          search_query, 
          search_context = 'any',
          file_pattern, 
          include_patterns = [],
          limit = 25, 
          start = 0 
        } = args;
    
        if (!workspace || !search_query) {
          throw new Error('Workspace and search_query are required');
        }
    
        // Only works for Bitbucket Server currently
        if (!this.apiClient.getIsServer()) {
          throw new Error('Code search is currently only supported for Bitbucket Server');
        }
    
        // Build the enhanced query string
        let query = `project:${workspace}`;
        if (repository) {
          query += ` repo:${repository}`;
        }
        if (file_pattern) {
          query += ` path:${file_pattern}`;
        }
        
        // Build smart search patterns
        const smartQuery = buildSmartQuery(search_query, search_context, include_patterns);
        query += ` ${smartQuery}`;
    
        // Prepare the request payload
        const payload: BitbucketServerSearchRequest = {
          query: query.trim(),
          entities: { 
            code: {
              start: start,
              limit: limit
            }
          }
        };
    
        // Make the API request (no query params needed, pagination is in payload)
        const response = await this.apiClient.makeRequest<BitbucketServerSearchResult>(
          'post',
          `/rest/search/latest/search?avatarSize=64`,
          payload
        );
    
        const searchResult = response;
    
        // Use simplified formatter for cleaner output
        const simplifiedOutput = formatCodeSearchOutput(searchResult);
    
        // Prepare pagination info
        const hasMore = searchResult.code?.isLastPage === false;
        const nextStart = hasMore ? (searchResult.code?.nextStart || start + limit) : undefined;
        const totalCount = searchResult.code?.count || 0;
    
        // Build a concise response with search context info
        let resultText = `Code search results for "${search_query}"`;
        if (search_context !== 'any') {
          resultText += ` (context: ${search_context})`;
        }
        resultText += ` in ${workspace}`;
        if (repository) {
          resultText += `/${repository}`;
        }
        
        // Show the actual search query used
        resultText += `\n\nSearch query: ${query.trim()}`;
        resultText += `\n\n${simplifiedOutput}`;
        
        if (totalCount > 0) {
          resultText += `\n\nTotal matches: ${totalCount}`;
          if (hasMore) {
            resultText += ` (showing ${start + 1}-${start + (searchResult.code?.values?.length || 0)})`;
          }
        }
    
        return {
          content: [{
            type: 'text',
            text: resultText
          }]
        };
      } catch (error: any) {
        const errorMessage = error.response?.data?.errors?.[0]?.message || error.message;
        return {
          content: [{
            type: 'text',
            text: JSON.stringify({
              error: `Failed to search code: ${errorMessage}`,
              details: error.response?.data
            }, null, 2)
          }],
          isError: true
        };
      }
    }
  • Tool schema definition including input schema with properties, descriptions, enums, and required fields.
    {
      name: 'search_code',
      description: 'Search for code across Bitbucket repositories with enhanced context-aware search patterns (currently only supported for Bitbucket Server)',
      inputSchema: {
        type: 'object',
        properties: {
          workspace: {
            type: 'string',
            description: 'Bitbucket workspace/project key (e.g., "PROJ")',
          },
          repository: {
            type: 'string',
            description: 'Repository slug to search in (optional, searches all repos if not specified)',
          },
          search_query: {
            type: 'string',
            description: 'The search term or phrase to look for in code (e.g., "variable")',
          },
          search_context: {
            type: 'string',
            enum: ['assignment', 'declaration', 'usage', 'exact', 'any'],
            description: 'Context to search for: assignment (term=value), declaration (defining term), usage (calling/accessing term), exact (quoted match), or any (all patterns)',
          },
          file_pattern: {
            type: 'string',
            description: 'File path pattern to filter results (e.g., "*.java", "src/**/*.ts") (optional)',
          },
          include_patterns: {
            type: 'array',
            items: { type: 'string' },
            description: 'Additional custom search patterns to include (e.g., ["variable =", ".variable"]) (optional)',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of results to return (default: 25)',
          },
          start: {
            type: 'number',
            description: 'Start index for pagination (default: 0)',
          },
        },
        required: ['workspace', 'search_query'],
      },
    },
  • src/index.ts:140-141 (registration)
    Registration of the search_code tool in the main request handler switch statement, delegating to SearchHandlers.handleSearchCode.
    case 'search_code':
      return this.searchHandlers.handleSearchCode(request.params.arguments);
  • Type guard function for validating search_code tool arguments.
    export const isSearchCodeArgs = (
      args: any
    ): args is {
      workspace: string;
      repository?: string;
      search_query: string;
      file_pattern?: string;
      limit?: number;
      start?: number;
    } =>
      typeof args === 'object' &&
      args !== null &&
      typeof args.workspace === 'string' &&
      typeof args.search_query === 'string' &&
      (args.repository === undefined || typeof args.repository === 'string') &&
      (args.file_pattern === undefined || typeof args.file_pattern === 'string') &&
      (args.limit === undefined || typeof args.limit === 'number') &&
      (args.start === undefined || typeof args.start === 'number');
  • Helper function to build contextual search query patterns based on search term and context (e.g., assignment, declaration). Used by the handler.
    function buildSmartQuery(
      searchTerm: string, 
      searchContext: string = 'any',
      includePatterns: string[] = []
    ): string {
      const contextPatterns = buildContextualPatterns(searchTerm);
      
      let patterns: string[] = [];
      
      // Add patterns based on context
      if (searchContext in contextPatterns) {
        patterns = [...contextPatterns[searchContext as keyof SearchContext]];
      } else {
        patterns = [...contextPatterns.any];
      }
      
      // Add user-provided patterns
      if (includePatterns && includePatterns.length > 0) {
        patterns = [...patterns, ...includePatterns];
      }
      
      // Remove duplicates and join with OR
      const uniquePatterns = [...new Set(patterns)];
      
      // If only one pattern, return it without parentheses
      if (uniquePatterns.length === 1) {
        return uniquePatterns[0];
      }
      
      // Wrap each pattern in quotes for safety and join with OR
      const quotedPatterns = uniquePatterns.map(pattern => `"${pattern}"`);
      return `(${quotedPatterns.join(' OR ')})`;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden for behavioral disclosure. It mentions 'enhanced context-aware search patterns' and Bitbucket Server limitation, but doesn't cover important aspects like whether this is a read-only operation, performance characteristics, rate limits, authentication requirements, or what the search results look like. For a search tool with 8 parameters and no annotations, this leaves significant behavioral gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately concise with a single sentence that communicates the core purpose and limitation. It's front-loaded with the main functionality and includes the Bitbucket Server constraint as important context. No wasted words, though it could be slightly more structured for clarity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (8 parameters, search functionality) and absence of both annotations and output schema, the description is somewhat incomplete. It covers the what and where but lacks details about behavioral characteristics, result format, and operational constraints that would help an agent use it effectively. The schema provides parameter details but the description should add more context for this non-trivial search tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description doesn't add any parameter-specific information beyond what's already in the schema, which has 100% coverage with detailed descriptions for all 8 parameters. The baseline is 3 since the schema does the heavy lifting, but the description doesn't compensate with additional context about how parameters interact or best practices for search patterns.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool searches for code across Bitbucket repositories with enhanced context-aware patterns, specifying the verb 'search' and resource 'code across Bitbucket repositories'. It distinguishes from siblings by focusing on code search rather than pull requests, branches, or file operations, though it doesn't explicitly contrast with similar tools.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for code search in Bitbucket repositories with context-aware patterns, and mentions 'currently only supported for Bitbucket Server' which provides some platform context. However, it doesn't explicitly state when to use this tool versus alternatives like get_file_content or list_directory_content for different search needs, nor does it provide exclusion criteria.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

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/pdogra1299/bitbucket-mcp-server'

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