# Fumadocs (Framework Mode, Next.js): AI 與 LLM
URL: /zh-TW/docs/next/integrations/llms
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/llms.zh-TW.mdx

讓 AI 代理能夠讀取您的文件，並加入 AI 聊天功能。

## 供 LLM 使用的 Markdown [#markdown-for-llms]

以 Markdown 格式將您的文件提供給 AI 代理：

```package-install
npx @fumadocs/cli feature llms
```

接著，您的網站會提供：

* `/llms.txt`：文件的索引，並連結至每個頁面的 Markdown。
* `/llms-full.txt`：所有頁面合併成的單一檔案。
* `/docs/ui.md`：單一頁面的 Markdown；位於 `/` 的頁面則為 `/index.md`。

CLI 會在 `lib/source.ts` 中加入 `docsLlms`，由它負責算繪這些檔案：

```ts title="lib/source.ts"
import { llms } from 'fumadocs-core/source';

export const docsLlms = llms(source, {
  // 每個頁面的 Markdown
  renderPage: async (page) => `# ${page.data.title} (${page.url})

${await page.data.getText('processed')}`,
  // 排除草稿；`llms.txt` 會改為連結到草稿的 HTML 頁面
  filter: (page) => page.slugs[0] !== 'drafts',
});
```

MDX 元件在 Markdown 中會維持 JSX 的形式。若要將其算繪為 Markdown，請為元件提供 [Markdown 形式](/docs/headless/utils/markdown#asmarkdown)，並[以頁面 props 的方式傳入](/docs/headless/mdx/remark-llms#jsx-elements)。

### 區段 [#sections]

每個根資料夾都是一個區段，並有各自的 `llms-full.txt`，例如 `/docs/ui/llms-full.txt`，讓 AI 代理能分段讀取大型文件。您可以透過 `section` 選擇其他區段：

```ts title="lib/source.ts"
export const docsLlms = llms(source, {
  section(folder) {
    // 收合：在 `llms.txt` 中僅列為一個連結，指向該區段自己的 `llms.txt`
    if (folder.root) return { collapse: true };

    return folder.index !== undefined;
  },
  // ...
});
```

### 頁面動作 [#page-actions]

頁面動作可讓讀者複製頁面的 Markdown 內容，或在 AI 應用程式中開啟該頁面。

![AI 頁面動作](/docs/ai-page-actions.png)

將它們加入您的文件頁面：

```tsx
import { MarkdownCopyButton, ViewOptionsPopover } from 'fumadocs-ui/layouts/docs/page';

// 使用靜態匯出時改為 `/llms.mdx${page.url}.md`
const markdownUrl = `${page.url}.md`;

<div className="flex flex-row gap-2 items-center border-b pt-2 pb-6">
  <MarkdownCopyButton markdownUrl={markdownUrl} />
  <ViewOptionsPopover
    markdownUrl={markdownUrl}
    githubUrl={`https://github.com/${owner}/${repo}/blob/main/content/docs/${encodeURI(page.path)}`}
  />
</div>;
```

### Accept 標頭 [#accept]

AI 代理可以透過 `Accept: text/markdown` 請求頁面的 Markdown。請在您的代理伺服器中回應這類請求：

```ts title="proxy.ts"
import { NextRequest, NextResponse } from 'next/server';
import { isMarkdownPreferred, rewritePath } from 'fumadocs-core/negotiation';

const { rewrite: rewriteDocs } = rewritePath('/docs{/*path}', '/llms.mdx/docs{/*path}.md');

export default function proxy(request: NextRequest) {
  const { pathname } = request.nextUrl;
  const result =
    isMarkdownPreferred(request) && !/\.(md|txt)$/.test(pathname) && rewriteDocs(pathname);

  if (result) {
    return NextResponse.rewrite(new URL(result, request.nextUrl), {
      // 同一個 URL 有兩種表示形式，別讓快取把它們混在一起
      headers: { Vary: 'Accept' },
    });
  }

  return NextResponse.next();
}
```

Next.js 會移除頁面 HTML 回應中的 `Vary`。若您的文件是透過共用快取提供，請在 CDN 上設定此標頭。

### 靜態輸出 [#static-output]

使用 `output: 'export'` 時，頁面檔案會放在 `/llms.mdx` 底下提供，例如 `/llms.mdx/docs/ui.md`。

## MCP 伺服器 [#mcp-server]

透過 [MCP](https://modelcontextprotocol.io) 伺服器，讓 AI 代理列出、搜尋及讀取您的文件：

```package-install
npx @fumadocs/cli feature mcp
```

接著，將 AI 代理連線至此伺服器：

```json
{
  "mcpServers": {
    "docs": { "url": "https://your-site.com/api/mcp" }
  }
}
```

若要自訂這些工具，請參閱 [`fumadocs-core/mcp`](/docs/headless/utils/mcp)。

## WebMCP [#webmcp]

<Callout type="warn" title="實驗性功能">
[WebMCP](https://webmachinelearning.github.io/webmcp/) 是一項仍處於早期階段的 Web 標準，目前可在
Chrome 149+ 中透過 `#enable-webmcp-testing` 旗標及來源試用 (origin trial) 啟用。
</Callout>

WebMCP 為讀者瀏覽器中的 AI 代理提供工具，讓其能搜尋並閱讀您的文件：

```package-install
npx @fumadocs/cli feature webmcp
```

請在 `<RootProvider />` 內算繪產生的 `WebMCP` 元件。

## 詢問 AI [#ask-ai]

![AI 搜尋](/docs/ai-search.png)

在您的文件版面配置中加入 AI 聊天功能：

```package-install
npx @fumadocs/cli feature ai
```

請選擇一個供應商，並在 `.env.local` 中設定其 API 金鑰，例如 OpenRouter 對應的是 `OPENROUTER_API_KEY`。若要改用其他模型，請編輯產生的 `/api/chat` 路由。

CLI 會將聊天功能加入您的文件版面配置。如果 CLI 找不到您的版面配置，請手動加入聊天功能：

```tsx tab="components/ai/layout.tsx"
'use client';
import { DocsLayout as Layout, type DocsLayoutProps } from 'fumadocs-ui/layouts/docs';
import { AIChat, AIChatPanel, useAIChat } from './search';

export function DocsLayout(props: DocsLayoutProps) {
  return (
    <AIChat>
      <ChatLayout {...props} />
    </AIChat>
  );
}

function ChatLayout(props: DocsLayoutProps) {
  const { open, setOpen } = useAIChat();

  return (
    <Layout
      {...props}
      aiChat={{ open, onOpenChange: setOpen, panel: <AIChatPanel />, trigger: true }}
    />
  );
}
```

```tsx tab="app/docs/layout.tsx"
import { DocsLayout } from 'fumadocs-ui/layouts/docs'; // [!code --]
import { DocsLayout } from '@/components/ai/layout'; // [!code ++]
```