Who Actually Executes Agent Tools? Inside Function Calling, MCP, and Pi's Harness
Introduction
Tool calling is already a common topic in the Agent field,
but I've noticed that many people use Agent every day without truly understanding the running mechanism behind tool calling.
For example,
Can the large model directly execute tools?
What is the relationship between Function Calling and MCP?
From initiation to execution and then to result return, what steps does a tool call go through?
Based on recent research on the Pi framework, I wrote this article, which is also the second article in the Pi source code research series.
In the previous article, we focused on Pi's Agent Loop. This time, we go deeper and discuss the Agent's tool calling mechanism.
First article: [Previous article link]
Understanding Tool Calling
What is Function Calling?
To put it officially,
Function Calling is a mechanism that enables large language models (LLMs) to call external tools.
This concept sounds simple, after all, when we use various Agent products, we often encounter tool calls,
such as read for reading files, write for writing files, and bash for executing commands.
But the execution process behind tool calling is actually worth discussing in detail.
How does a complete Function Calling happen?
When I first came across Function Calling, I always had a misconception:
I thought Function Calling meant the large model directly executes tools and completes various operations on its own.
I believe many people have a similar understanding.
But in reality, that is not the case.
Before formally introducing Function Calling, we need to clarify a concept:
Agent = LLM + Harness
LLM is the large model, Agent is what we use daily like Cursor, Claude, etc. The specific concept of Harness was introduced in the previous article and won't be repeated here.
Harness is actually an engineering-side concept.
What does engineering-side mean?
In simple terms, it is the part implemented by the program code we write, with clear execution logic and relatively controllable behavior.
For example, Java backends, frontend programs, and various TypeScript and Python services we usually develop all belong to the engineering side.
According to my previous understanding, Function Calling was done directly by the large model, with no engineering-side involvement at all.
But in fact, the opposite is true: the real executor of tool calls is exactly the Harness.
First, during Agent initialization, the developer registers a set of tools, such as read, write, bash, etc.
Each tool has a corresponding name, functional description, and parameter Schema.
When a user sends a request to the Agent, the Agent provides the model with the definitions of available tools when calling the LLM.
The model, based on the user request and context, determines on its own whether to call a tool.
If needed, it generates a structured tool call request, containing the tool name and call parameters.
Next, this is the key point.
The one who actually executes the tool is not the LLM, but the Harness.
After the Harness receives the model's response, it identifies the tool call request, finds the corresponding tool, validates the parameters, and executes the relevant code.
After tool execution, the Harness wraps the execution result as a Message, adds it to the conversation context, and makes it available for the next LLM call.
Therefore, the entire process can be summarized as:
User sends request
↓
Agent calls LLM
↓
LLM decides whether to call a tool
↓
LLM generates a structured Tool Call
↓
Harness parses the Tool Call
↓
Harness finds and executes the corresponding tool
↓
Wraps the tool execution result as a Message
↓
Calls LLM again
↓
LLM continues decision-making based on tool results
So, the most important thing to understand about Function Calling is:
LLM decides what tool to call and what parameters to pass, while Harness actually executes the tool and feeds the result back to the LLM.
What is the relationship between local Function Calling and MCP?
When it comes to tool calling, MCP is unavoidable.
To put it officially, MCP (Model Context Protocol) is a standardized protocol for connecting AI applications with external tools and data sources.
Function Calling solves the problem of how the model expresses tool call intent, while MCP solves how AI applications discover and access external tools through a unified protocol.
These two concepts do not conflict; they complement each other.
Because even external tools are initially mixed with internal tools, registered together, and sent to the LLM.
On the LLM side, it cannot tell whether a Tool is an MCP Tool or a local Tool; it only knows a checklist.
When the tool that the LLM wants to execute is an MCP Tool, then Harness, through the MCP protocol, executes the external interface, and then informs the LLM of the execution result.
The overall process is as follows
LLM
↓
Generates Tool Call
↓
Harness
↓
Finds the corresponding tool
↓
┌──────┴──────┐
↓ ↓
Local tool MCP tool
↓ ↓
Local execution MCP Client
↓
MCP Server
↓ ↓
└──────┬──────┘
↓
Tool execution result
↓
Returned to LLM
Now, some practical content
Pi's Tool Calling Implementation
In the previous article, we mainly introduced Pi's Agent Loop implementation.
At that time, we only briefly mentioned the tool execution part, because if we expanded on it, the article would be too long.
This time, we specifically study Pi's tool calling implementation.
Let's see what steps a Tool Call goes through from model initiation, to tool execution, to the result re-entering the Agent Loop.
Entry point
We return to the runLoop method, where the entry point for tool execution is:
When runLoop() receives the model response, it checks whether it contains a tool call. If it does, it enters executeToolCalls() and hands it over to the tool execution system.
Let's look into the executeToolCalls method.
Sequential and Parallel
Pi supports two tool execution modes: sequential and parallel, as shown below:
The judging criteria is:
config.toolExecution === "sequential" || hasSequentialToolCall
There are two conditions here:
The first is a global configuration that determines whether tool calls uniformly use sequential execution.
The second is hasSequentialToolCall, used to determine whether there exists a tool in the current batch that requires sequential execution.
If either condition is met, it enters sequential mode.
Otherwise, it uses parallel mode.
Without additional restrictions, Pi tends to use parallel execution to improve efficiency.
executeToolCallsSequential and executeToolCallsParallel are the specific implementations for sequential and parallel execution. The logic inside is basically the same; the biggest difference is:
Sequential: directly await in a for loop:
for (const toolCall of toolCalls) {
await executePreparedToolCall(...);
}
So it must wait for the current tool to finish before entering the next round.
Parallel: first save the execution logic as asynchronous functions:
finalizedCalls.push(async () => {
return await executePreparedToolCall(...);
});
Finally, use Promise.all to start them simultaneously:
await Promise.all(
finalizedCalls.map(entry => entry())
);
The code is as follows:
The rest of the execution logic is basically the same.
Pre-execution preparation: prepareToolCall()
What prepareToolCall does is mainly the four parts I've highlighted.
1. Check whether the tool exists
2. Adapt tool parameters
3. Validate schema
4. Execute beforeToolCall Hook
This step is very important.
Because whether a tool can be executed should not be entirely decided by the model.
In real development, we often need to add engineering control logic before tool execution.
For example:
Permission check: is the current operation allowed?
User confirmation: should we ask for confirmation before deleting files or executing dangerous commands?
Log audit: record execution information such as tool name and parameters.
Policy interception: does it hit any rules that forbid execution?
These logic can all be implemented through this Hook.
If the Hook decides to intercept the current operation, Pi will not continue to execute the corresponding tool, but instead return an error result.
In this way, tool execution is no longer a matter of the model generating whatever and the program executing it unconditionally.
Other
While reading the source code, I also noticed two signal?.aborted checks.
This field is used to check whether the current operation has received a cancellation signal, for example, when the user actively aborts the Agent's execution.
Why check twice? Because the two checks are at different positions.
The first: only executed when a beforeToolCall exists, ensuring "cancellation" takes precedence over "interception." The second: executed regardless of whether there is a beforeToolCall.
At this point, the preparation before tool execution is complete.
Tool execution: executePreparedToolCall()
There is not much to say about this method; the core is executing the tool, so we'll skip it directly.
Post-execution check: finalizeExecutedToolCall()
This mainly executes all the afterToolCall Hooks
This is suitable for operations like desensitization, truncation, format conversion, error correction, statistics, etc.
How to return to the model after execution
First, use the createToolResultMessage() method to wrap the execution result into a Message, then return it.
Then, in runLoop, the tool execution result is added to the global Message list as input for the next model call.
At this point, we can connect the whole process:
LLM returns Tool Call
↓
executeToolCalls()
↓
Choose sequential / parallel execution
↓
prepareToolCall()
↓
Find tool, adapt parameters, validate Schema
↓
Execute pre-Hook
↓
executePreparedToolCall()
↓
tool.execute()
↓
finalizeExecutedToolCall()
↓
Execute post-Hook
↓
createToolResultMessage()
↓
ToolResultMessage added to Context
↓
Enter next Agent Loop
↓
LLM continues decision-making based on tool results
Exception handling
Finally, let's briefly discuss exception handling.
Pi's core design here is:
Tool exceptions are not thrown out of the Agent Loop as much as possible; instead, they are converted into a ToolResultMessage with isError: true and returned to the model, allowing the model to correct or retry.
Why is this design chosen?
Because for an Agent, one tool execution failure does not necessarily mean the entire task must fail.
This is essentially allowing the model to participate in error recovery, rather than letting a tool error directly determine the end of the entire task.
Summary of Design Philosophy
At this point, the entire tool calling process of Pi has been introduced.
Actually, when looking at each function individually, the code is not as complex as one might imagine.
What is truly worth learning is the engineering design philosophy behind these functions.
I summarized three important points.
Everything is a Message
In the model interaction layer of an Agent, whether it is user input, model reply, or tool execution result, it can ultimately be organized in the form of Messages.
The entire Agent Loop is about continuously generating, processing, and passing Messages.
This is a deep realization I have after studying Agent Loop and tool calling.
Hook Mechanism is as Important as the Tool Itself
When studying Pi's tool calling source code, I found a very interesting phenomenon.
The core code that actually executes a tool is just a single tool.execute().
Instead, the preparation, validation, interception, and result processing before and after execution occupy a considerable portion of the code.
Why?
Because in a real engineering environment,
being able to execute a tool is only the most basic requirement; how to make tool execution stable, safe, and controllable is the more important problem.
Model Makes Decisions, Harness Executes
The last viewpoint, and also the most important point for understanding Agent architecture, is:
Back to the formula at the beginning:
Agent = LLM + Harness
LLM and Harness have a very clear division of responsibilities.
LLM is responsible for understanding user intent, analyzing the current context, and deciding what to do next.
Harness is responsible for turning the model's decisions into actual operations.
For example, if the model decides to call a certain tool, whether the tool exists, whether the parameters are legal, whether execution is allowed, and how to handle execution failures all need to be handled by Harness.
This design essentially separates the model's decision-making capability from the engineering system's execution capability.
LLM provides intelligent decision-making; Harness provides reliable execution.
They interact through Messages and Tool Calls, together forming an Agent system that can run continuously.
This also makes me increasingly feel that Agent development is not simply calling a large model API.
The truly valuable engineering work also includes tool systems, context management, execution control, exception recovery, and the design of the entire Agent Loop.
These capabilities are precisely the core value of Harness.
Final Words
At this point, we have started from the basic concept of Function Calling and completely analyzed Pi's tool calling process.
If there is one thing to remember,
that thing must be:
The large model decides what to do, and Harness ensures these things are executed in a controllable way.
This is one of my biggest gains from researching Pi source code recently.