Skip to main content
Mastra is a TypeScript framework for building AI agents. It provides first-class MCP support through its MCPClient class. By connecting Context7 as an MCP server, your Mastra agents can access up-to-date library documentation, enabling them to generate accurate code and answer technical questions with current API references. For more details on how Mastra handles MCP, see the Mastra MCP documentation.

Setup

1

Install the MCP Package

Add the Mastra MCP package to your project:
npm install @mastra/mcp@latest
2

Create an MCP Client

Configure an MCPClient that connects to Context7:
import { MCPClient } from "@mastra/mcp";

const context7 = new MCPClient({
  id: "context7",
  servers: {
    context7: {
      url: new URL("https://mcp.context7.com/mcp"),
      requestInit: {
        headers: {
          CONTEXT7_API_KEY: process.env.CONTEXT7_API_KEY!,
        },
      },
    },
  },
});
Set CONTEXT7_API_KEY in your environment with your API key from the Context7 dashboard.
3

Connect to Your Agent

Pass the Context7 tools to your Mastra agent:
import { Agent } from "@mastra/core/agent";

const agent = new Agent({
  id: "coding-assistant",
  name: "Coding Assistant",
  instructions: "You are a helpful coding assistant with access to up-to-date library documentation via Context7.",
  model: "anthropic/claude-sonnet-4-6",
  tools: await context7.listTools(),
});

How It Works

Once connected, your Mastra agents will query Context7 for relevant library documentation. This helps agents:
  • Answer technical questions with accurate, up-to-date API references
  • Generate code that follows current library patterns and best practices
  • Avoid hallucinating outdated or incorrect API usage
  • Provide version-specific guidance for any supported library