Claude integration: MCP server with render_page images, skill, JSON CLI
- nxhmi.mcp_server: dependency-free stdio MCP server, 14 tools; readable values in and out; render_page returns a PNG image - .mcp.json for Claude Code, .claude/skills/nextion-hmi/SKILL.md - CLI: info --json, schema <kind> - fix: project.add_page(h=...) clashed with the page height keyword - 75 tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b9d7410716
commit
6f8341c66c
9 files changed
+521
-6
No files matched your search
@@ -0,0 +1,105 @@
|
||||
"""MCP server: protocol round trip over stdio and every tool."""
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from helpers import FIXTURE, HAVE_PIL, ROOT
|
||||
|
||||
from nxhmi import mcp_server as M
|
||||
|
||||
|
||||
class Protocol(unittest.TestCase):
|
||||
def test_stdio_session(self):
|
||||
env = dict(os.environ, PYTHONPATH=os.path.join(ROOT, "src"))
|
||||
msgs = [{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18"}},
|
||||
{"jsonrpc": "2.0", "method": "notifications/initialized"},
|
||||
{"jsonrpc": "2.0", "id": 2, "method": "tools/list"},
|
||||
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "list_kinds", "arguments": {}}},
|
||||
{"jsonrpc": "2.0", "id": 4, "method": "nope"}]
|
||||
p = subprocess.run([sys.executable, "-m", "nxhmi.mcp_server"], input="\n".join(json.dumps(m) for m in msgs) + "\n",
|
||||
capture_output=True, text=True, env=env, timeout=60)
|
||||
replies = [json.loads(l) for l in p.stdout.splitlines()]
|
||||
self.assertEqual([r["id"] for r in replies], [1, 2, 3, 4])
|
||||
self.assertEqual(replies[0]["result"]["serverInfo"]["name"], "openhmi-nx")
|
||||
self.assertIn("render_page", [t["name"] for t in replies[1]["result"]["tools"]])
|
||||
self.assertIn("button", json.loads(replies[2]["result"]["content"][0]["text"])["kinds"])
|
||||
self.assertEqual(replies[3]["error"]["code"], -32601)
|
||||
|
||||
|
||||
class Tools(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.mkdtemp()
|
||||
self.hmi = os.path.join(self.tmp, "p.HMI")
|
||||
shutil.copy(FIXTURE, self.hmi)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmp)
|
||||
|
||||
def call(self, tool, **args):
|
||||
r = M.handle({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": tool, "arguments": args}})
|
||||
res = r["result"]
|
||||
if res.get("isError"):
|
||||
raise AssertionError(res["content"][0]["text"])
|
||||
return res["content"]
|
||||
|
||||
def j(self, tool, **args):
|
||||
return json.loads(self.call(tool, **args)[0]["text"])
|
||||
|
||||
def test_info_and_page(self):
|
||||
info = self.j("info", path=self.hmi)
|
||||
self.assertEqual([p["page"] for p in info["pages"]], ["home", "controls", "gallery"])
|
||||
page = self.j("get_page", path=self.hmi, page="home")
|
||||
title = next(c for c in page["components"] if c["name"] == "title")
|
||||
self.assertEqual(title["kind"], "text")
|
||||
self.assertEqual(title["attributes"]["xcen"], "left")
|
||||
|
||||
def test_edit_cycle(self):
|
||||
r = self.j("set_attributes", path=self.hmi, page="home", component="title",
|
||||
attributes={"txt": "edited", "xcen": "center", "pco": "#ff0000", "x": 20})
|
||||
self.assertEqual((r["attributes"]["txt"], r["attributes"]["xcen"], r["attributes"]["pco"]), ("edited", "center", "#ff0000"))
|
||||
self.assertEqual(r["attributes"]["endx"], 20 + r["attributes"]["w"] - 1)
|
||||
self.j("set_event", path=self.hmi, page="home", component="title", event="codesup", lines=["page 1"])
|
||||
c = self.j("add_component", path=self.hmi, page="home", kind="button", name="bNew",
|
||||
attributes={"x": 1, "y": 2, "w": 40, "h": 20, "txt": "go", "style": "3D_Auto"},
|
||||
events={"codesup": ["page 0"]})
|
||||
self.assertEqual(c["events"], {"codesup": ["page 0"]})
|
||||
self.j("remove_component", path=self.hmi, page="home", component="bNew")
|
||||
self.j("add_page", path=self.hmi, name="extra", attributes={"bco": "#000000"})
|
||||
self.assertTrue(self.j("verify", path=self.hmi)["ok"])
|
||||
|
||||
def test_errors_are_tool_results(self):
|
||||
r = M.handle({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
|
||||
"params": {"name": "get_page", "arguments": {"path": self.hmi, "page": "missing"}}})
|
||||
self.assertTrue(r["result"]["isError"])
|
||||
self.assertIn("pages: home", r["result"]["content"][0]["text"])
|
||||
|
||||
def test_schema(self):
|
||||
s = self.j("schema", kind="text")
|
||||
xcen = next(a for a in s["attributes"] if a["name"] == "xcen")
|
||||
self.assertEqual(xcen["values"], ["left", "center", "right"])
|
||||
|
||||
def test_new_project_refuses_overwrite(self):
|
||||
p = os.path.join(self.tmp, "new.HMI")
|
||||
self.assertEqual(self.j("new_project", path=p)["pages"][0]["page"], "page0")
|
||||
with self.assertRaises(AssertionError):
|
||||
self.j("new_project", path=p)
|
||||
|
||||
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
||||
def test_render_and_media(self):
|
||||
content = self.call("render_page", path=self.hmi, page="home")
|
||||
self.assertEqual(content[0]["type"], "image")
|
||||
self.assertEqual(content[0]["mimeType"], "image/png")
|
||||
from PIL import Image
|
||||
png = os.path.join(self.tmp, "i.png")
|
||||
Image.new("RGB", (4, 4), (9, 9, 9)).save(png)
|
||||
self.assertEqual(self.j("add_picture", path=self.hmi, image_path=png)["picture_id"], 6)
|
||||
self.assertEqual(self.j("add_font", path=self.hmi, height=12)["font_id"], 2)
|
||||
self.assertTrue(self.j("verify", path=self.hmi)["ok"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -60,6 +60,13 @@ class Cli(unittest.TestCase):
|
||||
self.assertEqual(self.run_cli("verify", self.hmi)[0], 0)
|
||||
self.assertIn('"title"', self.run_cli("dump", self.hmi, "--page", "home")[1])
|
||||
|
||||
def test_json_info_and_schema(self):
|
||||
import json
|
||||
info = json.loads(self.run_cli("info", self.hmi, "--json")[1])
|
||||
self.assertEqual(info["size"], [480, 320])
|
||||
sch = json.loads(self.run_cli("schema", "slider")[1])
|
||||
self.assertIn("vertical", next(x for x in sch["attributes"] if x["name"] == "mode")["values"])
|
||||
|
||||
def test_decode_encode(self):
|
||||
d = os.path.join(self.tmp, "dec")
|
||||
self.run_cli("decode", self.hmi, d)
|
||||
|
||||
Reference in new issue
Block a user