inspect_file
Retrieve file metadata and content previews for text, CSV, or image files to support workflow decisions.
Instructions
Return file metadata and content preview for text/csv/image workflows.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| preview_chars | No | ||
| include_base64 | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- server.py:201-230 (handler)The 'inspect_file' MCP tool handler function. It resolves the file path, reads file metadata (name, size, MIME type), provides text preview for text files (up to preview_chars), notes for images, and optionally includes base64-encoded content. Returns the result as a JSON string.
@mcp.tool() def inspect_file(path: str, preview_chars: int = 4000, include_base64: bool = False) -> str: """Return file metadata and content preview for text/csv/image workflows.""" target = _resolve_file_ops_path(path) if not target.is_file(): raise ValueError(f"File does not exist: {target}") mime_type, _ = mimetypes.guess_type(str(target)) if not mime_type: mime_type = "application/octet-stream" raw = target.read_bytes() result: dict[str, Any] = { "path": str(target), "name": target.name, "size_bytes": len(raw), "mime_type": mime_type, } if mime_type.startswith("text/") or mime_type in {"application/json", "text/csv"}: text = raw.decode("utf-8", errors="replace") result["text_preview"] = text[:preview_chars] result["text_preview_truncated"] = len(text) > preview_chars elif mime_type.startswith("image/"): result["image_note"] = "Use analyze_image_with_openai for model vision interpretation." if include_base64: result["base64"] = base64.b64encode(raw).decode("ascii") return json.dumps(result, indent=2) - server.py:201-201 (registration)Registration of inspect_file as an MCP tool via the @mcp.tool() decorator.
@mcp.tool() - server.py:66-76 (helper)Helper function _resolve_file_ops_path used by inspect_file to validate and resolve file paths within MCP_FILE_OPS_ROOT, preventing path traversal escapes.
def _resolve_file_ops_path(path: str | None = None) -> Path: if not FILE_OPS_ROOT: raise ValueError("MCP_FILE_OPS_ROOT is not configured in .env.") root = Path(FILE_OPS_ROOT).expanduser().resolve() root.mkdir(parents=True, exist_ok=True) target = root if path is None else (root / path).resolve() if target != root and root not in target.parents: raise ValueError("Path escapes the configured MCP_FILE_OPS_ROOT.") return target