read-context-command-implementation.json•7.18 kB
{
  "schema": "memory_document_v2",
  "metadata": {
    "id": "43e6cf1c-8bd9-40be-813d-ce72566cb625",
    "title": "`read_context`コマンド実装ガイド",
    "documentType": "implementation",
    "path": "03-implementation/read-context-command-implementation.json",
    "tags": [
      "implementation",
      "mcp",
      "command",
      "context",
      "v2"
    ],
    "lastModified": "2025-03-17T02:13:31.721Z",
    "createdAt": "2025-03-17T02:13:31.721Z",
    "version": 1
  },
  "content": {
    "rawContent": "# `read_context`コマンド実装ガイド\n\ntags: #implementation #mcp #command #context #v2\n\n## 概要\n\nこのドキュメントは、`read_context`コマンドの実装方法を説明します。このコマンドは、ルール、ブランチメモリバンク、グローバルメモリバンクの情報を一度に取得できるようにするものです。\n\n## 実装手順\n\n### 1. `AVAILABLE_TOOLS`配列への追加\n\n`src/index.ts`ファイルの`AVAILABLE_TOOLS`配列(行38-185)に以下のコード定義を追加します:\n\n```typescript\n{\n  name: 'read_context',\n  description: 'Read all context information (rules, branch memory bank, global memory bank) at once',\n  inputSchema: {\n    type: 'object',\n    properties: {\n      branch: {\n        type: 'string',\n        description: 'Branch name (required if includeBranchMemory is true)',\n      },\n      language: {\n        type: 'string',\n        enum: ['en', 'ja'],\n        description: 'Language code (en or ja)',\n      },\n      includeRules: {\n        type: 'boolean',\n        description: 'Whether to include rules (default: true)',\n      },\n      includeBranchMemory: {\n        type: 'boolean',\n        description: 'Whether to include branch memory bank (default: true)',\n      },\n      includeGlobalMemory: {\n        type: 'boolean',\n        description: 'Whether to include global memory bank (default: true)',\n      },\n    },\n  },\n},\n```\n\n### 2. `CallToolRequestSchema`ハンドラーへの追加\n\n`src/index.ts`ファイルの`CallToolRequestSchema`ハンドラーのswitch文(行220-450)に以下のケースを追加します:\n\n```typescript\ncase 'read_context': {\n  const branch = params.branch as string | undefined;\n  const language = (params.language as string) || 'ja';\n  const includeRules = params.includeRules !== false; // デフォルトはtrue\n  const includeBranchMemory = params.includeBranchMemory !== false; // デフォルトはtrue\n  const includeGlobalMemory = params.includeGlobalMemory !== false; // デフォルトはtrue\n\n  logger.info(`Reading context (branch: ${branch || 'none'}, language: ${language})`);\n\n  // ブランチメモリバンクを含める場合は、ブランチ名が必須\n  if (includeBranchMemory && !branch) {\n    throw new Error('Branch name is required when includeBranchMemory is true');\n  }\n\n  if (!app) {\n    throw new Error('Application not initialized');\n  }\n\n  // 結果を格納するオブジェクト\n  const result: Record<string, any> = {};\n\n  // ルールを取得\n  if (includeRules) {\n    logger.debug('Including rules in context');\n    if (!['en', 'ja'].includes(language)) {\n      throw new Error('Invalid language for rules');\n    }\n\n    const dirname = path.dirname(fileURLToPath(import.meta.url));\n    const filePath = path.join(dirname, 'templates', `rules-${language}.md`);\n    const content = await fs.readFile(filePath, 'utf-8');\n    result.rules = { content };\n  }\n\n  // ブランチメモリバンクを取得\n  if (includeBranchMemory && branch) {\n    logger.debug(`Including branch memory bank for branch: ${branch}`);\n    const branchResponse = await app.getBranchController().readCoreFiles(branch);\n    if (!branchResponse.success) {\n      throw new Error(branchResponse.error.message);\n    }\n    result.branchMemory = branchResponse.data;\n  }\n\n  // グローバルメモリバンクを取得\n  if (includeGlobalMemory) {\n    logger.debug('Including global memory bank in context');\n    const globalResponse = await app.getGlobalController().readCoreFiles();\n    if (!globalResponse.success) {\n      throw new Error(globalResponse.error.message);\n    }\n    result.globalMemory = globalResponse.data;\n  }\n\n  return {\n    content: [\n      {\n        type: 'text',\n        text: JSON.stringify(result, null, 2),\n      },\n    ],\n  };\n}\n```\n\n## 実装上の注意点\n\n1. `includeBranchMemory`がtrueで`branch`パラメータが指定されていない場合は、明確なエラーメッセージを返します。\n2. 言語が'en'または'ja'以外の場合は、エラーを返します。\n3. アプリケーションが初期化されていない場合は、エラーを返します。\n4. 各コンポーネント(ルール、ブランチメモリバンク、グローバルメモリバンク)の取得に失敗した場合は、エラーを返します。\n\n## テスト方法\n\n実装後、以下のようなコマンドでテストできます:\n\n```bash\n# すべての情報を取得\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"name\":\"read_context\",\"arguments\":{\"branch\":\"feature/example\",\"language\":\"ja\"}}' http://localhost:3000/api/mcp\n\n# ルールのみを取得\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"name\":\"read_context\",\"arguments\":{\"includeBranchMemory\":false,\"includeGlobalMemory\":false,\"language\":\"ja\"}}' http://localhost:3000/api/mcp\n\n# グローバルメモリバンクのみを取得\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"name\":\"read_context\",\"arguments\":{\"includeBranchMemory\":false,\"language\":\"ja\"}}' http://localhost:3000/api/mcp\n\n# ブランチメモリバンクとグローバルメモリバンクを取得\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"name\":\"read_context\",\"arguments\":{\"branch\":\"feature/example\",\"includeRules\":false}}' http://localhost:3000/api/mcp\n```\n\n## エラーケースのテスト\n\n以下のようなエラーケースもテストする必要があります:\n\n```bash\n# ブランチ名が指定されていない場合(エラーになるはず)\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"name\":\"read_context\",\"arguments\":{\"language\":\"ja\"}}' http://localhost:3000/api/mcp\n\n# 無効な言語が指定されている場合(エラーになるはず)\ncurl -X POST -H \"Content-Type: application/json\" -d '{\"name\":\"read_context\",\"arguments\":{\"branch\":\"feature/example\",\"language\":\"fr\"}}' http://localhost:3000/api/mcp\n```\n\n## 次のステップ\n\nこの実装が完了したら、以下の作業を行うことをお勧めします:\n\n1. 単体テストの作成\n2. 統合テストの作成\n3. ドキュメントの更新\n4. ユーザーガイドへの追加\n",
    "sections": {
      "": "この実装が完了したら、以下の作業を行うことをお勧めします:\n\n1. 単体テストの作成\n2. 統合テストの作成\n3. ドキュメントの更新\n4. ユーザーガイドへの追加"
    }
  }
}