How do i create a tool in mcp(model context protocol) which can search web using SERP…
- 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
- 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
-
Create and activate a virtual environment:
python -m venv mcp-serpapi source mcp-serpapi/bin/activate # On Windows: .\mcp-serpapi\Scripts\activate -
Install dependencies:
pip install mcp mcp[cli] google-search-results python-dotenv -
Create a
.envfile 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
searchtool 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
-
Add your server to the client configuration:
- For Claude Desktop: Edit
claude_desktop_config.jsonand add:"mcpServers": { "serpapi_web_search": { "command": "python", "args": ["/absolute/path/to/serpapi_mcp_server.py"] } } - For Cursor: Edit
mcp.jsonsimilarly.
- For Claude Desktop: Edit
-
Restart the client to load the new server.
-
Interact via the client UI:
- Use prompts like “Search for the latest news on AI regulation.”
- The client will detect and call the
searchtool, 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
searchtool, 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
Step Action 1. Prerequisites Python, SERP API key, MCP SDK, SerpApi client 2. Setup Virtualenv, install dependencies, set .env3. Server Code Implement MCP server with searchtool using SerpApi4. Test Run server, test with MCP Inspector 5. Integrate Client Add to Claude Desktop/Cursor config, restart client 6. Use Issue 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.
- 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?
- Assistant
The PowerShell command
Set-SPOTenant -ExtendPermissionsToUnprotectedFiles $trueenables 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-SPOTenantcmdlet 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
ExtendPermissionsToUnprotectedFilesproperty 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
Method Can Set ExtendPermissionsToUnprotectedFiles? Notes SharePoint PowerShell Yes Official, supported method PnP PowerShell Yes Supported, also PowerShell-based CSOM (C# or PowerShell) No Not exposed in CSOM object model REST API No Not available Microsoft Graph No Not 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-SPOTenantorSet-PnPTenant) to set-ExtendPermissionsToUnprotectedFiles. There is no CSOM or REST API equivalent as of now[2][8]. - The
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.