-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Configuring docs mcp server #15001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
codyde
wants to merge
4
commits into
master
Choose a base branch
from
feat-docs-nextjs-mcp-server
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Configuring docs mcp server #15001
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {createMcpHandler} from 'mcp-handler'; | ||
import {z} from 'zod'; | ||
|
||
import {readDocContent} from '../../shared/docs-utils'; | ||
import {formatMatchAsBlock, searchIndex} from '../search/searchIndex'; | ||
|
||
const handler = createMcpHandler( | ||
server => { | ||
server.tool( | ||
'search_docs', | ||
'Search the precomputed markdown index and return matching documentation entry points.', | ||
{ | ||
query: z.string().min(1), | ||
limit: z.number().int().min(1).max(25).default(5), | ||
}, | ||
async ({query, limit}) => { | ||
const matches = await searchIndex(query, limit); | ||
const contentText = matches.length | ||
? matches.map(formatMatchAsBlock).join('\n\n') | ||
: 'No matches found.'; | ||
|
||
return { | ||
content: [{type: 'text', text: contentText}], | ||
}; | ||
} | ||
); | ||
|
||
server.tool( | ||
'get_doc', | ||
'Fetch raw markdown from the documentation exports. Reads local files when available, otherwise fetches from DOCS_PUBLIC_BASE.', | ||
{ | ||
path: z.string().min(1), | ||
}, | ||
async ({path}) => { | ||
const content = await readDocContent(path); | ||
return { | ||
content: [{type: 'text', text: content}], | ||
}; | ||
} | ||
); | ||
}, | ||
{ | ||
// Optional server options | ||
}, | ||
{ | ||
basePath: '/api', | ||
maxDuration: 60, | ||
verboseLogs: false, | ||
} | ||
); | ||
|
||
function normalizeRequest(request: Request): Request { | ||
const url = new URL(request.url); | ||
if (url.pathname.endsWith('/') && url.pathname.length > 1) { | ||
url.pathname = url.pathname.slice(0, -1); | ||
} | ||
|
||
return new Request(url.toString(), { | ||
method: request.method, | ||
headers: request.headers, | ||
body: request.body, | ||
// @ts-ignore - duplex is needed for streaming | ||
duplex: 'half', | ||
}); | ||
} | ||
|
||
function wrappedHandler(request: Request) { | ||
const normalizedRequest = normalizeRequest(request); | ||
return handler(normalizedRequest); | ||
} | ||
|
||
export {wrappedHandler as GET, wrappedHandler as POST, wrappedHandler as DELETE}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {NextRequest, NextResponse} from 'next/server'; | ||
|
||
import {mapMatchToResponse, searchIndex} from './searchIndex'; | ||
|
||
export const runtime = 'nodejs'; | ||
|
||
export async function GET(request: NextRequest) { | ||
const {searchParams} = new URL(request.url); | ||
const query = searchParams.get('q') ?? ''; | ||
const limitParam = searchParams.get('limit'); | ||
const limit = limitParam ? Math.min(25, Math.max(1, Number(limitParam))) : 10; | ||
|
||
try { | ||
const matches = await searchIndex(query, limit); | ||
const results = matches.map(mapMatchToResponse); | ||
|
||
return NextResponse.json({ | ||
query, | ||
limit, | ||
count: results.length, | ||
results, | ||
}); | ||
} catch (error) { | ||
return NextResponse.json( | ||
{ | ||
query, | ||
limit, | ||
error: error instanceof Error ? error.message : 'Unknown error', | ||
}, | ||
{status: 500} | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
import {promises as fs} from 'node:fs'; | ||
import path from 'node:path'; | ||
|
||
import {buildDocUrl} from '../../shared/docs-utils'; | ||
|
||
const SEARCH_INDEX_PATH = path.join(process.cwd(), 'public', 'search-index.json'); | ||
|
||
type RawSearchIndexEntry = { | ||
content: string; | ||
hierarchy: string[]; | ||
path: string; | ||
summary: string; | ||
title: string; | ||
}; | ||
|
||
type SearchIndexFile = { | ||
entries: RawSearchIndexEntry[]; | ||
generatedAt: string; | ||
total: number; | ||
}; | ||
|
||
export type SearchMatch = { | ||
hierarchy: string[]; | ||
matchedTokens: number; | ||
path: string; | ||
score: number; | ||
snippet: string | null; | ||
summary: string; | ||
title: string; | ||
}; | ||
|
||
type CachedEntry = RawSearchIndexEntry & { | ||
contentLower: string; | ||
hierarchyLower: string[]; | ||
pathLower: string; | ||
titleLower: string; | ||
}; | ||
|
||
let searchIndexPromise: Promise<CachedEntry[]> | null = null; | ||
|
||
async function loadSearchIndexInternal(): Promise<CachedEntry[]> { | ||
const raw = await fs.readFile(SEARCH_INDEX_PATH, 'utf8'); | ||
const parsed = JSON.parse(raw) as SearchIndexFile; | ||
return parsed.entries.map(entry => ({ | ||
...entry, | ||
pathLower: entry.path.toLowerCase(), | ||
titleLower: entry.title.toLowerCase(), | ||
hierarchyLower: entry.hierarchy.map(segment => segment.toLowerCase()), | ||
contentLower: entry.content.toLowerCase(), | ||
})); | ||
} | ||
|
||
export function ensureSearchIndex(): Promise<CachedEntry[]> { | ||
if (!searchIndexPromise) { | ||
searchIndexPromise = loadSearchIndexInternal().catch(error => { | ||
searchIndexPromise = null; | ||
throw error; | ||
}); | ||
} | ||
|
||
return searchIndexPromise; | ||
} | ||
|
||
function scoreEntry(entry: CachedEntry, tokens: string[]) { | ||
let score = 0; | ||
let matchedTokens = 0; | ||
|
||
for (const token of tokens) { | ||
let tokenMatched = false; | ||
|
||
if (entry.titleLower.includes(token)) { | ||
score += 6; | ||
tokenMatched = true; | ||
} | ||
|
||
if (entry.pathLower.includes(token)) { | ||
score += 4; | ||
tokenMatched = true; | ||
} | ||
|
||
if (entry.hierarchyLower.some(segment => segment.includes(token))) { | ||
score += 3; | ||
tokenMatched = true; | ||
} | ||
|
||
if (entry.contentLower.includes(token)) { | ||
score += 1; | ||
tokenMatched = true; | ||
} | ||
|
||
if (tokenMatched) { | ||
matchedTokens += 1; | ||
} | ||
} | ||
|
||
if (matchedTokens === 0) { | ||
return null; | ||
} | ||
|
||
score += getInstallBias(entry); | ||
|
||
return {score, matchedTokens}; | ||
} | ||
|
||
function buildSnippet(entry: CachedEntry, tokens: string[]): string | null { | ||
const lines = entry.content.split(/\r?\n/); | ||
for (const line of lines) { | ||
const lineLower = line.toLowerCase(); | ||
if (tokens.some(token => lineLower.includes(token))) { | ||
const trimmed = line.trim(); | ||
if (trimmed.length === 0) { | ||
continue; | ||
} | ||
return trimmed.length > 200 ? `${trimmed.slice(0, 199)}…` : trimmed; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
export async function searchIndex(query: string, limit: number): Promise<SearchMatch[]> { | ||
const tokens = query | ||
.toLowerCase() | ||
.split(/\s+/) | ||
.map(token => token.trim()) | ||
.filter(Boolean); | ||
|
||
if (tokens.length === 0) { | ||
return []; | ||
} | ||
|
||
const entries = await ensureSearchIndex(); | ||
const matches: SearchMatch[] = []; | ||
|
||
for (const entry of entries) { | ||
const scoreResult = scoreEntry(entry, tokens); | ||
if (!scoreResult) { | ||
continue; | ||
} | ||
|
||
matches.push({ | ||
path: entry.path, | ||
title: entry.title, | ||
hierarchy: entry.hierarchy, | ||
summary: entry.summary, | ||
snippet: buildSnippet(entry, tokens), | ||
score: scoreResult.score, | ||
matchedTokens: scoreResult.matchedTokens, | ||
}); | ||
} | ||
|
||
matches.sort((a, b) => { | ||
if (b.score !== a.score) { | ||
return b.score - a.score; | ||
} | ||
if (b.matchedTokens !== a.matchedTokens) { | ||
return b.matchedTokens - a.matchedTokens; | ||
} | ||
return a.path.localeCompare(b.path); | ||
}); | ||
|
||
return matches.slice(0, limit); | ||
} | ||
|
||
function getInstallBias(entry: CachedEntry): number { | ||
const segments = entry.pathLower.split('/'); | ||
const fileName = segments[segments.length - 1] ?? ''; | ||
const baseName = fileName.replace(/\.md$/, ''); | ||
|
||
let bias = 0; | ||
|
||
// Top-level platform doc like "platforms/react.md" | ||
if (segments[0] === 'platforms' && segments.length === 2) { | ||
bias += 40; | ||
} | ||
|
||
// JavaScript guide root doc like "platforms/javascript/guides/react.md" | ||
if ( | ||
segments[0] === 'platforms' && | ||
segments[1] === 'javascript' && | ||
segments[2] === 'guides' && | ||
segments.length === 4 | ||
) { | ||
bias += 50; | ||
} | ||
|
||
// Files under an install directory get a boost | ||
if (segments.includes('install')) { | ||
bias += 20; | ||
} | ||
|
||
// Common install filenames get additional weight | ||
if (['install', 'installation', 'setup', 'getting-started'].includes(baseName)) { | ||
bias += 25; | ||
} | ||
|
||
return bias; | ||
} | ||
|
||
export function formatMatchAsBlock(match: SearchMatch): string { | ||
const header = `# ${match.hierarchy.join(' > ')}`; | ||
const link = `[${match.title}](${match.path})`; | ||
const lines = [header, link]; | ||
|
||
if (match.snippet) { | ||
lines.push(match.snippet); | ||
} | ||
|
||
return lines.join('\n'); | ||
} | ||
|
||
export function mapMatchToResponse(match: SearchMatch) { | ||
return { | ||
path: match.path, | ||
title: match.title, | ||
hierarchy: match.hierarchy, | ||
summary: match.summary, | ||
snippet: match.snippet, | ||
url: buildDocUrl(match.path), | ||
score: match.score, | ||
matchedTokens: match.matchedTokens, | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.