bots-garden/mini-mepublic Fork
⑂ e6108b0
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/mini-me.git
git clone ssh://git@rickub.com/bots-garden/mini-me.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

♻️ Quality: news-roundup and struggle-signals scripts without smells (2 → 0)

- ddg_news.py: do_POST with read_message and answer (a table of the
  methods that have a result)
- fake-llm.py: the scenarios as a SCENARIOS table

Checked: struggle-signals/run-all.sh (9 scenarios through mm -p) with
the old and the new fake model, and the same 10 JSON-RPC requests sent
to the old and new ddg_news.py (initialize ×2, a notification, ping,
tools/list, an unknown tool, an empty query, an unknown method, bad
JSON, another path): identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
agent committed 2026-11-28T13:00:50+01:00 Browse files
e6108b0 parent: 19f68c0
modified demos/news-roundup/ddg_news.py +22 -10
@@ -158,25 +158,37 @@ class Handler(BaseHTTPRequestHandler):
158158 def do_POST(self):
159159 if self.path.rstrip("/") != "/mcp":
160160 return self.reply(404)
161- try:
162- msg = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))))
163- except (ValueError, json.JSONDecodeError):
161+ msg = self.read_message()
162+ if msg is None:
164163 return self.reply(400, {"jsonrpc": "2.0", "id": None, "error": {"code": -32700, "message": "parse error"}})
165164 if "id" not in msg: # a notification (notifications/initialized…)
166165 return self.reply(202)
166+ return self.answer(msg)
167+
168+ def read_message(self):
169+ """The JSON-RPC message posted, None when it is not JSON."""
170+ try:
171+ return json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))))
172+ except (ValueError, json.JSONDecodeError):
173+ return None
174+
175+ def answer(self, msg):
176+ """Answers one request: initialize opens a session; ping, tools/list and tools/call have
177+ a result; any other method is unknown."""
167178 method, mid = msg.get("method"), msg["id"]
168179 if method == "initialize":
169180 version = msg.get("params", {}).get("protocolVersion", "2025-06-18")
170181 result = {"protocolVersion": version, "capabilities": {"tools": {}},
171182 "serverInfo": {"name": "ddg-news", "version": "1.0.0"}}
172183 return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": result}, session=f"ddg-{time.time_ns()}")
173- if method == "ping":
174- return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": {}})
175- if method == "tools/list":
176- return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": {"tools": [TOOL]}})
177- if method == "tools/call":
178- return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": self.call(msg.get("params", {}))})
179- return self.reply(200, {"jsonrpc": "2.0", "id": mid, "error": {"code": -32601, "message": f"unknown method {method}"}})
184+ results = {
185+ "ping": lambda: {},
186+ "tools/list": lambda: {"tools": [TOOL]},
187+ "tools/call": lambda: self.call(msg.get("params", {})),
188+ }
189+ if method not in results:
190+ return self.reply(200, {"jsonrpc": "2.0", "id": mid, "error": {"code": -32601, "message": f"unknown method {method}"}})
191+ return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": results[method]()})
180192
181193 def call(self, params):
182194 args = params.get("arguments") or {}
@@ -158,25 +158,37 @@ class Handler(BaseHTTPRequestHandler):
158 def do_POST(self):158 def do_POST(self):
159 if self.path.rstrip("/") != "/mcp":159 if self.path.rstrip("/") != "/mcp":
160 return self.reply(404)160 return self.reply(404)
161- try:161+ msg = self.read_message()
162- msg = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))))162+ if msg is None:
163- except (ValueError, json.JSONDecodeError):
164 return self.reply(400, {"jsonrpc": "2.0", "id": None, "error": {"code": -32700, "message": "parse error"}})163 return self.reply(400, {"jsonrpc": "2.0", "id": None, "error": {"code": -32700, "message": "parse error"}})
165 if "id" not in msg: # a notification (notifications/initialized…)164 if "id" not in msg: # a notification (notifications/initialized…)
166 return self.reply(202)165 return self.reply(202)
166+ return self.answer(msg)
167+
168+ def read_message(self):
169+ """The JSON-RPC message posted, None when it is not JSON."""
170+ try:
171+ return json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))))
172+ except (ValueError, json.JSONDecodeError):
173+ return None
174+
175+ def answer(self, msg):
176+ """Answers one request: initialize opens a session; ping, tools/list and tools/call have
177+ a result; any other method is unknown."""
167 method, mid = msg.get("method"), msg["id"]178 method, mid = msg.get("method"), msg["id"]
168 if method == "initialize":179 if method == "initialize":
169 version = msg.get("params", {}).get("protocolVersion", "2025-06-18")180 version = msg.get("params", {}).get("protocolVersion", "2025-06-18")
170 result = {"protocolVersion": version, "capabilities": {"tools": {}},181 result = {"protocolVersion": version, "capabilities": {"tools": {}},
171 "serverInfo": {"name": "ddg-news", "version": "1.0.0"}}182 "serverInfo": {"name": "ddg-news", "version": "1.0.0"}}
172 return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": result}, session=f"ddg-{time.time_ns()}")183 return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": result}, session=f"ddg-{time.time_ns()}")
173- if method == "ping":184+ results = {
174- return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": {}})185+ "ping": lambda: {},
175- if method == "tools/list":186+ "tools/list": lambda: {"tools": [TOOL]},
176- return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": {"tools": [TOOL]}})187+ "tools/call": lambda: self.call(msg.get("params", {})),
177- if method == "tools/call":188+ }
178- return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": self.call(msg.get("params", {}))})189+ if method not in results:
179- return self.reply(200, {"jsonrpc": "2.0", "id": mid, "error": {"code": -32601, "message": f"unknown method {method}"}})190+ return self.reply(200, {"jsonrpc": "2.0", "id": mid, "error": {"code": -32601, "message": f"unknown method {method}"}})
191+ return self.reply(200, {"jsonrpc": "2.0", "id": mid, "result": results[method]()})
180 192
181 def call(self, params):193 def call(self, params):
182 args = params.get("arguments") or {}194 args = params.get("arguments") or {}
modified demos/struggle-signals/fake-llm.py +15 -15
@@ -45,23 +45,23 @@ def text(content, usage=None):
4545 return [chunk({"role": "assistant", "content": content}), chunk({}, "stop", usage)]
4646
4747
48+# The answer of each scenario: a function, since some read the call count.
49+SCENARIOS = {
50+ "loop": lambda: tool_call("bash", json.dumps({"command": "cat /no/such/file"})),
51+ "unknown": lambda: tool_call("run_python", json.dumps({"code": "print(1)"})),
52+ "empty": lambda: text(""),
53+ "context": lambda: text("Done.", {"prompt_tokens": 900, "completion_tokens": 2, "total_tokens": 902}),
54+ "broken": lambda: tool_call("bash", '{"command": "ls'),
55+ "failing": lambda: tool_call("bash", json.dumps({"command": "cat /no/such/file-%d" % calls})),
56+ "turns": lambda: tool_call("bash", json.dumps({"command": "echo step %d" % calls})),
57+}
58+
59+
4860 def reply():
4961 """The SSE events of one answer, for the scenario."""
50- if SCENARIO == "loop":
51- return tool_call("bash", json.dumps({"command": "cat /no/such/file"}))
52- if SCENARIO == "unknown":
53- return tool_call("run_python", json.dumps({"code": "print(1)"}))
54- if SCENARIO == "empty":
55- return text("")
56- if SCENARIO == "context":
57- return text("Done.", {"prompt_tokens": 900, "completion_tokens": 2, "total_tokens": 902})
58- if SCENARIO == "broken":
59- return tool_call("bash", '{"command": "ls')
60- if SCENARIO == "failing":
61- return tool_call("bash", json.dumps({"command": "cat /no/such/file-%d" % calls}))
62- if SCENARIO == "turns":
63- return tool_call("bash", json.dumps({"command": "echo step %d" % calls}))
64- raise ValueError(SCENARIO)
62+ if SCENARIO not in SCENARIOS:
63+ raise ValueError(SCENARIO)
64+ return SCENARIOS[SCENARIO]()
6565
6666
6767 class Handler(BaseHTTPRequestHandler):
@@ -45,23 +45,23 @@ def text(content, usage=None):
45 return [chunk({"role": "assistant", "content": content}), chunk({}, "stop", usage)]45 return [chunk({"role": "assistant", "content": content}), chunk({}, "stop", usage)]
46 46
47 47
48+# The answer of each scenario: a function, since some read the call count.
49+SCENARIOS = {
50+ "loop": lambda: tool_call("bash", json.dumps({"command": "cat /no/such/file"})),
51+ "unknown": lambda: tool_call("run_python", json.dumps({"code": "print(1)"})),
52+ "empty": lambda: text(""),
53+ "context": lambda: text("Done.", {"prompt_tokens": 900, "completion_tokens": 2, "total_tokens": 902}),
54+ "broken": lambda: tool_call("bash", '{"command": "ls'),
55+ "failing": lambda: tool_call("bash", json.dumps({"command": "cat /no/such/file-%d" % calls})),
56+ "turns": lambda: tool_call("bash", json.dumps({"command": "echo step %d" % calls})),
57+}
58+
59+
48 def reply():60 def reply():
49 """The SSE events of one answer, for the scenario."""61 """The SSE events of one answer, for the scenario."""
50- if SCENARIO == "loop":62+ if SCENARIO not in SCENARIOS:
51- return tool_call("bash", json.dumps({"command": "cat /no/such/file"}))63+ raise ValueError(SCENARIO)
52- if SCENARIO == "unknown":64+ return SCENARIOS[SCENARIO]()
53- return tool_call("run_python", json.dumps({"code": "print(1)"}))
54- if SCENARIO == "empty":
55- return text("")
56- if SCENARIO == "context":
57- return text("Done.", {"prompt_tokens": 900, "completion_tokens": 2, "total_tokens": 902})
58- if SCENARIO == "broken":
59- return tool_call("bash", '{"command": "ls')
60- if SCENARIO == "failing":
61- return tool_call("bash", json.dumps({"command": "cat /no/such/file-%d" % calls}))
62- if SCENARIO == "turns":
63- return tool_call("bash", json.dumps({"command": "echo step %d" % calls}))
64- raise ValueError(SCENARIO)
65 65
66 66
67 class Handler(BaseHTTPRequestHandler):67 class Handler(BaseHTTPRequestHandler):