GeezerVison is showing github.com — feat: MCP render_page_png 도구 추가 (8 도구) + ImageContent 출고 · DanMeon/rhwp-python@d9880d9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/roadmap/v0.6.0/png-vlm-render.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -36,7 +36,7 @@ v0.4.0 view 렌더러 (Markdown / HTML) 와 v0.5.0 MCP server 는 *텍스트* | 36 | 36 | - **AC-2** — 동일 fixture 의 `Document.render_all_png()` 결과 길이 == `Document.page_count` (페이지 수 invariant) | 37 | 37 | - **AC-3** — `Document.render_png(0, scale=2.0)` 결과의 픽셀 너비 ≈ `Document.render_png(0, scale=1.0)` 결과의 2배 (image lib 디코드 후 검증 — 상류 raster_dimension 의 `value * scale` 산식 회귀 가드) | 38 | 38 | - **AC-4** — `Document.render_png(0, max_pixels=1)` 호출이 `ValueError` (메시지에 `"pixel count out of range"` 포함) 로 fail — 상류 raster_dimension 의 max_pixels 가드 wire-through 검증 | 39 | | -- **AC-5** — MCP `render_page_png(path, 0)` 도구 호출이 fastmcp `ImageContent` 인스턴스 반환 (속성 `mime_type == "image/png"`, `data` 가 base64-decoded PNG magic 일치). `outputSchema` 에 `ImageContent` `$ref` 노출 | | 39 | +- **AC-5** — MCP `render_page_png(path, 0)` 도구 호출이 fastmcp `ImageContent` 인스턴스 반환 (속성 `mimeType == "image/png"`, `data` 가 base64-decoded PNG magic 일치). `outputSchema` 에 `ImageContent` `$ref` 노출 | 40 | 40 | - **AC-6** — `arender_png(path, 0)` async 호출이 정상 PNG bytes 반환. `_Document` 인스턴스가 thread 경계를 넘지 않음 — `aparse` + sync `render_png` 구성 검증 (panic 미발생) | 41 | 41 | - **AC-7** — `Document.export_png(out_dir)` 가 `Document.page_count` 만큼 파일 생성, 각 파일 첫 8 byte PNG magic 일치, 반환 경로 리스트 길이 == 파일 수 | 42 | 42 | - **AC-8** — `Document.render_pdf` / `render_svg` / `to_ir` 등 v0.5.x 표면의 모든 기존 회귀 가드가 그대로 통과 (additive 보장) | |
Collapse file Copy file name to clipboardExpand all lines: python/rhwp/mcp/server.py +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 |
| 19 | 19 |
| 20 | 20 | def build_server() -> FastMCP: | 21 | | - """새 ``FastMCP`` 인스턴스를 만들고 도구 7 종을 등록해 반환. | | 21 | + """새 ``FastMCP`` 인스턴스를 만들고 도구 8 종을 등록해 반환. | 22 | 22 |
| 23 | 23 | 테스트가 ``mcp.list_tools()`` / ``mcp.call_tool(name, args)`` 를 in-process | 24 | 24 | 호출할 수 있도록 build 단계를 함수로 분리 — 모듈 import 부수 효과 없이 | 25 | 25 | fresh 인스턴스 획득. | 26 | 26 | """ | 27 | 27 | server = FastMCP("rhwp-mcp") | 28 | | - # ^ S1 코어 4 + S2 view 2 + S3 chunks 1 = 7. mcp.md AC-2 의 GA 기준 도구 수. | | 28 | + # ^ S1 코어 4 + S2 view 2 + S3 chunks 1 + v0.6.0 PNG 1 = 8. | 29 | 29 | # fastmcp v3 의 ``server.tool(fn)`` 은 데코레이터 호출 형태 — 동일 인스턴스에 | 30 | 30 | # 여러 도구를 program 적으로 등록할 때 같은 클로저 충돌 없이 안전. | 31 | 31 | server.tool(tools.parse_hwp_summary) | @@ -37,6 +37,8 @@ def build_server() -> FastMCP: | 37 | 37 | # ^ chunks 는 langchain-text-splitters 런타임 extras gate — 등록은 무조건, | 38 | 38 | # 호출 시점에 ImportError → fastmcp ToolError → MCP isError=True (AC-7). | 39 | 39 | server.tool(tools.chunks) | | 40 | + # ^ v0.6.0: VLM 시각 입력 — fastmcp ImageContent 반환 (base64 + image/png). | | 41 | + server.tool(tools.render_page_png) | 40 | 42 | return server | 41 | 43 |
| 42 | 44 |
| |
Collapse file Copy file name to clipboardExpand all lines: python/rhwp/mcp/tools.py +40Lines changed: 40 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | - S1 — `parse_hwp_summary` / `extract_text` / `get_ir` / `iter_blocks` (코어 4) | 9 | 9 | - S2 — `to_markdown` / `to_html` (v0.4.0 view API thin wrapper) | 10 | 10 | - S3 — `chunks` (RAG 청킹 — langchain-text-splitters 런타임 extras gate) | | 11 | +- v0.6.0 — `render_page_png` (페이지 PNG → fastmcp ImageContent — VLM 입력) | 11 | 12 | """ | 12 | 13 |
| | 14 | +import base64 | 13 | 15 | from typing import Any, Literal | 14 | 16 |
| | 17 | +from mcp.types import ImageContent | 15 | 18 | from pydantic import BaseModel, ConfigDict, Field | 16 | 19 |
| 17 | 20 | import rhwp | @@ -232,3 +235,40 @@ def chunks( | 232 | 235 | splitter = RecursiveCharacterTextSplitter(chunk_size=size, chunk_overlap=overlap) | 233 | 236 | split_docs = splitter.split_documents(docs) | 234 | 237 | return [ChunkRecord(page_content=d.page_content, metadata=d.metadata) for d in split_docs] | | 238 | + | | 239 | + | | 240 | +def render_page_png( | | 241 | + path: str, | | 242 | + page: int, | | 243 | + scale: float = 1.0, | | 244 | + max_pixels: int | None = None, | | 245 | +) -> ImageContent: | | 246 | + """HWP 또는 HWPX 의 특정 페이지를 PNG 로 렌더해 fastmcp ``ImageContent`` 반환. | | 247 | +
| | 248 | + VLM (Vision-Language Model — Claude / GPT-4V / Gemini 등) 의 시각 입력 1차 | | 249 | + 시민. fastmcp v3 의 ``ImageContent`` 표준 (base64 + ``mimeType="image/png"``) | | 250 | + 으로 출고 → MCP client (Claude Desktop / Cline / Cursor) 가 LLM 메시지의 | | 251 | + ``image`` content block 으로 자동 wire — bytes 반환 후 클라이언트 측 base64 | | 252 | + 변환은 비결정 dict 가 되므로 거부. | | 253 | +
| | 254 | + A4 페이지 PNG 가 약 100-500 KB → base64 약 130-660 KB. stdio MCP transport | | 255 | + 의 JSON-RPC 메시지 크기 제한과 충돌 가능성 — 큰 페이지 / 다수 페이지는 | | 256 | + ``--transport streamable-http`` 권장. | | 257 | +
| | 258 | + Args: | | 259 | + path: HWP 또는 HWPX 파일 경로. | | 260 | + page: 0-based 페이지 인덱스. | | 261 | + scale: 페이지 크기 배율 (기본 1.0). VLM 입력 권장 값은 1.0-2.0 — 너무 | | 262 | + 크면 LLM context 비용이 급증. | | 263 | + max_pixels: DoS 방어용 픽셀 상한. 미지정 시 상류 default | | 264 | + 67_108_864 (= 8192 × 8192). | | 265 | +
| | 266 | + Returns: | | 267 | + ``ImageContent(type="image", data=base64-encoded PNG, mimeType="image/png")``. | | 268 | + """ | | 269 | + png_bytes = rhwp.parse(path).render_png(page, scale=scale, max_pixels=max_pixels) | | 270 | + return ImageContent( | | 271 | + type="image", | | 272 | + data=base64.b64encode(png_bytes).decode("ascii"), | | 273 | + mimeType="image/png", | | 274 | + ) |
Collapse file Copy file name to clipboardExpand all lines: uv.lock +1-5Lines changed: 1 addition & 5 deletions
|