Files
quantux_mcp/tests/mcp_create_flow_test.py
T
2026-08-17 10:24:19 +08:00

65 lines
3.1 KiB
Python

import asyncio
import json
import os
def main():
KEY = open("/home/ubuntu/quantux-mcp/.env").read().split("MCP_API_KEY=")[1].splitlines()[0].strip()
URL = os.environ.get("MCP_URL", "http://127.0.0.1:8091/mcp")
import httpx2
from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client
async def call(session, name, args):
r = await session.call_tool(name, args)
return r.content[0].text
async def run():
http_client = httpx2.AsyncClient(headers={"Authorization": f"Bearer {KEY}"})
async with streamable_http_client(URL, http_client=http_client) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# 1. create app
app = json.loads(await call(session, "quantux_create_app", {
"name": "MCP创建的应用", "description": "通过 MCP 工具链自动创建", "width": 390, "height": 844}))
app_id = app["app_id"]
print("1. create_app ->", app_id)
# 2. add screen
scr = json.loads(await call(session, "quantux_add_screen", {"app_id": app_id, "name": "欢迎页"}))
screen_id = scr["screen_id"]
print("2. add_screen ->", screen_id)
# 3. add widgets
lbl = json.loads(await call(session, "quantux_add_widget", {
"app_id": app_id, "screen_id": screen_id, "widget_type": "Label",
"x": 40, "y": 120, "w": 310, "h": 50, "name": "标题",
"style_json": json.dumps({"fontSize": 30, "fontWeight": 700, "color": "#1F2937", "textAlign": "center"})}))
btn = json.loads(await call(session, "quantux_add_widget", {
"app_id": app_id, "screen_id": screen_id, "widget_type": "Button",
"x": 60, "y": 400, "w": 270, "h": 52, "name": "开始按钮",
"props_json": json.dumps({"label": "开始使用"}),
"style_json": json.dumps({"background": "#10B981", "color": "#FFFFFF", "fontSize": 16,
"borderTopLeftRadius": 26, "borderTopRightRadius": 26,
"borderBottomLeftRadius": 26, "borderBottomRightRadius": 26})}))
print("3. add_widgets ->", lbl["widget_id"], btn["widget_id"])
# 4. connect flow: button -> (self, demo only) create second screen and wire
scr2 = json.loads(await call(session, "quantux_add_screen", {"app_id": app_id, "name": "首页"}))
screen2 = scr2["screen_id"]
await call(session, "quantux_connect_flow",
{"app_id": app_id, "from_widget_id": btn["widget_id"], "to_screen_id": screen2})
print("4. connect_flow ->", btn["widget_id"], "->", screen2)
# 5. verify
desc = json.loads(await call(session, "quantux_get_app", {"app_id": app_id}))
print("5. get_app ->", json.dumps(desc, ensure_ascii=False))
asyncio.run(run())
if __name__ == "__main__":
main()