Skip to content

API Reference parrot_tools.lsp.protocol

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.lsp.protocol

View source

Bounded Content-Length framing for the LSP pilot (FEAT-580, M2).

Implements the byte-level transport LSP requires — Content-Length headers followed by a raw JSON-RPC body — independently of the line- delimited JSON transport StdioMCPServer already uses for MCP (:mod:parrot.mcp.local_server). The two protocols are not compatible: LSP framing needs an exact byte count that never conflates JSON characters with UTF-8 bytes (a single non-ASCII character can be one Python str character and two or more UTF-8 bytes), so Content-Length is always computed and consumed in bytes, never in decoded-string length.

Everything here is a private, byte-oriented helper meant to be consumed by PyrightSession (session.py, a later task) — nothing in this module is an agent-facing tool, spawns a process, or knows anything about Pyright specifically. Reading/writing one frame never blocks longer than the underlying stream allows; timeouts and cancellation are the caller's responsibility (asyncio.wait_for around :func:read_message, or cancelling the awaiting task).

Two bounded caps enforce spec §2.10 ("Bounds"):

  • :data:MAX_HEADER_BYTES (8 KiB) — the entire header block, including every \r\n-terminated line up to (and including) the blank line that ends it.
  • :data:MAX_FRAME_BYTES (8 MiB) — the declared Content-Length body size. A frame that declares more than this is rejected before the body is read, so an adversarial/oversized declaration never forces an unbounded read.

A malformed frame (bad header shape, non-integer/negative/oversized Content-Length, non-UTF-8 body, non-JSON body, or a JSON-RPC shape that is neither a request, a response, nor a notification) raises :class:~parrot_tools.lsp.models.LSPFailure with code "protocol_error". A connection that closes before a frame is complete (premature EOF) raises :class:~parrot_tools.lsp.models.LSPFailure with code "server_crashed" instead, since — from the transport's point of view — an unexpected close of the child's stdout is indistinguishable from the child having died.

Attribute FrameKind

Attribute JSONRPC_VERSION

Attribute MAX_FRAME_BYTES

Attribute MAX_HEADER_BYTES

Class ParsedMessage

One decoded, discriminated JSON-RPC message read off the wire.

Parameter Type Default Description
kind FrameKind
id int | str | None
method str | None
params Any
result Any
error dict[str, Any] | None
raw dict[str, Any]

Fields:

Field Type Default Description
kind FrameKind "request" (has id and method), "response" (has id and exactly one of result/error), or "notification" (has method, no id).
id int | str | None The exact, type-preserving JSON-RPC id (int, str, or None for a notification) — never coerced to another type.
method str | None The method name for a request/notification; None for a response.
params Any The raw params value for a request/notification, or None if absent.
result Any The raw result value for a response, or None otherwise (including a response whose actual result is null, which is indistinguishable at this layer).
error dict[str, Any] | None The raw error object for an error response, or None otherwise.
raw dict[str, Any] The complete decoded JSON object, for callers that need a field this dataclass does not surface individually.

Function build_notification

Build a JSON-RPC notification body (no id, never replied to).

build_notification(method: str, params: Any = None) -> dict[str, Any]

Parameter Type Default Description
method str The notification method name.
params Any None The notification parameters, omitted from the body if None.

Returns: dict[str, Any] — A plain dict ready for :func:write_message.

Function build_request

Build a JSON-RPC request body, preserving request_id's exact type.

build_request(request_id: int | str, method: str, params: Any = None) -> dict[str, Any]

Parameter Type Default Description
request_id int | str The id the eventual response must echo back unchanged.
method str The request method name.
params Any None The request parameters, omitted from the body if None.

Returns: dict[str, Any] — A plain dict ready for :func:write_message.

Function build_response

Build a JSON-RPC response body, echoing request_id unchanged.

build_response(request_id: int | str, *, result: Any = None, error: dict[str, Any] | None = None) -> dict[str, Any]

Parameter Type Default Description
request_id int | str The exact id from the request being answered.
result Any None The success result. Ignored if error is given.
error dict[str, Any] | None None The JSON-RPC error object, if this is an error response.

Returns: dict[str, Any] — A plain dict ready for :func:write_message.

Raises:

  • ValueError — If both result and error are given — a response must carry exactly one of them.

Function read_message

Read and strictly parse one Content-Length-framed JSON-RPC message.

Correctly handles a header/body arriving fragmented across many reads, and multiple complete frames already buffered from a single underlying read — both cases are transparent because :class:asyncio.StreamReader buffers internally; this function only ever consumes exactly one frame per call.

read_message(reader: asyncio.StreamReader, *, max_header_bytes: int = MAX_HEADER_BYTES, max_frame_bytes: int = MAX_FRAME_BYTES) -> ParsedMessage

Parameter Type Default Description
reader asyncio.StreamReader The stream to read one frame from (typically a child process's stdout).
max_header_bytes int MAX_HEADER_BYTES Cap on the header block, before the body is even located. Defaults to :data:MAX_HEADER_BYTES.
max_frame_bytes int MAX_FRAME_BYTES Cap on the declared Content-Length body size, checked before the body is read. Defaults to :data:MAX_FRAME_BYTES.

Returns: ParsedMessage — The parsed, discriminated message.

Raises:

  • LSPFailure — "protocol_error" for a malformed/oversized frame or an invalid JSON-RPC shape; "server_crashed" for a connection that closed before a complete frame was received.

Function write_message

Encode message as one Content-Length-framed JSON-RPC frame.

Content-Length is computed from the UTF-8-encoded body, in bytes — never from the decoded string's character count — so a message containing multi-byte Unicode content is always framed correctly.

write_message(writer: asyncio.StreamWriter, message: Mapping[str, Any], *, max_frame_bytes: int = MAX_FRAME_BYTES) -> None

Parameter Type Default Description
writer asyncio.StreamWriter The stream to write the frame to (typically a child process's stdin). Only write()/drain() are used, so any object exposing that shape (e.g. a test double) works.
message Mapping[str, Any] The JSON-RPC message body. jsonrpc is defaulted to :data:JSONRPC_VERSION if not already present.
max_frame_bytes int MAX_FRAME_BYTES Cap on the encoded body size. Defaults to :data:MAX_FRAME_BYTES.

Raises:

  • LSPFailure — "protocol_error" if message is not JSON-serializable or its encoded body exceeds max_frame_bytes.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally