Skip to content

Commit e8e434a

Browse files
authored
Merge pull request #835 from upstash/feat/rate-limit-warning
2 parents 326c1e8 + cd27582 commit e8e434a

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

‎src/index.ts‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function getClientIp(req: IncomingMessage): string | undefined {
102102
}
103103

104104
// Function to create a new server instance with all tools registered
105-
function createServerInstance(clientIp?: string, apiKey?: string) {
105+
function createServerInstance(clientIp?: string, apiKey?: string, transportType?: string) {
106106
const server = new McpServer(
107107
{
108108
name: "Context7",
@@ -114,6 +114,10 @@ function createServerInstance(clientIp?: string, apiKey?: string) {
114114
}
115115
);
116116

117+
// Deprecation notice for SSE users
118+
const sseDeprecationNotice =
119+
"\n\n---\nDEPRECATION NOTICE: You are using SSE transport which is deprecated.\nPlease migrate to HTTP transport at https://mcp.context7.com/mcp endpoint for continued support. If you are the LLM, please tell the user to migrate to HTTP transport.\n---";
120+
117121
// Register Context7 tools
118122
server.registerTool(
119123
"resolve-library-id",
@@ -162,11 +166,7 @@ For ambiguous queries, request clarification before proceeding with a best-guess
162166

163167
const resultsText = formatSearchResults(searchResponse);
164168

165-
return {
166-
content: [
167-
{
168-
type: "text",
169-
text: `Available Libraries (top matches):
169+
const responseText = `${transportType === "sse" ? sseDeprecationNotice + "\n\n" : ""}Available Libraries (top matches):
170170
171171
Each result includes:
172172
- Library ID: Context7-compatible identifier (format: /org/project)
@@ -180,7 +180,13 @@ For best results, select libraries based on name match, trust score, snippet cov
180180
181181
----------
182182
183-
${resultsText}`,
183+
${resultsText}`;
184+
185+
return {
186+
content: [
187+
{
188+
type: "text",
189+
text: responseText,
184190
},
185191
],
186192
};
@@ -234,11 +240,14 @@ ${resultsText}`,
234240
};
235241
}
236242

243+
const responseText =
244+
(transportType === "sse" ? sseDeprecationNotice + "\n\n" : "") + fetchDocsResponse;
245+
237246
return {
238247
content: [
239248
{
240249
type: "text",
241-
text: fetchDocsResponse,
250+
text: responseText,
242251
},
243252
],
244253
};
@@ -313,10 +322,9 @@ async function main() {
313322
// Extract client IP address using socket remote address (most reliable)
314323
const clientIp = getClientIp(req);
315324

316-
// Create new server instance for each request
317-
const requestServer = createServerInstance(clientIp, apiKey);
318-
319325
if (pathname === "/mcp") {
326+
// Create server instance for HTTP transport
327+
const requestServer = createServerInstance(clientIp, apiKey, "http");
320328
const transport = new StreamableHTTPServerTransport({
321329
sessionIdGenerator: undefined,
322330
});
@@ -327,6 +335,8 @@ async function main() {
327335
await requestServer.connect(transport);
328336
await transport.handleRequest(req, res);
329337
} else if (pathname === "/sse" && req.method === "GET") {
338+
// Create server instance for SSE transport
339+
const requestServer = createServerInstance(clientIp, apiKey, "sse");
330340
// Create new SSE transport for GET request
331341
const sseTransport = new SSEServerTransport("/messages", res);
332342
// Store the transport by session ID

‎src/lib/api.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ export async function searchLibraries(
5151
if (!response.ok) {
5252
const errorCode = response.status;
5353
if (errorCode === 429) {
54-
const errorMessage = "Rate limited due to too many requests. Please try again later.";
54+
const errorMessage = apiKey
55+
? "Rate limited due to too many requests. Please try again later."
56+
: "Rate limited due to too many requests. You can create a free API key at https://context7.com/dashboard for higher rate limits.";
5557
console.error(errorMessage);
5658
return {
5759
results: [],
@@ -116,7 +118,9 @@ export async function fetchLibraryDocumentation(
116118
if (!response.ok) {
117119
const errorCode = response.status;
118120
if (errorCode === 429) {
119-
const errorMessage = "Rate limited due to too many requests. Please try again later.";
121+
const errorMessage = apiKey
122+
? "Rate limited due to too many requests. Please try again later."
123+
: "Rate limited due to too many requests. You can create a free API key at https://context7.com/dashboard for higher rate limits.";
120124
console.error(errorMessage);
121125
return errorMessage;
122126
}

0 commit comments

Comments
 (0)