Requirements
@meilisearch/ai-sdk adds Meilisearch search tools to the Vercel AI SDK, letting an LLM decide when to search your data and how to use the results.
Requirements
- A Meilisearch project with at least one index containing documents
- A search API key for that project
- An API key from an LLM provider (this example uses OpenAI, but any provider supported by the AI SDK works)
Install
npm install ai @ai-sdk/openai @meilisearch/ai-sdk
Add your credentials to your environment:
.env
MEILISEARCH_URL=https://your-project.meilisearch.io
MEILISEARCH_KEY=your-search-api-key
OPENAI_API_KEY=your-openai-api-key
Give an agent a search tool
import { generateText, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { meilisearchSearch } from "@meilisearch/ai-sdk";
const { text } = await generateText({
model: openai("gpt-5.4-mini"),
prompt: "Find two movies about time travel, then summarize the difference.",
tools: {
search: meilisearchSearch({
host: "MEILISEARCH_URL",
apiKey: "MEILISEARCH_KEY",
indexUid: "movies",
description: "Search the movies database",
}),
},
stopWhen: stepCountIs(5),
});
console.log(text);
The model decides whether and how many times to call search before answering. The description field tells it what the index contains and when to use it.
Agentic movie search demo
See a movie recommendation agent built with this integration
AI SDK demo
Next.js chatbox example application
Use Meilisearch MCP
The AI SDK can also connect to the Meilisearch MCP server. It wraps the tools exposed by the server and passes them to the model like any other tool. This is the quickest way to get started, because you do not configure any search tool yourself. If you need typed tools and control over search parameters, use meilisearchSearch as shown above.
Before you start, enable the MCP route and create an API key for MCP. The key needs the search, documents.get, and indexes.get actions, otherwise calls to listIndexes and describeIndex fail. The default search API key is not enough.
Install the MCP client package:
npm install @ai-sdk/mcp
Create an MCP client, load its tools, and pass them to generateText:
import { createMCPClient } from "@ai-sdk/mcp";
import { generateText, isStepCount } from "ai";
import { openai } from "@ai-sdk/openai";
const mcpClient = await createMCPClient({
transport: {
type: "http",
url: "https://your-project.meilisearch.io/mcp",
headers: { Authorization: "Bearer YOUR_API_KEY" },
},
});
try {
const tools = await mcpClient.tools();
const { text } = await generateText({
model: openai("gpt-5.4-mini"),
tools,
// Allow several steps: list indexes, describe, search, then answer
stopWhen: isStepCount(5),
prompt: "Recommend recent action movies about revenge",
});
console.log(text);
} finally {
await mcpClient.close();
}
For the list of available tools and limitations, see the MCP guide. If the client cannot connect, see troubleshooting.
Going further
Getting started with agentic search
Build a multi-turn chatbot with streaming responses.
Display source documents
Show users which documents an agent used to answer.
Configure guardrails
Keep agents on topic and grounded in your data.
Handle errors and fallbacks
Make your agent resilient to search and LLM failures.
For the full list of available tools, see the SDK repository.
