From ea2d82abab7206937937d34c2cd981c161f6d832 Mon Sep 17 00:00:00 2001 From: akbxr Date: Thu, 1 May 2025 20:27:03 +0700 Subject: [PATCH] feat: Add environment variable support for minimum token configuration --- README.md | 21 ++++++++++++++++++++- package.json | 1 + src/index.ts | 17 ++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cda2039..27a25c6 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,25 @@ If you prefer to run the MCP server in a Docker container: ``` *Note: This is an example configuration. Please refer to the specific examples for your MCP client (like Cursor, VS Code, etc.) earlier in this README to adapt the structure (e.g., `mcpServers` vs `servers`). Also, ensure the image name in `args` matches the tag used during the `docker build` command.* +### Environment Variables + +- `DEFAULT_MINIMUM_TOKENS`: Set the minimum token count for documentation retrieval (default: 5000). + +Examples: +```json +{ + "mcpServers": { + "context7": { + "command": "npx", + "args": ["-y", "@upstash/context7-mcp@latest"], + "env": { + "DEFAULT_MINIMUM_TOKENS": "10000" + } + } + } +} +``` + ### Available Tools - `resolve-library-id`: Resolves a general library name into a Context7-compatible library ID. @@ -236,7 +255,7 @@ If you prefer to run the MCP server in a Docker container: - `get-library-docs`: Fetches documentation for a library using a Context7-compatible library ID. - `context7CompatibleLibraryID` (required) - `topic` (optional): Focus the docs on a specific topic (e.g., "routing", "hooks") - - `tokens` (optional, default 5000): Max number of tokens to return. Values less than 5000 are automatically increased to 5000. + - `tokens` (optional, default 5000): Max number of tokens to return. Values less than the configured `DEFAULT_MINIMUM_TOKENS` value are automatically increased to that value. ## Development diff --git a/package.json b/package.json index 7dd1379..9188559 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "homepage": "https://github.com/upstash/context7#readme", "dependencies": { "@modelcontextprotocol/sdk": "^1.8.0", + "dotenv": "^16.5.0", "zod": "^3.24.2" }, "devDependencies": { diff --git a/src/index.ts b/src/index.ts index 69e430d..328160a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,8 +5,23 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" import { z } from "zod"; import { searchLibraries, fetchLibraryDocumentation } from "./lib/api.js"; import { formatSearchResults } from "./lib/utils.js"; +import dotenv from "dotenv"; -const DEFAULT_MINIMUM_TOKENS = 5000; +// Load environment variables from .env file if present +dotenv.config(); + +// Get DEFAULT_MINIMUM_TOKENS from environment variable or use default +let DEFAULT_MINIMUM_TOKENS = 5000; +if (process.env.DEFAULT_MINIMUM_TOKENS) { + const parsedValue = parseInt(process.env.DEFAULT_MINIMUM_TOKENS, 10); + if (!isNaN(parsedValue) && parsedValue > 0) { + DEFAULT_MINIMUM_TOKENS = parsedValue; + } else { + console.error( + `Warning: Invalid DEFAULT_MINIMUM_TOKENS value provided in environment variable. Using default value of 5000` + ); + } +} // Create server instance const server = new McpServer({