メインコンテンツへ移動

ドキュメントマネージャーを構築する

このガイドでは、プロジェクトのドキュメントを保守するドキュメントマネージャーを構築します。このマネージャーは、誤って上書きすることを防ぎながら、適切に構造化された Markdown ファイルを作成し、ドキュメントを整理された状態に保ちます。Workspace の Filesystem をセットアップし、ドキュメント管理の指示を与えた Agent を作成します。その後、会話形式のプロンプトを使用してドキュメントを生成、更新します。

前提条件
前提条件への直接リンク

  • Node.js v22.13.0 以降がインストールされていること
  • サポートされているモデル Provider の API キー
  • 既存の Mastra プロジェクト(新しいプロジェクトをセットアップするには、インストールガイドに従ってください)

Workspace をセットアップする
Workspace をセットアップするへの直接リンク

Workspace はローカル Filesystem を使用してドキュメントファイルを管理します。Agent は Workspace ディレクトリ内のファイルを読み書きします。src/mastra/index.ts ファイルで、Workspace クラスと LocalFilesystem クラスを import します。

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { resolve } from 'node:path'
import { Workspace, LocalFilesystem } from '@mastra/core/workspace'

const workspace = new Workspace({
filesystem: new LocalFilesystem({
basePath: resolve(import.meta.dirname, '../../workspace'),
}),
})

export const mastra = new Mastra({
workspace,
})

プロジェクトのルートに、workspace という新しいフォルダーを作成します。すべてのドキュメントファイルはここに保存され、Agent によって管理されます。

ドキュメントのサンプルを追加する
ドキュメントのサンプルを追加するへの直接リンク

workspace ディレクトリ内に、次のフォルダーを作成します。

  • docs/guides/:ハウツーガイド用
  • docs/api/:API リファレンスドキュメント用
  • docs/tutorials/:ステップ別チュートリアル用

ドキュメントのインデックスとして workspace/docs/README.md を作成します。

workspace/docs/README.md
# Project Documentation

Welcome to the documentation!

## Sections

- [Guides](./guides/): How-to guides
- [API](./api/): API reference
- [Tutorials](./tutorials/): Step-by-step tutorials

既存のドキュメントスタイルを Agent が確認できるように、サンプルガイドを追加します。

workspace/docs/guides/getting-started.md
# Getting Started

Quickstart guide for the project.

## Installation

```bash npm2yarn
npm install example-package
```

## Quick example

```typescript
import { Example } from 'example-package'

const example = new Example()
example.run()
```

ドキュメントマネージャーを作成する
ドキュメントマネージャーを作成するへの直接リンク

次に、ドキュメントマネージャー Agent を作成します。この Agent には、Workspace 内で Markdown ファイルを作成、更新するための指示を与えます。新しいファイル src/mastra/agents/docs-manager.ts を作成し、Agent を定義します。

src/mastra/agents/docs-manager.ts
import { Agent } from '@mastra/core/agent'

export const docsManager = new Agent({
id: 'docs-manager',
name: 'Docs Manager',
instructions: `You are a documentation manager that creates and maintains markdown docs.

When creating new docs:
1. Ask for topic and target audience
2. Create well-structured markdown with clear sections
3. Include relevant code examples with syntax highlighting
4. Save in the appropriate directory:
- /docs/guides/ for user guides and how-tos
- /docs/api/ for API reference
- /docs/tutorials/ for step-by-step tutorials

When updating existing docs:
1. ALWAYS read the file first
2. Make targeted updates without removing unrelated content
3. Preserve existing structure and formatting

Use kebab-case naming for files (getting-started.md).
Always explain what you're creating and why.`,
model: 'openai/gpt-5.6-sol',
})

src/mastra/index.ts 内で Agent を import し、Mastra インスタンスに登録します。

src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { resolve } from 'node:path'
import { Workspace, LocalFilesystem } from '@mastra/core/workspace'
import { docsManager } from './agents/docs-manager'

const workspace = new Workspace({
filesystem: new LocalFilesystem({
basePath: resolve(import.meta.dirname, '../../workspace'),
}),
})

export const mastra = new Mastra({
workspace,
agents: { docsManager },
})

ドキュメントマネージャーをテストする
ドキュメントマネージャーをテストするへの直接リンク

Studio を起動し、Agent と対話して動作を確認します。

npm run dev

localhost:4111 を開き、ドキュメントマネージャーに移動します。

新しいドキュメントを作成する
新しいドキュメントを作成するへの直接リンク

チュートリアルを作成するよう Agent に依頼します。

Create a tutorial for setting up authentication. Cover installation, configuration, and a basic example.

Agent は docs/tutorials/authentication-setup.md のようなファイルを作成します。Agent のレスポンスは非決定的なため、正確な内容は異なりますが、次のような内容が表示されます。

# Authentication Setup

Learn how to add authentication to your application.

## Installation

Install the auth package:

```bash npm2yarn
npm install @example/auth
```

## Configuration

Create a config file:

```typescript
// auth.config.ts
export const authConfig = {
provider: 'oauth',
clientId: process.env.AUTH_CLIENT_ID,
secret: process.env.AUTH_SECRET,
}
```

## Basic example

```typescript
import { createAuth } from '@example/auth'
import { authConfig } from './auth.config'

const auth = createAuth(authConfig)

app.get('/protected', auth.requireAuth(), (req, res) => {
res.json({ user: req.user })
})
```

既存のドキュメントを更新する
既存のドキュメントを更新するへの直接リンク

既存のドキュメントを更新してみます。

Update the getting started guide to include a section on configuration after the Quick Example

Agent は既存の getting-started.md ファイルを読み、適切な挿入位置を見つけます。既存の内容を崩すことなく、新しいセクションを追加します。

ドキュメントを整理する
ドキュメントを整理するへの直接リンク

インデックスを作成するよう Agent に依頼します。

List all tutorial files and create an index page that links to all of them

Agent は、利用可能なすべてのチュートリアルへのリンクを含む /docs/tutorials/index.md のようなファイルを作成します。

次のステップ
次のステップへの直接リンク

このマネージャーは、次のように拡張できます。

  • 関連ドキュメントを見つけるために BM25 またはベクトル検索を追加する
  • ドキュメントテンプレート用の Skill を作成する
  • ソースコードからドキュメントを自動生成する仕組みを構築する
  • GitHub と統合し、コミット時にドキュメントを更新する
  • リンクと書式をチェックする検証を追加する