Reach out

Command Palette

Search for a command to run...

[Agents]

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

The core of an agent relies on its tool usage capabilities, enabling it to use and call tools and workflows depending on the task it must accomplish.

Built into our API, we provide connector tools such as websearch, code_interpreter, image_generation and document_library. However, you can also use standard function tool calling by defining a JSON schema for your function.

You can also leverage our MCP Orchestration to implement local Function Calling, visit our Local MCP docs for further details.

For more information regarding function calling, we recommend to visit our function calling docs.

Creating an Agent with Function Calling

We need to define our function that we want our model to call when needed, in this case, the function is a dummy for demonstration purposes.

1from typing import Dict
2
3def get_european_central_bank_interest_rate(date: str) -> Dict[str, str]:
4    """
5    Retrieve the real interest rate of the European Central Bank for a given date.
6
7    Parameters:
8    - date (str): The date for which to retrieve the interest rate in the format YYYY-MM-DD.
9
10    Returns:
11    - dict: A dictionary containing the date and the corresponding interest rate.
12    """
13    # This is a mock implementation. In a real scenario, you would fetch this data from an API or database.
14    # For demonstration, let's assume the interest rate is fixed at 2.5% for any date.
15    interest_rate = "2.5%"
16
17    return {
18        "date": date,
19        "interest_rate": interest_rate
20    }

Once defined, we provide a Shema corresponding to the same function.

1ecb_interest_rate_agent = client.beta.agents.create(
2    model="mistral-medium-2505",
3    description="Can find the current interest rate of the European central bank",
4    name="ecb-interest-rate-agent",
5    tools=[
6        {
7            "type": "function",
8            "function": {
9                "name": "get_european_central_bank_interest_rate",
10                "description": "Retrieve the real interest rate of European central bank.",
11                "parameters": {
12                    "type": "object",
13                    "properties": {
14                        "date": {
15                            "type": "string",
16                        },
17                    },
18                    "required": [
19                        "date",
20                    ]
21                },
22            },
23        },
24    ],
25)

We need to define our function that we want our model to call when needed, in this case, the function is a dummy for demonstration purposes.

1async function getEuropeanentralBankInterestRate(date: str) {
2  /*
3    Retrieve the real interest rate of the European Central Bank for a given date.
4
5    Parameters:
6    - date (str): The date for which to retrieve the interest rate in the format YYYY-MM-DD.
7
8    Returns:
9    - dict: A dictionary containing the date and the corresponding interest rate.
10    */
11  // This is a mock implementation. In a real scenario, you would fetch this data from an API or database.
12  // For demonstration, let's assume the interest rate is fixed at 2.5% for any date.
13  let interestRate = '2.5%';
14
15  return {
16    date: date,
17    interestRate: interestRate,
18  };
19}

Once defined, we provide a Shema corresponding to the same function.

1let ecbInterestRateAgent = await client.beta.agents.create({
2  model: 'mistral-medium-2505',
3  description:
4    'Can find the current interest rate of the European central bank',
5  name: 'ecb-interest-rate-agent',
6  tools: [
7    {
8      type: 'function',
9      function: {
10        name: 'getEuropeanCentralBankInterestRate',
11        description:
12          'Retrieve the real interest rate of European central bank.',
13        parameters: {
14          type: 'object',
15          properties: {
16            date: {
17              type: 'string',
18            },
19          },
20          required: ['date'],
21        },
22      },
23    },
24  ],
25});
1curl --location "https://api.mistral.ai/v1/agents" \
2     --header 'Content-Type: application/json' \
3     --header 'Accept: application/json' \
4     --header "Authorization: Bearer $MISTRAL_API_KEY" \
5     --data '{
6     "model": "mistral-medium-2505",
7     "name": "ecb-interest-rate-agent",
8     "description": "Can find the current interest rate of the European central bank",
9     "instructions": "You can provide interest rate and information regarding the European central bank.",
10     "tools": [
11         {
12             "function": {
13                 "name": "get_european_central_bank_interest_rate",
14                 "parameters": {
15                     "type": "object",
16                     "properties": {
17                         "date": {
18                             "type": "string"
19                         }
20                     },
21                     "required": ["date"]
22                 },
23                 "description": "Retrieve the real interest rate of European central bank."
24             },
25             "type": "function"
26         }
27     ]
28 }'
29
1{
2  "model": "mistral-medium-2505",
3  "name": "ecb-interest-rate-agent",
4  "description": "Can find the current interest rate of the European central bank",
5  "id": "ag_06835a34f2c476518000c372a505c2c4",
6  "version": 0,
7  "created_at": "2025-05-27T11:34:39.175924Z",
8  "updated_at": "2025-05-27T11:34:39.175926Z",
9  "instructions": "You can provide interest rate and information regarding the European central bank.",
10  "tools": [
11    {
12      "function": {
13        "name": "get_european_central_bank_interest_rate",
14        "parameters": {
15          "type": "object",
16          "properties": {
17            "date": {
18              "type": "string"
19            }
20          },
21          "required": ["date"]
22        },
23        "description": "Retrieve the real interest rate of European central bank.",
24        "strict": false
25      },
26      "type": "function"
27    }
28  ],
29  "completion_args": {
30    "stop": null,
31    "presence_penalty": null,
32    "frequency_penalty": null,
33    "temperature": 0.3,
34    "top_p": null,
35    "max_tokens": null,
36    "random_seed": null,
37    "prediction": null,
38    "response_format": null,
39    "tool_choice": "auto"
40  },
41  "handoffs": null,
42  "object": "agent"
43}

Using an Agent with Function Calling

