Mistral
MillionShort search for Mistral models, as a function tool with the Mistral SDKs in Python and TypeScript, or the hosted MCP server in Mistral's Agents SDK.
Copy as MarkdownOpen in ChatGPT (opens in a new tab)Open in Claude (opens in a new tab)
RecipePython and TypeScriptNothing of ours to installWorks with no keyChecked 2026-11-19
With the Mistral SDKs, chat.complete takes function tools: the model asks for a call, your code runs the search, and the loop sends the results back until the model answers. Mistral's separate Agents SDK for Python (the mistralai-agents package) can also reach our hosted MCP server itself, which gives the agent every tool of the server (the MCP page lists them).
1Install
pip install mistralai mistralai-agents requests
export MILLIONSHORT_API_KEY=ms-YOUR_API_KEYnpm install @mistralai/mistralai
export MILLIONSHORT_API_KEY=ms-YOUR_API_KEYThe examples read your Mistral key from MISTRAL_API_KEY. Get a MillionShort key in the dashboard; an agent can ask for a trial key by itself. mistralai-agents is only for the MCP section further down.
2Minimal example
import json
import os
import requests
from mistralai.client import Mistral
def millionshort_search(query: str) -> str:
r = requests.post(
"https://api.millionshort.com/v1/search",
headers={"Authorization": f"Bearer {os.environ['MILLIONSHORT_API_KEY']}"},
json={"query": query, "remove_top_sites": 1000, "num_results": 5},
timeout=30,
)
r.raise_for_status()
return json.dumps([{"title": x["title"], "url": x["url"], "snippet": x.get("snippet")} for x in r.json()["data"]["results"]])
tools = [{"type": "function", "function": {
"name": "millionshort_search",
"description": "Search the web with MillionShort, leaving out the 1,000 most popular sites.",
"parameters": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]},
}}]
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
messages = [{"role": "user", "content": "Find independent blogs about home espresso."}]
while True:
message = client.chat.complete(model="mistral-large-latest", messages=messages, tools=tools).choices[0].message
messages.append(message)
if not message.tool_calls:
break
for call in message.tool_calls:
args = call.function.arguments # a JSON string or an already parsed dict
content = millionshort_search(**(json.loads(args) if isinstance(args, str) else args))
messages.append({"role": "tool", "name": call.function.name, "tool_call_id": call.id, "content": content})
print(message.content)import { Mistral } from '@mistralai/mistralai';
async function millionshortSearch(query: string): Promise<string> {
const res = await fetch('https://api.millionshort.com/v1/search', {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.MILLIONSHORT_API_KEY}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ query, remove_top_sites: 1000, num_results: 5 }),
});
if (!res.ok) throw new Error(`search failed: ${res.status}`);
const { data } = (await res.json()) as { data: { results: { title: string; url: string; snippet?: string }[] } };
return JSON.stringify(data.results.map(({ title, url, snippet }) => ({ title, url, snippet })));
}
const tools = [{ type: 'function' as const, function: {
name: 'millionshort_search',
description: 'Search the web with MillionShort, leaving out the 1,000 most popular sites.',
parameters: { type: 'object', properties: { query: { type: 'string' } }, required: ['query'] },
} }];
const client = new Mistral({ apiKey: process.env.MISTRAL_API_KEY });
const messages: Parameters<typeof client.chat.complete>[0]['messages'] = [{ role: 'user', content: 'Find independent blogs about home espresso.' }];
while (true) {
const message = (await client.chat.complete({ model: 'mistral-large-latest', messages, tools })).choices[0]?.message;
if (!message?.toolCalls?.length) {
console.log(message?.content);
break;
}
messages.push({ ...message, role: 'assistant' });
for (const call of message.toolCalls) {
const args = typeof call.function.arguments === 'string' ? JSON.parse(call.function.arguments) : call.function.arguments;
messages.push({ role: 'tool', name: call.function.name, toolCallId: call.id, content: await millionshortSearch(String(args.query)) });
}
}3Tell it when to search
Mistral's built-in web search is a tool of its Agents and Conversations APIs that runs only when you add it, and chat.complete has only the tools you pass. Put a line like this in a system message at the start of messages (or in instructions= on an Agents SDK agent):
Use the MillionShort search tool for anything recent, niche, or that needs a source. To reach lesser-known sites, set remove_top_sites (for example 1000). Cite the URLs it returns.
4Check it works
Run the example, then ask:
Search MillionShort for forum threads about restoring a cast iron skillet, with the 1,000 most popular sites removed. List the five most useful with their links.
After the first call, the message's tool calls (tool_calls in Python, toolCalls in TypeScript) name millionshort_search with a query.
With MCP: the Agents SDK
In the Agents SDK, agents.RemoteMCP is an MCP server the agent reaches over HTTP. It does not take the key itself: auth_header_env_vars maps a header to the name of an environment variable that holds the header's full value, so put the whole Bearer value in one (here MILLIONSHORT_AUTH_HEADER). Without a key, leave auth_header_env_vars out. The Agents SDK needs Python 3.12 or later.
# export MILLIONSHORT_AUTH_HEADER="Bearer ms-YOUR_API_KEY"
import asyncio
from mistralai import agents
agent = agents.Agent(
model="mistral-large-4",
instructions="Use the millionshort search tool for anything recent or niche, and cite the URLs.",
mcps={"millionshort": agents.RemoteMCP(url="https://mcp.millionshort.com/mcp", auth_header_env_vars={"Authorization": "MILLIONSHORT_AUTH_HEADER"})},
)
async def main() -> None:
result = await agent.run("Find independent blogs about home espresso, without the 1,000 most popular sites.")
if isinstance(result, agents.Completed):
print(result.text)
print([call.name for call in result.tool_calls]) # e.g. millionshort.search
asyncio.run(main())The tools are named <key>.<tool>, so the agent sees millionshort.search, millionshort.get_domain_info, millionshort.list_sources and so on. A missing variable fails before the run starts.
MCP configuration
The same address for every client: https://mcp.millionshort.com/mcp, streamable HTTP.
| Mode | How | What the agent gets |
|---|---|---|
| API key | Send Authorization: Bearer ms-YOUR_API_KEY | Every tool, at your plan's limits; calls use your credits |
| No key | Send no Authorization header | search, list_sources and a few more; one source and up to 5 results a search, 10 free searches a day |
Tools
| Tool | What it does | Without a key | Cost |
|---|---|---|---|
search | Searches the web and the specialist sources, with the popularity, domain, page-type, topic, site and date filters | Yes: one source, up to 5 results | 1 credit per source searched, first 10 results |
get_domain_info | Popularity and risk for up to 25 sites | No | 1 credit |
list_sources | The sources, page types and topics search accepts | Yes: source names | Free |
| The tools of the other endpoints | extract, map, crawl, places and more: the MCP page lists them | A few, such as places | What the API call behind each costs |
The function tool in step 2 is search alone; the Agents SDK server gives the agent every tool of the server.
Settings
| Variable | Meaning |
|---|---|
MILLIONSHORT_API_KEY | Your key, read by the function tool |
| MILLIONSHORT_AUTH_HEADER | The full header value, Bearer ms-..., read by the Agents SDK for RemoteMCP |
| MISTRAL_API_KEY | Your Mistral key |
Costs and limits
A tool call costs what the API call behind it costs: 1 credit per source searched for the first 10 results, 1 credit for a domain lookup, nothing for the list of sources, the price of the endpoint for the other tools, and nothing at all in the no-key mode. An agent that loops can make many calls: give it a step limit, and ask for fewer results when a few will do. Credits and limits has the numbers.
When it fails
| What you see | Why | Fix |
|---|---|---|
401 AUTH_MISSING_KEY or AUTH_INVALID_KEY | The key is missing or wrong | Set MILLIONSHORT_API_KEY; keys start with ms- |
400 INVALID_PARAM | A field name or value the API does not take | The message names the field; the search reference lists them all |
429 RATE_LIMITED or CAPACITY | Too many calls a minute, or no free capacity right now | Wait the Retry-After seconds, then retry |
429 INSUFFICIENT_CREDITS | The plan's credits are used up | Stop and add credits; do not retry |
| The Agents SDK stops before the run with a missing variable | The variable named in auth_header_env_vars is not set where the agent runs | Export it with the full Bearer ms-... value |
401 from the MCP server with the Agents SDK | The variable holds the bare key without Bearer | Put Bearer in front of the key in the variable |
Related
- Agent tool use: when to give a model tools, and what to tell it.
- Le Chat: the same server in Mistral's assistant.
- Other model APIs: OpenAI, Cohere, OpenRouter, Ollama.
- Search API reference and MCP server.
- Mistral's own documentation, read on 2026-11-19: Function calling, Connectors and MCP in agents, MCP reference.