How to connect Tavily (https://www.tavily.com) MCP with the Vercel AI SDK
Asked v0 for the answer, and here’s what it gave me:
You can connect Tavily’s MCP (Model Context Protocol) server with the Vercel AI SDK using the new MCP client support introduced in AI SDK 4.2.
Here’s how to set it up:
Using Tavily’s Remote MCP Server
The easiest way is to use Tavily’s remote MCP server directly:
import { createMCPClient } from '@ai-sdk/mcp' const mcpClient = createMCPClient({ name: 'tavily', version: '1.0.0', transport: { type: 'http', url: 'https://mcp.tavily.com/mcp/?tavilyApiKey=YOUR_TAVILY_API_KEY' } })Using with AI SDK Functions
Once connected, you can use Tavily’s search tools in your AI SDK functions:
import { generateText } from 'ai' import { openai } from '@ai-sdk/openai' const { text } = await generateText({ model: openai('gpt-4'), prompt: 'Search for the latest news about AI developments', tools: await mcpClient.getTools(), // This includes tavily-search, tavily-extract, etc. })Available Tavily Tools
The Tavily MCP server provides several tools:
tavily-search: Real-time web searchtavily-extract: Intelligent data extraction from web pagestavily-map: Creates structured maps of websitestavily-crawl: Systematically explores websitesMake sure to get your Tavily API key from tavily.com and replace
YOUR_TAVILY_API_KEYin the URL.Resources:
I hope that helps!