Learn / Local AI & Tooling / Module 02
Local AI & Tooling · Module 02
Running Local Models with Ollama and Connecting Tools via MCP
Run open-weights language and embedding models privately on your own hardware with Ollama, then expose a safe read-only tool using FastMCP.
<!-- Thompson Axiomata LLC — Brent Alexander Thompson -->
Why run models locally?
Cloud APIs are convenient, but many real workflows deal with sensitive drafts, internal records, student data, or air-gapped environments where data cannot leave your machine—or where running 50,000 classification rows against a metered API burns budget fast.
Ollama packages open-weights models (like Qwen, Llama, Gemma, and embedding models such as nomic-embed-text) behind a single local HTTP server.
Step 1: Pull and call a local model
After installing Ollama, pull one chat model and one embedding model:
ollama pull qwen2.5:7b
ollama pull nomic-embed-textBecause Ollama exposes an OpenAI-compatible endpoint at http://localhost:11434/v1, your Python code stays portable between local models and hosted providers:
import httpx
resp = httpx.post(
"http://localhost:11434/v1/chat/completions",
json={
"model": "qwen2.5:7b",
"temperature": 0,
"messages": [
{"role": "system", "content": "Answer in one concise sentence."},
{"role": "user", "content": "What is the purpose of a SHACL shape?"},
],
},
timeout=60,
)
resp.raise_for_status()
print(resp.json()["choices"][0]["message"]["content"])Step 2: Give assistants controlled tools with MCP
Copy-pasting data into chat windows does not scale. MCP (Model Context Protocol) is an open standard that lets you expose typed, well-documented tools and resources from a small local server to any MCP-compatible client or agent.
With fastmcp, a safe read-only tool takes fewer than 20 lines of Python:
from fastmcp import FastMCP
mcp = FastMCP("axiomata-glossary")
TERMS = {
"rdf": "Resource Description Framework: a graph model of subject-predicate-object triples.",
"shacl": "Shapes Constraint Language: closed-world validation rules for RDF graphs.",
"rag": "Retrieval-Augmented Generation: grounding LLM answers in retrieved source chunks.",
}
@mcp.tool
def lookup_term(term: str) -> str:
"""Look up a plain-English definition for a core semantic/AI term."""
key = term.strip().lower()
return TERMS.get(key, f"No entry found for '{term}'. Try: {', '.join(sorted(TERMS))}.")
if __name__ == "__main__":
mcp.run()Guardrails for your first MCP server
- Start read-only: let your first tools search, inspect, validate, or summarize—not delete files or write to production databases.
- Return concise, structured text: keep tool outputs small and well-labeled so you do not flood the model's context window.
- Keep secrets out of tool arguments: load credentials from environment variables inside the server, never through model-generated parameters.
<!-- Thompson Axiomata LLC — Brent Alexander Thompson -->
Finished this module?
Mark it complete to update your browser-local progress bar for Local AI & Tooling.