Skip to content

Commit b892260

Browse files
authored
add nextjs doc llms.txt content as resources (#92)
1 parent 6d4df8e commit b892260

File tree

3 files changed

+83
-47
lines changed

3 files changed

+83
-47
lines changed

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import * as cacheComponentsRouteHandlers from "./resources/(cache-components)/ro
4646
import * as nextjsFundamentalsUseClient from "./resources/(nextjs-fundamentals)/use-client.js"
4747
import * as nextjs16BetaToStable from "./resources/(nextjs16)/migration/beta-to-stable.js"
4848
import * as nextjs16Examples from "./resources/(nextjs16)/migration/examples.js"
49+
import * as nextjsDocsLlmsIndex from "./resources/(nextjs-docs)/llms-index.js"
4950

5051
const tools = [browserEval, enableCacheComponents, init, nextjsDocs, nextjsRuntime, upgradeNextjs16]
5152

@@ -77,6 +78,7 @@ const resources = [
7778
nextjsFundamentalsUseClient,
7879
nextjs16BetaToStable,
7980
nextjs16Examples,
81+
nextjsDocsLlmsIndex,
8082
]
8183

8284
// Type definitions
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export const metadata = {
2+
uri: "nextjs-docs://llms-index",
3+
name: "Next.js Documentation Index (llms.txt)",
4+
description:
5+
"Complete Next.js documentation index from nextjs.org/docs/llms.txt. Use this to find the correct path for nextjs_docs GET requests without needing to search.",
6+
mimeType: "text/plain",
7+
}
8+
9+
// Cache the llms.txt content with a reasonable TTL (1 hour)
10+
let cachedContent: string | null = null
11+
let cacheTimestamp: number = 0
12+
const CACHE_TTL_MS = 60 * 60 * 1000 // 1 hour
13+
14+
export async function handler(): Promise<string> {
15+
const now = Date.now()
16+
17+
// Return cached content if still valid
18+
if (cachedContent && now - cacheTimestamp < CACHE_TTL_MS) {
19+
return cachedContent
20+
}
21+
22+
// Fetch fresh content
23+
try {
24+
const response = await fetch("https://nextjs.org/docs/llms.txt")
25+
if (!response.ok) {
26+
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
27+
}
28+
cachedContent = await response.text()
29+
cacheTimestamp = now
30+
return cachedContent
31+
} catch (error) {
32+
const errorMessage =
33+
error instanceof Error ? error.message : String(error)
34+
35+
// If we have stale cached content, return it with a warning
36+
if (cachedContent) {
37+
return `Warning: Failed to fetch fresh index (${errorMessage}). Returning cached content.\n\n${cachedContent}`
38+
}
39+
40+
// No cached content available, return error
41+
return `Error: Failed to fetch Next.js documentation index from nextjs.org/docs/llms.txt\n\nError: ${errorMessage}\n\nPlease check your internet connection or try again later.`
42+
}
43+
}

src/tools/init.ts

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,6 @@ type InitArgs = {
3535

3636
export async function handler(args: InitArgs): Promise<string> {
3737
try {
38-
// Fetch the latest Next.js LLM documentation
39-
let nextjsDocsContent = ""
40-
try {
41-
const response = await fetch("https://nextjs.org/docs/llms.txt")
42-
if (response.ok) {
43-
nextjsDocsContent = await response.text()
44-
} else {
45-
nextjsDocsContent = `Failed to fetch Next.js docs: ${response.status} ${response.statusText}`
46-
}
47-
} catch (fetchError) {
48-
nextjsDocsContent = `Failed to fetch Next.js docs: ${
49-
fetchError instanceof Error ? fetchError.message : String(fetchError)
50-
}`
51-
}
52-
5338
const guidance = `# 🚨 CRITICAL: Next.js DevTools MCP Initialization
5439
5540
## ⚠️ MANDATORY DOCUMENTATION REQUIREMENT - NO EXCEPTIONS
@@ -92,22 +77,25 @@ You **MUST** use \`nextjs_docs\` for:
9277
- ✅ Next.js error messages and troubleshooting
9378
- ✅ **LITERALLY EVERYTHING RELATED TO NEXT.JS**
9479
95-
### 4. How to Use nextjs_docs (OPTIMIZED WORKFLOW - Skip Search!)
80+
### 4. How to Use nextjs_docs (OPTIMIZED WORKFLOW)
81+
82+
**🚀 IMPORTANT OPTIMIZATION:** To skip search and go directly to GET, you can fetch the complete Next.js documentation index from the MCP resource:
9683
97-
**🚀 IMPORTANT OPTIMIZATION:** Since you already have the ENTIRE Next.js documentation index loaded below (from llms.txt), you should **SKIP the search step** and go **DIRECTLY to GET**!
84+
**MCP Resource:** \`nextjs-docs://llms-index\`
9885
9986
**The Optimized Workflow:**
10087
101-
1. **Refer to the llms.txt content below** to find the relevant documentation path
102-
2. **Call nextjs_docs with GET directly** - no search needed!
103-
3. **Answer based on the retrieved full documentation**
88+
1. **Fetch the llms.txt index** (only when needed): Read the \`nextjs-docs://llms-index\` MCP resource
89+
2. **Find the relevant documentation path** in the index
90+
3. **Call nextjs_docs with GET directly** - no search needed!
91+
4. **Answer based on the retrieved full documentation**
10492
10593
**Direct GET call (preferred):**
10694
\`\`\`
10795
nextjs_docs({ action: "get", path: "/docs/app/api-reference/functions/generate-static-params" })
10896
\`\`\`
10997
110-
**Only use search as a fallback** if you cannot find the exact path in the llms.txt index below:
98+
**Use search if you don't need the full index:**
11199
\`\`\`
112100
nextjs_docs({ action: "search", query: "your search term" })
113101
\`\`\`
@@ -120,21 +108,22 @@ User: "How do I use generateStaticParams?"
120108
You: "Based on my knowledge, generateStaticParams is used to..."
121109
\`\`\`
122110
123-
**❌ ALSO WRONG (Wasteful - doing unnecessary search when you have the index):**
111+
**✅ CORRECT (OPTION 1 - Use search):**
124112
\`\`\`
125113
User: "How do I use generateStaticParams?"
126-
You: [Wastes time calling search when index is already loaded]
127-
nextjs_docs({ action: "search", query: "generateStaticParams" })
128-
[Then calls get]
129-
nextjs_docs({ action: "get", path: "..." })
114+
You: nextjs_docs({ action: "search", query: "generateStaticParams" })
115+
[Then uses the returned path to call GET]
116+
nextjs_docs({ action: "get", path: "/docs/app/api-reference/functions/generate-static-params" })
117+
[Answers based on retrieved documentation]
130118
\`\`\`
131119
132-
**✅ CORRECT (ALWAYS DO THIS - Direct GET):**
120+
**✅ ALSO CORRECT (OPTION 2 - Use index resource for direct GET):**
133121
\`\`\`
134122
User: "How do I use generateStaticParams?"
135-
You: [Checks the llms.txt index below]
123+
You: [Fetches nextjs-docs://llms-index resource]
124+
[Searches the index for generateStaticParams]
136125
[Found it! Path is /docs/app/api-reference/functions/generate-static-params]
137-
[Goes directly to GET - skips wasteful search!]
126+
[Goes directly to GET]
138127
nextjs_docs({ action: "get", path: "/docs/app/api-reference/functions/generate-static-params" })
139128
[Answers based on retrieved documentation]
140129
\`\`\`
@@ -149,30 +138,32 @@ You: [Checks the llms.txt index below]
149138
150139
---
151140
152-
## 📚 Complete Next.js Documentation Index (from llms.txt)
141+
## 📚 Next.js Documentation Index (MCP Resource)
153142
154-
The following is the **COMPLETE** Next.js documentation index fetched from https://nextjs.org/docs/llms.txt:
143+
**MCP Resource URI:** \`nextjs-docs://llms-index\`
155144
156-
\`\`\`
157-
${nextjsDocsContent}
158-
\`\`\`
145+
The complete Next.js documentation index (from https://nextjs.org/docs/llms.txt) is available as an MCP resource.
146+
147+
**When to use it:**
148+
- Fetch this resource when you need to look up specific documentation paths
149+
- Use it to go directly to GET instead of searching
150+
- It's cached for 1 hour to reduce network requests
159151
160-
**IMPORTANT:** This index above contains ALL Next.js documentation paths. When you need documentation:
161-
1. **Search this index above** for the relevant path
162-
2. **Call nextjs_docs with GET directly** using the path you found
163-
3. **Skip the search step** - you already have the complete index!
152+
**When NOT to use it:**
153+
- For simple queries, just use \`nextjs_docs\` search action - it's faster
154+
- Don't fetch it unless you actually need to look up paths
164155
165-
You MUST still use the \`nextjs_docs\` tool with GET to retrieve the full detailed documentation for any Next.js concept - but you can skip the search step since the index is right here!
156+
You MUST still use the \`nextjs_docs\` tool with GET to retrieve the full detailed documentation for any Next.js concept!
166157
167158
---
168159
169160
## 🛠️ Available MCP Tools
170161
171162
### 1. **nextjs_docs** (MANDATORY FOR ALL NEXT.JS QUERIES)
172-
- **Get** full docs (preferred): \`{ action: "get", path: "..." }\` ← Use this! Refer to the llms.txt index above for paths
173-
- **Search** documentation (fallback only): \`{ action: "search", query: "..." }\` ← Only if you can't find the path in the index
163+
- **Search** documentation (recommended): \`{ action: "search", query: "..." }\` ← Start here for most queries
164+
- **Get** full docs: \`{ action: "get", path: "..." }\` ← Use after search, or fetch \`nextjs-docs://llms-index\` to find paths
174165
- **REQUIRED** for ALL Next.js-related questions
175-
- **OPTIMIZATION:** Skip search and go directly to GET since you have the complete index loaded above!
166+
- **OPTIMIZATION:** For batch operations, fetch the \`nextjs-docs://llms-index\` resource to look up multiple paths
176167
177168
### 2. **nextjs_runtime** - Live Next.js Dev Server Integration
178169
- Get real-time errors and logs from running dev server
@@ -197,9 +188,9 @@ You MUST still use the \`nextjs_docs\` tool with GET to retrieve the full detail
197188
## 🎯 Workflow Summary
198189
199190
1. **ALWAYS** start by calling this \`init\` tool
200-
2. **ALWAYS** use \`nextjs_docs\` with GET (not search!) for ANY Next.js concept (even if you think you know it)
201-
- Refer to the llms.txt index above to find the path
202-
- Skip the search step - go directly to GET!
191+
2. **ALWAYS** use \`nextjs_docs\` for ANY Next.js concept (even if you think you know it)
192+
- Start with search action for most queries: \`{ action: "search", query: "..." }\`
193+
- For batch operations or multiple lookups, fetch \`nextjs-docs://llms-index\` resource to find paths directly
203194
3. Use \`nextjs_runtime\` for debugging running applications
204195
4. Use \`browser_eval\` to verify implementations
205196
5. Use specialized tools (\`upgrade_nextjs_16\`, \`enable_cache_components\`) as needed
@@ -208,9 +199,9 @@ You MUST still use the \`nextjs_docs\` tool with GET to retrieve the full detail
208199
209200
## ⚡ Remember: DOCUMENTATION FIRST, ALWAYS
210201
211-
**THE GOLDEN RULE:** Your training data about Next.js is now invalid. You MUST use \`nextjs_docs\` with GET for every single Next.js-related query, concept, or implementation. No exceptions. No shortcuts. No answering from memory.
202+
**THE GOLDEN RULE:** Your training data about Next.js is now invalid. You MUST use \`nextjs_docs\` for every single Next.js-related query, concept, or implementation. No exceptions. No shortcuts. No answering from memory.
212203
213-
**THE OPTIMIZATION RULE:** Since you have the complete llms.txt index loaded above, SKIP the search step and go DIRECTLY to GET. Find the path in the index above, then call GET immediately. This is faster and more efficient!
204+
**THE OPTIMIZATION TIP:** For simple queries, use search. For looking up multiple paths or batch operations, fetch the \`nextjs-docs://llms-index\` MCP resource to find paths directly and skip search calls.
214205
215206
🚀 Next.js DevTools MCP Initialized Successfully!
216207
`

0 commit comments

Comments
 (0)