Skip to main content

MCP Client for Python

Project description

📦 kl-mcp-client

kl-mcp-client là SDK Python giúp giao tiếp với MCP (Model Context Protocol) Browser Server — một server trung gian điều khiển Chrome/Chromium qua CDP.

Thư viện này được thiết kế để:

  • Điều khiển Chrome tự động: click, nhập liệu, screenshot, đọc DOM…
  • Kết nối trình duyệt Chrome Remote qua CDP (Chrome DevTools Protocol)
  • Tích hợp làm Web Automation Agent trong Google ADK
  • Hoạt động độc lập như một browser automation SDK

🚀 Cài đặt

pip install kl-mcp-client

🧩 Thành phần của thư viện

Package gồm 2 module chính:

File Vai trò
client.py JSON-RPC HTTP Client giao tiếp với MCP Server
tools.py Wrapper cấp cao, cung cấp API tiện dụng cho các Agent & automation

✨ Tính năng chính

✔ Điều khiển trình duyệt

  • Mở tab, load URL
  • Click CSS selector, click bằng text, click bằng nodeId
  • Nhập text vào input
  • Screenshot (chuẩn ADK hiển thị được)

✔ DOM Tools nâng cao

  • Find element by selector / text / XPath
  • Lấy bounding box
  • Lấy toàn bộ DOM Tree
  • Lấy danh sách clickable elements
  • Highlight elements (nếu server hỗ trợ)

✔ Tương tác hệ thống

  • Upload file bằng base64
  • Import cookies
  • Evaluate JavaScript

✔ Chrome Remote Debugging

  • Tạo session từ địa chỉ:
    http://localhost:9222/json/version

🧭 Cách sử dụng

1. Import client & tools

from kl_mcp_client.client import MCPClient
from kl_mcp_client.tools import MCPTools

2. Tạo kết nối tới MCP Server

mcp = MCPClient(
    base_url="http://localhost:3000/mcp",
    timeout=30,
    retries=2
)
tools = MCPTools(mcp)

3. Tạo session (Chrome Remote)

session = tools.create_session("http://localhost:9222/json/version")
sid = session["sessionId"]

4. Mở URL

tools.open_page(sid, "https://google.com")

5. Screenshot (hiển thị được trong ADK Web)

img = tools.screenshot(sid)
print(img)

Trả về:

{
  "type": "image",
  "mimeType": "image/png",
  "data": "<base64>"
}

6. Click & Type

tools.click(sid, "#login")
tools.type(sid, "input[name=q]", "Hello world")

7. Find Elements

tools.find_element(sid, "#content")
tools.find_element_by_text(sid, "Đăng nhập")
tools.find_element_xpath(sid, "//input[@type='email']")

8. Upload File

import base64
data = base64.b64encode(open("test.pdf", "rb").read()).decode()

tools.upload_file(
    sid,
    selector="input[type=file]",
    filename="test.pdf",
    base64Data=data
)

9. Import Cookies

tools.import_cookies(sid, [
    {"name": "token", "value": "abc", "domain": "example.com", "path": "/"}
])

10. Đóng session

tools.close_session(sid)

🧪 Ví dụ đầy đủ

from kl_mcp_client.client import MCPClient
from kl_mcp_client.tools import MCPTools
import base64

mcp = MCPClient("http://localhost:3000/mcp")
tools = MCPTools(mcp)

# Create session
sid = tools.create_session("http://localhost:9222/json/version")["sessionId"]

# Navigate
tools.open_page(sid, "https://google.com")

# Screenshot
img = tools.screenshot(sid)
print("Screenshot returned:", img["mimeType"])

# Search
tools.type(sid, "input[name=q]", "Hello MCP")
tools.click_to_text(sid, "Google Search")

# Close
tools.close_session(sid)

🏗 Kiến trúc

Python App / ADK Agent
          ↓
      kl-mcp-client
          ↓
   MCP Browser Server
          ↓
      Chrome / CDP

📘 Yêu cầu

  • Python ≥ 3.8
  • MCP Server chạy sẵn (Chromedp backend)
  • Chrome/Chromium với cờ:
chrome.exe --remote-debugging-port=9222

📝 License

MIT License.


📚 Liên hệ

Nếu bạn cần:

  • MCP Server đầy đủ
  • Hỗ trợ tích hợp ADK Web Agent
  • Thêm tool: DOM Tree, highlight, selector map…

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kl_mcp_client-2.0.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kl_mcp_client-2.0.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file kl_mcp_client-2.0.2.tar.gz.

File metadata

  • Download URL: kl_mcp_client-2.0.2.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for kl_mcp_client-2.0.2.tar.gz
Algorithm Hash digest
SHA256 83265c0d3f3ee09b6f0155ac965ac0b4d73406a27acef89cb92c272d21b45c08
MD5 6f3503b4c5dc92b2e86534dd626321dd
BLAKE2b-256 e8ca4dcbbaf21276e60f4fcd6d8f6abfda5f44d25ef23333a0faa8822b02e79b

See more details on using hashes here.

File details

Details for the file kl_mcp_client-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: kl_mcp_client-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for kl_mcp_client-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 70f6dec1ae63878f70cf02d98e1605096d17c6a6a1bdbda5de7fdba97f276bd7
MD5 82077664cf9ad048a581981030c26e33
BLAKE2b-256 fb0ffb4fb2b193f9482a8f8c3c63ae4f6e24d9f37b6e7bc04120865b5e3389b5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page