50 lines
4.3 KiB
Python
50 lines
4.3 KiB
Python
import asyncio
|
|
import json
|
|
import httpx2
|
|
from mcp import ClientSession
|
|
from mcp.client.streamable_http import streamable_http_client
|
|
|
|
KEY = open("/home/ubuntu/quantux-mcp/.env").read().split("MCP_API_KEY=")[1].splitlines()[0].strip()
|
|
URL = "http://127.0.0.1:8091/mcp"
|
|
|
|
|
|
async def main():
|
|
hc = httpx2.AsyncClient(headers={"Authorization": f"Bearer {KEY}"})
|
|
async with streamable_http_client(URL, http_client=hc) as (r, w):
|
|
async with ClientSession(r, w) as s:
|
|
await s.initialize()
|
|
|
|
res = await s.call_tool("quantux_create_app", {"name": "数据组件测试", "width": 800, "height": 1200})
|
|
app = json.loads(res.content[0].text)
|
|
app_id = app["app_id"]
|
|
print("app:", app_id)
|
|
res = await s.call_tool("quantux_add_screen", {"app_id": app_id, "name": "数据面板"})
|
|
sid = json.loads(res.content[0].text)["screen_id"]
|
|
|
|
async def add(typ, x, y, w, h, props, style):
|
|
res = await s.call_tool("quantux_add_widget", {
|
|
"app_id": app_id, "screen_id": sid, "widget_type": typ,
|
|
"x": x, "y": y, "w": w, "h": h, "name": typ,
|
|
"props_json": json.dumps(props, ensure_ascii=False),
|
|
"style_json": json.dumps(style, ensure_ascii=False),
|
|
})
|
|
return json.loads(res.content[0].text)["widget_id"]
|
|
|
|
await add("BarChart", 30, 30, 340, 220, {"data": [["一月", 40, 30], ["二月", 70, 45], ["三月", 50, 60], ["四月", 90, 55]]}, {"background": "#fff", "borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#e5e7eb", "borderRightColor": "#e5e7eb", "borderTopColor": "#e5e7eb", "borderBottomColor": "#e5e7eb"})
|
|
await add("PieChart", 430, 30, 220, 220, {"data": [["线上", 55], ["门店", 25], ["分销", 20]]}, {"background": "#fff", "borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#e5e7eb", "borderRightColor": "#e5e7eb", "borderTopColor": "#e5e7eb", "borderBottomColor": "#e5e7eb"})
|
|
await add("RingChart", 660, 30, 140, 220, {"data": [["完成", 68], ["剩余", 32]], "value": "68%"}, {"background": "#fff"})
|
|
await add("Table", 30, 270, 770, 160, {"data": [["商品", "单价", "库存"], ["鼠标", 39.9, 120], ["键盘", 129, 45], ["显示器", 899, 18]]}, {"background": "#fff", "borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#e5e7eb", "borderRightColor": "#e5e7eb", "borderTopColor": "#e5e7eb", "borderBottomColor": "#e5e7eb", "fontSize": 13})
|
|
await add("ProgressBar", 30, 450, 300, 18, {"value": 72}, {"fontSize": 13})
|
|
await add("Rating", 350, 445, 200, 30, {"value": 4, "max": 5}, {"fontSize": 20})
|
|
await add("Stepper", 30, 490, 400, 34, {"value": 1, "steps": ["填写信息", "确认订单", "支付完成"]}, {"fontSize": 12})
|
|
await add("HSlider", 450, 492, 200, 26, {"value": 60}, {"fontSize": 13})
|
|
await add("QDate", 30, 540, 200, 40, {"value": "2026-08-15"}, {"background": "#fff", "borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#999", "borderRightColor": "#999", "borderTopColor": "#999", "borderBottomColor": "#999"})
|
|
await add("IFrameWidget", 260, 540, 300, 150, {"url": "https://example.com"}, {"borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#ccc", "borderRightColor": "#ccc", "borderTopColor": "#ccc", "borderBottomColor": "#ccc"})
|
|
await add("NavBar", 30, 710, 740, 46, {"items": ["首页", "商品", "订单", "我的"]}, {"background": "#4F46E5", "color": "#fff", "fontSize": 15})
|
|
await add("LineChart", 30, 780, 740, 200, {"isLine": True, "data": [["周一", 30], ["周二", 55], ["周三", 40], ["周四", 80], ["周五", 65], ["周六", 95], ["周日", 75]]}, {"background": "#fff", "borderLeftWidth": 1, "borderRightWidth": 1, "borderTopWidth": 1, "borderBottomWidth": 1, "borderLeftColor": "#e5e7eb", "borderRightColor": "#e5e7eb", "borderTopColor": "#e5e7eb", "borderBottomColor": "#e5e7eb"})
|
|
|
|
res = await s.call_tool("quantux_export_html", {"app_id": app_id})
|
|
print("export:", res.content[0].text)
|
|
|
|
asyncio.run(main())
|