Self-contained: vendored util/ (CC BY 4.0, fixes noted in NOTICE), portable setup.sh (venv + weights + Pascal lane), start.sh with real-parse readiness gate, FastAPI service (bearer, serial GPU queue, LRU cache, OOM->503), MCP stdio shim (MCPServer 2.x, token strip), hardware-honest README.
72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
"""MCP (Model Context Protocol) stdio shim for the omniparse REST service.
|
|
|
|
Registers one tool, `omniparse`, that forwards to POST /parse on the HTTP
|
|
service, so MCP-capable harnesses (omp /mcp.json, Claude Desktop, Cursor...)
|
|
get the parser as a first-class tool without custom plugin code.
|
|
|
|
Register in your harness's MCP config - e.g. ~/.omp/agent/mcp.json (stdio):
|
|
|
|
"omniparse": {
|
|
"command": "<repo>/.venv/bin/python",
|
|
"args": ["-m", "omni_api.mcp_server"],
|
|
"cwd": "<repo>",
|
|
"timeout": 120000,
|
|
"env": { "OMNI_API_URL": "http://127.0.0.1:8898",
|
|
"OMNI_API_TOKEN": "!cat ~/.config/omniparse_token" }
|
|
}
|
|
|
|
(!cmd values are executed by omp at connect time - a bare env name copies
|
|
the launching shell's variable or, when unset, ships the literal string.)
|
|
"""
|
|
import base64
|
|
import os
|
|
|
|
import requests
|
|
from mcp.server.mcpserver import MCPServer
|
|
|
|
API = os.environ.get('OMNI_API_URL', 'http://127.0.0.1:8898').rstrip('/')
|
|
|
|
|
|
def _token() -> str:
|
|
# strip both branches: omp !cmd env values may arrive with the file's
|
|
# trailing newline, and a \n in a Bearer header trips httpx validation
|
|
return (os.environ.get('OMNI_API_TOKEN') or
|
|
open(os.path.expanduser(os.environ.get(
|
|
'OMNI_TOKEN_FILE', '~/.config/omniparse_token'))).read()).strip()
|
|
|
|
|
|
mcp = MCPServer('omniparse')
|
|
|
|
|
|
@mcp.tool()
|
|
def omniparse(image_path: str = '', image_b64: str = '', format: str = 'view',
|
|
include_som: bool = False) -> dict:
|
|
"""Parse a GUI screenshot into numbered, captioned interactable elements.
|
|
|
|
Give the screenshot as image_path (local file the agent can read, e.g. a
|
|
captured VM/desktop screenshot) OR image_b64 (base64 PNG/JPEG). Returns
|
|
elements: compact 'view' lines (id + pixel center + caption - the primary
|
|
LLM-facing form), and/or 'full' per-element JSON with normalized bbox and
|
|
center. Set include_som=true only when a vision model or a human must see
|
|
the annotated Set-of-Mark rendering. Click targets use center_px scaled to
|
|
the ACTUAL screen (image_size tells you the parsed resolution)."""
|
|
if image_path:
|
|
b64 = base64.b64encode(open(image_path, 'rb').read()).decode()
|
|
elif image_b64:
|
|
b64 = image_b64
|
|
else:
|
|
return {'error': 'provide image_path or image_b64'}
|
|
r = requests.post(f'{API}/parse',
|
|
json={'image_b64': b64, 'format': format,
|
|
'include_som': include_som},
|
|
headers={'Authorization': f'Bearer {_token()}'}, timeout=300)
|
|
if r.status_code != 200:
|
|
return {'error': f'{r.status_code}: {r.text[:200]}'}
|
|
d = r.json()
|
|
d.pop('som_png_b64', None) if not include_som else None
|
|
return d
|
|
|
|
|
|
if __name__ == '__main__':
|
|
mcp.run(transport='stdio')
|