Skip to main content
Glama
Raheem-19

Serena MCP Server

by Raheem-19

search_for_pattern

Search for any regex pattern across code and non-code files. Filter by file path or glob, and include context lines around matches.

Instructions

Offers a flexible search for arbitrary patterns in the codebase, including the possibility to search in non-code files. Generally, symbolic operations like find_symbol or find_referencing_symbols should be preferred if you know which symbols you are looking for.

Pattern Matching Logic: For each match, the returned result will contain the full lines where the substring pattern is found, as well as optionally some lines before and after it. The pattern will be compiled with DOTALL, meaning that the dot will match all characters including newlines. This also means that it never makes sense to have .* at the beginning or end of the pattern, but it may make sense to have it in the middle for complex patterns. If a pattern matches multiple lines, all those lines will be part of the match. Be careful to not use greedy quantifiers unnecessarily, it is usually better to use non-greedy quantifiers like .*? to avoid matching too much content.

File Selection Logic: The files in which the search is performed can be restricted very flexibly. Using restrict_search_to_code_files is useful if you are only interested in code symbols (i.e., those symbols that can be manipulated with symbolic tools like find_symbol). You can also restrict the search to a specific file or directory, and provide glob patterns to include or exclude certain files on top of that. The globs are matched against relative file paths from the project root (not to the relative_path parameter that is used to further restrict the search). Smartly combining the various restrictions allows you to perform very targeted searches. Returns A mapping of file paths to lists of matched consecutive lines.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
relative_pathNoOnly subpaths of this path (relative to the repo root) will be analyzed. If a path to a single file is passed, only that will be searched. The path must exist, otherwise a `FileNotFoundError` is raised.
max_answer_charsNoIf the output is longer than this number of characters, no content will be returned. -1 means the default value from the config will be used. Don't adjust unless there is really no other way to get the content required for the task. Instead, if the output is too long, you should make a stricter query.
substring_patternYesRegular expression for a substring pattern to search for.
paths_exclude_globNoOptional glob pattern specifying files to exclude from the search. Matches against relative file paths from the project root (e.g., "*test*", "**/*_generated.py"). Takes precedence over paths_include_glob. Only matches files, not directories. If left empty, no files are excluded.
paths_include_globNoOptional glob pattern specifying files to include in the search. Matches against relative file paths from the project root (e.g., "*.py", "src/**/*.ts"). Only matches files, not directories. If left empty, all non-ignored files will be included.
context_lines_afterNoNumber of lines of context to include after each match.
context_lines_beforeNoNumber of lines of context to include before each match.
restrict_search_to_code_filesNoWhether to restrict the search to only those files where analyzed code symbols can be found. Otherwise, will search all non-ignored files. Set this to True if your search is only meant to discover code that can be manipulated with symbolic tools. For example, for finding classes or methods from a name pattern. Setting to False is a better choice if you also want to search in non-code files, like in html or yaml files, which is why it is the default.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes
Behavior5/5

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

With no annotations, the description carries the full burden and does so thoroughly by disclosing DOTALL compilation, greedy/non-greedy quantifier advice, multi-line matching behavior, line-based return content, and file selection semantics including glob matching relative to project root. It also clarifies the return mapping at the end.

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

Conciseness5/5

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

The description is somewhat long but tightly structured with clear sections for pattern matching logic and file selection logic. Each paragraph adds necessary operational detail without fluff, and key guidance is front-loaded in the first sentence.

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

Completeness5/5

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

For a complex tool with 8 parameters, no annotations, and an output schema, the description covers the essential usage, edge cases, performance pitfalls (greedy quantifiers), and file filtering nuances. It adequately explains how to restrict searches and what is returned, making it sufficient for an agent to invoke correctly.

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

Parameters4/5

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

Schema coverage is 100%, so the baseline is 3. The description adds value by explaining the pattern matching implications of substring_pattern (e.g., DOTALL, leading/trailing .* being useless) and clarifying how relative_path and globs interact, which goes beyond the schema descriptions.

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

Purpose5/5

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

The description clearly states the tool offers 'flexible search for arbitrary patterns in the codebase' including non-code files, which is a specific verb+resource. It also distinguishes from siblings by explicitly recommending find_symbol or find_referencing_symbols when known symbols are sought.

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

Usage Guidelines5/5

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

The description explicitly says that symbolic operations like find_symbol or find_referencing_symbols should be preferred when symbols are known, providing an alternative usage path. It also gives guidance on when to use restrict_search_to_code_files and how to combine file selection restrictions for targeted searches.

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

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/Raheem-19/serena-optimized'

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