find_in_doc
Search a Google Doc for literal or regex text and return match locations as document character offsets, ready for applying styles to specific ranges.
Instructions
Search a Google Doc's text and return match locations as document character offsets.
Matches feed directly into style_doc_range's ranges — e.g. to link up bare URLs already sitting in a doc: find_in_doc(doc_id, query=r'https?://[^\s<>"]+(?<![.,;:!?)])', regex=True), then style_doc_range with link_url per match. The trailing negative lookbehind keeps a URL followed by sentence punctuation (or an unmatched closing paren) from swallowing it into the link.
Args: doc_id: The Google Doc file ID. query: Text to search for. Interpreted literally by default; pass regex=True to treat it as a Python regular expression. regex: Whether query is a regular expression (default False). case_sensitive: Whether the search is case-sensitive (default False). max_results: Maximum number of matches to return (default 50). local_path: Optional local filesystem path (file or directory) to write the result to instead of returning it inline. Bypasses the response-size cap. A path ending in '/' is a directory, created if missing. A file in place of a parent directory, or at a path ending in '/', raises ValueError; an existing file at a plain file path is replaced. Same caveat as download_file/download_folder: this path is resolved on the server's filesystem, not the caller's.
Returns: List of matches, each with start_index/end_index (document offsets usable directly as a style_doc_range range), matched_text, and context (the containing paragraph's full text). Searches paragraph text in the document body, including table cells; headers, footers, and footnotes are not searched. A zero-length regex match (e.g. "x*" with no "x" present) is skipped. Returns {"error": ...} if query is an invalid regex, or if the Docs API call itself fails (e.g. doc_id doesn't exist). max_results bounds match count, not response size — matched context can still be large. Raises ValueError if the response exceeds a safety cap (see MAX_TOOL_RESPONSE_CHARS in docs/configuration.md for the configured default) and local_path is not set — lower max_results, or pass local_path. If local_path is set, returns {local_path, doc_id, query, match_count, bytes_written} instead.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"doc_id": "<doc_id>",
"query": "<query>"
},
"name": "find_in_doc"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| regex | No | ||
| doc_id | Yes | ||
| local_path | No | ||
| max_results | No | ||
| case_sensitive | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |