♻️ 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>
e6108b0 parent: 19f68c0 modified
demos/news-roundup/ddg_news.py +22 -10 | @@ -158,25 +158,37 @@ class Handler(BaseHTTPRequestHandler): | ||
| 158 | 158 | def do_POST(self): |
| 159 | 159 | if self.path.rstrip("/") != "/mcp": |
| 160 | 160 | 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: | |
| 164 | 163 | return self.reply(400, {"jsonrpc": "2.0", "id": None, "error": {"code": -32700, "message": "parse error"}}) |
| 165 | 164 | if "id" not in msg: # a notification (notifications/initialized…) |
| 166 | 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 | 178 | method, mid = msg.get("method"), msg["id"] |
| 168 | 179 | if method == "initialize": |
| 169 | 180 | version = msg.get("params", {}).get("protocolVersion", "2025-06-18") |
| 170 | 181 | result = {"protocolVersion": version, "capabilities": {"tools": {}}, |
| 171 | 182 | "serverInfo": {"name": "ddg-news", "version": "1.0.0"}} |
| 172 | 183 | 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]()}) | |
| 180 | 192 | |
| 181 | 193 | def call(self, params): |
| 182 | 194 | 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): | ||
| 45 | 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 | 60 | def reply(): |
| 49 | 61 | """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]() | |
| 65 | 65 | |
| 66 | 66 | |
| 67 | 67 | 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): |