Skip to content

Local video editor for coding agents: 42 FFmpeg tools with a machine-readable contract and an MCP server, for Claude Code, Cursor and Codex. Cut, captions, reframe, loudness, platform exports. No cloud, no API keys.

Fix the MCP server crashing on a non-object JSON-RPC line (#120)

Shogo Kajiyama21f977f796af
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 866fd11..69f67b1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,19 @@
(nothing yet)
+## 0.16.9 — fix the MCP server crashing on a non-object JSON-RPC line
+
+`mcp/server.py`'s stdio loop parsed each line with `json.loads()`, which
+accepts any valid JSON value, not just an object -- a bare `42`, `null`,
+`true` or `[1,2,3]` line parses without error. The very next check,
+`"id" not in req`, then raised an uncaught `TypeError` for a non-dict
+`req` (an int/bool/None isn't iterable the way `in` needs). That check
+sat outside the `try/except` wrapping `handle()`, so the exception
+propagated out of the stdin loop and killed the entire stdio server
+process -- not just that one malformed line, but every other in-flight
+and future tool call in the session along with it. Fixed by skipping any
+parsed JSON value that isn't a dict before the `"id" not in req` check.
+
## 0.16.7 — fix a silently-dropped render.py fit height and a multicam.py drift-trim ordering bug
- `render.py`'s single-clip fit path only ever inherited `width`/`fps` from
diff --git a/docs/contract.md b/docs/contract.md
index fe19621..1c8177b 100644
--- a/docs/contract.md
+++ b/docs/contract.md
@@ -21,7 +21,7 @@ The contract is derived from the code that runs, not maintained beside it:
| Field | Meaning | Changes when |
|---|---|---|
| `contract_version` | shape of this document (`1.0`) | a key is renamed, removed or changes meaning |
-| `skill.version` | the npm / package.json version (`0.16.7`) | any release |
+| `skill.version` | the npm / package.json version (`0.16.9`) | any release |
A release that adds a tool or a flag keeps `contract_version`; a breaking change to the
ToolSpec shape bumps it. Consumers pin on `contract_version` and read `skill.version`
@@ -39,7 +39,7 @@ drifting silently.
```json
{
"contract_version": "1.0",
- "skill": {"id": "ffmpeg-skill", "version": "0.16.7", "execution_mode": "local", "kind": "execution",
+ "skill": {"id": "ffmpeg-skill", "version": "0.16.9", "execution_mode": "local", "kind": "execution",
"entrypoints": {"cli": "...", "mcp": "...", "contract": "...", "doctor": "..."},
"not_provided": ["AI reasoning", "decisions", "production plans", "project IR", "approvals", "network access", "transcription engine"]},
"requirements": {"python": ">=3.9 (standard library only)", "ffmpeg": ">=5.0", "ffprobe": ">=5.0"},
diff --git a/mcp/server.py b/mcp/server.py
index f2273da..d819609 100755
--- a/mcp/server.py
+++ b/mcp/server.py
@@ -158,6 +158,14 @@ def main() -> int:
req = json.loads(line)
except ValueError:
continue
+ # json.loads accepts any valid JSON value, not just an object -- a bare `42`, `null`,
+ # `true` or `[1,2]` line parses fine but isn't a JSON-RPC request. Without this guard,
+ # "id" not in req raised TypeError for a non-dict req (ints/bools/None aren't iterable
+ # the way `in` needs), uncaught by the try/except below it, killing the whole stdio
+ # server process -- not just failing that one malformed line, but every other in-flight
+ # and future tool call in the session along with it.
+ if not isinstance(req, dict):
+ continue
if "id" not in req: # notification
continue
try:
diff --git a/package.json b/package.json
index 18df5d8..f952116 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "ffmpeg-skill",
- "version": "0.16.7",
+ "version": "0.16.9",
"description": "Agent Skill that gives coding agents (Claude Code, Cursor, Codex) a local video editor: 40 FFmpeg tools with a machine-readable contract, contract-derived MCP server, FFmpeg capability detection, probe-first / verify-last workflow. Cut, join, silence removal, fit, captions and karaoke, overlays, motion graphics, HDR to SDR, LUTs, audio clean-up and typed dynamics, sync with drift correction, multicam, loudness, delivery checks, project rendering, batch. No API keys, no cloud, no dependencies.",
"keywords": [
"ffmpeg",
diff --git a/tests/test_all.py b/tests/test_all.py
index 0dbc862..68a2058 100644
--- a/tests/test_all.py
+++ b/tests/test_all.py
@@ -2193,6 +2193,25 @@ class FFmpegSkillTests(unittest.TestCase):
self.assertTrue(resp[6]["result"].get("isError"))
self.assertEqual(resp[7]["error"]["code"], -32601)
+ def test_mcp_server_survives_a_non_object_json_line(self):
+ """json.loads accepts any valid JSON value, not just an object -- a bare `42`, `null`,
+ `true` or `[1,2]` line parses without raising, but main()'s very next line, `"id" not in
+ req`, raised an uncaught TypeError for a non-dict req (an int/bool/None isn't iterable the
+ way `in` needs). That check sat outside the try/except wrapping handle(), so the exception
+ propagated out of the stdin loop and killed the whole stdio server process -- not just
+ that one malformed line, but every other in-flight and future tool call in the session.
+ Verify a line like this is now skipped, and the server stays alive and answers the next
+ (valid) request instead of exiting non-zero with nothing produced for it."""
+ server = ROOT / "mcp" / "server.py"
+ lines = ["42", "null", "true", "[1,2,3]",
+ json.dumps({"jsonrpc": "2.0", "id": 1, "method": "tools/list"})]
+ proc = subprocess.run([sys.executable, str(server)], input="\n".join(lines) + "\n", stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
+ self.assertEqual(proc.returncode, 0, f"server must not crash on non-object JSON lines; stderr: {proc.stderr}")
+ resp = [json.loads(response_line) for response_line in proc.stdout.splitlines() if response_line.strip()]
+ self.assertEqual(len(resp), 1, "only the one real request should get a response")
+ self.assertEqual(resp[0]["id"], 1)
+ self.assertIn("tools", resp[0]["result"])
+
def test_batch_recipe_and_cache(self):
folder = OUT / "batch_in"
folder.mkdir(exist_ok=True)