Skip to content

Commit f820f7f

Browse files
CTX7-879: Context7 Vercel AI SDK Tools (#1122)
* feat: context7 vercel ai sdk tools package * ci: remove master target condition on test action * ci: manual actions trigger for test * ci: always run all tests * update tests and imports * ci: typecheck command * fix: tests * docs: add readme * update folder structure and tool descriptions * tests: fix test env vars * ci: bump pnpm version * update ai sdk step count api * Add stopWhen to docstring examples * update tests * update prompt name * update context7 agent name * update tool description var to description * make context7agent config optional * ci: add changeset * make context7-sdk peer deps * update pnpm-lock.yaml * update package name to @upstash/context7-tools-ai-sdk * remove check workflow * update pnpm lock * make the agent a class instead of function * replace all name instances * add agent generate test * simplify agent config * fix refs
1 parent d65c580 commit f820f7f

24 files changed

+1183
-21
lines changed

.changeset/slimy-dancers-wait.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@upstash/context7-tools-ai-sdk": minor
3+
---
4+
5+
Initial release of `@upstash/context7-tools-ai-sdk` - Vercel AI SDK integration for Context7.
6+
7+
### Features
8+
9+
- **Tools**: `resolveLibrary()` and `getLibraryDocs()` tools compatible with AI SDK's `generateText` and `streamText`
10+
- **Agent**: Pre-configured `Context7Agent` that handles the multi-step documentation retrieval workflow automatically
11+

.github/workflows/canary-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Setup pnpm
2929
uses: pnpm/action-setup@v4
3030
with:
31-
version: 9
31+
version: 10
3232

3333
- name: Configure npm authentication
3434
run: |

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- name: Setup pnpm
2828
uses: pnpm/action-setup@v4
2929
with:
30-
version: 9
30+
version: 10
3131

3232
- name: Configure npm authentication
3333
run: |
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
name: Build Check
1+
name: Test
22

33
on:
4-
push:
5-
branches: [master]
64
pull_request:
5+
push:
76
branches: [master]
7+
workflow_dispatch:
88

99
concurrency:
1010
group: ${{ github.workflow }}-${{ github.ref }}
1111
cancel-in-progress: true
1212

1313
jobs:
14-
build:
15-
name: Build
14+
test:
1615
runs-on: ubuntu-latest
1716
steps:
18-
- name: Checkout repository
19-
uses: actions/checkout@v6
17+
- uses: actions/checkout@v4
2018

21-
- name: Setup Node.js
19+
- name: Setup Node
2220
uses: actions/setup-node@v4
2321
with:
2422
node-version: "20"
2523

2624
- name: Setup pnpm
2725
uses: pnpm/action-setup@v4
2826
with:
29-
version: 9
27+
version: 10
3028

3129
- name: Get pnpm store directory
3230
id: pnpm-cache
@@ -42,14 +40,24 @@ jobs:
4240
restore-keys: |
4341
${{ runner.os }}-pnpm-store-
4442
45-
- name: Install dependencies
43+
- name: Install Dependencies
4644
run: pnpm install --frozen-lockfile
4745

48-
- name: Run linter
49-
run: pnpm run lint:check
46+
- name: Lint
47+
run: pnpm lint:check
48+
49+
- name: Format
50+
run: pnpm format:check
51+
52+
- name: Build
53+
run: pnpm build
5054

51-
- name: Check formatting
52-
run: pnpm run format:check
55+
- name: Typecheck
56+
run: pnpm typecheck
5357

54-
- name: Build project
55-
run: pnpm run build
58+
- name: Test
59+
run: pnpm test
60+
env:
61+
AWS_REGION: ${{ secrets.AWS_REGION }}
62+
AWS_BEARER_TOKEN_BEDROCK: ${{ secrets.AWS_BEARER_TOKEN_BEDROCK }}
63+
CONTEXT7_API_KEY: ${{ secrets.CONTEXT7_API_KEY }}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
"build": "pnpm -r run build",
1111
"build:sdk": "pnpm --filter @upstash/context7-sdk build",
1212
"build:mcp": "pnpm --filter @upstash/context7-mcp build",
13+
"build:ai-sdk": "pnpm --filter @upstash/context7-tools-ai-sdk build",
14+
"typecheck": "pnpm -r run typecheck",
1315
"test": "pnpm -r run test",
1416
"test:sdk": "pnpm --filter @upstash/context7-sdk test",
17+
"test:tools-ai-sdk": "pnpm --filter @upstash/context7-tools-ai-sdk test",
1518
"clean": "pnpm -r run clean && rm -rf node_modules",
1619
"lint": "pnpm -r run lint",
1720
"lint:check": "pnpm -r run lint:check",

packages/mcp/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"description": "MCP server for Context7",
66
"scripts": {
77
"build": "tsc && chmod 755 dist/index.js",
8-
"test": "echo \"Error: no test specified\" && exit 1",
8+
"test": "echo \"No tests yet\"",
9+
"typecheck": "tsc --noEmit",
910
"lint": "eslint .",
1011
"lint:check": "eslint .",
1112
"format": "prettier --write .",

packages/sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"build": "tsup",
77
"test": "vitest run",
88
"test:watch": "vitest",
9+
"typecheck": "tsc --noEmit",
910
"dev": "tsup --watch",
1011
"clean": "rm -rf dist",
1112
"lint": "eslint .",

packages/tools-ai-sdk/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { defineConfig } from "eslint/config";
2+
import tseslint from "typescript-eslint";
3+
import eslintPluginPrettier from "eslint-plugin-prettier";
4+
5+
export default defineConfig(
6+
{
7+
// Base ESLint configuration
8+
ignores: ["node_modules/**", "build/**", "dist/**", ".git/**", ".github/**"],
9+
},
10+
{
11+
files: ["**/*.ts", "**/*.tsx"],
12+
languageOptions: {
13+
ecmaVersion: 2020,
14+
sourceType: "module",
15+
parser: tseslint.parser,
16+
parserOptions: {
17+
project: "./tsconfig.json",
18+
tsconfigRootDir: import.meta.dirname,
19+
},
20+
globals: {
21+
// Add Node.js globals
22+
process: "readonly",
23+
require: "readonly",
24+
module: "writable",
25+
console: "readonly",
26+
},
27+
},
28+
// Settings for all files
29+
linterOptions: {
30+
reportUnusedDisableDirectives: true,
31+
},
32+
plugins: {
33+
"@typescript-eslint": tseslint.plugin,
34+
prettier: eslintPluginPrettier,
35+
},
36+
rules: {
37+
// TypeScript recommended rules
38+
...tseslint.configs.recommended.rules,
39+
// TypeScript rules
40+
"@typescript-eslint/explicit-module-boundary-types": "off",
41+
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
42+
"@typescript-eslint/no-explicit-any": "warn",
43+
// Prettier integration
44+
"prettier/prettier": "error",
45+
},
46+
}
47+
);

packages/tools-ai-sdk/package.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"name": "@upstash/context7-tools-ai-sdk",
3+
"version": "0.1.0",
4+
"description": "Context7 tools for Vercel AI SDK",
5+
"type": "module",
6+
"main": "./dist/index.cjs",
7+
"module": "./dist/index.js",
8+
"types": "./dist/index.d.ts",
9+
"exports": {
10+
".": {
11+
"types": "./dist/index.d.ts",
12+
"import": "./dist/index.js",
13+
"require": "./dist/index.cjs"
14+
},
15+
"./agent": {
16+
"types": "./dist/agent.d.ts",
17+
"import": "./dist/agent.js",
18+
"require": "./dist/agent.cjs"
19+
}
20+
},
21+
"files": [
22+
"dist"
23+
],
24+
"scripts": {
25+
"build": "tsup",
26+
"dev": "tsup --watch",
27+
"test": "vitest run",
28+
"typecheck": "tsc --noEmit",
29+
"clean": "rm -rf dist"
30+
},
31+
"peerDependencies": {
32+
"@upstash/context7-sdk": ">=0.1.0",
33+
"ai": ">=5.0.0",
34+
"zod": ">=3.24.0"
35+
},
36+
"devDependencies": {
37+
"@upstash/context7-sdk": "workspace:*",
38+
"ai": "^5.0.0",
39+
"zod": "^3.24.0",
40+
"@ai-sdk/amazon-bedrock": "^3.0.55",
41+
"@types/node": "^22.13.14",
42+
"dotenv": "^17.2.3",
43+
"tsup": "^8.5.1",
44+
"typescript": "^5.8.2",
45+
"vitest": "^4.0.13"
46+
},
47+
"repository": {
48+
"type": "git",
49+
"url": "git+https://github.com/upstash/context7.git",
50+
"directory": "packages/tools-ai-sdk"
51+
},
52+
"keywords": [
53+
"context7",
54+
"ai-sdk",
55+
"vercel",
56+
"documentation",
57+
"agent",
58+
"upstash"
59+
],
60+
"author": "Upstash",
61+
"license": "MIT",
62+
"bugs": {
63+
"url": "https://github.com/upstash/context7/issues"
64+
},
65+
"homepage": "https://github.com/upstash/context7#readme",
66+
"publishConfig": {
67+
"access": "public"
68+
}
69+
}

0 commit comments

Comments
 (0)