Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 53 ยท Tools and Agents

MCP, Workflows and Multi-Agent Systems

Learn what the Model Context Protocol does, how workflows differ from agents, and how routing, orchestrator-workers and evaluator loops work, with live Python.

So far you have seen a model call tools (Tool Use and Function Calling), run in a loop (Building an Agent Loop) and manage memory (Memory, Planning and Context Management). This lesson zooms out. How do you connect a model to many tools without writing glue code for every pair? And when a task is bigger than one prompt, how do you arrange several model calls into a system that stays predictable?

Two ideas answer those questions. The Model Context Protocol (MCP) standardizes how applications expose tools and data to models. Workflows and multi-agent patterns describe how to combine model calls. Most of this lesson is conceptual, because MCP servers and real agents need a network and a model. The live examples use plain Python and fake models so you can watch the control flow.

The Model Context Protocol

Imagine you have 5 AI applications (a chat app, an IDE assistant, a support bot, ...) and 8 data sources (a database, a ticket system, a file store, ...). Without a standard, every application needs its own custom integration for every source: up to 40 integrations. With a shared protocol, each source is wrapped once as a server and each application implements the client side once: 5 + 8 pieces of work.

MCP is that shared protocol. It is an open standard that uses JSON-RPC messages between three roles:

  • The host is the AI application the user talks to.
  • The client lives inside the host and keeps one connection to one server.
  • The server is a small program that exposes capabilities.

A server can offer three kinds of capability:

  • Tools: functions the model may ask to run (for example search_tickets).
  • Resources: data the application can read and place in context (a file, a database row).
  • Prompts: reusable prompt templates the user can pick.

One protocol, many tools

Host The app the user talks to.
Client One connection per server.
Server Exposes tools, resources and prompts.

Write a server once, use it from any compatible app.

1 / 2

A session starts with the client asking the server what it can do, and the model then sees the returned tool descriptions exactly like the schemas from the function-calling lesson:

sequenceDiagram
    participant H as Host app
    participant C as MCP client
    participant S as MCP server
    H->>C: Start session
    C->>S: initialize
    C->>S: tools/list
    S-->>C: tool names and JSON schemas
    H->>C: Model requests a tool call
    C->>S: tools/call with arguments
    S-->>C: result
    C-->>H: result goes back to the model

The model never talks to the server directly. The host decides what to forward.

Servers run either as a local process that talks over standard input/output, or as a remote service over HTTP. Here is the rough shape of a tool listing. This is read-only, because the site cannot start a server:

JSON
{
  "tools": [
    {
      "name": "search_tickets",
      "description": "Find support tickets by keyword.",
      "inputSchema": {
        "type": "object",
        "properties": { "query": { "type": "string" } },
        "required": ["query"]
      }
    }
  ]
}

MCP standardizes the plumbing, not the safety. A tool description is text that goes into the model's context, and a tool result can contain anything. A malicious or compromised server can try prompt injection. Connect only servers you trust, give each the least access it needs, and keep a human confirmation step for actions with side effects.

Workflows versus agents

A useful distinction (popularized in engineering write-ups on building with LLMs) is this:

  • A workflow is code you wrote that calls the model at fixed steps. The path through the system is decided by your program.
  • An agent lets the model decide the next step, including which tool to call and when to stop. The path is decided at run time.

Workflows are cheaper, faster, easier to test and easier to debug. Agents are flexible for open-ended problems where you cannot list the steps ahead. A good rule: start with a single prompt, add retrieval or examples if it is weak, move to a workflow when the task has natural stages, and use an agent only when the number and order of steps truly cannot be known in advance.

Five workflow patterns

Most useful systems are built from a handful of patterns.

  1. Prompt chaining. Output of step 1 feeds step 2 (draft, then translate, then check format). You can add a code "gate" between steps that stops bad intermediate results early.
  2. Routing. A cheap classifier decides which specialized prompt or model handles the input. A refund question goes to the billing prompt; a bug report goes to the engineering prompt. Routing lets each path be short and focused, and lets easy questions use a small, cheap model.
  3. Parallelization. Run independent calls at the same time: check one document against ten rules, or ask the same question several times and vote (see self-consistency).
  4. Orchestrator-workers. One model call breaks a task into subtasks that are not known in advance (which files to edit?), worker calls handle each one, and the orchestrator merges the results.
  5. Evaluator-optimizer. One call produces an answer, another critiques it against criteria, and the loop repeats until the critique passes or a limit is hit.
Python A router with fallback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the unknown route. A router must always have a safe default, such as a human queue or a general prompt, because real inputs never fit your categories perfectly.

Python Orchestrator-workers with an evaluator loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The evaluator loop has a hard round limit. Without one, a picky critic and a stubborn writer can loop forever and burn money. Every loop in an LLM system needs a stop condition that does not depend on the model agreeing.

Multi-agent systems

A multi-agent system uses several model-driven agents, each with its own prompt, tools and context. Common shapes are a manager that delegates to specialists, a pipeline of roles (researcher, writer, reviewer) or agents that debate and a judge that picks a winner.

Reasons to split work across agents:

  • Context isolation. A research sub-agent can read fifty pages and return a 200-word summary, so the main context stays small.
  • Specialization. A narrow prompt with three tools is easier to get right than one prompt with thirty.
  • Parallel work. Independent sub-agents run at the same time.

Reasons not to:

  • Cost. Each agent is its own set of model calls, and multi-agent runs often use several times more tokens than a single call.
  • Compounding errors. If each step is 90% reliable, ten chained steps succeed only about 35% of the time (0.9 to the power 10).
  • Debugging. You need traces of every message between agents to know what happened.
  • Coordination. Agents can duplicate work, contradict each other or pass along errors as facts.

When not to use an agent

Skip agents when a fixed sequence of steps solves the task, when a mistake is expensive and irreversible, when you need a guaranteed response time, or when you cannot measure whether the system works. If you do use one, set step and budget limits, log every step, restrict tools to the minimum, and build an evaluation set first so you can tell whether added complexity helped.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What problem does the Model Context Protocol mainly solve?

    MCP standardizes how hosts discover and call tools, resources and prompts, so one server works with many applications.
  2. 2. What is the key difference between a workflow and an agent?

    Workflows follow a path you wrote. Agents decide steps at run time, which is flexible but less predictable.
  3. 3. Why should an evaluator loop have a maximum number of rounds?

    A hard stop that does not rely on the model protects you from endless loops and runaway cost.
  4. 4. A pipeline has ten steps, each 90% reliable and independent. Roughly how often does the whole chain succeed?

    0.9 multiplied by itself ten times is about 0.35. Errors compound across steps.

Summary

  • MCP is an open protocol with hosts, clients and servers; servers expose tools, resources and prompts.
  • A protocol does not make tools safe: trust servers carefully, limit access and confirm risky actions.
  • Workflows are code-controlled paths; agents choose their own steps. Prefer the simplest design that works.
  • Common patterns: chaining, routing, parallelization, orchestrator-workers and evaluator-optimizer.
  • Multi-agent systems help with context isolation and specialization but cost more and compound errors.
  • Always add step limits, safe defaults and traces.

Next: learn to measure whether any of this works in Evaluating LLM Applications.