Skip to main content
Glama
tan-yong-sheng

TriliumNext Notes' MCP Server

search_and_replace_note

Replace text in TriliumNext notes using search patterns. Update note content by specifying search and replacement text, with regex support and version safety checks.

Instructions

Search and replace content within a single note. When someone wants to replace text in a note, first call get_note to get the current content and hash, then use this function to make the changes. This ensures you're working with the latest version of their note.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
noteIdYesID of the note to perform search and replace on
searchPatternYesWhat to search for in the note.
replacePatternYesWhat to replace it with. For regex: supports patterns like '$1' for captured groups.
useRegexNoWhether to use regex patterns (default: true).
searchFlagsNoSearch options. Defaults to 'gi' (global, case-insensitive). Remove 'i' for exact case matching.gi
expectedHashYes⚠️ REQUIRED: Content hash from get_note response. Always get the note content first to obtain this hash.
revisionNoWhether to create a backup before replacing (default: true for safety).

Implementation Reference

  • Core implementation of the search_and_replace_note tool. Fetches note content, validates expectedHash to prevent conflicts, performs search and replace using regex or literal matching, validates new content against note type rules, optionally creates a revision, and updates the note via Trilium API.
    export async function handleSearchReplaceNote(
      args: NoteOperation,
      axiosInstance: any
    ): Promise<NoteSearchReplaceResponse> {
      const {
        noteId,
        searchPattern,
        replacePattern,
        useRegex = true,
        searchFlags = 'g',
        revision = true,
        expectedHash
      } = args;
    
      if (!noteId) {
        throw new Error("noteId is required for search_and_replace operation.");
      }
    
      if (!searchPattern) {
        throw new Error("searchPattern is required for search_and_replace operation.");
      }
    
      if (!replacePattern) {
        throw new Error("replacePattern is required for search_and_replace operation.");
      }
    
      if (!expectedHash) {
        throw new Error("expectedHash is required for search_and_replace operation. You must call get_note first to retrieve the current blobId.");
      }
    
      let revisionCreated = false;
    
      try {
        // Step 1: Get current note state and content
        const currentNote = await axiosInstance.get(`/notes/${noteId}`);
        const currentContent = await axiosInstance.get(`/notes/${noteId}/content`, {
          responseType: 'text'
        });
    
        // Step 2: Hash validation
        const currentBlobId = currentNote.data.blobId;
        if (currentBlobId !== expectedHash) {
          return {
            noteId,
            message: `CONFLICT: Note has been modified by another user. ` +
                     `Current blobId: ${currentBlobId}, expected: ${expectedHash}. ` +
                     `Please get the latest note content and retry.`,
            matchesFound: 0,
            replacementsMade: 0,
            revisionCreated: false,
            conflict: true,
            searchPattern,
            replacePattern,
            useRegex
          };
        }
    
        const noteType = currentNote.data.type;
        const originalContent = currentContent.data;
    
        // Step 3: Execute search and replace
        const { newContent, replacements } = executeSearchReplace(
          originalContent,
          searchPattern,
          replacePattern,
          useRegex,
          searchFlags
        );
    
        // Step 4: Handle no matches case
        if (replacements === 0) {
          return {
            noteId,
            message: `No matches found for pattern "${searchPattern}" in note ${noteId}. No changes made.`,
            matchesFound: 0,
            replacementsMade: 0,
            revisionCreated: false,
            conflict: false,
            searchPattern,
            replacePattern,
            useRegex
          };
        }
    
        // Step 5: Validate new content based on note type
        const validationResult = await validateContentForNoteType(
          newContent,
          noteType as NoteType,
          originalContent
        );
    
        if (!validationResult.valid) {
          return {
            noteId,
            message: `CONTENT_TYPE_MISMATCH: ${validationResult.error}`,
            matchesFound: replacements,
            replacementsMade: 0,
            revisionCreated: false,
            conflict: false,
            searchPattern,
            replacePattern,
            useRegex
          };
        }
    
        // Use validated/corrected content
        const finalContent = validationResult.content;
    
        // Step 6: Create revision if requested
        if (revision) {
          try {
            await axiosInstance.post(`/notes/${noteId}/revision`);
            revisionCreated = true;
          } catch (error) {
            console.error(`Warning: Failed to create revision for note ${noteId}:`, error);
            // Continue with update even if revision creation fails
          }
        }
    
        // Step 7: Update content
        const contentResponse = await axiosInstance.put(`/notes/${noteId}/content`, finalContent, {
          headers: {
            "Content-Type": "text/plain"
          }
        });
    
        if (contentResponse.status !== 204) {
          throw new Error(`Unexpected response status: ${contentResponse.status}`);
        }
    
        // Step 8: Return success response
        const correctionMsg = (finalContent !== newContent) ? " (content auto-corrected)" : "";
        const revisionMsg = revisionCreated ? " (revision created)" : " (no revision)";
    
        return {
          noteId,
          message: `Search and replace completed successfully for note ${noteId}. Found ${replacements} match(es) and made ${replacements} replacement(s).${correctionMsg}${revisionMsg}`,
          matchesFound: replacements,
          replacementsMade: replacements,
          revisionCreated,
          conflict: false,
          searchPattern,
          replacePattern,
          useRegex
        };
    
      } catch (error) {
        if ((error as any).response?.status === 404) {
          throw new Error(`Note ${noteId} not found`);
        }
        throw error;
      }
    }
  • Input schema definition for the search_and_replace_note tool, including parameters like noteId, searchPattern, replacePattern, useRegex, expectedHash, etc., with descriptions and validation rules.
      name: "search_and_replace_note",
      description: "Search and replace content within a single note. When someone wants to replace text in a note, first call get_note to get the current content and hash, then use this function to make the changes. This ensures you're working with the latest version of their note.",
      inputSchema: {
        type: "object",
        properties: {
          noteId: {
            type: "string",
            description: "ID of the note to perform search and replace on"
          },
          searchPattern: {
            type: "string",
            description: "What to search for in the note.",
          },
          replacePattern: {
            type: "string",
            description: "What to replace it with. For regex: supports patterns like '$1' for captured groups.",
          },
          useRegex: {
            type: "boolean",
            description: "Whether to use regex patterns (default: true).",
            default: true
          },
          searchFlags: {
            type: "string",
            description: "Search options. Defaults to 'gi' (global, case-insensitive). Remove 'i' for exact case matching.",
            default: "gi"
          },
          expectedHash: {
            type: "string",
            description: "⚠️ REQUIRED: Content hash from get_note response. Always get the note content first to obtain this hash.",
          },
          revision: {
            type: "boolean",
            description: "Whether to create a backup before replacing (default: true for safety).",
            default: true
          }
        },
        required: ["noteId", "searchPattern", "replacePattern", "expectedHash"]
      }
    },
  • src/index.ts:105-106 (registration)
    Registration of the search_and_replace_note tool in the MCP server's CallToolRequest handler switch statement, dispatching calls to the noteHandler's request handler function.
    case "search_and_replace_note":
      return await handleSearchReplaceNoteRequest(request.params.arguments, this.axiosInstance, this);
  • MCP-specific request handler for search_and_replace_note. Performs permission checks, validates input parameters, constructs the NoteOperation object, calls the core handleSearchReplaceNote function, and formats the response in MCP content format.
    export async function handleSearchReplaceNoteRequest(
      args: any,
      axiosInstance: any,
      permissionChecker: PermissionChecker
    ): Promise<{ content: Array<{ type: string; text: string }> }> {
      if (!permissionChecker.hasPermission("WRITE")) {
        throw new McpError(ErrorCode.InvalidRequest, "Permission denied: Not authorized to modify notes.");
      }
    
      // Validate that expectedHash is provided (required for data integrity)
      if (!args.expectedHash) {
        throw new McpError(
          ErrorCode.InvalidParams,
          "Missing required parameter 'expectedHash'. You must call get_note first to retrieve the current blobId (content hash) before performing search and replace. This ensures data integrity by preventing overwriting changes made by other users."
        );
      }
    
      // Validate required parameters
      if (!args.searchPattern) {
        throw new McpError(
          ErrorCode.InvalidParams,
          "Missing required parameter 'searchPattern'. The pattern to search for is required."
        );
      }
    
      if (!args.replacePattern) {
        throw new McpError(
          ErrorCode.InvalidParams,
          "Missing required parameter 'replacePattern'. The replacement pattern is required."
        );
      }
    
      try {
        const noteOperation: NoteOperation = {
          noteId: args.noteId,
          searchPattern: args.searchPattern,
          replacePattern: args.replacePattern,
          useRegex: args.useRegex !== false, // Default to true
          searchFlags: args.searchFlags || 'g',
          revision: args.revision !== false, // Default to true for safety
          expectedHash: args.expectedHash
        };
    
        const result = await handleSearchReplaceNote(noteOperation, axiosInstance);
    
        return {
          content: [{
            type: "text",
            text: result.message
          }]
        };
      } catch (error) {
        if (error instanceof McpError) {
          throw error;
        }
        throw new McpError(ErrorCode.InvalidParams, error instanceof Error ? error.message : String(error));
      }
    }
  • Helper function that executes the actual search and replace operation on note content using RegExp.replace, supporting both regex and escaped literal patterns.
    function executeSearchReplace(
      content: string,
      searchPattern: string,
      replacePattern: string,
      useRegex: boolean = true,
      flags: string = 'g'
    ): { newContent: string; replacements: number } {
      try {
        let newContent = content;
        let replacements = 0;
    
        if (useRegex) {
          // Regex-based replacement
          const regex = new RegExp(searchPattern, flags);
          replacements = (content.match(regex) || []).length;
          newContent = content.replace(regex, replacePattern);
        } else {
          // Literal string replacement
          const searchRegex = new RegExp(escapeRegExp(searchPattern), flags);
          replacements = (content.match(searchRegex) || []).length;
          newContent = content.replace(searchRegex, replacePattern);
        }
    
        return { newContent, replacements };
      } catch (error) {
        throw new Error(`Search and replace failed: ${error instanceof Error ? error.message : String(error)}`);
      }
    }
