liveBook · Manning

Tool execution

As explained in Build an AI Agent (From Scratch)

Tool execution is the process of sending a tool request with arguments to a server, running the tool, and returning its result.

Tool execution turns an LLM’s requested action into a real function call. The LLM selects a tool and supplies arguments, but it does not run the tool. An application—or, with the Model Context Protocol (MCP), a server—runs it and returns the result for the LLM to use. section 3.2.1 section 3.4.1

A minimal execution path has four steps:

  • Provide tool definitions so the LLM knows each tool’s name, purpose, and required arguments. section 3.2.1
  • Read the returned tool call, parse its arguments, look up the named function, and call it. section 3.3.3
  • Append the assistant’s tool-call message and a tool-result message to the conversation. Associate the result with the request using its tool-call ID. section 3.2.1
  • Send the updated conversation to the LLM. Repeat if it requests another tool; otherwise, return its final answer. section 3.3.3

Dispatching a requested function

A Python executor can keep a dictionary from tool names to functions. For each tool call, it reads `tool_call.function.name`, parses `tool_call.function.arguments` with `json.loads`, and runs `tool_box[function_name](**function_args)`. A calculator request with `multiply`, `1234`, and `5678` produces `7006652`; the application then supplies that result to the LLM for its answer. The calculator itself raises errors for division by zero and unsupported operators. section 3.3.3 section 3.2.1

Listing 3.20 Simple agent loop for tool execution
from litellm import completion

def simple_agent_loop(system_prompt, question):
    tools = [search_web]
    tool_box = {tool.__name__: tool for tool in tools}
    tool_definitions = [function_to_tool_definition(tool) for tool in tools]

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": question}
    ]

    while True:
        response = completion(
            model="gpt-5-mini",
            messages=messages,
            tools=tool_definitions
        )

        assistant_message = response.choices[0].message

        if assistant_message.tool_calls:
            messages.append(assistant_message)
            for tool_call in assistant_message.tool_calls:
                tool_result = tool_execution(tool_box, tool_call)
                messages.append({
                    "role": "tool", 
                    "content": str(tool_result), 
                    "tool_call_id": tool_call.id
                })
        else:
            return assistant_message.content

The Python agent loop shows where execution fits: collect tool definitions, request a completion, run every returned tool call, append results, and continue until there are no tool calls. section 3.3.3

MCP moves tool execution behind a standardized client–server interface. The host decides when a tool is needed; its client forwards the request and arguments to a server that hosts and runs the tool, then returns the result. This replaces a project-specific execution bridge and lets compatible clients discover and invoke server-hosted tools. section 3.4.1 section 3.5

How Build an AI Agent (From Scratch) covers it

How deep it goes
Works through Python dispatch and a repeating agent loop, then describes standardized MCP execution.
Examples
Uses a calculator multiplication call and a web-search agent loop for a current-information question.
What it assumes
Assumes basic Python function and dictionary use while developing tool definitions and execution step by step.
What you can do after
Build it from scratch with Python function dispatch and a loop that returns tool results to the LLM.
How it connects
Connects tool calling, tool definitions, conversation history, agent loops, and the Model Context Protocol.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.