1response = client.beta.conversations.start(
2    agent_id=ecb_interest_rate_agent.id,
3    inputs=[{"role": "user", "content": "Whats the current 2025 real interest rate?"}]
4)
1{
2  "conversation_id": "conv_06835a34f58773bd8000f46c0d11e42c",
3  "outputs": [
4    {
5      "tool_call_id": "6TI17yZkV",
6      "name": "get_european_central_bank_interest_rate",
7      "arguments": "{\"date\": \"2024-06-06\"}",
8      "object": "entry",
9      "type": "function.call",
10      "created_at": "2025-05-27T11:34:39.610632Z",
11      "completed_at": null,
12      "id": "fc_06835a34f9c47fc88000e0370a295774"
13    }
14  ],
15  "usage": {
16    "prompt_tokens": 91,
17    "completion_tokens": 29,
18    "total_tokens": 120,
19    "connector_tokens": null,
20    "connectors": null
21  },
22  "object": "conversation.response"
23}

The model will output either an answer, or a function call, we need to detect and return the result of the expected function.

1from mistralai import FunctionResultEntry
2import json
3
4if response.outputs[-1].type == "function.call" and response.outputs[-1].name == "get_european_central_bank_interest_rate":
5
6    # Running our function
7    function_result = json.dumps(get_european_central_bank_interest_rate(**json.loads(response.outputs[-1].arguments)))
8
9    # Providing the result to our Agent
10    user_function_calling_entry = FunctionResultEntry(
11        tool_call_id=response.outputs[-1].tool_call_id,
12        result=function_result,
13    )
14
15    # Retrieving the final response
16    response = client.beta.conversations.append(
17        conversation_id=response.conversation_id,
18        inputs=[user_function_calling_entry]
19    )
20    print(response.outputs[-1])
21else:
22
23    # In case the model did not call our function
24    print(response.outputs[-1])
1let response = await client.beta.conversations.start({
2  agentId: (await ecbInterestRateAgent).id,
3  inputs: [
4    { role: 'user', content: 'Whats the current 2025 real interest rate?' },
5  ],
6});
1{
2  "conversation_id": "conv_06835a34f58773bd8000f46c0d11e42c",
3  "outputs": [
4    {
5      "tool_call_id": "6TI17yZkV",
6      "name": "get_european_central_bank_interest_rate",
7      "arguments": "{\"date\": \"2024-06-06\"}",
8      "object": "entry",
9      "type": "function.call",
10      "created_at": "2025-05-27T11:34:39.610632Z",
11      "completed_at": null,
12      "id": "fc_06835a34f9c47fc88000e0370a295774"
13    }
14  ],
15  "usage": {
16    "prompt_tokens": 91,
17    "completion_tokens": 29,
18    "total_tokens": 120,
19    "connector_tokens": null,
20    "connectors": null
21  },
22  "object": "conversation.response"
23}

The model will output either an answer, or a function call, we need to detect and return the result of the expected function.

First, let's add the following imports:

1import type {
2  FunctionResultEntry,
3  MessageOutputEntry,
4} from '@mistralai/mistralai/models/components/index.js';

Then, we check whether or not a function call was triggered:

1let output = response.outputs[response.outputs.length - 1];
2
3if (
4  output.type === 'function.call' &&
5  output.name === 'getEuropeanCentralBankInterestRate'
6) {
7  const args = output.arguments as unknown as string;
8  const parsedArgs = JSON.parse(args);
9  const date = parsedArgs.date;
10
11  const functionResult = JSON.stringify(
12    await getEuropeanCentralBankInterestRate(date)
13  );
14
15  const toolCallId = output.toolCallId;
16
17  const userFunctionCallingEntry: FunctionResultEntry = {
18    toolCallId: toolCallId,
19    result: functionResult,
20  };
21
22  response = await client.beta.conversations.append({
23    conversationId: response.conversationId,
24    conversationAppendRequest: {
25      inputs: [userFunctionCallingEntry],
26    },
27  });
28
29  const finalEntry = response.outputs[response.outputs.length - 1];
30  const finalMessageOutputEntry = finalEntry as MessageOutputEntry;
31  console.log(finalMessageOutputEntry);
32} else {
33  console.log(output);
34}

For starting a conversation:

1curl --location "https://api.mistral.ai/v1/conversations" \
2     --header 'Content-Type: application/json' \
3     --header 'Accept: application/json' \
4     --header "Authorization: Bearer $MISTRAL_API_KEY" \
5     --data '{
6     "inputs": [
7         {
8             "role": "user",
9             "content": "Whats the current 2025 real interest rate?",
10             "object": "entry",
11             "type": "message.input"
12         }
13     ],
14     "stream": false,
15     "agent_id": "<agent_id>"
16 }'

For continuing a conversation:

1curl --location "https://api.mistral.ai/v1/conversations/<conv_id>" \
2     --header 'Content-Type: application/json' \
3     --header 'Accept: application/json' \
4     --header "Authorization: Bearer $MISTRAL_API_KEY" \
5     --data '{
6     "inputs": [
7         {
8             "tool_call_id": "6TI17yZkV",
9             "result": "{\"date\": \"2024-06-06\", \"interest_rate\": \"2.5%\"}",
10             "object": "entry",
11             "type": "function.result"
12         }
13     ],
14     "stream": false,
15     "store": true,
16     "handoff_execution": "server"
17 }'
1{
2  "content": "The current interest rate as of June 6, 2024, is 2.5%. This information is relevant for understanding the economic conditions in 2025.",
3  "object": "entry",
4  "type": "message.output",
5  "created_at": "2025-05-27T11:34:40.142767Z",
6  "completed_at": "2025-05-27T11:34:40.801117Z",
7  "id": "msg_06835a35024879bc80005b1bf9ab0f12",
8  "agent_id": "ag_06835a34f2c476518000c372a505c2c4",
9  "model": "mistral-medium-2505",
10  "role": "assistant"
11}