Subagent with an MCP Tool — A Subagent Is Just an LLMAgent¶
This notebook builds websearch_subagent, wired to an MCP-discovered
websearch tool, and registers it alongside the framework's general
and explore default recipes. Nothing about SubAgentSpec restricts
what a dispatched agent can be equipped with: builder is an ordinary
LLMAgentBuilder, the same one used for a top-level agent.
Setting up the LLM¶
I recommend running these notebooks on Ollama Cloud. It serves much larger models than most laptops can run. The default cloud model is picked by benchmarking candidates on these notebooks, and an automated run checks every notebook against it each week. The notebook status page shows the current model and results.
- Create an API key at https://ollama.com/settings/keys.
- Export it before launching Jupyter:
export OLLAMA_API_KEY=<your key>. - That's all. When a notebook builds its LLM, it detects the key and uses Ollama Cloud.
Prefer to run locally? Install Ollama from https://ollama.com/download
and pull the model with ollama pull qwen3:14b. If no key is set, the
notebook starts the Ollama service for you, so there is no need to run
ollama serve. Local models are smaller, so your outputs will differ from
the recorded ones.
Prefer OpenAI or Anthropic?
- Install the extra:
uv sync --extra openaioruv sync --extra anthropic. - Export
OPENAI_API_KEYorANTHROPIC_API_KEYbefore launching Jupyter. If you forget, you will be prompted for it, which keeps the key out of the saved notebook. - Set
LLM_PROVIDER=openai(oranthropic), or passprovider="openai"tomake_llm(). A key on its own never switches providers, so one exported for unrelated work cannot reroute you.
The switch applies wherever a notebook builds its LLM with make_llm().
A notebook that constructs OllamaLLM directly stays on Ollama. Output on
gpt-5 or Claude will differ from the recorded outputs, and prompt-sensitive
examples (the ch09 evaluator pattern, ch08 supervised trajectories) may
behave noticeably differently.
# Uncomment the line below to install `llm-agents-from-scratch` from PyPI
# !pip install llm-agents-from-scratch
from llm_agents_from_scratch.notebook_utils import make_llm
Connecting to a Websearch MCP Server¶
duckduckgo-mcp-server
exposes search and fetch_content tools over stdio. Unlike the
GitHub and GoodNews MCP servers from Chapter 5, it needs no API key
and no local clone; uvx fetches and runs it on demand, requiring
only that uv is installed on your machine. The browser extra
below is required: DuckDuckGo's search endpoint blocks the server's
default HTTP client, and the fallback that handles this only works
with that extra installed.
from mcp import StdioServerParameters
from llm_agents_from_scratch.tools.mcp import MCPToolProvider
websearch_mcp_provider = MCPToolProvider(
name="websearch_mcp",
stdio_params=StdioServerParameters(
command="uvx",
args=[
"--with",
"duckduckgo-mcp-server[browser]",
"duckduckgo-mcp-server",
],
),
)
Defining the Roster¶
websearch_subagent's builder is nothing special: an
LLMAgentBuilder pointed at the MCP provider above, the same builder
class every other subagent in this book uses. Registering it alongside
general_subagent and explore_subagent shows the coordinator's
subagent registry is an open list, not a fixed pair of defaults.
from llm_agents_from_scratch import LLMAgent, LLMAgentBuilder
from llm_agents_from_scratch.data_structures import Task
from llm_agents_from_scratch.subagents import SubAgentSpec
from llm_agents_from_scratch.subagents.recipes import (
explore_subagent,
general_subagent,
)
llm = make_llm()
websearch_subagent = SubAgentSpec(
name="websearch_subagent",
description=(
"Searches the live web and fetches page content for current info."
),
builder=LLMAgentBuilder(llm=llm, mcp_providers=[websearch_mcp_provider]),
max_steps=5,
)
coordinator = LLMAgent(
llm=llm,
subagents=[
general_subagent(llm),
explore_subagent(llm),
websearch_subagent,
],
)
✓ Using Ollama Cloud (kimi-k2.7-code:cloud)
Example — Routing to the Websearch Specialist¶
The task below asks for today's actual, current release of Python.
websearch_subagent is the only specialist built with a dedicated
search tool, so it's the intended routing target, even though
general_subagent's Python interpreter could technically improvise
a network call of its own.
task = Task(
instruction=(
"Ask websearch_subagent to search the web for the current "
"official version number of the Python programming language "
"and report it back with the source URL. Report exactly what "
"it returns."
),
)
handler = coordinator.run(task, max_steps=10)
result = await handler
print(result.content)
The websearch_subagent returned: > The current official Python version shown on python.org is **Python 3.14.7**. > > This is confirmed by the official Python downloads page at https://www.python.org/downloads/, which lists **Python 3.14.7** as the latest release (dated Aug. 5, 2026) and also states "Latest: Python 3.14.7" on the homepage.
handler.rollout shows the coordinator dispatching straight to
websearch_subagent, the only registered specialist actually equipped
to answer; general_subagent and explore_subagent stay registered
but unused, exactly as a real open registry should behave.
print(handler.rollout)
=== Task Step Start ===
💬 assistant: My current instruction is 'Ask websearch_subagent to search the web for the current official version number of the Python programming language and report it back with the source URL. Report exactly what it returns.'
💬 assistant: I need to make the following tool call(s):
{
"id_": "3c567290-f94f-4cfb-8f8f-ecc494e16b2e",
"tool_name": "from_scratch__use_subagent",
"arguments": {
"name": "websearch_subagent",
"task": "Search the web for the current official version number of the Python programming language. Report the version number and include the source URL where you found it. Be specific and use authoritative sources such as python.org."
}
}.
🔧 tool: {
"tool_call_id": "3c567290-f94f-4cfb-8f8f-ecc494e16b2e",
"content": "The current official Python version shown on python.org is **Python 3.14.7**.\n\nThis is confirmed by the official Python downloads page at https://www.python.org/downloads/, which lists **Python 3.14.7** as the latest release (dated Aug. 5, 2026) and also states \"Latest: Python 3.14.7\" on the homepage.",
"error": false
}
💬 assistant: The websearch_subagent returned:
> The current official Python version shown on python.org is **Python 3.14.7**.
>
> This is confirmed by the official Python downloads page at https://www.python.org/downloads/, which lists **Python 3.14.7** as the latest release (dated Aug. 5, 2026) and also states "Latest: Python 3.14.7" on the homepage.
=== Task Step End ===
The MCP provider keeps its uvx subprocess and session alive for
reuse across dispatches. In a notebook the kernel can stay up long
after this cell runs, so close it explicitly rather than relying on
process exit to clean it up.
await websearch_mcp_provider.close()