- 
                Notifications
    You must be signed in to change notification settings 
- Fork 847
examples: add mcp server for web_search web_crawl #585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9a4b946
              
                mcp
              
              
                ParthSareen 5e54b42
              
                update mcp server
              
              
                ParthSareen 43090cf
              
                update readme
              
              
                ParthSareen 9dbdcdf
              
                update docs
              
              
                ParthSareen c5bbd45
              
                lint
              
              
                ParthSareen 0e5b69b
              
                Update examples/README.md
              
              
                ParthSareen 533e823
              
                cleanup
              
              
                ParthSareen 2328bb6
              
                Update examples/README.md
              
              
                ParthSareen 12ce9e3
              
                update readme
              
              
                ParthSareen a11e9b4
              
                api key update
              
              
                ParthSareen File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,80 +1,124 @@ | ||
| # Running Examples | ||
|  | ||
| Run the examples in this directory with: | ||
|  | ||
| ```sh | ||
| # Run example | ||
| python3 examples/<example>.py | ||
|  | ||
| # or with uv | ||
| uv run examples/<example>.py | ||
| ``` | ||
|  | ||
| See [ollama/docs/api.md](https://github.com/ollama/ollama/blob/main/docs/api.md) for full API documentation | ||
|  | ||
| ### Chat - Chat with a model | ||
|  | ||
| - [chat.py](chat.py) | ||
| - [async-chat.py](async-chat.py) | ||
| - [chat-stream.py](chat-stream.py) - Streamed outputs | ||
| - [chat-with-history.py](chat-with-history.py) - Chat with model and maintain history of the conversation | ||
|  | ||
|  | ||
| ### Generate - Generate text with a model | ||
|  | ||
| - [generate.py](generate.py) | ||
| - [async-generate.py](async-generate.py) | ||
| - [generate-stream.py](generate-stream.py) - Streamed outputs | ||
| - [fill-in-middle.py](fill-in-middle.py) - Given a prefix and suffix, fill in the middle | ||
|  | ||
|  | ||
| ### Tools/Function Calling - Call a function with a model | ||
|  | ||
| - [tools.py](tools.py) - Simple example of Tools/Function Calling | ||
| - [async-tools.py](async-tools.py) | ||
| - [multi-tool.py](multi-tool.py) - Using multiple tools, with thinking enabled | ||
|  | ||
| #### gpt-oss | ||
| #### gpt-oss | ||
|  | ||
| - [gpt-oss-tools.py](gpt-oss-tools.py) | ||
| - [gpt-oss-tools-stream.py](gpt-oss-tools-stream.py) | ||
| - [gpt-oss-tools-stream.py](gpt-oss-tools-stream.py) | ||
| - [gpt-oss-tools-browser.py](gpt-oss-tools-browser.py) - Using browser research tools with gpt-oss | ||
| - [gpt-oss-tools-browser-stream.py](gpt-oss-tools-browser-stream.py) - Using browser research tools with gpt-oss, with streaming enabled | ||
|  | ||
| ### Web search | ||
|  | ||
| An API key from Ollama's cloud service is required. You can create one [here](https://ollama.com/settings/keys). | ||
|  | ||
| ```shell | ||
| export OLLAMA_API_KEY="your_api_key_here" | ||
| ``` | ||
|  | ||
| - [web-search.py](web-search.py) | ||
|  | ||
| #### MCP server | ||
|  | ||
| The MCP server can be used with an MCP client like Cursor, Cline, Codex, Open WebUI, Goose, and more. | ||
|  | ||
| ```sh | ||
| uv run examples/web-search-mcp.py | ||
| ``` | ||
|  | ||
| Configuration to use with an MCP client: | ||
|  | ||
| ```json | ||
| { | ||
| "mcpServers": { | ||
| "web_search": { | ||
| "type": "stdio", | ||
| "command": "uv", | ||
| "args": ["run", "path/to/ollama-python/examples/web-search-mcp.py"], | ||
| "env": { "OLLAMA_API_KEY": "your_api_key_here" } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|  | ||
| - [web-search-mcp.py](web-search-mcp.py) | ||
|  | ||
| ### Multimodal with Images - Chat with a multimodal (image chat) model | ||
|  | ||
| - [multimodal-chat.py](multimodal-chat.py) | ||
| - [multimodal-generate.py](multimodal-generate.py) | ||
|  | ||
|  | ||
| ### Structured Outputs - Generate structured outputs with a model | ||
|  | ||
| - [structured-outputs.py](structured-outputs.py) | ||
| - [async-structured-outputs.py](async-structured-outputs.py) | ||
| - [structured-outputs-image.py](structured-outputs-image.py) | ||
|  | ||
|  | ||
| ### Ollama List - List all downloaded models and their properties | ||
| - [list.py](list.py) | ||
|  | ||
| - [list.py](list.py) | ||
|  | ||
| ### Ollama Show - Display model properties and capabilities | ||
| - [show.py](show.py) | ||
|  | ||
| - [show.py](show.py) | ||
|  | ||
| ### Ollama ps - Show model status with CPU/GPU usage | ||
| - [ps.py](ps.py) | ||
|  | ||
| - [ps.py](ps.py) | ||
|  | ||
| ### Ollama Pull - Pull a model from Ollama | ||
|  | ||
| Requirement: `pip install tqdm` | ||
| - [pull.py](pull.py) | ||
|  | ||
| - [pull.py](pull.py) | ||
|  | ||
| ### Ollama Create - Create a model from a Modelfile | ||
| - [create.py](create.py) | ||
|  | ||
| - [create.py](create.py) | ||
|  | ||
| ### Ollama Embed - Generate embeddings with a model | ||
| - [embed.py](embed.py) | ||
|  | ||
| - [embed.py](embed.py) | ||
|  | ||
| ### Thinking - Enable thinking mode for a model | ||
|  | ||
| - [thinking.py](thinking.py) | ||
|  | ||
| ### Thinking (generate) - Enable thinking mode for a model | ||
|  | ||
| - [thinking-generate.py](thinking-generate.py) | ||
|  | ||
| ### Thinking (levels) - Choose the thinking level | ||
|  | ||
| - [thinking-levels.py](thinking-levels.py) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # /// script | ||
| # requires-python = ">=3.11" | ||
| # dependencies = [ | ||
| # "mcp", | ||
| # "rich", | ||
| # "ollama", | ||
| # ] | ||
| # /// | ||
| """ | ||
| MCP stdio server exposing Ollama web_search and web_fetch as tools. | ||
|  | ||
| Environment: | ||
| - OLLAMA_API_KEY (required): if set, will be used as Authorization header. | ||
| """ | ||
|  | ||
| from __future__ import annotations | ||
|  | ||
| import asyncio | ||
| from typing import Any, Dict | ||
|  | ||
| from ollama import Client | ||
|  | ||
| try: | ||
| # Preferred high-level API (if available) | ||
| from mcp.server.fastmcp import FastMCP # type: ignore | ||
|  | ||
| _FASTMCP_AVAILABLE = True | ||
| except Exception: | ||
| _FASTMCP_AVAILABLE = False | ||
|  | ||
| if not _FASTMCP_AVAILABLE: | ||
| # Fallback to the low-level stdio server API | ||
| from mcp.server import Server # type: ignore | ||
| from mcp.server.stdio import stdio_server # type: ignore | ||
|  | ||
|  | ||
| client = Client() | ||
|  | ||
|  | ||
| def _web_search_impl(query: str, max_results: int = 3) -> Dict[str, Any]: | ||
| res = client.web_search(query=query, max_results=max_results) | ||
| return res.model_dump() | ||
|  | ||
|  | ||
| def _web_fetch_impl(url: str) -> Dict[str, Any]: | ||
| res = client.web_fetch(url=url) | ||
| return res.model_dump() | ||
|  | ||
|  | ||
| if _FASTMCP_AVAILABLE: | ||
| app = FastMCP('ollama-search-fetch') | ||
|  | ||
| @app.tool() | ||
| def web_search(query: str, max_results: int = 3) -> Dict[str, Any]: | ||
| """ | ||
| Perform a web search using Ollama's hosted search API. | ||
|  | ||
| Args: | ||
| query: The search query to run. | ||
| max_results: Maximum results to return (default: 3). | ||
|  | ||
| Returns: | ||
| JSON-serializable dict matching ollama.WebSearchResponse.model_dump() | ||
| """ | ||
|  | ||
| return _web_search_impl(query=query, max_results=max_results) | ||
|  | ||
| @app.tool() | ||
| def web_fetch(url: str) -> Dict[str, Any]: | ||
| """ | ||
| Fetch the content of a web page for the provided URL. | ||
|  | ||
| Args: | ||
| url: The absolute URL to fetch. | ||
|  | ||
| Returns: | ||
| JSON-serializable dict matching ollama.WebFetchResponse.model_dump() | ||
| """ | ||
|  | ||
| return _web_fetch_impl(url=url) | ||
|  | ||
| if __name__ == '__main__': | ||
| app.run() | ||
|  | ||
| else: | ||
| server = Server('ollama-search-fetch') # type: ignore[name-defined] | ||
|  | ||
| @server.tool() # type: ignore[attr-defined] | ||
| async def web_search(query: str, max_results: int = 3) -> Dict[str, Any]: | ||
| """ | ||
| Perform a web search using Ollama's hosted search API. | ||
|  | ||
| Args: | ||
| query: The search query to run. | ||
| max_results: Maximum results to return (default: 3). | ||
| """ | ||
|  | ||
| return await asyncio.to_thread(_web_search_impl, query, max_results) | ||
|  | ||
| @server.tool() # type: ignore[attr-defined] | ||
| async def web_fetch(url: str) -> Dict[str, Any]: | ||
| """ | ||
| Fetch the content of a web page for the provided URL. | ||
|  | ||
| Args: | ||
| url: The absolute URL to fetch. | ||
| """ | ||
|  | ||
| return await asyncio.to_thread(_web_fetch_impl, url) | ||
|  | ||
| async def _main() -> None: | ||
| async with stdio_server() as (read, write): # type: ignore[name-defined] | ||
| await server.run(read, write) # type: ignore[attr-defined] | ||
|  | ||
| if __name__ == '__main__': | ||
| asyncio.run(_main()) | 
            File renamed without changes.
          
    
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a small note on what this example is for:
For tools that act as MCP clients (e.g. Cursor, example 2, example 3) web search can be accessed via an mcp server: