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 clientkl_mcp_client.tools.MCPTools: sync tools wrapperkl_mcp_client.asyncio.client.MCPClient: async JSON-RPC clientkl_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ậnuploadIdset_file_for_input(sessionId, selector, uploadId): gắnuploadIdvàoinput[type=file]upload_file(sessionId, selector, file_path): wrapper tương thích ngược, tự chạy cả 2 bước trênimport_cookies(sessionId, cookies)get_cookies(sessionId)cookies(sessionId): alias củaget_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ủaget_current_urllist_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ấyuploadId, sau đó gọi tooluploadFile. - 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
Release history Release notifications | RSS feed
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.23.tar.gz
(12.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kl_mcp_client-2.1.23.tar.gz.
File metadata
- Download URL: kl_mcp_client-2.1.23.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c01b649855c829a61d46857f2333ac78243d4b15ddaf3311b977925f6769952
|
|
| MD5 |
f618dff6d0f3ab525ba9fb327e63d60c
|
|
| BLAKE2b-256 |
77aab8d642665e5eae9afae3b9a90f51e414b15e3a1690e3b533f8046a2e44ec
|
File details
Details for the file kl_mcp_client-2.1.23-py3-none-any.whl.
File metadata
- Download URL: kl_mcp_client-2.1.23-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
794db658461a8d798220295cff07627bb4febbe445f46e694d6f56f9d6911c1f
|
|
| MD5 |
53f5ac2a60e21ae91cae798ff147ee3b
|
|
| BLAKE2b-256 |
fbdff7e719d8c99b497334098657950501b9bde4f3fde767c3e9ad6696da4e8b
|