|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.11" |
| 3 | +# dependencies = [ |
| 4 | +# "mcp", |
| 5 | +# "rich", |
| 6 | +# "ollama", |
| 7 | +# ] |
| 8 | +# /// |
| 9 | +""" |
| 10 | +MCP stdio server exposing Ollama web_search and web_fetch as tools. |
| 11 | +
|
| 12 | +Environment: |
| 13 | +- OLLAMA_API_KEY (required): if set, will be used as Authorization header. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import asyncio |
| 19 | +from typing import Any, Dict |
| 20 | + |
| 21 | +from ollama import Client |
| 22 | + |
| 23 | +try: |
| 24 | + # Preferred high-level API (if available) |
| 25 | + from mcp.server.fastmcp import FastMCP # type: ignore |
| 26 | + |
| 27 | + _FASTMCP_AVAILABLE = True |
| 28 | +except Exception: |
| 29 | + _FASTMCP_AVAILABLE = False |
| 30 | + |
| 31 | +if not _FASTMCP_AVAILABLE: |
| 32 | + # Fallback to the low-level stdio server API |
| 33 | + from mcp.server import Server # type: ignore |
| 34 | + from mcp.server.stdio import stdio_server # type: ignore |
| 35 | + |
| 36 | + |
| 37 | +client = Client() |
| 38 | + |
| 39 | + |
| 40 | +def _web_search_impl(query: str, max_results: int = 3) -> Dict[str, Any]: |
| 41 | + res = client.web_search(query=query, max_results=max_results) |
| 42 | + return res.model_dump() |
| 43 | + |
| 44 | + |
| 45 | +def _web_fetch_impl(url: str) -> Dict[str, Any]: |
| 46 | + res = client.web_fetch(url=url) |
| 47 | + return res.model_dump() |
| 48 | + |
| 49 | + |
| 50 | +if _FASTMCP_AVAILABLE: |
| 51 | + app = FastMCP('ollama-search-fetch') |
| 52 | + |
| 53 | + @app.tool() |
| 54 | + def web_search(query: str, max_results: int = 3) -> Dict[str, Any]: |
| 55 | + """ |
| 56 | + Perform a web search using Ollama's hosted search API. |
| 57 | +
|
| 58 | + Args: |
| 59 | + query: The search query to run. |
| 60 | + max_results: Maximum results to return (default: 3). |
| 61 | +
|
| 62 | + Returns: |
| 63 | + JSON-serializable dict matching ollama.WebSearchResponse.model_dump() |
| 64 | + """ |
| 65 | + |
| 66 | + return _web_search_impl(query=query, max_results=max_results) |
| 67 | + |
| 68 | + @app.tool() |
| 69 | + def web_fetch(url: str) -> Dict[str, Any]: |
| 70 | + """ |
| 71 | + Fetch the content of a web page for the provided URL. |
| 72 | +
|
| 73 | + Args: |
| 74 | + url: The absolute URL to fetch. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + JSON-serializable dict matching ollama.WebFetchResponse.model_dump() |
| 78 | + """ |
| 79 | + |
| 80 | + return _web_fetch_impl(url=url) |
| 81 | + |
| 82 | + if __name__ == '__main__': |
| 83 | + app.run() |
| 84 | + |
| 85 | +else: |
| 86 | + server = Server('ollama-search-fetch') # type: ignore[name-defined] |
| 87 | + |
| 88 | + @server.tool() # type: ignore[attr-defined] |
| 89 | + async def web_search(query: str, max_results: int = 3) -> Dict[str, Any]: |
| 90 | + """ |
| 91 | + Perform a web search using Ollama's hosted search API. |
| 92 | +
|
| 93 | + Args: |
| 94 | + query: The search query to run. |
| 95 | + max_results: Maximum results to return (default: 3). |
| 96 | + """ |
| 97 | + |
| 98 | + return await asyncio.to_thread(_web_search_impl, query, max_results) |
| 99 | + |
| 100 | + @server.tool() # type: ignore[attr-defined] |
| 101 | + async def web_fetch(url: str) -> Dict[str, Any]: |
| 102 | + """ |
| 103 | + Fetch the content of a web page for the provided URL. |
| 104 | +
|
| 105 | + Args: |
| 106 | + url: The absolute URL to fetch. |
| 107 | + """ |
| 108 | + |
| 109 | + return await asyncio.to_thread(_web_fetch_impl, url) |
| 110 | + |
| 111 | + async def _main() -> None: |
| 112 | + async with stdio_server() as (read, write): # type: ignore[name-defined] |
| 113 | + await server.run(read, write) # type: ignore[attr-defined] |
| 114 | + |
| 115 | + if __name__ == '__main__': |
| 116 | + asyncio.run(_main()) |
0 commit comments