Skip to content
The Chat Archive
sonar-pro-high· Perplexity LMArena Search Arena 24K

How do i create a tool in mcp(model context protocol) which can search web using SERP…

  1. User

    How do i create a tool in mcp(model context protocol) which can search web using SERP api?

    My goal is to use it from mcp client. Research and tell me in details

  2. Assistant

    Overview

    To create a tool in the Model Context Protocol (MCP) that searches the web using the SERP API, you need to build an MCP server that exposes a "search" tool. This tool will interact with the SERP API to fetch search results and return them in a structured format. You can then connect this server to any MCP-compatible client (such as Claude Desktop, Cursor, or your own custom client) and invoke the tool via natural language or programmatic queries.

    Below is a detailed, step-by-step guide for building, configuring, and using such a tool.


    Step 1: Prerequisites

    • Python 3.8+ (or Node.js if you prefer JavaScript)
    • SERP API key (sign up at SerpApi to get one)
    • MCP Python SDK (pip install mcp mcp[cli])
    • SERP API Python client (pip install google-search-results)
    • Basic familiarity with Python and virtual environments

    Step 2: Environment Setup

    1. Create and activate a virtual environment:

      python -m venv mcp-serpapi
      source mcp-serpapi/bin/activate  # On Windows: .\mcp-serpapi\Scripts\activate
      
    2. Install dependencies:

      pip install mcp mcp[cli] google-search-results python-dotenv
      
    3. Create a .env file in your project directory:

      SERPAPI_API_KEY=your_serpapi_key_here
      

    Step 3: Implement the MCP Server with SERP API Tool

    Create a file named serpapi_mcp_server.py:

    import os
    from mcp.server.fastmcp import FastMCP
    from serpapi import GoogleSearch
    from dotenv import load_dotenv
    
    load_dotenv()
    SERPAPI_API_KEY = os.getenv("SERPAPI_API_KEY")
    
    mcp = FastMCP("SERP API Web Search")
    
    @mcp.tool()
    def search(query: str, engine: str = "google", location: str = "United States", num: int = 5) -> dict:
        """
        Perform a web search using SerpApi.
        Args:
            query: The search query string.
            engine: Search engine to use (e.g., 'google', 'bing', etc.).
            location: Location for search results.
            num: Number of results to return.
        Returns:
            Dictionary containing search results.
        """
        params = {
            "q": query,
            "engine": engine,
            "location": location,
            "num": num,
            "api_key": SERPAPI_API_KEY
        }
        search = GoogleSearch(params)
        results = search.get_dict()
        # Extract top results
        organic_results = results.get("organic_results", [])
        return {
            "results": [
                {
                    "title": r.get("title"),
                    "link": r.get("link"),
                    "snippet": r.get("snippet")
                }
                for r in organic_results[:num]
            ]
        }
    
    if __name__ == "__main__":
        mcp.run(transport="stdio")
    
    • This registers a search tool with the MCP server, which takes a query and optional parameters, calls the SERP API, and returns structured results[6][11][12].

    Step 4: Test the MCP Server

    • Run the server locally:

      python serpapi_mcp_server.py
      
    • (Optional) Use MCP Inspector for testing:

      mcp dev serpapi_mcp_server.py
      
      • Open the provided localhost URL in your browser to interactively test the tool[2][9].

    Step 5: Integrate with an MCP Client

    For Claude Desktop or Cursor

    1. Add your server to the client configuration:

      • For Claude Desktop: Edit claude_desktop_config.json and add:
        "mcpServers": {
          "serpapi_web_search": {
            "command": "python",
            "args": ["/absolute/path/to/serpapi_mcp_server.py"]
          }
        }
        
      • For Cursor: Edit mcp.json similarly.
    2. Restart the client to load the new server.

    3. Interact via the client UI:

      • Use prompts like “Search for the latest news on AI regulation.”
      • The client will detect and call the search tool, display results, and allow further interaction[7][8][11].

    Step 6: Usage Example

    • From the MCP client, you can now issue a query:
      Search for "best Python web frameworks in 2025"
      
    • The client will invoke the search tool, which calls SERP API and returns the top results as structured data.

    Additional Notes

    • Customization: You can add more parameters (e.g., image search, news search) by extending the tool’s input schema and SERP API parameters[6][12].
    • Supported Engines: SERP API supports Google, Bing, Yahoo, Baidu, Yandex, DuckDuckGo, and more[11].
    • Security: Never hardcode your API key; always use environment variables.
    • Rate Limits: Be aware of SERP API’s rate limits and pricing.

    Summary Table

    StepAction
    1. PrerequisitesPython, SERP API key, MCP SDK, SerpApi client
    2. SetupVirtualenv, install dependencies, set .env
    3. Server CodeImplement MCP server with search tool using SerpApi
    4. TestRun server, test with MCP Inspector
    5. Integrate ClientAdd to Claude Desktop/Cursor config, restart client
    6. UseIssue search queries from client, receive structured results

    References for Further Reading

    • Official MCP Documentation
    • [SerpApi Python Integration Guide][6]
    • [Example SerpApi MCP Server][11][12]
    • [MCP Inspector and Client Integration][2][7][8]

    By following these steps, you will have a fully functional MCP tool that performs web searches using the SERP API and can be invoked from any MCP-compatible client. This approach is extensible, production-ready, and leverages the latest standards for LLM tool integration.

  3. User

    This is a command Set-SPOTenant -ExtendPermissionsToUnprotectedFiles $true

    which enabled sharepoint extended permission on tenant, can the same be done via csom appraoch? Is there any pointer for this?

  4. Assistant

    The PowerShell command

    Set-SPOTenant -ExtendPermissionsToUnprotectedFiles $true
    

    enables the SharePoint Online tenant setting that extends permissions to unprotected files, which is a prerequisite for certain sensitivity label features in SharePoint[1][9]. This is a tenant-level configuration and is only available via the SharePoint Online Management Shell or compatible PowerShell modules.

    Can This Be Done via CSOM?

    No, this specific tenant-level setting cannot currently be set via CSOM (Client-Side Object Model).
    Here’s why:

    • The Set-SPOTenant cmdlet and its parameters (including -ExtendPermissionsToUnprotectedFiles) are exposed only through the SharePoint Online Management Shell (PowerShell) and not through the CSOM API surface[2][8].
    • The CSOM API is primarily designed for site, list, library, and item-level operations, not for setting global tenant properties.
    • There is no public documentation or community reference indicating that the ExtendPermissionsToUnprotectedFiles property is exposed in the CSOM object model or via REST endpoints as of April 2025[2][8].

    Pointers and Alternatives

    • PnP PowerShell: The Set-PnPTenant[8] cmdlet exposes many tenant-level settings, including -ExtendPermissionsToUnprotectedFiles. This is still PowerShell-based, not CSOM[8].
    • Graph API: As of now, Microsoft Graph does not expose this specific SharePoint tenant property.
    • Automation: If you need to automate this setting from code, you must invoke PowerShell scripts from your application (for example, using PowerShell automation from C# or another language), but you cannot set it directly via CSOM.

    Summary Table

    MethodCan Set ExtendPermissionsToUnprotectedFiles?Notes
    SharePoint PowerShellYesOfficial, supported method
    PnP PowerShellYesSupported, also PowerShell-based
    CSOM (C# or PowerShell)NoNot exposed in CSOM object model
    REST APINoNot available
    Microsoft GraphNoNot available

    References

    • [Microsoft Docs: Configure SharePoint with a sensitivity label to extend permissions][1][9]
    • [Set-SPOTenant documentation][2]
    • [Set-PnPTenant documentation][8]

    Conclusion:
    You must use PowerShell (Set-SPOTenant or Set-PnPTenant) to set -ExtendPermissionsToUnprotectedFiles. There is no CSOM or REST API equivalent as of now[2][8].

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.