Skip to main content

MCP Client for Python

Project description

kl-mcp-client

SDK Python để gọi MCP Browser Server qua JSON-RPC HTTP.

Cài đặt

pip install kl-mcp-client

Thành phần

  • kl_mcp_client.client.MCPClient: sync JSON-RPC client
  • kl_mcp_client.tools.MCPTools: sync tools wrapper
  • kl_mcp_client.asyncio.client.MCPClient: async JSON-RPC client
  • kl_mcp_client.asyncio.tools.MCPTools: async tools wrapper

Quickstart (sync)

from kl_mcp_client.tools import MCPTools

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

sid = tools.create_session("http://localhost:9222/json/version")["sessionId"]
tools.open_page(sid, "https://example.com")
print(tools.get_html(sid))

Quickstart (async)

import asyncio
from kl_mcp_client.asyncio.tools import MCPTools


async def main():
    tools = MCPTools()
    tools.connect_mcp("http://localhost:3000/mcp")

    sid = (await tools.create_session("http://localhost:9222/json/version"))["sessionId"]
    await tools.open_page(sid, "https://example.com")
    print(await tools.get_html(sid))
    await tools.close()


asyncio.run(main())

API chính

Kết nối / core

  • connect_mcp(mcp_url=None, mcpUrl=None, headers=None, timeout=30, retries=2, proxies=None)
  • close()

Session / navigation

  • create_session(cdpUrl)
  • close_session(sessionId)
  • list_sessions()
  • open_page(sessionId, url)
  • get_html(sessionId)
  • screenshot(sessionId)
  • wait_for_selector(sessionId, selector, timeout=None, timeoutMs=None)

Element / action

  • click(sessionId, selector)
  • type(sessionId, selector, text)
  • click_to_text(sessionId, text)
  • find_element(sessionId, selector)
  • find_all(sessionId, selector)
  • find_element_by_xpath(sessionId, xpath)
  • find_element_by_text(sessionId, text)
  • find_element_by_prompt(sessionId, prompt)
  • get_bounding_box(sessionId, selector)
  • click_bounding_box(sessionId, selector)
  • perform(...)
  • scroll(...)

File / cookie

  • upload_file_to_server(file_path): upload file local lên MCP server và nhận uploadId
  • set_file_for_input(sessionId, selector, uploadId): gắn uploadId vào input[type=file]
  • upload_file(sessionId, selector, file_path): wrapper tương thích ngược, tự chạy cả 2 bước trên
  • import_cookies(sessionId, cookies)
  • get_cookies(sessionId)
  • cookies(sessionId): alias của get_cookies

Stream / parsing / viewport

  • evaluate(sessionId, expression)
  • evaluate_stream(sessionId, expression, chunkSize=100)
  • stream_pull(stream_id, offset=0, limit=100)
  • evaluate_stream_all(sessionId, expression, chunkSize=100, max_items=None)
  • parse_html_by_prompt(html, prompt)
  • viewport(sessionId, viewport=None)
  • set_viewport(...)
  • get_viewport(sessionId)

Tab / browser helpers

  • get_current_url(sessionId)
  • current_url(sessionId): alias của get_current_url
  • list_tabs(sessionId)
  • switch_tab(sessionId, tabId=None, targetId=None)
  • close_tab(sessionId, tabId)
  • new_tab(sessionId, url=None)
  • switch_tab_by_title(sessionId, title)
  • switch_tab_by_url(sessionId, url)
  • current_tab(sessionId)
  • create_browser(payload=None)
  • release_browser(pod_name)
  • drag_and_drop(...)
  • hover(...)

Upload file 2 bước

upload = tools.upload_file_to_server("/absolute/path/to/test.pdf")

tools.set_file_for_input(
    sessionId=sid,
    selector="input[type=file]",
    uploadId=upload["uploadId"],
)

Hoặc dùng wrapper cũ:

tools.upload_file(
    sessionId=sid,
    selector="input[type=file]",
    file_path="/absolute/path/to/test.pdf",
)

Lưu ý

  • Cần gọi connect_mcp(...) trước khi gọi tool khác.
  • Flow upload dùng endpoint HTTP /upload để lấy uploadId, sau đó gọi tool uploadFile.
  • Bản async hỗ trợ cùng semantics với bản sync, nhưng tất cả lời gọi là bất đồng bộ (await).

License

MIT License.

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.1.24.tar.gz (12.4 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.1.24-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for kl_mcp_client-2.1.24.tar.gz
Algorithm Hash digest
SHA256 451941a36dbf31d6fe055b173643bac1c1e0d0d110a87157b96d8d7c1dbf1756
MD5 2c27e4247c1fcb5a2b641182c8926552
BLAKE2b-256 d34249dbb3321d336f2136df094c72f65b99e4d663f181b33a129f5928cd8495

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for kl_mcp_client-2.1.24-py3-none-any.whl
Algorithm Hash digest
SHA256 3f2c2c03429dd2dbffe632edbef3be5aeffefcc4ad478bd614d288ef0bb25fa2
MD5 6026d875552eff180ec411f0445d519d
BLAKE2b-256 f020e07e6b877172d2fbb6086dd609d1ab5646f1a52afd6f6240a4027bf9bb7c

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