Skip to content

LlamaIndex

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

mimir.integrations.llamaindex.as_llamaindex_tool converts a decision tool into a tool compatible with FunctionAgent, ReActAgent, and AgentWorkflow. The typed result is available as the tool output's raw_output.

LlamaIndex does not expose a hook that runs 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 LlamaIndex pipeline from outside the framework.

Tested from llama-index-core 0.14.25.

Native tools

"""A LlamaIndex `FunctionAgent` that routes support tickets with a MIMIR decision tool.

The typed result is each tool output's `raw_output`. Install `mimir-decisions[local,llamaindex]` and
`llama-index-llms-openai`, set `OPENAI_API_KEY`, then run `make example NAME=llamaindex/agent`.
"""

import asyncio
from typing import Final

from llama_index.core.agent.workflow import FunctionAgent
from llama_index.core.llms.function_calling import FunctionCallingLLM

from mimir import Choice, Decider, Mimir
from mimir.integrations.llamaindex import as_llamaindex_tool

MODEL: Final = "gpt-5.5"
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, llm: FunctionCallingLLM) -> FunctionAgent:
    """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 FunctionAgent(
        tools=[as_llamaindex_tool(route_ticket)], llm=llm, system_prompt=INSTRUCTIONS
    )


async def main() -> None:
    from llama_index.llms.openai import OpenAI

    agent = build_agent(Mimir.from_pretrained("Mythologic/MIMIR-1"), OpenAI(model=MODEL))
    print(await agent.run(user_msg="I was charged twice for order 4412."))


if __name__ == "__main__":
    asyncio.run(main())

Over MCP

"""A LlamaIndex `FunctionAgent` using MIMIR's MCP server over stdio.

The server runs `examples/tools.yaml`, so the agent sees `route_ticket` and passes only the
ticket. Install `llama-index-tools-mcp`, `llama-index-llms-openai` and `uv`, set
`OPENAI_API_KEY`, then run `make example NAME=llamaindex/mcp_agent`.
"""

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

from llama_index.core.agent.workflow import FunctionAgent
from llama_index.core.llms.function_calling import FunctionCallingLLM
from llama_index.core.tools import FunctionTool
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec

MODEL: Final = "gpt-5.5"
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."
)


async def mimir_tools(command: str, arguments: Sequence[str]) -> list[FunctionTool]:
    """The tools of MIMIR's MCP server, started as `command` with `arguments`."""
    client = BasicMCPClient(command, args=list(arguments), timeout=90)
    return await McpToolSpec(client=client).to_tool_list_async()


def build_agent(tools: Sequence[FunctionTool], llm: FunctionCallingLLM) -> FunctionAgent:
    """The support agent, with the server's `tools`."""
    return FunctionAgent(tools=list(tools), llm=llm, system_prompt=INSTRUCTIONS)


async def main() -> None:
    from llama_index.llms.openai import OpenAI

    agent = build_agent(await mimir_tools(*SERVER), OpenAI(model=MODEL))
    print(await agent.run(user_msg="I was charged twice for order 4412."))


if __name__ == "__main__":
    asyncio.run(main())