Behavior4/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes the mutation nature of the operation ('replace content'), specifies a safety mechanism ('create a backup before replacing' via the revision parameter), and outlines a concurrency control requirement ('expectedHash' to ensure working with the latest version). However, it doesn't mention potential error conditions or rate limits.

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 perfectly concise with two sentences that each serve distinct purposes: the first states the core functionality, and the second provides critical workflow guidance. There's no redundancy or unnecessary elaboration, and the information is front-loaded with the most important details.

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

Completeness4/5

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

For a mutation tool with 7 parameters, no annotations, and no output schema, the description does well by explaining the core operation, safety considerations, and required workflow. However, it doesn't describe what the tool returns or potential error cases, leaving some gaps in understanding the complete interaction context.

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?

With 100% schema description coverage, the baseline is 3. The description adds some context about the 'expectedHash' parameter ('Content hash from get_note response') and implies the workflow relationship, but doesn't provide additional semantic meaning beyond what's already documented in the comprehensive schema descriptions for all 7 parameters.

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 explicitly states the specific action ('search and replace content within a single note'), clearly identifying both the verb and resource. It distinguishes from siblings like 'update_note' by focusing on text replacement rather than general updates, and from 'search_notes' by operating on content within a single note rather than searching across notes.

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 provides explicit guidance on when to use this tool ('When someone wants to replace text in a note') and includes a clear prerequisite workflow ('first call get_note to get the current content and hash, then use this function'). It also distinguishes from alternatives by specifying this is for content replacement within a single note, not for other note operations.

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/tan-yong-sheng/triliumnext-mcp'

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