Skip to content

Instantly share code, notes, and snippets.

@wware
Created June 6, 2026 20:45
Show Gist options
  • Select an option

  • Save wware/9735ce1407c8965a220d8b7529774ac2 to your computer and use it in GitHub Desktop.

Select an option

Save wware/9735ce1407c8965a220d8b7529774ac2 to your computer and use it in GitHub Desktop.
Steps to write a little REPL loop for the Ollama server on my local network

To create an LLM-REPL (Read-Eval-Print Loop) in Python that can interact with your Ollama host on the local network and potentially access MCP servers, you'll need to follow a few steps. Here's a general guide to help you get started:

Step 1: Understand Your Environment

Before diving into coding, ensure you understand how your Ollama host works and what kind of communication protocols it uses (e.g., REST API, gRPC, etc.). You might also want to check if there are any official SDKs or libraries provided by the service.

Step 2: Set Up Your Python Environment

Make sure you have Python installed on your system. It's a good idea to use a virtual environment for your project:

python3 -m venv llm-repl-env
source llm-repl-env/bin/activate  # On Windows use `llm-repl-env\Scripts\activate`

Step 3: Install Necessary Libraries

You might need libraries like requests for HTTP communication, or specific SDKs provided by Ollama. For example:

pip install requests

If there's an official Python client library available for your Ollama host, you should use that instead of manually handling the API calls.

Step 4: Write the REPL Loop

Here’s a basic structure for a REPL loop in Python. This will take user input, send it to your Ollama host, and print the response:

import requests

def get_ollama_response(prompt):
    url = 'http://your-ollama-host-url/api/predict'  # Adjust this URL based on your API endpoint
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        'prompt': prompt
    }

    response = requests.post(url, headers=headers, json=data)
    if response.status_code == 200:
        return response.json().get('response', '')
    else:
        print(f"Error: {response.status_code}")
        return "An error occurred while communicating with the server."

def main():
    print("Welcome to the LLM REPL. Type 'exit' to quit.")
    while True:
        user_input = input("> ")
        if user_input.lower() == 'exit':
            break
        response = get_ollama_response(user_input)
        print(response)

if __name__ == "__main__":
    main()

Step 5: Handle MCP Servers and Tools

If you need to interact with MCP servers or other tools, you'll need to integrate that functionality. This might involve additional API calls or direct connections depending on the protocols used by these systems.

  • For APIs: You can use similar requests calls as in the example above.
  • For Direct Connections: Depending on the protocol (e.g., TCP/IP, UDP), you might need to use libraries like socket for Python.

Step 6: Testing and Debugging

Test your script thoroughly. Ensure that it correctly communicates with your Ollama host and any other services you're interacting with. Use print statements or a debugger to trace through the code if needed.

Additional Resources

By following these steps, you should be able to create a basic LLM-REPL loop in Python that interacts with your Ollama host and potentially other tools on your local network.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment