#!/usr/bin/env python3
"""
MCP Ollama Query Tool — patch for homelab-mcp/server.py

HOW TO APPLY:
  Copy the ollama_query function below and paste it into
  scripts/homelab-mcp/server.py AFTER the existing tool definitions
  (before the `if __name__` block or the mcp.run() call).

PREREQUISITES:
  The server already imports httpx and has the @_safe decorator defined.
  No additional imports are needed — the function uses httpx.Client inline
  (matching the existing pattern in server.py).

LOCATION: Add after the last @mcp.tool() function in server.py.
"""

# ─── CUT HERE ─── paste everything below into server.py ─────────────────────

# @mcp.tool()
# @_safe
# def ollama_query(prompt: str, model: str = "qwen3-coder:latest",
#                  max_tokens: int = 2000, temperature: float = 0.3) -> str:
#     """Query the local Ollama LLM for homelab-specific analysis.
#
#     Useful for: analyzing logs, explaining configs, generating commands,
#     summarizing infrastructure state, or any homelab question.
#
#     Args:
#         prompt: The question or analysis prompt.
#         model: Ollama model name (default: qwen3-coder:latest).
#         max_tokens: Maximum response tokens.
#         temperature: Sampling temperature (0.0-1.0, lower = more focused).
#     """
#     import re
#
#     OLLAMA_URL = "http://192.168.0.145:31434"
#     with httpx.Client(timeout=120) as client:
#         resp = client.post(
#             f"{OLLAMA_URL}/api/generate",
#             json={
#                 "model": model,
#                 "prompt": prompt,
#                 "stream": False,
#                 "options": {
#                     "temperature": temperature,
#                     "num_predict": max_tokens,
#                 },
#             },
#         )
#         resp.raise_for_status()
#         raw = resp.json().get("response", "").strip()
#         return re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL).strip()

# ─── END CUT ────────────────────────────────────────────────────────────────


# The same function without comment markers, for easy copy-paste:

PATCH_CODE = '''
@mcp.tool()
@_safe
def ollama_query(prompt: str, model: str = "qwen3-coder:latest",
                 max_tokens: int = 2000, temperature: float = 0.3) -> str:
    """Query the local Ollama LLM for homelab-specific analysis.

    Useful for: analyzing logs, explaining configs, generating commands,
    summarizing infrastructure state, or any homelab question.

    Args:
        prompt: The question or analysis prompt.
        model: Ollama model name (default: qwen3-coder:latest).
        max_tokens: Maximum response tokens.
        temperature: Sampling temperature (0.0-1.0, lower = more focused).
    """
    import re

    OLLAMA_URL = "http://192.168.0.145:31434"
    with httpx.Client(timeout=120) as client:
        resp = client.post(
            f"{OLLAMA_URL}/api/generate",
            json={
                "model": model,
                "prompt": prompt,
                "stream": False,
                "options": {
                    "temperature": temperature,
                    "num_predict": max_tokens,
                },
            },
        )
        resp.raise_for_status()
        raw = resp.json().get("response", "").strip()
        return re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL).strip()
'''


if __name__ == "__main__":
    print("This file is a patch reference — not meant to be executed directly.")
    print("Copy the ollama_query function into homelab-mcp/server.py.")
    print()
    print(PATCH_CODE)
