A Knative Function implementing a Model Context Protocol (MCP) server that provides integration with Ollama for local LLM interactions. This function exposes Ollama capabilities through standardized MCP tools, enabling the interaction with locally hosted language models.
The communication flow is as follows:
MCP client -> MCP Server (Function) -> Ollama Server
- Setup
ollamaserver usingollama serve - Run your function (MCP server)
- Connect using MCP client in
client/dir (python client.py)
This project implements an ASGI-based Knative function with the following key components:
- Function Class: Main ASGI application entry point (This is your base Function)
- MCPServer Class: FastMCP-based server implementing HTTP-streamable MCP protocol
- MCP Tools: Three primary tools for Ollama interaction:
list_models: Enumerate available models on the Ollama serverpull_model: Download and install new modelscall_model: Send prompts to models and receive responses
- Python 3.9 or higher
- Ollama server running locally or accessible via network
-
Install dependencies & setup env
# (optional) # setup venv python -m venv venv # and run it source venv/bin/activate # install deps pip install -e .
-
Start Ollama server:
# Install Ollama (if not already installed) curl -fsSL https://ollama.com/install.sh | sh # Start Ollama service (in bg or different terminal) ollama serve # Pull a model (optional, can be done via MCP tool) ollama pull llama3.2:3b
Now you have a running Ollama Server.
- Run the function locally:
# Using func CLI func run --builder=host
Now you have a running MCP Server which has access to the Ollama server.
- Run MCP client
# In client/ directory python client.py
Now you connect via MCP protocol to the running function, which will call a tool
call_model which will invoke a request from the LLM running on Ollama server.
Edit the client/client.py file to change any requests for the MCP server.
Edit the function/func.py file to edit the MCP server and its tools etc.
# Deploy to cluster with Knative
func deploy
# Or build and deploy with custom image
func deploy --image your-registry/mcp-ollama-functionConnection Issues:
- Ensure Ollama server is running and accessible
- Check firewall settings for port 11434 (Ollama default)
- Verify model availability with
ollama list - Confirm function is running on expected port (default: 8080)
Performance Considerations:
- Model loading time varies by size (3B models ~2-5s, 7B+ models 10-30s)
- Consider pre-loading frequently used models
- Monitor memory usage for large models
- mcp: Model Context Protocol implementation
- ollama: Python client for Ollama API
- httpx: Async HTTP client for external requests
- pytest/pytest-asyncio: Testing framework with async support