search_messages
Find specific text across all iMessage conversations by searching message history with a query string.
Instructions
Search across all iMessage conversations for messages containing a query string.
Args: query: Text to search for (case-insensitive) limit: Max results to return (default 30)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| limit | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- server.py:142-187 (handler)Implementation of the search_messages tool, which queries the iMessage SQLite database for messages containing a specific query string.
@mcp.tool() async def search_messages( query: str, limit: int = 30, ) -> str: """Search across all iMessage conversations for messages containing a query string. Args: query: Text to search for (case-insensitive) limit: Max results to return (default 30) """ db = _get_db() rows = db.execute( """ SELECT m.text, m.date as apple_date, m.is_from_me, COALESCE(h.id, 'me') as sender, c.chat_identifier, c.display_name FROM message m LEFT JOIN handle h ON h.ROWID = m.handle_id LEFT JOIN chat_message_join cmj ON cmj.message_id = m.ROWID LEFT JOIN chat c ON c.ROWID = cmj.chat_id WHERE m.text LIKE '%' || ? || '%' ORDER BY m.date DESC LIMIT ? """, (query, limit), ).fetchall() db.close() results = [] for r in rows: results.append( { "text": r["text"], "date": _apple_ts_to_iso(r["apple_date"]), "from_me": bool(r["is_from_me"]), "sender": r["sender"] if not r["is_from_me"] else "me", "chat_identifier": r["chat_identifier"] or "", "chat_name": r["display_name"] or "", } ) return json.dumps(results, indent=2)