Learn › Local AI Agents: Hermes 3 and the Model Context Protocol › The Agent Loop: Hermes + MCP in Action

The Agent Loop: Hermes + MCP in Action

Advanced 🕐 15 min Lesson 10 of 12
What you'll learn
  • Assemble the complete agent loop: build system prompt, call Ollama, parse tool_call, call MCP, inject tool_response, repeat
  • Use AsyncExitStack to manage multiple MCP server connections with clean shutdown on exit
  • Parse the tool_call block from a Hermes response using re.search with DOTALL to handle multi-line JSON
  • Run the agent from the terminal and trace the tool call log to verify the loop works end to end

The Complete Loop

The agent loop follows seven steps on every turn:

  1. Call Ollama with the current conversation history (system + all previous messages)
  2. Read the assistant response
  3. If the response contains no <tool_call>: return the response to the user — the agent is done
  4. If the response contains <tool_call>: parse the JSON inside the tags
  5. Look up the tool name in the registry to find the right MCP session
  6. Call the tool via MCP and get the result
  7. Append both the assistant message and the tool result to the conversation history, then go back to step 1

This loop repeats until Hermes produces a response with no tool call. That response is the agent's final answer.

The Full Agent Script

Create agent.py in your project directory:

import asyncio
import json
import re
import requests
from contextlib import AsyncExitStack
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

OLLAMA_URL = "http://localhost:11434/v1/chat/completions"
MODEL = "hermes3"
MAX_TURNS = 10  # safety limit on tool call depth

SERVERS = [
    {"name": "notes", "cmd": "python", "args": ["notes_server.py"]},
    {"name": "filesystem", "cmd": "npx",
     "args": ["-y", "@modelcontextprotocol/server-filesystem", "./workspace"]},
    {"name": "fetch", "cmd": "npx",
     "args": ["-y", "@modelcontextprotocol/server-fetch"]},
]


def call_ollama(messages: list) -> str:
    resp = requests.post(
        OLLAMA_URL,
        json={"model": MODEL, "messages": messages, "temperature": 0.1, "stream": False},
        timeout=120,
    )
    resp.raise_for_status()
    return resp.json()["choices"][0]["message"]["content"]


def parse_tool_call(text: str) -> dict | None:
    match = re.search(r"<tool_call>s*({.*?})s*</tool_call>", text, re.DOTALL)
    if not match:
        return None
    return json.loads(match.group(1))


def mcp_tool_to_hermes(tool) -> dict:
    return {
        "type": "function",
        "function": {
            "name": tool.name,
            "description": tool.description or "",
            "parameters": tool.inputSchema,
        }
    }


def build_system_prompt(all_tools: list) -> str:
    tools_json = json.dumps([mcp_tool_to_hermes(t) for t in all_tools], indent=2)
    return (
        "You are a function calling AI model. You are provided with function "
        "signatures within <tools></tools> XML tags. You may call one or more "
        "functions to assist with the user query. Do not make assumptions about "
        "what values to plug into functions.

"
        f"<tools>
{tools_json}
</tools>

"
        "For each function call return a JSON object with function name and "
        "arguments within <tool_call></tool_call> XML tags as follows:
"
        "<tool_call>
"
        "{"name": "function_name", "arguments": {"arg1": "value1"}}
"
        "</tool_call>

"
        "If no tool is relevant, respond in natural language."
    )


async def run_agent(user_query: str):
    async with AsyncExitStack() as stack:
        # Connect to all servers
        sessions = []
        all_tools = []
        tool_registry = {}

        for srv in SERVERS:
            params = StdioServerParameters(command=srv["cmd"], args=srv["args"])
            read, write = await stack.enter_async_context(stdio_client(params))
            session = await stack.enter_async_context(ClientSession(read, write))
            await session.initialize()
            result = await session.list_tools()
            for tool in result.tools:
                all_tools.append(tool)
                tool_registry[tool.name] = session
            print(f"[{srv['name']}] {len(result.tools)} tools loaded")

        # Build conversation
        system_prompt = build_system_prompt(all_tools)
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_query},
        ]

        # Agent loop
        for turn in range(MAX_TURNS):
            response = call_ollama(messages)
            messages.append({"role": "assistant", "content": response})

            tool_call = parse_tool_call(response)
            if tool_call is None:
                # Final answer — no tool call in the response
                return response

            tool_name = tool_call["name"]
            tool_args = tool_call.get("arguments", {})
            print(f"[tool] {tool_name}({tool_args})")

            session = tool_registry.get(tool_name)
            if session is None:
                tool_result = f"Error: tool {tool_name!r} not found in any connected server."
            else:
                call_result = await session.call_tool(tool_name, arguments=tool_args)
                if call_result.content:
                    tool_result = call_result.content[0].text
                else:
                    tool_result = "No output returned."

            tool_response_msg = {
                "role": "tool",
                "content": (
                    f"<tool_response>
"
                    f"{json.dumps({'name': tool_name, 'content': tool_result})}
"
                    f"</tool_response>"
                ),
            }
            messages.append(tool_response_msg)

        return "Agent reached maximum turn limit without a final answer."


if __name__ == "__main__":
    import sys
    query = " ".join(sys.argv[1:]) or "List all saved notes."
    answer = asyncio.run(run_agent(query))
    print("
--- Answer ---")
    print(answer)

Running the Agent

Make sure the workspace directory exists first:

mkdir -p workspace

Then run a query:

python agent.py "Save a note about MCP architecture and then list all my notes."

You should see the tool loading output for each server, then the tool calls printed as Hermes makes them, then the final answer. A typical run for this query looks like:

[notes] 4 tools loaded
[filesystem] 6 tools loaded
[fetch] 1 tools loaded
[tool] add_note({"title": "MCP Architecture", "content": "Three participants: host, client, server..."})
[tool] list_notes({})

--- Answer ---
I saved a note about MCP Architecture. You currently have 1 note:
[1] MCP Architecture (2025-11-03)

AsyncExitStack: Managing Multiple Context Managers

AsyncExitStack from Python's contextlib manages multiple async context managers in one stack. When the async with AsyncExitStack() as stack: block exits — normally or via an exception — it closes all entered contexts in reverse order. This ensures all MCP server processes are terminated cleanly when the agent finishes, without nested async with blocks for each server.

stack.enter_async_context(cm) enters the context manager and returns its value, adding its cleanup to the stack. Each stdio_client and ClientSession is entered this way, so they are all closed when the stack exits.

The MAX_TURNS Safety Limit

The MAX_TURNS = 10 constant prevents infinite loops. If Hermes keeps calling tools without producing a final answer — due to a logic error in the tools, a confusing user request, or a model quirk — the loop exits after 10 turns and returns a timeout message. For most queries, the loop completes in 1–4 turns. Increase MAX_TURNS for tasks that genuinely require many sequential tool calls.

Key takeaways
  • AsyncExitStack manages all server connections in one context — when the outer block exits, every stdio server process is terminated in reverse order automatically
  • parse_tool_call uses re.search with DOTALL so it finds the tool_call block anywhere in the response — this handles cases where Hermes outputs text or a scratch_pad before the tool call
  • The tool_response message uses the tool role and wraps the result in tool_response tags — Hermes reads this in its next turn to decide whether to call another tool or give a final answer
  • MAX_TURNS is a safety limit, not a target — most queries complete in 1-4 turns; set it high enough to allow complex tasks but low enough to prevent runaway loops
Learn more every week Get 5 hand-picked prompts + new AI tools every Monday. Free.