QA Interview Hub · Knowledge base · Quizzes · Українська

AI › Prompts, RAG, tools, agents, MCP

Tool calling, agent and MCP

In tool calling the model does not run a tool: it returns a call request that the application code usually executes. An agent is a loop of such steps with side effects, so it needs a step limit, argument validation, idempotent tools and human confirmation of dangerous actions. MCP is an open protocol for connecting external tools: it standardises the connection and adds no capabilities.

AI for QA · Agents and RAG

The agent sent the customer the same email twice. Did the model make a mistake or did the tests check nothing?

A support agent closes a ticket and emails the customer. Once the email tool answered with a timeout, the model repeated the call, and the email had already been sent: the customer got two. Nobody made a mistake in the sense a buggy piece of code does: the model acted reasonably, and the tool was not ready for a repeat. This is a typical defect of agent systems, and to look for it you need to understand how an agent is built.

How it works and why. In tool calling (function calling) we describe tools to the model: a name, a description and a parameter schema. The model does not run them. It returns a request: "I want to call create_refund with these arguments". Our code runs it, and we return the result to the context, after which the model continues (the exception is the vendor's built-in server tools, such as web search, which run on its platform). It is built this way because only the application has access to systems, rights and validation, while the model only proposes an action.

An agent is a loop: the model receives a goal, decides which tool to call, sees the result, decides what next, and so on to a final answer or a stop at a limit. Unlike a chat bot, an agent has actions with side effects (create a ticket, send an email, change data) and chooses their order itself, so an error means not only wrong text but also a change in the state of the world.

The agent loop

Goal and available toolsmessages + tool schemasModel: "I want to call a tool"name and arguments, no executionOur code runs the toolvalidation, rights, side effectsResult into the contextand another question to the modelAnswer or stop at a limitMAX_STEPS, tokens, time
Figure 1. The model only proposes a call; execution and its safety are on the side of our code. The loop always has an external boundary.

A tool description is part of the prompt, and its quality decides whether the model picks the tool correctly. Parameters are described with JSON Schema (the name of the schema field depends on the vendor, for example input_schema or parameters):

const tools = [{
  name: 'create_refund',
  description: 'Creates a refund. Call only after get_order, when the order is paid.',
  input_schema: {
    type: 'object',
    properties: {
      orderId: { type: 'integer' },
      reason: { type: 'string', maxLength: 200 },
    },
    required: ['orderId'],
    additionalProperties: false,
  },
}];

Example: from a duplicate email to a safe tool. The cause of the duplicate: the model did not see the result of the first call (a timeout), so it repeated it, and the tool could not recognise a repeat. It is fixed on the tool side, not in the prompt. Here it is on a refund tool, where a duplicate costs money; for the email the key also comes from the data, e.g. 'close-email-' + ticketId:

async function runTool(call: { name: string; args: unknown }) {
  // model arguments are input: validate them like any external input
  const args = schemas[call.name].parse(call.args);
  if (call.name === 'create_refund') {
    // the key comes from the order, not the call id: a retry gives the same one
    return refunds.createOnce({ orderId: args.orderId, key: 'refund-' + args.orderId });
  }
  throw new Error('unknown tool');
}

Three protections in one function. Arguments are checked with a schema, because the model may send anything, including text from a foreign document that tries to steer it. The action is idempotent: a repeat with the same key does not create a second refund. An unknown tool is rejected. The loop itself also needs a boundary:

while (steps++ < MAX_STEPS) {
  const reply = await model.call(messages, tools);
  if (!reply.toolCalls.length) return reply.text; // final answer
  messages.push(reply.message);
  for (const call of reply.toolCalls) {
    const result = await runTool(call); // our code, not the model
    messages.push(toolResult(call.id, result));
  }
}
throw new Error('Agent exceeded step limit');

MAX_STEPS stops an agent that has looped: without it the agent burns tokens and money (see the lesson on price and limits) and can repeat side effects. For dangerous actions (a payment, a deletion) a human confirmation is added before execution, and the tools get rights by the principle of least privilege.

MCP. The Model Context Protocol is an open protocol through which an application with a model connects external tools, resources and prompts via a standard interface. One MCP server can be used by different clients, and the transport is a local process (stdio) or remote HTTP. A comparison with own tools:

Own tools or an MCP server

Own tools

Defined in the application code

Simpler for one application: schemas, execution and rights in one repository. Every new client or model needs its own integration.

MCP server

A standard interface

One server works with different clients. It standardises the connection but does not add abilities: rights and security stay the server task.

Figure 2. MCP does not add abilities to the model, it standardises the connection. Responsibility for rights stays with the server.

For QA, MCP is one more surface: the contract of tools and schemas, server error handling, rights and authentication. A separate risk: tool descriptions and results come from the server and the model trusts them, so a third-party server can influence the agent behaviour (prompt injection through a description or a result). So only verified servers with minimal rights are connected.

Limits and alternatives. Not every task should be given to an agent. If the sequence of steps is known, a rigid flow that calls the model only where text understanding is needed is more reliable and cheaper. An agent is justified when the path depends on intermediate results. Many tools sharply complicate the choice: the model more often picks the wrong one, so the tool set is kept compact and the descriptions precise.

Common mistakes.

  • Believing the model runs the tool. Consequence: validation and rights are not implemented on the code side.
  • A tool with a side effect and no idempotency. Consequence: a retry after a timeout creates a duplicate.
  • A loop with no step limit. Consequence: a looping agent burns the budget and repeats actions.
  • Trusting arguments and results as reliable data. Consequence: an injection through a document or an API response steers the agent.
  • Tool rights that are too broad. Consequence: a model error touches what the agent should not have access to.

Check yourself

The model called the payment tool with an orderId that is not in the user request. Which check should have stopped this and where does it live?

Answer

A check on the tool side, in our code: does the order belong to the current user, and does the user have permission for this action. The argument schema catches only the shape, not the fact that the value is foreign. The model is not a trust boundary, so rights are checked as in any API.

Why is the idempotency key for create_refund taken from the order data and not from the tool call id?

Answer

On a retry after a timeout the model may generate a new call with a new id, and a key from the id would not recognise the repeat. A key from the order data is stable, so the second request returns the existing refund instead of creating a new one.

Does MCP give an agent new abilities compared with own tools?

Answer

No, it standardises the connection: one server works with different clients. The abilities, rights and security are defined by the server, and they are tested as a contract and an attack surface.

In an interview. Junior: The model does not run tools, it returns a call request, and our code runs it. An agent is a loop of such steps that must stop at a limit. Middle/Senior: I validate arguments from the model as external input, make tools with side effects idempotent, have a human confirm dangerous actions and keep rights minimal. For MCP I test the contract and schemas, error handling and rights, and I do not treat the descriptions and results of a third-party server as safe data.

More articles in this chapter