Files
omniparse-mcp/omni_api/mcp_server.py
T
plean 61a0f353cd Fresh-machine hardening from review
- util: drop dead openai/json/sys/requests/PIL extras (openai import would
  ImportError before engine load); ultralytics fallback path now raises
  (AGPL isolation - only MIT icon_detect_v3 ships)
- requirements: default path only; paddle + ultralytics moved to README
  optional section
- setup.sh: venv fallback for distros without ensurepip; --skip-weights
  verified
- start.sh: bind 127.0.0.1 by default (LAN = explicit opt-in)
- docs: absolute-path !cat examples, HF startup/airgap notes, token
  placement, security defaults
2026-11-18 02:34:13 +00:00

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 /home/YOU/.config/omniparse_token" }
}
(!cmd values are executed at connect time without a shell - use an absolute
path; a bare env name copies the harness's variable or ships it literally.)
"""
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')