MCPMCP Python SDK 2.2 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
24 small wins to finish your pathNext lesson

A stand-in model that chooses tools

An agent needs a model to choose a tool. Instead of a paid one, write a stand-in: a function that reads the message and offered tools and picks a call.

Example
import re


def choose_tool(message, model_tools):
    """Decide which tool to call, and with what, from the message alone."""
    offered = {tool["function"]["name"] for tool in model_tools}
    order = re.search(r"\b[A-Z]\d+\b", message)
    text = message.lower()
    if order and "refund" in text and "refund_order" in offered:
        return {"name": "refund_order", "arguments": {"order_id": order.group(), "reason": message}}
    if order and "lookup_order" in offered:
        return {"name": "lookup_order", "arguments": {"order_id": order.group()}}
    if "search_help" in offered:
        return {"name": "search_help", "arguments": {"query": text.split()[-1].strip("?.!")}}
    return None

It returns what a model returns when it calls a tool: a name and arguments, or None for no tool. re.search(r"\b[A-Z]\d+\b", ...) finds an order id like A17: a capital letter followed by digits, as a whole word.

It only calls tools that were offered, as a real model can only call tools in its request. Take refund_order out of the list and it falls back to a lookup. That rule is what makes the offered tool list matter.

Example
offered = [{"type": "function", "function": {"name": name}} for name in ("lookup_order", "search_help", "refund_order")]

for message in [
    "Where is my order B42?",
    "Please refund order A17, it arrived broken",
    "How do I reset my password?",
]:
    print(choose_tool(message, offered))
Example
only_lookup = [{"type": "function", "function": {"name": "lookup_order"}}]
print(choose_tool("Please refund order A17, it arrived broken", only_lookup))
print(choose_tool("How do I reset my password?", only_lookup))

With only lookup_order offered, the refund request becomes a lookup and the password question gets no tool. A real model makes the same kind of choice from the descriptions, with far better judgement and far less predictability, which is why the next lessons check what it does.

Try it yourself
  • Add a rule: a message containing "article" calls search_help with the next word.
  • Send "order a17" in lower case. Why is no order found, and should the regex change?
  • Return None for any message over 200 characters.

Every expert started right here.