refactor: update token limits and improve constant organization for documentation retrieval

This commit is contained in:
enesgules 2025-09-05 14:09:55 +03:00
parent d44bfafda3
commit 02d87cf33f
2 changed files with 13 additions and 8 deletions

View File

@ -53,7 +53,7 @@ Check out our [project addition guide](./docs/adding-projects.md) to learn how t
> [!WARNING] > [!WARNING]
> **SSE Protocol Deprecation Notice** > **SSE Protocol Deprecation Notice**
> >
> The Server-Sent Events (SSE) transport protocol is deprecated and it's endpoint will be removed in upcoming releases. Please use HTTP or stdio transport methods instead. > The Server-Sent Events (SSE) transport protocol is deprecated and its endpoint will be removed in upcoming releases. Please use HTTP or stdio transport methods instead.
<details> <details>
<summary><b>Installing via Smithery</b></summary> <summary><b>Installing via Smithery</b></summary>
@ -975,7 +975,7 @@ Context7 MCP provides the following tools that LLMs can use:
- `get-library-docs`: Fetches documentation for a library using a Context7-compatible library ID. - `get-library-docs`: Fetches documentation for a library using a Context7-compatible library ID.
- `context7CompatibleLibraryID` (required): Exact Context7-compatible library ID (e.g., `/mongodb/docs`, `/vercel/next.js`) - `context7CompatibleLibraryID` (required): Exact Context7-compatible library ID (e.g., `/mongodb/docs`, `/vercel/next.js`)
- `topic` (optional): Focus the docs on a specific topic (e.g., "routing", "hooks") - `topic` (optional): Focus the docs on a specific topic (e.g., "routing", "hooks")
- `tokens` (optional, default 10000): Max number of tokens to return. Values less than the default value of 10000 are automatically increased to 10000. - `tokens` (optional, default 5000): Max number of tokens to return. Values less than 1000 are automatically increased to 1000.
## 🛟 Tips ## 🛟 Tips

View File

@ -12,12 +12,17 @@ import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import { Command } from "commander"; import { Command } from "commander";
import { IncomingMessage } from "http"; import { IncomingMessage } from "http";
const DEFAULT_MINIMUM_TOKENS = 1000; /** Minimum allowed tokens for documentation retrieval */
const MINIMUM_TOKENS = 1000;
/** Default tokens when none specified */
const DEFAULT_TOKENS = 5000;
/** Default HTTP server port */
const DEFAULT_PORT = 3000;
// Parse CLI arguments using commander // Parse CLI arguments using commander
const program = new Command() const program = new Command()
.option("--transport <stdio|http>", "transport type", "stdio") .option("--transport <stdio|http>", "transport type", "stdio")
.option("--port <number>", "port for HTTP transport", "3000") .option("--port <number>", "port for HTTP transport", DEFAULT_PORT.toString())
.option("--api-key <key>", "API key for authentication") .option("--api-key <key>", "API key for authentication")
.allowUnknownOption() // let MCP Inspector / other wrappers pass through extra flags .allowUnknownOption() // let MCP Inspector / other wrappers pass through extra flags
.parse(process.argv); .parse(process.argv);
@ -200,14 +205,14 @@ ${resultsText}`,
.describe("Topic to focus documentation on (e.g., 'hooks', 'routing')."), .describe("Topic to focus documentation on (e.g., 'hooks', 'routing')."),
tokens: z tokens: z
.preprocess((val) => (typeof val === "string" ? Number(val) : val), z.number()) .preprocess((val) => (typeof val === "string" ? Number(val) : val), z.number())
.transform((val) => (val < DEFAULT_MINIMUM_TOKENS ? DEFAULT_MINIMUM_TOKENS : val)) .transform((val) => (val < MINIMUM_TOKENS ? MINIMUM_TOKENS : val))
.optional() .optional()
.describe( .describe(
`Maximum number of tokens of documentation to retrieve (default: ${DEFAULT_MINIMUM_TOKENS}). Higher values provide more context but consume more tokens.` `Maximum number of tokens of documentation to retrieve (default: ${DEFAULT_TOKENS}). Higher values provide more context but consume more tokens.`
), ),
}, },
}, },
async ({ context7CompatibleLibraryID, tokens = DEFAULT_MINIMUM_TOKENS, topic = "" }) => { async ({ context7CompatibleLibraryID, tokens = DEFAULT_TOKENS, topic = "" }) => {
const fetchDocsResponse = await fetchLibraryDocumentation( const fetchDocsResponse = await fetchLibraryDocumentation(
context7CompatibleLibraryID, context7CompatibleLibraryID,
{ {
@ -248,7 +253,7 @@ async function main() {
if (transportType === "http") { if (transportType === "http") {
// Get initial port from environment or use default // Get initial port from environment or use default
const initialPort = CLI_PORT ?? 3000; const initialPort = CLI_PORT ?? DEFAULT_PORT;
// Keep track of which port we end up using // Keep track of which port we end up using
let actualPort = initialPort; let actualPort = initialPort;
const httpServer = createServer(async (req, res) => { const httpServer = createServer(async (req, res) => {