11"""Search tools for Basic Memory MCP server."""
22
3- from typing import Optional , List
3+ from typing import List , Optional
4+
45from loguru import logger
56
7+ from basic_memory .mcp .async_client import client
68from basic_memory .mcp .server import mcp
79from basic_memory .mcp .tools .utils import call_post
8- from basic_memory .schemas .search import SearchQuery , SearchResponse , SearchItemType
9- from basic_memory .mcp .async_client import client
10+ from basic_memory .schemas .search import SearchItemType , SearchQuery , SearchResponse
1011
1112
1213@mcp .tool (
1314 description = "Search across all content in the knowledge base." ,
1415)
1516async def search_notes (
16- query : str ,
17- page : int = 1 ,
17+ query : str ,
18+ page : int = 1 ,
1819 page_size : int = 10 ,
1920 search_type : str = "text" ,
2021 types : Optional [List [str ]] = None ,
@@ -31,9 +32,9 @@ async def search_notes(
3132 query: The search query string
3233 page: The page number of results to return (default 1)
3334 page_size: The number of results to return per page (default 10)
34- search_type: Type of search to perform, one of: "text", "title", "permalink", "permalink_match" (default: "text")
35- types: Optional list of content types to search (e.g., ["entity ", "observation "])
36- entity_types: Optional list of entity types to filter by (e.g., ["note ", "person "])
35+ search_type: Type of search to perform, one of: "text", "title", "permalink" (default: "text")
36+ types: Optional list of note types to search (e.g., ["note ", "person "])
37+ entity_types: Optional list of entity types to filter by (e.g., ["entity ", "observation "])
3738 after_date: Optional date filter for recent content (e.g., "1 week", "2d")
3839
3940 Returns:
@@ -61,6 +62,12 @@ async def search_notes(
6162 types=["entity"],
6263 )
6364
65+ # Search with entity type filter, e.g., note vs
66+ results = await search_notes(
67+ query="meeting notes",
68+ types=["entity"],
69+ )
70+
6471 # Search for recent content
6572 results = await search_notes(
6673 query="bug report",
@@ -70,32 +77,32 @@ async def search_notes(
7077 # Pattern matching on permalinks
7178 results = await search_notes(
7279 query="docs/meeting-*",
73- search_type="permalink_match "
80+ search_type="permalink "
7481 )
7582 """
7683 # Create a SearchQuery object based on the parameters
7784 search_query = SearchQuery ()
78-
85+
7986 # Set the appropriate search field based on search_type
8087 if search_type == "text" :
8188 search_query .text = query
8289 elif search_type == "title" :
8390 search_query .title = query
91+ elif search_type == "permalink" and "*" in query :
92+ search_query .permalink_match = query
8493 elif search_type == "permalink" :
8594 search_query .permalink = query
86- elif search_type == "permalink_match" :
87- search_query .permalink_match = query
8895 else :
8996 search_query .text = query # Default to text search
90-
97+
9198 # Add optional filters if provided
92- if types :
93- search_query .types = [SearchItemType (t ) for t in types ]
9499 if entity_types :
95- search_query .entity_types = entity_types
100+ search_query .entity_types = [SearchItemType (t ) for t in entity_types ]
101+ if types :
102+ search_query .types = types
96103 if after_date :
97104 search_query .after_date = after_date
98-
105+
99106 logger .info (f"Searching for { search_query } " )
100107 response = await call_post (
101108 client ,
0 commit comments