Neural Mastery
← Practice
Parse Content-Length Stdio Framingmedium

Parse Content-Length Stdio Framing

medium Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement Content-Length-prefixed message framing parsing, the real wire format MCP's stdio transport uses (identical in shape to LSP's framing).

Task

Implement parse_stdio_frame(raw_bytes). The format is a header line 'Content-Length: N\r\n', a blank line '\r\n', then exactly N bytes of body. Parse N from the header, extract the body of that exact length, and return (body, remainder) where remainder is whatever bytes come after the frame (may be '').

Function Signature

parse_stdio_frame(raw_bytes: str) -> tuple

Examples

Example 1: single complete frame
Input: raw_bytes = 'Content-Length: 13 \n \n{"a": "bcd"}\n'
Output: ['{"a": "bcd"}\n', '']
Example 2: frame with remainder
Input: raw_bytes = 'Content-Length: 5 \n \nhelloEXTRA'
Output: ['hello', 'EXTRA']
Example 3: minimal empty-ish body
Input: raw_bytes = 'Content-Length: 2 \n \n{}'
Output: ['{}', '']

Constraints

  • raw_bytes contains at least one complete, well-formed frame
Python3Saved ✓
def parse_stdio_frame(raw_bytes):
# Your implementation here
pass

Case 1: single complete frame
Input: raw_bytes = 'Content-Length: 13 \n \n{"a": "bcd"}\n'
Expected: ['{"a": "bcd"}\n', '']
Case 2: frame with remainder
Input: raw_bytes = 'Content-Length: 5 \n \nhelloEXTRA'
Expected: ['hello', 'EXTRA']
Case 3: minimal empty-ish body
Input: raw_bytes = 'Content-Length: 2 \n \n{}'
Expected: ['{}', '']