Code reference¶
Generated from the docstrings in src/ollama_mcp_bridge/. For the narrative version of
how these fit together, see Architecture.
CLI¶
ollama_mcp_bridge.main ¶
Simple CLI entry point for MCP Proxy
cli_app ¶
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",
),
)
Start the API proxy server with Ollama REST API compatibility and MCP tool integration
Source code in src/ollama_mcp_bridge/main.py
Application¶
ollama_mcp_bridge.api ¶
FastAPI application
health
async
¶
Health check endpoint.
Source code in src/ollama_mcp_bridge/api.py
chat
async
¶
Transparent proxy for Ollama's /api/chat, with MCP tool injection.
Source code in src/ollama_mcp_bridge/api.py
version
async
¶
Version information endpoint.
Source code in src/ollama_mcp_bridge/api.py
proxy_to_ollama
async
¶
Transparent proxy for all other Ollama endpoints.
Source code in src/ollama_mcp_bridge/api.py
Lifecycle¶
ollama_mcp_bridge.lifecycle ¶
Application lifecycle management for FastAPI
lifespan
async
¶
FastAPI lifespan events
Source code in src/ollama_mcp_bridge/lifecycle.py
get_mcp_manager ¶
Proxy service¶
ollama_mcp_bridge.proxy_service ¶
Service for handling proxy requests to Ollama
ProxyService ¶
Service handling all proxy-related operations to Ollama with or without MCP tools
Source code in src/ollama_mcp_bridge/proxy_service.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
health_check
async
¶
Check the health of the Ollama server and MCP setup
Source code in src/ollama_mcp_bridge/proxy_service.py
proxy_chat_with_tools
async
¶
proxy_chat_with_tools(
payload: Dict[str, Any], stream: bool = False
) -> Union[Dict[str, Any], StreamingResponse]
Handle chat requests with potential tool integration
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Dict[str, Any]
|
The request payload |
required |
stream
|
bool
|
Whether to use streaming response |
False
|
Returns:
| Type | Description |
|---|---|
Union[Dict[str, Any], StreamingResponse]
|
Either a dictionary response or a StreamingResponse |
Source code in src/ollama_mcp_bridge/proxy_service.py
proxy_generic_request
async
¶
Proxy any request to Ollama
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path to proxy to |
required |
request
|
Request
|
The FastAPI request object |
required |
Returns:
| Type | Description |
|---|---|
Response
|
FastAPI Response object |
Source code in src/ollama_mcp_bridge/proxy_service.py
MCP manager¶
ollama_mcp_bridge.mcp_manager ¶
MCP Server Management
MCPManager ¶
Manager for MCP servers, handling tool definitions and session management.
Source code in src/ollama_mcp_bridge/mcp_manager.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
load_servers
async
¶
Load and connect to all MCP servers from config
Source code in src/ollama_mcp_bridge/mcp_manager.py
call_tool
async
¶
Call a specific tool by name with provided arguments.
Source code in src/ollama_mcp_bridge/mcp_manager.py
Utilities¶
ollama_mcp_bridge.utils ¶
Utility functions for ollama-mcp-bridge
ClientDisconnected ¶
get_ollama_proxy_timeout_config ¶
Return (is_set, timeout_seconds) based on OLLAMA_PROXY_TIMEOUT.
- Unset/empty: (False, None) meaning "do not override"
- 0: (True, None) meaning "explicitly disable timeout" (warns once)
-
0: (True, seconds)
Invalid/negative values are ignored with a warning.
Source code in src/ollama_mcp_bridge/utils.py
is_port_in_use ¶
Check if a port is already in use on a given host.
Returns:
| Type | Description |
|---|---|
bool
|
Tuple[bool, Optional[str]]: (has_error, error_message) |
Optional[str]
|
|
Tuple[bool, Optional[str]]
|
|
Source code in src/ollama_mcp_bridge/utils.py
configure_cors ¶
Configure CORS middleware for the FastAPI app.
Source code in src/ollama_mcp_bridge/utils.py
check_ollama_health ¶
check_ollama_health(
ollama_url: str,
timeout: int = 3,
headers: Optional[Dict[str, str]] = None,
) -> bool
Check if Ollama server is running and accessible (sync version for CLI).
Source code in src/ollama_mcp_bridge/utils.py
check_ollama_health_async
async
¶
check_ollama_health_async(
ollama_url: str,
timeout: int = 3,
headers: Optional[Dict[str, str]] = None,
) -> bool
Check if Ollama server is running and accessible (async version for FastAPI).
Source code in src/ollama_mcp_bridge/utils.py
iter_ndjson_chunks
async
¶
Async generator that yields parsed JSON objects from NDJSON (newline-delimited JSON) byte chunks.
Source code in src/ollama_mcp_bridge/utils.py
run_until_client_disconnects
async
¶
Run coro, cancelling it if the client disconnects first.
Nothing cancels a buffered handler when the client hangs up, so the bridge would keep driving Ollama for a client that left. Cancelling closes the connection to Ollama, which stops the generation there too.
The watcher reads from the ASGI receive channel, so the request body must already have been consumed or a body message could be swallowed.
Returns the result of coro, or raises ClientDisconnected.
Source code in src/ollama_mcp_bridge/utils.py
parse_upstream_headers ¶
parse_upstream_headers(
env_value: Optional[str],
header_flags: Optional[list] = None,
) -> Optional[Dict[str, str]]
Build the upstream headers dict from the UPSTREAM_HEADERS env var and CLI flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env_value
|
Optional[str]
|
Raw UPSTREAM_HEADERS env var, expected to be a JSON object of header name/value pairs (e.g. '{"Authorization": "Bearer xxx"}'). |
required |
header_flags
|
Optional[list]
|
Repeatable --upstream-header values, each "Name: Value". |
None
|
CLI flags override env entries with the same header name. Returns None when no headers are configured.
Source code in src/ollama_mcp_bridge/utils.py
validate_cli_inputs ¶
validate_cli_inputs(
config: str,
host: str,
port: int,
ollama_url: str,
max_tool_rounds: int = None,
system_prompt: str = None,
)
Validate CLI inputs for config file, host, port, ollama_url, max_tool_rounds and system_prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_prompt
|
str
|
optional system prompt string; if provided, must be a non-empty string and not excessively long. |
None
|
Source code in src/ollama_mcp_bridge/utils.py
check_for_updates
async
¶
Check if a newer version of ollama-mcp-bridge is available on PyPI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_version
|
str
|
The current version of the package |
required |
print_message
|
bool
|
If True, print the update message to stdout instead of logging |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The latest version if an update is available, otherwise the current version |
Source code in src/ollama_mcp_bridge/utils.py
expand_env_vars ¶
Expand environment variable references in a string. Supports ${env:VAR_NAME} and ${workspaceFolder} syntax.
Source code in src/ollama_mcp_bridge/utils.py
expand_dict_env_vars ¶
Recursively expand environment variables in a dictionary.