Skip to main content
Glama
RayanZaki

MCP Google Contacts Server

by RayanZaki

search_contacts

Find contacts in Google Contacts by searching names, emails, or phone numbers with a query and optional result limit.

Instructions

Search contacts by name, email, or phone number.

    Args:
        query: Search term to find in contacts
        max_results: Maximum number of results to return (default: 10)
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYes
max_resultsNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The main handler function for the 'search_contacts' MCP tool. Decorated with @mcp.tool(), it initializes the Google Contacts service, lists contacts, performs case-insensitive substring search across displayName, givenName, familyName, email, and phone fields, limits results, and formats output using format_contacts_list.
    @mcp.tool()
    async def search_contacts(query: str, max_results: int = 10) -> str:
        """Search contacts by name, email, or phone number.
        
        Args:
            query: Search term to find in contacts
            max_results: Maximum number of results to return (default: 10)
        """
        service = init_service()
        if not service:
            return "Error: Google Contacts service is not available. Please check your credentials."
        
        try:
            # Get all contacts and filter locally with more flexible search
            all_contacts = service.list_contacts(max_results=max(100, max_results*2))
            
            query = query.lower()
            matches = []
            
            for contact in all_contacts:
                if (query in contact.get('displayName', '').lower() or
                    query in contact.get('givenName', '').lower() or
                    query in contact.get('familyName', '').lower() or
                    query in str(contact.get('email', '')).lower() or
                    query in str(contact.get('phone', '')).lower()):
                    matches.append(contact)
                    
                if len(matches) >= max_results:
                    break
                    
            if not matches:
                return f"No contacts found matching '{query}'."
                
            return f"Search results for '{query}':\n\n{format_contacts_list(matches)}"
        except Exception as e:
            return f"Error: Failed to search contacts - {str(e)}"
  • In the main server entrypoint, creates the FastMCP server instance and calls register_tools(mcp), which defines and registers the search_contacts tool (along with others) using the @mcp.tool() decorator.
    mcp = FastMCP("google-contacts")
    
    # Register all tools
    register_tools(mcp)
  • Global helper function init_service() used by search_contacts (and all tools) to lazily initialize the GoogleContactsService instance from env vars or credential files.
    def init_service() -> Optional[GoogleContactsService]:
        """Initialize and return a Google Contacts service instance.
        
        Returns:
            GoogleContactsService instance or None if initialization fails
        """
        global contacts_service
        
        if contacts_service:
            return contacts_service
        
        try:
            # First try environment variables
            try:
                contacts_service = GoogleContactsService.from_env()
                print("Successfully loaded credentials from environment variables.")
                return contacts_service
            except GoogleContactsError:
                pass
                
            # Then try default file locations
            for path in config.credentials_paths:
                if path.exists():
                    try:
                        print(f"Found credentials file at {path}")
                        contacts_service = GoogleContactsService.from_file(path)
                        print("Successfully loaded credentials from file.")
                        return contacts_service
                    except GoogleContactsError as e:
                        print(f"Error with credentials at {path}: {e}")
                        continue
                    
            print("No valid credentials found. Please provide credentials to use Google Contacts.")
            return None
            
        except Exception as e:
            print(f"Error initializing Google Contacts service: {str(e)}")
            traceback.print_exc()
            return None
  • The GoogleContactsService.list_contacts() method called by search_contacts to fetch a batch of contacts for local filtering.
    def list_contacts(self, name_filter: Optional[str] = None, 
                     max_results: int = None) -> List[Dict[str, Any]]:
        """List contacts, optionally filtering by name.
        
        Args:
            name_filter: Optional filter to find contacts by name
            max_results: Maximum number of results to return
            
        Returns:
            List of contact dictionaries
            
        Raises:
            GoogleContactsError: If API request fails
        """
        max_results = max_results or config.default_max_results
        
        try:
            # Get list of connections (contacts)
            results = self.service.people().connections().list(
                resourceName='people/me',
                pageSize=max_results,
                personFields='names,emailAddresses,phoneNumbers',
                sortOrder='FIRST_NAME_ASCENDING'
            ).execute()
            
            connections = results.get('connections', [])
            
            if not connections:
                return []
            
            contacts = []
            for person in connections:
                names = person.get('names', [])
                if not names:
                    continue
                
                name = names[0]
                given_name = name.get('givenName', '')
                family_name = name.get('familyName', '')
                display_name = name.get('displayName', '')
                
                # Apply name filter if provided
                if name_filter and name_filter.lower() not in display_name.lower():
                    continue
                
                # Get email addresses
                emails = person.get('emailAddresses', [])
                email = emails[0].get('value') if emails else None
                
                # Get phone numbers
                phones = person.get('phoneNumbers', [])
                phone = phones[0].get('value') if phones else None
                
                contacts.append({
                    'resourceName': person.get('resourceName'),
                    'givenName': given_name,
                    'familyName': family_name,
                    'displayName': display_name,
                    'email': email,
                    'phone': phone
                })
            
            return contacts
        
        except HttpError as error:
            raise GoogleContactsError(f"Error listing contacts: {error}")
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool searches contacts but doesn't mention important behavioral aspects like whether it's read-only, requires authentication, has rate limits, returns paginated results, or what happens on errors. For a search tool with zero annotation coverage, this leaves significant gaps.

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 appropriately sized and front-loaded with the core purpose in the first sentence. The parameter explanations are brief and directly relevant. There's no wasted text, and the structure is clear and efficient.

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

Completeness3/5

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

Given that there's an output schema (which handles return values), the description covers the basic purpose and parameters adequately. However, for a search tool with no annotations and multiple sibling tools, it lacks context about behavioral traits and usage differentiation, making it only minimally complete.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds meaningful context beyond the input schema. While schema description coverage is 0%, the description explains that 'query' searches 'by name, email, or phone number' and that 'max_results' has a default of 10. This compensates well for the lack of schema descriptions, though it doesn't cover all possible parameter nuances.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Search contacts by name, email, or phone number.' This specifies the verb (search), resource (contacts), and searchable fields. However, it doesn't explicitly differentiate from sibling tools like 'list_contacts' or 'search_directory', which would be needed for a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'list_contacts', 'get_contact', or 'search_directory'. It mentions what the tool does but gives no context about appropriate use cases, prerequisites, or exclusions.

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/RayanZaki/mcp-google-contacts-server'

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