get_doc
Retrieve document content from a specified path, with pagination support to fetch a specific page or the entire document.
Instructions
指定したドキュメントの内容を取得(ページネーション対応)
Args:
path: ドキュメントのファイルパス
page: ページ番号(1から開始、Noneの場合は全文取得)Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| page | No |
Implementation Reference
- src/mcp_server_docs/server.py:32-40 (handler)The MCP tool handler for 'get_doc'. Defined as an async FastMCP tool with @mcp.tool() decorator. Takes 'path' (str) and optional 'page' (int) parameters. Delegates to DocumentManager.get_document().
@mcp.tool() async def get_doc(path: str, page: int | None = None) -> str: """指定したドキュメントの内容を取得(ページネーション対応) Args: path: ドキュメントのファイルパス page: ページ番号(1から開始、Noneの場合は全文取得) """ return doc_manager.get_document(path, page) - The core implementation of get_document logic in DocumentManager. Handles document lookup, pagination (character-count-based), metadata header generation, and error handling for missing documents or invalid page numbers.
def get_document(self, path: str, page: int | None = None) -> str: """指定されたドキュメントの内容を返す(文字数ベースページネーション対応) Args: path: ドキュメントのファイルパス page: ページ番号(1から開始、Noneの場合は自動判定) """ if path not in self.docs_content: return f"Error: Document not found: {path}" content = self.docs_content[path] total_chars = len(content) # ページ指定がない場合(従来の動作) if page is None: # 大きなファイルには自動的に1ページ目を返す if total_chars > self.large_file_threshold: # 1ページ目として処理 page = 1 else: # 小さなファイルは従来通り全文を返す return content # ページネーション処理(文字数ベース) total_pages = ( total_chars + self.max_chars_per_page - 1 ) // self.max_chars_per_page # エラーハンドリング if page < 1: return "Error: Page number must be 1 or greater" if page > total_pages: return f"Error: Page {page} not found. Total pages: {total_pages} (max chars per page: {self.max_chars_per_page:,})" # ページ範囲計算(文字数ベース、行を分割しないよう調整) start_char = (page - 1) * self.max_chars_per_page end_char = min(start_char + self.max_chars_per_page, total_chars) # 行の途中で切れないよう調整 if end_char < total_chars: # 次の改行文字まで含める next_newline = content.find("\n", end_char) if next_newline != -1: end_char = next_newline + 1 page_content = content[start_char:end_char] # 行数カウント(表示用) lines_before_start = content[:start_char].count("\n") page_lines = page_content.count("\n") total_lines = content.count("\n") + 1 start_line = lines_before_start + 1 end_line = min(start_line + page_lines, total_lines) # メタデータヘッダー header = f"📄 Document: {path}\n" header += f"📖 Page {page}/{total_pages} (chars {start_char + 1:,}-{end_char:,}/{total_chars:,})\n" header += f"📏 Lines {start_line}-{end_line}/{total_lines:,} | Max chars per page: {self.max_chars_per_page:,}\n" # 大きなファイルで自動的にページ1を表示した場合は使い方を追加 if page == 1 and total_chars > self.large_file_threshold: header += "⚠️ Large document auto-paginated. To see other pages:\n" header += f"💡 get_doc('{path}', page=2) # Next page\n" header += f"💡 get_doc('{path}', page={total_pages}) # Last page\n" header += "─" * 60 + "\n\n" return header + page_content - src/mcp_server_docs/server.py:32-33 (registration)Tool registration via the @mcp.tool() decorator on the 'get_doc' async function. FastMCP automatically registers any function decorated with @mcp.tool() as an MCP tool.
@mcp.tool() async def get_doc(path: str, page: int | None = None) -> str: - src/mcp_server_docs/server.py:33-39 (schema)Input schema for the 'get_doc' tool. Parameters: 'path' (str, required) and 'page' (int | None, optional). The docstring serves as the description/schema for the MCP tool.
async def get_doc(path: str, page: int | None = None) -> str: """指定したドキュメントの内容を取得(ページネーション対応) Args: path: ドキュメントのファイルパス page: ページ番号(1から開始、Noneの場合は全文取得) """