factory.core/ObjMcpClient.py
ObjMcpClient is a synchronous wrapper around the Model Context Protocol (MCP) Python SDK. It lets any Axion service call MCP-compatible tool servers — in particular the @playwright/mcp headless-browser server — without managing an async event loop.
Not an AI wrapper. The
factory.ai/package.mcp/directory contains "Multi-Cloud Provider" adapters for LLM backends.ObjMcpClientis entirely separate: it implements the MCP wire protocol to call any MCP tool server.
One-time system setup (requires Node.js ≥ 18):
# Install the Playwright MCP server globally
npm install -g @playwright/mcp
# Install Playwright's bundled Chromium (no system Chrome needed)
npx playwright install chromium
# Install the Python SDK
pip install mcp playwright
If npx is not on PATH, ObjMcpClient logs a clear error and returns "" / [].
from ObjMcpClient import ObjMcpClient, HAS_MCP
if HAS_MCP:
client = ObjMcpClient()
HAS_MCP is True only when pip install mcp has been run. Callers should guard on it before instantiating the class, or handle the graceful degradation built into each method.
call_tool(command, args, tool_name, tool_params) → dictOpens an MCP stdio server, calls one tool, and closes the connection.
client = ObjMcpClient()
result = client.call_tool(
command="npx",
args=["@playwright/mcp@latest", "--headless"],
tool_name="browser_navigate",
tool_params={"url": "https://example.com"},
)
# result = {"is_error": False, "text": "...", "image_base64": "", "image_mime": ""}
Return keys:
| Key | Type | Description |
|---|---|---|
is_error |
bool |
Whether the tool reported an error |
text |
str |
Concatenated text blocks from the response |
image_base64 |
str |
Base64-encoded image data (if any) |
image_mime |
str |
MIME type of the image (e.g. image/png) |
list_tools(command, args) → list[dict]Returns tool descriptors (name, description) from the MCP server.
tools = client.list_tools("npx", ["@playwright/mcp@latest"])
for tool in tools:
print(tool["name"], tool["description"])
These handle the full navigate → act → close lifecycle in a single asyncio.run() call.
playwright_screenshot(url, output_path, width, height, headless) → strNavigate to url and save a PNG to output_path.
client = ObjMcpClient()
path = client.playwright_screenshot(
url="https://example.com",
output_path="/tmp/shot.png",
width=1920,
height=1080,
)
if path:
print(f"Saved to {path}")
Returns output_path on success, "" on failure. No system Chrome required — Playwright uses its own bundled Chromium.
playwright_get_text(url) → strNavigate to url and return the visible text content of the page.
text = client.playwright_get_text("https://example.com")
playwright_pdf(url, output_path, css_string, css_url, headless) → strNavigate to url and save a PDF to output_path.
Uses the playwright Python SDK directly (not via MCP) so that page.pdf() and
page.add_style_tag() are available. @media print CSS is applied automatically.
# Default — page uses its own print stylesheets
path = client.playwright_pdf(
url="https://example.com/report.py?rep=myreport",
output_path="/tmp/report.pdf",
)
# Inject a CSS string from the database
path = client.playwright_pdf(
url="https://example.com/report.py?rep=myreport",
output_path="/tmp/report.pdf",
css_string="body { font-size: 12pt; }",
)
# Inject a CSS URL as a stylesheet link
path = client.playwright_pdf(
url="https://example.com/report.py?rep=myreport",
output_path="/tmp/report.pdf",
css_url="https://example.com/style/pdf.css",
)
css_string takes priority over css_url; if both are empty the page renders with its own stylesheetsoutput_path on success, "" on failurePrerequisites (in addition to the MCP setup above):
pip install playwright
playwright install chromium
All public methods call asyncio.run() internally, so they are fully synchronous from the caller's perspective. Do not call them from within a running event loop (e.g. from an async FastAPI endpoint) — use await on the internal _*_async coroutines directly, or wrap with asyncio.get_event_loop().run_in_executor().
# Take a screenshot
python factory.core/ObjMcpClient.py screenshot https://example.com /tmp/test.png
# List tools exposed by the Playwright MCP server
python factory.core/ObjMcpClient.py tools npx @playwright/mcp@latest