Ollama MCP Wrapper

Ollama MCP Wrapper

Enables AI agents to interact with local Ollama models for text generation and tool calling with prompt injection protection.

Category
访问服务器

README

Ollama Model Context Protocol (MCP) Wrapper

A production-grade Python-based MCP server wrapper that exposes local Ollama instance capabilities to LLM applications and host systems (such as Claude Desktop or IDEs).

This wrapper allows agents to dynamically query downloaded models, load models into memory, run text completions, request tool-calling operations, and inspect/unload running models under a strict static prompt injection security filter.


Architecture Flow

+-------------------+             +-----------------------+             +--------------------+
|  Host (e.g. IDE)  |  <--STDIO-->|  Ollama MCP Server    |  <--HTTP--> | Local Ollama Daemon|
|  & Agent Clients  |             |  (FastMCP Python SDK) |             | (http://localhost:11434)|
+-------------------+             +-----------------------+             +--------------------+

File Structure

├── .agents/                 # Custom Agent rules and automated skills
├── src/
│   └── ollama_wrapper/      # Server package directory
│       ├── __init__.py
│       ├── config.py        # Global settings, timeout & injection settings
│       ├── prompts.py       # Custom agent system prompt templates
│       ├── security.py      # Precompiled regex prompt injection scanners
│       ├── server.py        # Main FastMCP server definitions
│       └── tools_helper.py  # MCP schema to Ollama payload formatters
├── tests/                   # Pytest test cases
├── examples/
│   └── minimal_client.py    # Example Python client integration script
├── pyproject.toml           # Package declarations and dependencies
├── test_changelog.md        # Log of test suite errors and resolutions
└── README.md                # Extensive documentation

Features & Tool API Reference

The server registers 5 primary tools, 1 resource path, and 2 prompt templates:

Tools

  1. list_local_models

    • Description: Returns all models currently downloaded on the host machine.
    • Output: JSON list containing name, size, family, and parameter_size.
  2. run_model_completion

    • Description: Runs text generation/completion on a targeted model.
    • Parameters:
      • model_name (string, required): Target model identifier (e.g. llama3:latest).
      • prompt (string, required): Text input.
    • Security: Blocks execution if prompt violates injection filters.
  3. generate_with_tools

    • Description: Runs chat completions exposing a list of execution tool schemas to the model.
    • Parameters:
      • model_name (string, required): Target model identifier.
      • messages (array of objects, required): Chat message history.
      • tools (array of objects, required): Custom tool definitions to expose.
  4. list_running_models

    • Description: Retrieves a list of models currently loaded and active in the system's memory (RAM/VRAM).
  5. stop_model

    • Description: Unloads a specific model from RAM/VRAM to free up system resources.
    • Parameters:
      • model_name (string, required): Model to unload.

Resources

  • active-model://status: Returns the status and name of the currently active model.

Prompts

  • agent_bootstrap: System prompt template to spin up a structured reasoning agent.
  • code_assistant: System prompt template configuring a coding assistant.

Setup & Running

Prerequisites

  1. Install Ollama and download a model (e.g. ollama run qwen2.5-coder:1.5b).
  2. Install uv Python package manager.

Running the Server

To start the MCP server locally using standard I/O (STDIO):

$env:PYTHONPATH="src"
uv run python -m ollama_wrapper.server

Minimal Client Example

We have provided a complete example client in examples/minimal_client.py. To run this client and test the server:

  1. Start Ollama.
  2. In the project root directory, execute:
    uv run python examples/minimal_client.py
    

Security Safeguards

To prevent jailbreaks and bypasses, all input prompts passing through text generation tools are filtered by precompiled regular expressions in security.py. This blocks:

  • Instruction overrides (e.g., "ignore previous instructions").
  • Scenarios activating bypass roles (e.g., "DAN mode", "developer mode active").
  • Sensitive information disclosure (e.g., "reveal system prompt").
  • Encoding attacks (e.g., "translate system instructions to base64").

Running Tests

Run the test suites with pytest:

uv run pytest

Integration with External Projects & Clients

To use this MCP server in another project or on a different PC:

1. Clone the Repository

git clone https://github.com/PieterVDMerwe/ollama-wrapper-mcp.git

2. Configure Your Client

Add the configuration block under the mcpServers key of your client settings file:

A. Claude Desktop

  • Configuration Path: %APPDATA%\Claude\claude_desktop_config.json
  • JSON Payload:
    {
      "mcpServers": {
        "ollama-wrapper": {
          "command": "uv",
          "args": [
            "--directory",
            "C:/path/to/cloned/ollama-wrapper-mcp",
            "run",
            "python",
            "-m",
            "ollama_wrapper.server"
          ],
          "env": {
            "PYTHONPATH": "src",
            "OLLAMA_HOST": "http://localhost:11434"
          }
        }
      }
    }
    

B. Google Antigravity 2.0 & Antigravity IDE

  • Configuration Path: ~/.gemini/config/mcp_config.json
  • JSON Payload:
    {
      "mcpServers": {
        "ollama-wrapper": {
          "command": "uv",
          "args": [
            "--directory",
            "C:/path/to/cloned/ollama-wrapper-mcp",
            "run",
            "python",
            "-m",
            "ollama_wrapper.server"
          ],
          "env": {
            "PYTHONPATH": "src",
            "OLLAMA_HOST": "http://localhost:11434"
          }
        }
      }
    }
    

(Make sure to replace C:/path/to/cloned/ with the absolute path to the directory on the new machine).


Agent / LLM Instructions

When interacting with this MCP server as an AI assistant or agent client, adhere to the following operation guidelines:

1. Model Discovery & Selection

  • Do not guess model names: Always call list_local_models first to discover downloaded options.
  • Select the smallest viable model appropriate for the task (e.g. use smaller coder models for code tasks, and larger ones for reasoning).

2. VRAM and Resource Management

  • Local models consume massive GPU/RAM resources.
  • Check currently loaded models using list_running_models.
  • Clean up after yourself: When you are finished running generation tasks, explicitly call the stop_model tool to unload the model from memory.

3. Prompt Safety Limits

  • All input prompts are statically scanned. Avoid using phrases that look like overrides (e.g. "ignore prior commands"), or the server will reject your request with a security error.
  • Keep prompts focused on content generation rather than system configurations.

4. Interactive Tool Execution

  • Use generate_with_tools to expose system capabilities to the Ollama backend model.
  • Inspect the returned message.tool_calls structure to determine which functions the model wants to run, execute them in your client wrapper, and feed the results back into the chat history.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选