tool_check_contacts
List available contacts from the macOS Messages address book using a Python bridge for MCP, enabling streamlined interaction with the Messages app.
Instructions
List available contacts in the address book.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Input Schema (JSON Schema)
{
"properties": {},
"title": "tool_check_contactsArguments",
"type": "object"
}
Implementation Reference
- mac_messages_mcp/server.py:122-146 (handler)The main handler function for the 'tool_check_contacts' tool. It is registered via the @mcp.tool() decorator and retrieves a list of cached contacts from the address book, displaying the total count and a sample of the first 10 entries.@mcp.tool() def tool_check_contacts(ctx: Context) -> str: """ List available contacts in the address book. """ logger.info("Checking available contacts") try: contacts = get_cached_contacts() if not contacts: return "No contacts found in AddressBook." contact_count = len(contacts) sample_entries = list(contacts.items())[:10] # Show first 10 contacts formatted_samples = [f"{number} -> {name}" for number, name in sample_entries] result = [ f"Found {contact_count} contacts in AddressBook.", "Sample entries (first 10):", *formatted_samples ] return "\n".join(result) except Exception as e: logger.error(f"Error checking contacts: {str(e)}") return f"Error checking contacts: {str(e)}"
- mac_messages_mcp/messages.py:375-384 (helper)Helper function get_cached_contacts() that manages a time-based cache of contacts from the AddressBook database. Called by the tool handler to fetch the contacts mapping (phone -> name).def get_cached_contacts() -> Dict[str, str]: """Get cached contacts map or refresh if needed""" global _CONTACTS_CACHE, _LAST_CACHE_UPDATE current_time = time.time() if _CONTACTS_CACHE is None or (current_time - _LAST_CACHE_UPDATE) > _CACHE_TTL: _CONTACTS_CACHE = get_addressbook_contacts() _LAST_CACHE_UPDATE = current_time return _CONTACTS_CACHE