### Clone and setup development environment Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/CONTRIBUTING.md Initializes the project repository and installs all necessary dependencies including development tools. ```bash # Clone the repository git clone https://github.com/jonigl/ollama-mcp-bridge.git cd ollama-mcp-bridge # Install dependencies (including dev tools) uv sync ``` -------------------------------- ### CLI Usage Examples Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/cli.md Common command-line patterns for starting the bridge server with various configurations. ```bash # Start with default configuration ollama-mcp-bridge # Start with custom config and host/port ollama-mcp-bridge --config /etc/mcp-config.json --host 127.0.0.1 --port 8080 # Custom Ollama server and max tool rounds ollama-mcp-bridge --ollama-url http://remote-ollama:11434 --max-tool-rounds 5 # With upstream headers for authentication ollama-mcp-bridge --upstream-header "Authorization: Bearer token123" # System prompt with streaming ollama-mcp-bridge --system-prompt "You are a helpful assistant" --config custom.json # Check version ollama-mcp-bridge --version ``` -------------------------------- ### Install in editable mode Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Install the project for development purposes. ```bash # Install the project in editable mode uv tool install --editable . # Run it like this: ollama-mcp-bridge ``` -------------------------------- ### Install Package from TestPyPI Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/CI.md Commands to set up a virtual environment, install dependencies, and then install your package from TestPyPI. Use the --no-deps flag when installing from TestPyPI to avoid dependency conflicts if your package's dependencies are already installed. ```bash # Create a fresh virtual environment python -m venv test_venv source test_venv/bin/activate # On macOS/Linux # Or on Windows: test_venv\Scripts\activate # Install dependencies first from PyPI pip install fastapi httpx loguru mcp typer uvicorn # Install your package from TestPyPI with --no-deps pip install --index-url https://test.pypi.org/simple/ --no-deps ollama-mcp-bridge # Test the CLI command ollama-mcp-bridge --version ``` -------------------------------- ### Install with uvx Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Quickly install and run the bridge using uvx. ```bash uvx ollama-mcp-bridge ``` -------------------------------- ### Install and run from source Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Clone the repository and run the bridge locally. ```bash # Clone the repository git clone https://github.com/jonigl/ollama-mcp-bridge.git cd ollama-mcp-bridge # Start Ollama (if not already running) ollama serve # Run the bridge uv run ollama-mcp-bridge ``` -------------------------------- ### Instantiate ProxyService Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/proxy-service.md Example showing how to initialize the ProxyService with an existing MCPManager instance. ```python from ollama_mcp_bridge.proxy_service import ProxyService from ollama_mcp_bridge.mcp_manager import MCPManager manager = MCPManager() service = ProxyService(manager) ``` -------------------------------- ### Instantiate MCPManager Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md Example showing how to initialize the MCPManager with custom Ollama configuration. ```python from ollama_mcp_bridge.mcp_manager import MCPManager # Create manager manager = MCPManager( ollama_url="http://localhost:11434", system_prompt="You are helpful.", ollama_headers={"Authorization": "Bearer token"} ) ``` -------------------------------- ### Start the server with default settings Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Launches the bridge server using default configuration and network settings. ```bash ollama-mcp-bridge ``` -------------------------------- ### Example Tool Definition Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md A concrete example of a tool definition for a weather service. ```python { "type": "function", "function": { "name": "weather.get_temperature", "description": "Get the current temperature for a location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "City name (e.g., 'Paris', 'New York')" }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit" } }, "required": ["location"] } }, "server": "weather", "original_name": "get_temperature" } ``` -------------------------------- ### Start the Bridge via CLI Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/00-START-HERE.txt Command to initialize the bridge with a specific configuration file and port. ```bash ollama-mcp-bridge --config mcp-config.json --port 8000 ``` -------------------------------- ### Tool Call Example Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md A concrete example of a tool call object with function name and arguments. ```python { "function": { "name": "weather.get_temperature", "arguments": { "location": "Paris", "unit": "celsius" } } } ``` -------------------------------- ### GET /version Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md Retrieves the current installed version and the latest available version of the bridge. ```APIDOC ## GET /version ### Description Returns the current installed version of the bridge and the latest version available on PyPI. ### Method GET ### Endpoint /version ### Response #### Success Response (200) - **version** (string) - Current installed version - **latest_version** (string) - Latest version on PyPI #### Response Example { "version": "0.9.0", "latest_version": "0.10.0" } ``` -------------------------------- ### Message Role Examples Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md Example dictionary representations for different message roles. ```python { "role": "system", "content": "You are a helpful weather assistant." } ``` ```python { "role": "user", "content": "What is the weather like in Paris?" } ``` ```python { "role": "assistant", "content": "I'll check the weather for you.", "tool_calls": [ { "function": { "name": "weather.get_temperature", "arguments": {"location": "Paris"} } } ] } ``` ```python { "role": "tool", "tool_name": "weather.get_temperature", "content": "The current temperature in Paris is 15°C." } ``` -------------------------------- ### Run with Docker Compose Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Start the bridge using the provided docker-compose.yml file. ```bash docker compose up ``` ```yaml image: ghcr.io/jonigl/ollama-mcp-bridge:latest ``` -------------------------------- ### Configuration Precedence Example Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Demonstrates how CLI arguments take precedence over environment variables when setting the Ollama URL. ```bash # Environment variable sets base URL OLLAMA_URL=http://remote:11434 # CLI option overrides environment variable ollama-mcp-bridge --ollama-url http://localhost:11434 # Result: Uses http://localhost:11434 (CLI wins) ``` -------------------------------- ### Retrieve Version Information Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Returns the current installed version and the latest version available on PyPI. ```json { "version": "0.10.0", "latest_version": "0.10.0" } ``` -------------------------------- ### Start the Bridge via CLI Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/README.md Launch the bridge service with specific configuration, port, and Ollama URL parameters. ```bash ollama-mcp-bridge \ --config mcp-config.json \ --port 8000 \ --ollama-url http://localhost:11434 ``` -------------------------------- ### Fetch Version via cURL Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Example request to the version endpoint using cURL. ```bash curl -X GET http://localhost:8000/version # Response { "version": "0.9.0", "latest_version": "0.10.0" } ``` -------------------------------- ### Error Response Examples Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md Common examples of error responses returned by the system. ```python { "detail": "Invalid request body" } ``` ```python { "detail": "Could not connect to Ollama server: Connection refused" } ``` ```python { "detail": "Services not initialized" } ``` -------------------------------- ### GET /version Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/fastapi-app.md Get version information and check for updates. ```APIDOC ## GET /version ### Description Get version information and check for updates on PyPI. ### Method GET ### Endpoint /version ### Response #### Success Response (200) - **version** (string) - Current version - **latest_version** (string) - Latest available version #### Response Example { "version": "0.10.0", "latest_version": "0.11.0" } ``` -------------------------------- ### Conventional commit examples Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/CONTRIBUTING.md Illustrates the expected format for commit messages based on the project's conventional commit policy. ```bash feat: add OLLAMA_PROXY_TIMEOUT environment variable for configurable HTTP timeouts fix: resolve timeout issues with large models on localhost docs: update README with new environment variable documentation style: apply black formatting to test files test: add comprehensive timeout behavior test coverage ``` -------------------------------- ### Check for updates usage example Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/utils.md Demonstrates how to invoke the update check function for logging or CLI output. ```python from ollama_mcp_bridge.utils import check_for_updates # Check and log latest = await check_for_updates("0.9.0", print_message=False) # Check and print (for CLI) latest = await check_for_updates("0.9.0", print_message=True) ``` -------------------------------- ### GET /docs Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Provides access to the interactive Swagger UI documentation for the API. ```APIDOC ## GET /docs ### Description Interactive API documentation provided via Swagger UI. ### Method GET ### Endpoint http://localhost:8000/docs ``` -------------------------------- ### POST /api/chat Response Examples Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Examples of non-streaming and streaming NDJSON responses from the chat endpoint. ```json { "model": "qwen3:0.6b", "created_at": "2024-01-15T10:30:45.123Z", "message": { "role": "assistant", "content": "The current time in Paris is 3:45 PM." }, "done": true, "total_duration": 1234567890, "load_duration": 123456789, "prompt_eval_count": 45, "prompt_eval_duration": 234567890, "eval_count": 12, "eval_duration": 345678901 } ``` ```json {"model":"qwen3:0.6b","created_at":"2024-01-15T10:30:45.123Z","message":{"role":"assistant","content":"The"},"done":false} {"model":"qwen3:0.6b","created_at":"2024-01-15T10:30:45.234Z","message":{"role":"assistant","content":" current"},"done":false} {"model":"qwen3:0.6b","created_at":"2024-01-15T10:30:45.345Z","message":{"role":"assistant","content":" time"},"done":false} ... {"model":"qwen3:0.6b","created_at":"2024-01-15T10:30:46.789Z","message":{"role":"assistant","content":""},"done":true} ``` -------------------------------- ### Enable development auto-reload Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Starts the server with auto-reload enabled, intended for development environments. ```bash ollama-mcp-bridge --reload ``` -------------------------------- ### GET /version Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Retrieve the current version and latest available version on PyPI. ```APIDOC ## GET /version ### Description Retrieve the current version and latest available version on PyPI. ### Method GET ### Endpoint http://localhost:8000/version ### Response #### Success Response (200) - **version** (string) - Current installed version - **latest_version** (string) - Latest version available on PyPI #### Response Example { "version": "0.10.0", "latest_version": "0.10.0" } ``` -------------------------------- ### Start the ollama-mcp-bridge server Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Commands to launch the bridge with various configurations, including custom hosts, ports, and upstream headers. ```bash # Start with default settings (config: ./mcp-config.json, host: 0.0.0.0, port: 8000) ollama-mcp-bridge # Start with custom configuration file ollama-mcp-bridge --config /path/to/custom-config.json # Custom host and port ollama-mcp-bridge --host 0.0.0.0 --port 8080 # Custom Ollama server URL (local or cloud) ollama-mcp-bridge --ollama-url http://192.168.1.100:11434 # Send custom header(s) to the upstream server (repeatable, curl-style "Name: Value") ollama-mcp-bridge --upstream-header "X-API-Key: your-key" ollama-mcp-bridge --upstream-header "Authorization: Bearer xxx" --upstream-header "X-API-Key: yyy" # Keep the secret out of your shell history by letting the shell expand an env var ollama-mcp-bridge --upstream-header "Authorization: Bearer $MY_API_KEY" # Limit tool execution rounds (prevents excessive tool calls) ollama-mcp-bridge --max-tool-rounds 5 # Set a system prompt to prepend to all /api/chat requests ollama-mcp-bridge --system-prompt "You are a concise assistant." # Combine options ollama-mcp-bridge --config custom.json --host 0.0.0.0 --port 8080 --ollama-url http://remote-ollama:11434 --max-tool-rounds 10 # Combine options with an upstream API key header ollama-mcp-bridge --config custom.json --ollama-url http://remote-ollama:11434 --upstream-header "X-API-Key: your-key" --port 8080 # Check version and available updates ollama-mcp-bridge --version ``` -------------------------------- ### Parse upstream headers usage examples Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/utils.md Demonstrates how to parse headers from environment variables, CLI flags, and how CLI flags override environment variables. ```python from ollama_mcp_bridge.utils import parse_upstream_headers # From environment variable env_headers = parse_upstream_headers( '{"Authorization": "Bearer token123", "X-API-Key": "secret"}' ) # From CLI flags cli_headers = parse_upstream_headers( None, ["Authorization: Bearer token123", "X-Custom: value"] ) # Both combined (CLI overrides env) merged = parse_upstream_headers( '{"Authorization": "Bearer env_token"}', ["Authorization: Bearer cli_token"] ) # Result: {"Authorization": "Bearer cli_token"} ``` -------------------------------- ### validate_cli_inputs(config, host, port, ollama_url, max_tool_rounds, system_prompt) Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/utils.md Validates all CLI inputs before starting the server, ensuring configuration files, network settings, and prompt constraints meet requirements. ```APIDOC ## validate_cli_inputs ### Description Validates all CLI inputs before starting the server. It checks the existence of the config file, validates network parameters, and ensures constraints on tool rounds and system prompts are met. ### Signature `validate_cli_inputs(config: str, host: str, port: int, ollama_url: str, max_tool_rounds: int = None, system_prompt: str = None) -> None` ### Parameters - **config** (str) - Required - Path to MCP config file. - **host** (str) - Required - Host to bind to. - **port** (int) - Required - Port to bind to (1-65535). - **ollama_url** (str) - Required - Ollama server URL (must match pattern ^https?://[\w\.-]+(:\d+)?). - **max_tool_rounds** (int) - Optional - Maximum tool rounds (must be >= 1 if provided). - **system_prompt** (str) - Optional - System prompt for chat (max 10000 characters). ### Error Handling - Raises `BadParameter` with a descriptive message for any validation failure. ### Example ```python from ollama_mcp_bridge.utils import validate_cli_inputs try: validate_cli_inputs( config="mcp-config.json", host="0.0.0.0", port=8000, ollama_url="http://localhost:11434", max_tool_rounds=5, system_prompt="You are helpful" ) except BadParameter as e: print(f"Validation error: {e}") ``` ``` -------------------------------- ### Production MCP Server Configuration Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md A complex configuration example including tool filtering, environment variable interpolation for headers, and remote SSE endpoints. ```json { "mcpServers": { "filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"], "toolFilter": { "mode": "exclude", "tools": ["delete_file", "write_file"] } }, "secure_api": { "url": "https://internal-api.example.com/mcp", "headers": { "Authorization": "Bearer ${env:MCP_API_TOKEN}", "X-Client-Version": "1.0" } }, "sse_endpoint": { "url": "https://sse-service.example.com/sse", "headers": { "X-API-Key": "${env:SSE_API_KEY}" } } } } ``` -------------------------------- ### Configure FastAPI Lifespan Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/lifecycle.md Example of integrating the lifespan manager into a FastAPI application and setting required configuration via app.state. ```python from fastapi import FastAPI from ollama_mcp_bridge.lifecycle import lifespan app = FastAPI(lifespan=lifespan) # Configuration is passed via app.state before startup app.state.config_file = "mcp-config.json" app.state.ollama_url = "http://localhost:11434" app.state.max_tool_rounds = 5 app.state.system_prompt = "You are helpful" ``` -------------------------------- ### Validate CLI inputs example Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/utils.md Demonstrates how to call the validation function within a try-except block to handle potential BadParameter exceptions. ```python from ollama_mcp_bridge.utils import validate_cli_inputs try: validate_cli_inputs( config="mcp-config.json", host="0.0.0.0", port=8000, ollama_url="http://localhost:11434", max_tool_rounds=5, system_prompt="You are helpful" ) except BadParameter as e: print(f"Validation error: {e}") ``` -------------------------------- ### ollama-mcp-bridge CLI Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/cli.md The main command-line interface for starting the proxy server. It supports various configuration flags and environment variables to customize server behavior, tool execution, and upstream communication. ```APIDOC ## CLI Command: ollama-mcp-bridge ### Description Starts the API proxy server with Ollama REST API compatibility and MCP tool integration. The command validates inputs, checks server availability, and initializes the uvicorn server. ### Parameters - **--config** (str) - Optional - Path to MCP configuration JSON file (default: mcp-config.json) - **--host** (str) - Optional - Host address to bind the server to (default: 0.0.0.0) - **--port** (int) - Optional - Port number to bind the server to (default: 8000) - **--ollama-url** (str) - Optional - Ollama server URL (default: http://localhost:11434) - **--upstream-header** (List[str]) - Optional - Header to send to the upstream server as 'Name: Value' (repeatable) - **--max-tool-rounds** (int) - Optional - Maximum number of tool execution rounds - **--system-prompt** (str) - Optional - System prompt to prepend to messages - **--reload** (bool) - Optional - Enable auto-reload during development - **--version** (bool) - Optional - Show version information and exit ### Usage Example ```bash # Start with custom config and host/port ollama-mcp-bridge --config /etc/mcp-config.json --host 127.0.0.1 --port 8080 # With upstream headers for authentication ollama-mcp-bridge --upstream-header "Authorization: Bearer token123" ``` ``` -------------------------------- ### Model List Response Format Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Example JSON response structure for the tags endpoint. ```json { "models": [ { "name": "mistral:latest", "modified_at": "2024-01-15T10:30:45.123Z", "size": 4109262667, "digest": "abcd1234...", "details": { "format": "gguf", "family": "llama", "families": ["llama"], "parameter_size": "7B", "quantization_level": "Q4_0" } } ] } ``` -------------------------------- ### Interact with the Chat API Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Example curl request to the /api/chat endpoint, which supports MCP tool integration and streaming. ```bash curl -N -X POST http://localhost:8000/api/chat \ -H "accept: application/json" \ -H "Content-Type: application/json" \ -d '{ "model": "qwen3:0.6b", "messages": [ { "role": "system", "content": "You are a weather assistant." }, { "role": "user", "content": "What is the weather like in Paris today?" } ], "think": true, "stream": true, "options": { "temperature": 0.7, "top_p": 0.9 } }' ``` -------------------------------- ### Initializing Services Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/lifecycle.md The sequence for creating and loading the MCP manager and proxy service. ```python mcp_manager = MCPManager( ollama_url=ollama_url, system_prompt=system_prompt, ollama_headers=ollama_headers ) mcp_manager.max_tool_rounds = max_tool_rounds ``` ```python await mcp_manager.load_servers(config_file) ``` ```python proxy_service = ProxyService(mcp_manager) ``` ```python await check_for_updates(__version__) ``` -------------------------------- ### GET /version Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/MANIFEST.md Retrieves the current version of the bridge service. ```APIDOC ## GET /version ### Description Returns the version information of the bridge. ### Method GET ### Endpoint /version ``` -------------------------------- ### Combine multiple configuration options Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Demonstrates usage of multiple flags simultaneously to configure the server environment. ```bash ollama-mcp-bridge \ --config custom.json \ --host 127.0.0.1 \ --port 8080 \ --ollama-url http://remote-ollama:11434 \ --max-tool-rounds 10 \ --system-prompt "You are helpful" \ --upstream-header "Authorization: Bearer token" ``` -------------------------------- ### GET /health Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/MANIFEST.md Checks the health status of the bridge service. ```APIDOC ## GET /health ### Description Returns the current health status of the service. ### Method GET ### Endpoint /health ``` -------------------------------- ### Initialize FastAPI Application Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md Configures the main FastAPI instance with metadata and lifespan management. ```python from fastapi import FastAPI app = FastAPI( title="Ollama MCP Bridge", description="...", version=__version__, lifespan=lifespan ) ``` -------------------------------- ### Show Model Information Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Retrieve metadata and configuration for a specific model. ```bash curl -X POST http://localhost:8000/api/show \ -H "Content-Type: application/json" \ -d '{"name": "mistral"}' ``` -------------------------------- ### GET /openapi.json Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Provides the machine-readable OpenAPI 3.0 specification for the API. ```APIDOC ## GET /openapi.json ### Description Machine-readable OpenAPI 3.0 specification of all endpoints. ### Method GET ### Endpoint http://localhost:8000/openapi.json ``` -------------------------------- ### Load MCP Servers Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md Initializes the manager and loads server configurations from a JSON file. ```python async def load_servers(self, config_path: str) -> None ``` ```python import asyncio async def main(): manager = MCPManager() await manager.load_servers("mcp-config.json") print(f"Loaded {len(manager.all_tools)} tools") asyncio.run(main()) ``` -------------------------------- ### Initialize ProxyService Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/proxy-service.md Constructor signature for the ProxyService class. ```python def __init__(self, mcp_manager: MCPManager) -> None ``` -------------------------------- ### Complete Tool Filtering Configuration Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Demonstrates a full configuration with multiple MCP servers using different filter modes. ```json { "mcpServers": { "weather": { "command": "uv", "args": ["--directory", "./weather-server", "run", "main.py"], "toolFilter": { "mode": "include", "tools": ["get_temperature", "get_forecast"] } }, "filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"], "toolFilter": { "mode": "exclude", "tools": ["delete_file", "write_file"] } } } } ``` -------------------------------- ### GET /health Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/fastapi-app.md Check the health status of the MCP Proxy and Ollama server. ```APIDOC ## GET /health ### Description Check the health status of the bridge and Ollama server. ### Method GET ### Endpoint /health ### Response #### Success Response (200) - **status** (string) - healthy or degraded - **ollama_status** (string) - running or not accessible - **tools** (number) - Count of available tools #### Response Example { "status": "healthy", "ollama_status": "running", "tools": 5 } ``` -------------------------------- ### MCPManager.__init__ Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md Initializes a new instance of the MCPManager to handle connections to MCP servers and communicate with the Ollama API. ```APIDOC ## MCPManager.__init__(ollama_url, system_prompt, ollama_headers) ### Description Initializes the MCP Manager, setting up the HTTP client and preparing the environment for MCP server sessions. ### Parameters - **ollama_url** (str) - Optional - URL of the Ollama server (default: "http://localhost:11434") - **system_prompt** (str) - Optional - Optional system prompt to prepend to chat messages - **ollama_headers** (Dict[str, str]) - Optional - Optional headers to send with Ollama requests ### Example ```python from ollama_mcp_bridge.mcp_manager import MCPManager manager = MCPManager( ollama_url="http://localhost:11434", system_prompt="You are helpful.", ollama_headers={"Authorization": "Bearer token"} ) ``` ``` -------------------------------- ### Standardized Error Responses Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Examples of JSON error responses returned by the API endpoints. ```json { "detail": "Invalid request body" } ``` ```json { "detail": "Services not initialized" } ``` ```json { "detail": "Could not connect to Ollama server: Connection refused" } ``` ```json { "detail": "Internal server error details" } ``` -------------------------------- ### Configure SYSTEM_PROMPT Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Sets a system prompt to prepend to all /api/chat requests. ```bash SYSTEM_PROMPT="You are a helpful assistant focused on accuracy." ollama-mcp-bridge ``` -------------------------------- ### Pull a Model Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Download a model from the registry. ```bash curl -X POST http://localhost:8000/api/pull \ -H "Content-Type: application/json" \ -d '{"name": "mistral"}' ``` -------------------------------- ### Get Ollama Headers Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/proxy-service.md Merges configured Ollama headers with optional forwarded request headers. ```python def _get_ollama_headers(self, request_headers: Optional[Dict[str, str]] = None) -> Dict[str, str] ``` -------------------------------- ### Project File Structure Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/_VERIFICATION.md Displays the directory layout of the workspace output. ```text /workspace/home/output/ ├── 00-START-HERE.txt ← Start here ├── INDEX.md ← Reference index ├── MANIFEST.md ← Coverage summary ├── README.md ← Overview ├── configuration.md ← Configuration guide ├── endpoints.md ← API reference ├── types.md ← Type definitions ├── _VERIFICATION.md ← This file └── api-reference/ ├── cli.md ├── fastapi-app.md ├── lifecycle.md ├── mcp-manager.md ├── proxy-service.md └── utils.md ``` -------------------------------- ### Initialize FastAPI Application Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/fastapi-app.md The main application instance configured with lifespan management and CORS middleware. ```python app = FastAPI( title="Ollama MCP Bridge", description="Simple API proxy server with Ollama REST API compatibility and MCP tool integration", version=__version__, lifespan=lifespan, ) ``` -------------------------------- ### Run with Docker Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Execute the bridge container directly with volume mounts for configuration. ```bash docker run -p 8000:8000 \ -e OLLAMA_URL=http://host.docker.internal:11434 \ -v "$PWD/mcp-config.json:/mcp-config.json" \ -v "$PWD/mock-weather-mcp-server:/mock-weather-mcp-server" \ -w / \ ghcr.io/jonigl/ollama-mcp-bridge:latest ``` -------------------------------- ### Configure CORS Origins Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/fastapi-app.md Sets allowed origins for CORS via environment variables before starting the application. ```bash CORS_ORIGINS="http://localhost:3000,https://myapp.com" ollama-mcp-bridge ``` -------------------------------- ### Configuring Application State Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/lifecycle.md Sets the required configuration parameters on the FastAPI app state during startup. ```python app.state.config_file = "mcp-config.json" app.state.ollama_url = "http://localhost:11434" app.state.ollama_headers = {"Authorization": "Bearer token"} app.state.max_tool_rounds = 5 app.state.system_prompt = "You are a helpful assistant" ``` -------------------------------- ### Stream and Parse NDJSON Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/utils.md Usage example demonstrating how to stream bytes from an HTTP response and parse them into JSON objects. ```python async def stream_from_ollama(payload): async with httpx.AsyncClient() as client: async with client.stream("POST", url, json=payload) as resp: async for chunk in resp.aiter_bytes(): yield chunk ndjson_iter = iter_ndjson_chunks(stream_from_ollama(payload)) async for json_obj in ndjson_iter: print(json_obj) ``` -------------------------------- ### Configure custom path and port Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Overrides the default configuration file location and network port. ```bash ollama-mcp-bridge --config /etc/mcp-config.json --port 8080 ``` -------------------------------- ### Variable Expansion in Configuration Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Uses placeholders for workspace paths and environment variables within configuration strings. ```json { "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", "${workspaceFolder}/data" ] }, "remote_with_headers": { "url": "https://example.com/mcp", "headers": { "X-Client-Name": "ollama-mcp-bridge", "X-Request-Tag": "${env:MCP_REQUEST_TAG}" } } } } ``` -------------------------------- ### Set CORS Origins via Environment Variables Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/utils.md Examples of setting the CORS_ORIGINS environment variable to control allowed origins for the bridge. ```bash # All origins (default, not recommended) CORS_ORIGINS="*" ollama-mcp-bridge # Specific origins CORS_ORIGINS="http://localhost:3000,https://myapp.com" ollama-mcp-bridge # Multiple ports CORS_ORIGINS="http://localhost:3000,http://localhost:8080" ollama-mcp-bridge ``` -------------------------------- ### Complete Variable Expansion Configuration Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Combines environment variables and workspace folder paths in a single configuration. ```json { "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", "${workspaceFolder}/data" ] }, "remote_api": { "url": "https://example.com/mcp", "headers": { "Authorization": "Bearer ${env:MCP_API_KEY}", "User-Agent": "ollama-mcp-bridge" } } } } ``` -------------------------------- ### Initialize MCPManager Constructor Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md The constructor signature for initializing the MCP manager with Ollama connection details. ```python def __init__( self, ollama_url: str = "http://localhost:11434", system_prompt: str = None, ollama_headers: Optional[Dict[str, str]] = None, ) -> None ``` -------------------------------- ### Perform Asynchronous Ollama Health Check Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/utils.md Checks if the Ollama server is reachable using an asynchronous GET request, suitable for use in API handlers. ```python async def check_ollama_health_async( ollama_url: str, timeout: int = 3, headers: Optional[Dict[str, str]] = None ) -> bool ``` ```python from ollama_mcp_bridge.utils import check_ollama_health_async is_healthy = await check_ollama_health_async("http://localhost:11434") ``` -------------------------------- ### Perform Synchronous Ollama Health Check Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/utils.md Checks if the Ollama server is reachable using a synchronous GET request. Returns a boolean indicating accessibility. ```python def check_ollama_health( ollama_url: str, timeout: int = 3, headers: Optional[Dict[str, str]] = None ) -> bool ``` ```python from ollama_mcp_bridge.utils import check_ollama_health is_healthy = check_ollama_health("http://localhost:11434") if is_healthy: print("Ollama is running") else: print("Ollama is not accessible") ``` -------------------------------- ### Create and Push Git Tag Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/CI.md Use these commands to create a Git tag for a release and push it to the remote repository. This action triggers the TestPyPI publishing workflow. ```bash git tag v1.0.0 git push origin v1.0.0 ``` -------------------------------- ### Main Entry Point Definition Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/cli.md The primary entry point for the CLI application, which initializes the Typer framework. ```python def main() ``` -------------------------------- ### Check version information Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Displays the current version and checks for available updates. ```bash ollama-mcp-bridge --version ``` -------------------------------- ### Docker Deployment Configuration Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Running the bridge container with environment variables and a mounted configuration file. ```bash docker run -p 8000:8000 \ -e OLLAMA_URL=http://host.docker.internal:11434 \ -e CORS_ORIGINS="http://localhost:3000,https://myapp.com" \ -e MAX_TOOL_ROUNDS=5 \ -v "$PWD/mcp-config.json:/mcp-config.json" \ -w / \ ghcr.io/jonigl/ollama-mcp-bridge:latest ``` -------------------------------- ### load_servers(config_path: str) Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md Loads and connects to all MCP servers defined in a configuration file. ```APIDOC ## load_servers(config_path: str) ### Description Loads and connects to all MCP servers from a specified configuration file. It validates the configuration and initializes connections for each server. ### Parameters - **config_path** (str) - Required - Path to the MCP configuration JSON file. ### Raises - **ValueError** - Invalid JSON or missing mcpServers key. - **FileNotFoundError** - Config file not found. - **IOError** - File reading error. ``` -------------------------------- ### Run Unit Tests Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Executes unit tests for configuration and structure validation without requiring a running server. ```bash # Install test dependencies uv sync --extra test # Run unit tests (no server required) uv run pytest tests/test_unit.py -v ``` -------------------------------- ### Running with Environment Variables Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Executing the bridge with multiple environment variables and CLI flags for production settings. ```bash OLLAMA_URL=https://cloud-ollama.example.com \ CORS_ORIGINS="https://app.example.com,https://admin.example.com" \ MAX_TOOL_ROUNDS=10 \ MCP_API_TOKEN=secret123 \ SSE_API_KEY=sse_secret \ SYSTEM_PROMPT="You are a helpful assistant. Always be accurate." \ OLLAMA_PROXY_TIMEOUT=30000 \ ollama-mcp-bridge --config production.json --host 0.0.0.0 --port 8000 ``` -------------------------------- ### CLI Application Configuration Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/cli.md Defines the parameters and options for the bridge server, including host, port, and Ollama connectivity settings. ```python def cli_app( config: str = typer.Option("mcp-config.json", "--config", help="Path to MCP config JSON file"), host: str = typer.Option("0.0.0.0", "--host", help="Host to bind to"), port: int = typer.Option(8000, "--port", help="Port to bind to"), ollama_url: str = typer.Option( os.getenv("OLLAMA_URL", "http://localhost:11434"), "--ollama-url", help="Ollama server URL" ), upstream_header: List[str] = typer.Option( [], "--upstream-header", help="Header to send to the upstream server as 'Name: Value' (repeatable). " "Can also be set via the UPSTREAM_HEADERS env var (JSON object).", ), max_tool_rounds: Optional[int] = typer.Option( os.getenv("MAX_TOOL_ROUNDS", None), "--max-tool-rounds", help="Maximum tool execution rounds (default: unlimited)", ), system_prompt: Optional[str] = typer.Option( os.getenv("SYSTEM_PROMPT", None), "--system-prompt", help="System prompt to prepend to messages (can also be set with SYSTEM_PROMPT env var)", ), reload: bool = typer.Option(False, "--reload", help="Enable auto-reload"), version: bool = typer.Option(False, "--version", help="Show version information, check for updates and exit"), ) -> None ``` -------------------------------- ### List Available Models Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Retrieve a list of models available on the Ollama server. ```bash curl -X GET http://localhost:8000/api/tags ``` -------------------------------- ### Use Workspace Folder Variable Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Resolves the ${workspaceFolder} variable to the directory containing the configuration file. ```json { "mcpServers": { "local": { "command": "python", "args": ["${workspaceFolder}/mcp_server/main.py"] } } } ``` -------------------------------- ### Set a system prompt Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Defines a custom system prompt for chat interactions. ```bash ollama-mcp-bridge --system-prompt "You are a helpful assistant" ``` -------------------------------- ### Execute MCP Tools Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md Invokes a specific tool by its namespace-prefixed name and returns the result as a string. ```python async def call_tool(self, tool_name: str, arguments: dict) -> str ``` ```python import asyncio async def main(): manager = MCPManager() await manager.load_servers("mcp-config.json") result = await manager.call_tool( "weather.get_temperature", {"location": "Paris"} ) print(result) asyncio.run(main()) ``` -------------------------------- ### Generate Text Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/endpoints.md Send a prompt to a model for text generation. ```bash curl -X POST http://localhost:8000/api/generate \ -H "Content-Type: application/json" \ -d '{ "model": "mistral", "prompt": "Write a poem", "stream": false }' ``` -------------------------------- ### Minimal MCP Server Configuration Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md A basic JSON configuration defining a single local Python-based MCP server. ```json { "mcpServers": { "weather": { "command": "python", "args": ["weather_server.py"] } } } ``` -------------------------------- ### Access FastAPI Application Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/README.md Import the FastAPI app instance for mounting or extending the application. ```python from ollama_mcp_bridge.api import app # Access app for mounting, extending, etc. ``` -------------------------------- ### Module Dependencies Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/README.md Visual representation of the module dependency tree. ```text main.py (CLI) ↓ api.py (FastAPI app) ↓ lifecycle.py (startup/shutdown) ├─→ mcp_manager.py (MCP servers) └─→ proxy_service.py (HTTP proxying) └─→ mcp_manager.py (tool execution) utils.py (helpers used by all modules) schemas.py (API examples) ``` -------------------------------- ### Invoke CLI Entry Point Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/README.md Execute the main entry point programmatically or via the command line interface. ```python from ollama_mcp_bridge.main import main main() ``` ```bash ollama-mcp-bridge [options] ``` -------------------------------- ### Stdio Configuration Schema Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md Defines the structure for Stdio-based MCP server configuration. ```python { "command": str, "args": List[str], "env": Dict[str, str], "cwd": str, "toolFilter": dict } ``` -------------------------------- ### Use Environment Variables Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Injects environment variables into configuration strings using the ${env:VAR_NAME} syntax. ```json { "mcpServers": { "remote": { "url": "https://api.example.com", "headers": { "Authorization": "Bearer ${env:API_TOKEN}", "X-Client-ID": "${env:CLIENT_ID}" } } } } ``` -------------------------------- ### Format and check code style Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/CONTRIBUTING.md Applies Black formatting to the codebase or verifies compliance without modifying files. ```bash # Format all code black . # Check formatting without changes black --check . ``` -------------------------------- ### Connect to a remote Ollama server Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Specifies a custom URL for the Ollama backend instead of the default local instance. ```bash ollama-mcp-bridge --ollama-url http://192.168.1.100:11434 ``` -------------------------------- ### Configure an MCP Server Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/00-START-HERE.txt JSON structure for defining MCP servers, including command execution and tool filtering. ```json { "mcpServers": { "weather": { "command": "python", "args": ["weather.py"], "toolFilter": {"mode": "include", "tools": ["get_temp"]} } } } ``` -------------------------------- ### Run Integration Tests Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/README.md Executes integration tests that validate API endpoints and Ollama connectivity; requires the server to be active. ```bash # First, start the server in one terminal ollama-mcp-bridge # Then in another terminal, run the integration tests uv run pytest tests/test_api.py -v ``` -------------------------------- ### Configure OLLAMA_URL Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/configuration.md Sets the Ollama server URL. Defaults to http://localhost:11434. ```bash OLLAMA_URL=http://192.168.1.100:11434 ollama-mcp-bridge OLLAMA_URL=https://ollama.example.com ollama-mcp-bridge ``` -------------------------------- ### HTTP Configuration Schema Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md Defines the structure for HTTP-based MCP server configuration. ```python { "url": str, "headers": Dict[str, str], "toolFilter": dict } ``` -------------------------------- ### Proxy API Requests via cURL Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/fastapi-app.md Demonstrates how to interact with the proxied Ollama endpoints using standard HTTP requests. ```bash # List available models curl -X GET "http://localhost:8000/api/tags" # Pull a model curl -X POST "http://localhost:8000/api/pull" \ -H "Content-Type: application/json" \ -d '{"name": "mistral"}' ``` -------------------------------- ### Define MCP Configuration Format Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md The expected JSON structure for configuring MCP servers, including command, arguments, and environment variables. ```json { "mcpServers": { "server_name": { "command": "python", "args": ["script.py"], "env": {"VAR": "value"} } } } ``` -------------------------------- ### Configure Tool Filtering Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md Configuration modes for controlling tool availability via allow-lists or deny-lists. ```json { "toolFilter": { "mode": "include", "tools": ["get_temperature", "get_forecast"] } } ``` ```json { "toolFilter": { "mode": "exclude", "tools": ["dangerous_tool", "internal_tool"] } } ``` -------------------------------- ### Set Authentication Headers Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/00-START-HERE.txt CLI flag to include custom upstream headers for authentication. ```bash ollama-mcp-bridge --upstream-header "Authorization: Bearer token" ``` -------------------------------- ### Connect to MCP Server Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/api-reference/mcp-manager.md Defines the connection logic for individual servers, supporting Stdio, StreamableHTTP, and SSE transports. ```python async def _connect_server(self, name: str, config: dict) -> None ``` ```json { "command": "python", "args": ["script.py"], "env": {"VAR": "value"}, "cwd": "/path/to/dir" } ``` ```json { "url": "https://example.com/mcp", "headers": {"Authorization": "Bearer token"} } ``` ```json { "url": "https://example.com/sse", "headers": {"X-Custom": "value"} } ``` ```json { "toolFilter": { "mode": "include", "tools": ["tool1", "tool2"] } } ``` -------------------------------- ### Tool Definition Structure Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/types.md The base dictionary structure for defining tools compatible with Ollama. ```python { "type": "function", "function": { "name": str, # "server_name.tool_name" "description": str, # Tool description from MCP server "parameters": dict # JSON Schema for input }, "server": str, # Server name "original_name": str # Original tool name (without server prefix) } ``` -------------------------------- ### Interact with the API Source: https://github.com/jonigl/ollama-mcp-bridge/blob/main/_autodocs/00-START-HERE.txt cURL commands for performing chat requests and verifying service health. ```bash curl -X POST http://localhost:8000/api/chat \ -H "Content-Type: application/json" \ -d '{"model": "qwen3:0.6b", "messages": [...], "stream": false}' ``` ```bash curl http://localhost:8000/health ```