from mcp.server.fastmcp import FastMCP
import json
import os
from .ocr import recognize_text, recognize_text_with_layout
# Initialize FastMCP server
mcp = FastMCP("macos-ocr")
@mcp.tool()
def read_image_text(image_path: str) -> str:
"""
Read text from an image using macOS native OCR.
Args:
image_path: Absolute path to the local image file.
"""
if not os.path.exists(image_path):
raise FileNotFoundError(f"Image file not found: {image_path}")
try:
return recognize_text(image_path)
except Exception as e:
return f"Error processing image: {str(e)}"
@mcp.tool()
def read_image_layout(image_path: str) -> str:
"""
Read text with layout information (bounding boxes) from an image.
Returns a JSON string containing a list of text blocks with their content, confidence, and normalized bounding boxes.
Args:
image_path: Absolute path to the local image file.
"""
if not os.path.exists(image_path):
raise FileNotFoundError(f"Image file not found: {image_path}")
try:
data = recognize_text_with_layout(image_path)
return json.dumps(data, ensure_ascii=False)
except Exception as e:
return json.dumps({"error": str(e)})
def main():
mcp.run()
if __name__ == "__main__":
main()