Skip to content

Commit 5e11d35

Browse files
CTX7-855: TypeScript SDK (#1061)
* init sdk and monorepo * init sdk and monorepo * comment cleanup * remove bun.lock * cleanup: remove sdk package and deps * update tsconfigs * chore: add temp changelog * ci: update scripts * ci: update github workflows * fmt: workflows * fix: search libraries response type * ci: update pack-mcpb script * feat: init ts sdk * feat: add commands and http client * update keywords and author * update eslint config for mcp * include license and readme in build * update release.yaml * add readme symlink * include readme in mcp package * chore: add canary release workflow and update configs * chore: format files * ci: add changeset check workflow * canary release trigger * ci: login to npm before release * bump mcp version on package * remove pr trigger from canary release * ci: remove package input * finalize sdk with tests and proper types * address reviews * add client tests * feat: use env var automatically * add docs * remove docs changes (moving to separate PR) * remove docs * update build config with tsup * cleanup comments * fix error hangling on http client * minor fixes * address reviews * add txt pagination feature * remove unnecessary types * update default format * cleanup * add mcp lint command * update format scripts * fix: update eslint and tsconfig with update * add readme * make default format json * change docType to mode * update basic usage * update changeset
1 parent 6cba71f commit 5e11d35

File tree

23 files changed

+2782
-0
lines changed

23 files changed

+2782
-0
lines changed

.changeset/heavy-heads-build.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@upstash/context7-sdk": minor
3+
---
4+
5+
Initial release of the Context7 TypeScript SDK
6+
7+
- HTTP/REST client for the Context7 API
8+
- `searchLibrary()` - Search for libraries in the Context7 database
9+
- `getDocs()` - Retrieve documentation with filtering options
10+
- Environment variable support for API key configuration

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
],
99
"scripts": {
1010
"build": "pnpm -r run build",
11+
"build:sdk": "pnpm --filter @upstash/context7-sdk build",
12+
"build:mcp": "pnpm --filter @upstash/context7-mcp build",
1113
"test": "pnpm -r run test",
14+
"test:sdk": "pnpm --filter @upstash/context7-sdk test",
1215
"clean": "pnpm -r run clean && rm -rf node_modules",
1316
"lint": "pnpm -r run lint",
1417
"lint:check": "pnpm -r run lint:check",

packages/sdk/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 Upstash, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/sdk/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Upstash Context7 SDK
2+
3+
`@upstash/context7-sdk` is an HTTP/REST based client for TypeScript, built on top of the [Context7 API](https://context7.com).
4+
5+
## Why Context7?
6+
7+
LLMs rely on outdated or generic training data about the libraries you use. This leads to:
8+
9+
- Code examples based on year-old training data
10+
- Hallucinated APIs that don't exist
11+
- Generic answers for old package versions
12+
13+
Context7 solves this by providing up-to-date, version-specific documentation and code examples directly from the source. Use this SDK to:
14+
15+
- Build AI agents with accurate, current documentation context
16+
- Create RAG pipelines with reliable library documentation
17+
- Power code generation tools with real API references
18+
19+
## Quick Start
20+
21+
### Install
22+
23+
```bash
24+
npm install @upstash/context7-sdk
25+
```
26+
27+
### Get API Key
28+
29+
Get your API key from [Context7](https://context7.com)
30+
31+
## Basic Usage
32+
33+
```ts
34+
import { Context7 } from "@upstash/context7-sdk";
35+
36+
const client = new Context7({
37+
apiKey: "<CONTEXT7_API_KEY>",
38+
});
39+
40+
// Search for libraries in the Context7 database
41+
const libraries = await client.searchLibrary("react");
42+
console.log(libraries.results);
43+
44+
// Query the documentation with specific topics
45+
const filteredDocs = await client.getDocs("/facebook/react", {
46+
topic: "hooks",
47+
limit: 10,
48+
page: 1,
49+
});
50+
51+
// Get documentation as JSON by default
52+
const docs = await client.getDocs("/vercel/next.js");
53+
console.log(docs.snippets);
54+
55+
// Get documentation as TXT
56+
const codeDocs = await client.getDocs("/mongodb/docs", {
57+
format: "txt",
58+
mode: "code",
59+
});
60+
console.log(codeDocs.content);
61+
```
62+
63+
## Configuration
64+
65+
### Environment Variables
66+
67+
You can set your API key via environment variable:
68+
69+
```sh
70+
CONTEXT7_API_KEY=ctx7sk-...
71+
```
72+
73+
Then initialize without options:
74+
75+
```ts
76+
const client = new Context7();
77+
```
78+
79+
## Docs
80+
81+
See the [documentation](https://context7.com/docs/sdks/ts/getting-started) for details.
82+
83+
## Contributing
84+
85+
### Running tests
86+
87+
```sh
88+
pnpm test
89+
```
90+
91+
### Building
92+
93+
```sh
94+
pnpm build
95+
```

packages/sdk/eslint.config.js

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/sdk/package.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "@upstash/context7-sdk",
3+
"version": "0.0.1",
4+
"description": "JavaScript/TypeScript SDK for Context7",
5+
"scripts": {
6+
"build": "tsup",
7+
"test": "vitest run",
8+
"test:watch": "vitest",
9+
"dev": "tsup --watch",
10+
"clean": "rm -rf dist",
11+
"lint": "eslint .",
12+
"lint:check": "eslint ."
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/upstash/context7.git",
17+
"directory": "packages/sdk"
18+
},
19+
"keywords": [
20+
"context7",
21+
"sdk",
22+
"documentation",
23+
"ai",
24+
"upstash"
25+
],
26+
"author": "Upstash",
27+
"license": "MIT",
28+
"type": "module",
29+
"exports": {
30+
".": {
31+
"import": "./dist/client.js",
32+
"require": "./dist/client.cjs"
33+
}
34+
},
35+
"main": "./dist/client.cjs",
36+
"module": "./dist/client.js",
37+
"types": "./dist/client.d.ts",
38+
"files": [
39+
"dist"
40+
],
41+
"bugs": {
42+
"url": "https://github.com/upstash/context7/issues"
43+
},
44+
"homepage": "https://github.com/upstash/context7#readme",
45+
"devDependencies": {
46+
"@types/node": "^22.13.14",
47+
"dotenv": "^17.2.3",
48+
"tsup": "^8.5.1",
49+
"typescript": "^5.8.2",
50+
"vitest": "^4.0.13"
51+
}
52+
}

packages/sdk/prettier.config.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @type {import('prettier').Config}
3+
*/
4+
const config = {
5+
endOfLine: "lf",
6+
singleQuote: false,
7+
tabWidth: 2,
8+
trailingComma: "es5",
9+
printWidth: 100,
10+
arrowParens: "always",
11+
};
12+
13+
export default config;

0 commit comments

Comments
 (0)