|
| 1 | +# Upstash Context7 AI SDK |
| 2 | + |
| 3 | +`@upstash/context7-tools-ai-sdk` provides [Vercel AI SDK](https://ai-sdk.dev/) compatible tools and agents that give your AI applications access to up to date library documentation through Context7. |
| 4 | + |
| 5 | +Use this package to: |
| 6 | + |
| 7 | +- Add documentation lookup tools to your AI SDK workflows with `generateText` or `streamText` |
| 8 | +- Create documentation aware agents using the pre-configured `Context7Agent` |
| 9 | +- Build RAG pipelines that retrieve accurate, version specific code examples |
| 10 | + |
| 11 | +The package provides two main tools: |
| 12 | + |
| 13 | +- `resolveLibrary` - Searches Context7's database to find the correct library ID |
| 14 | +- `getLibraryDocs` - Fetches documentation for a specific library with optional topic filtering |
| 15 | + |
| 16 | +## Quick Start |
| 17 | + |
| 18 | +### Install |
| 19 | + |
| 20 | +```bash |
| 21 | +npm install @upstash/context7-tools-ai-sdk @upstash/context7-sdk ai zod |
| 22 | +``` |
| 23 | + |
| 24 | +### Get API Key |
| 25 | + |
| 26 | +Get your API key from [Context7](https://context7.com) |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +### Using Tools with `generateText` |
| 31 | + |
| 32 | +```typescript |
| 33 | +import { resolveLibrary, getLibraryDocs } from "@upstash/context7-tools-ai-sdk"; |
| 34 | +import { generateText, stepCountIs } from "ai"; |
| 35 | +import { openai } from "@ai-sdk/openai"; |
| 36 | + |
| 37 | +const { text } = await generateText({ |
| 38 | + model: openai("gpt-4o"), |
| 39 | + prompt: "How do I use React Server Components?", |
| 40 | + tools: { |
| 41 | + resolveLibrary: resolveLibrary(), |
| 42 | + getLibraryDocs: getLibraryDocs(), |
| 43 | + }, |
| 44 | + stopWhen: stepCountIs(5), |
| 45 | +}); |
| 46 | + |
| 47 | +console.log(text); |
| 48 | +``` |
| 49 | + |
| 50 | +### Using the Context7 Agent |
| 51 | + |
| 52 | +The package provides a pre-configured agent that handles the multi-step workflow automatically: |
| 53 | + |
| 54 | +```typescript |
| 55 | +import { Context7Agent } from "@upstash/context7-tools-ai-sdk"; |
| 56 | +import { anthropic } from "@ai-sdk/anthropic"; |
| 57 | + |
| 58 | +const agent = new Context7Agent({ |
| 59 | + model: anthropic("claude-sonnet-4-20250514"), |
| 60 | +}); |
| 61 | + |
| 62 | +const { text } = await agent.generate({ |
| 63 | + prompt: "How do I set up routing in Next.js?", |
| 64 | +}); |
| 65 | + |
| 66 | +console.log(text); |
| 67 | +``` |
| 68 | + |
| 69 | +## Configuration |
| 70 | + |
| 71 | +### Environment Variables |
| 72 | + |
| 73 | +Set your API key via environment variable: |
| 74 | + |
| 75 | +```sh |
| 76 | +CONTEXT7_API_KEY=ctx7sk-... |
| 77 | +``` |
| 78 | + |
| 79 | +Then use tools and agents without explicit configuration: |
| 80 | + |
| 81 | +```typescript |
| 82 | +const tool = resolveLibrary(); // Uses CONTEXT7_API_KEY automatically |
| 83 | +``` |
| 84 | + |
| 85 | +## Docs |
| 86 | + |
| 87 | +See the [documentation](https://context7.com/docs/agentic-tools/ai-sdk/getting-started) for details. |
| 88 | + |
| 89 | +## Contributing |
| 90 | + |
| 91 | +### Running tests |
| 92 | + |
| 93 | +```sh |
| 94 | +pnpm test |
| 95 | +``` |
| 96 | + |
| 97 | +### Building |
| 98 | + |
| 99 | +```sh |
| 100 | +pnpm build |
| 101 | +``` |
0 commit comments