How do I connect Tavily MCP with Vercel AI SDK?

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 search
  • tavily-extract: Intelligent data extraction from web pages
  • tavily-map: Creates structured maps of websites
  • tavily-crawl: Systematically explores websites

Make sure to get your Tavily API key from tavily.com and replace YOUR_TAVILY_API_KEY in the URL.

Resources:

I hope that helps!