Skip to content

smolagents

pip install "mimir-decisions[local,smolagents]"

mimir.integrations.smolagents.as_smolagents_tool converts a decision tool into a tool for ToolCallingAgent and CodeAgent. The typed result is returned as a JSON object, so agent code reads result["status"] and result["answer"].

smolagents callbacks fire after each agent step rather than before a tool call, so tool-call checks cannot be wired in as pre-call middleware. Use the MCP transport or the HTTP client to integrate checks into a smolagents pipeline from outside the framework.

Note. smolagents uses MCP SDK 2, which conflicts with CrewAI's pinned MCP SDK 1. Keep them in separate environments.

Tested from smolagents 1.26.

Native tools

"""A smolagents `ToolCallingAgent` that routes support tickets with a MIMIR decision tool.

Install `mimir-decisions[local,smolagents]`, set `HF_TOKEN` for the Inference Providers model, then
run `make example NAME=smolagents/agent`.
"""

from typing import Final

from smolagents import InferenceClientModel, Model, ToolCallingAgent

from mimir import Choice, Decider, Mimir
from mimir.integrations.smolagents import as_smolagents_tool

TEAMS: Final = {
    "billing": "Billing: payments, refunds and invoices",
    "security": "Security: account access, passwords and fraud",
    "shipping": "Shipping: deliveries, tracking and returns",
}
INSTRUCTIONS: Final = (
    "Route the customer's ticket with route_ticket, then tell the customer which team will "
    "answer. If the decision is deferred, say that a person will review the ticket."
)


def build_agent(decider: Decider, model: Model) -> ToolCallingAgent:
    """The support agent, answering with `decider`."""
    route_ticket = decider.tool(
        "route_ticket",
        Choice("Which team should handle this ticket?", TEAMS),
        description="Route a customer support ticket to the team that owns it.",
    )
    return ToolCallingAgent(
        tools=[as_smolagents_tool(route_ticket)], model=model, instructions=INSTRUCTIONS
    )


def main() -> None:
    agent = build_agent(Mimir.from_pretrained("Mythologic/MIMIR-1"), InferenceClientModel())
    print(agent.run("I was charged twice for order 4412."))


if __name__ == "__main__":
    main()

Over MCP

"""A smolagents `ToolCallingAgent` using MIMIR's MCP server over stdio, through `MCPClient`.

The server runs `examples/tools.yaml`, so the agent sees `route_ticket` and passes only the
ticket. The smolagents MCP client needs the MCP SDK 1, so the server runs in its own
environment through `uvx`. Install `smolagents[mcp]`, `mcp<2` and `uv`, set `HF_TOKEN`, then
run `make example NAME=smolagents/mcp_agent`.
"""

from collections.abc import Sequence
from pathlib import Path
from typing import Final

from mcp import StdioServerParameters
from smolagents import InferenceClientModel, MCPClient, Model, Tool, ToolCallingAgent

TOOLS: Final = Path(__file__).parents[1] / "tools.yaml"
SERVER: Final = (
    "uvx",
    ("--from", "mimir-decisions[local,mcp]", "mimir-decisions", "mcp", "--tools", str(TOOLS)),
)
INSTRUCTIONS: Final = (
    "Route the customer's ticket with route_ticket, then tell the customer which team will "
    "answer. If the decision is deferred, say that a person will review the ticket."
)


def mimir_server(command: str, arguments: Sequence[str]) -> MCPClient:
    """MIMIR's MCP server, started as `command` with `arguments`."""
    parameters = StdioServerParameters(command=command, args=list(arguments))
    return MCPClient(parameters, structured_output=True)


def build_agent(tools: Sequence[Tool], model: Model) -> ToolCallingAgent:
    """The support agent, with the server's `tools`."""
    return ToolCallingAgent(tools=list(tools), model=model, instructions=INSTRUCTIONS)


def main() -> None:
    with mimir_server(*SERVER) as tools:
        agent = build_agent(tools, InferenceClientModel())
        print(agent.run("I was charged twice for order 4412."))


if __name__ == "__main__":
    main()