Skip to main content

Codec

Trait Codec 

Source
pub trait Codec {
    type Wire;

    // Required methods
    fn version(&self) -> ProtocolVersion;
    fn capabilities(&self) -> FeatureProfile;
    fn lift(&self, wire: &Self::Wire) -> ToolResult;
    fn lower(
        &self,
        value: ToolResult,
        policy: DegradePolicy,
        report: &mut TranslationReport,
    ) -> Result<Self::Wire, LowerError>;
}
Expand description

Translates between one revision’s wire type and the canonical IR.

See the module docs at the top of this file for the contract. The short version: lift is total, lower is gated.

Required Associated Types§

Source

type Wire

The generated wire type for this revision.

Named per implementation rather than fixed, because the revisions do not agree on their shapes: CallToolResult.content is Vec<CallToolResultContentChoice> at 2024-11-05 and Vec<ContentBlock> from 2025-06-18, and only structuredContent distinguishes the revisions that can carry it. A single wire type would have to be the least common denominator, which is a type that cannot express anything — the mistake the IR exists to avoid.

Required Methods§

Source

fn version(&self) -> ProtocolVersion

The revision this codec speaks.

Source

fn capabilities(&self) -> FeatureProfile

What this revision can express, as a negotiated profile.

Used by the gate, so it must be the real profile for a peer speaking this revision — the intersection of what both sides advertise, not what the revision permits in the abstract. A codec that answers with the revision’s maximum would let a value through that the actual peer cannot read.

Source

fn lift(&self, wire: &Self::Wire) -> ToolResult

Wire to IR.

Infallible by design: a message the peer sent always has an IR form. Use UnknownContent for a block this build does not recognise, and Extensions for fields it does not name, so nothing is lost in the crossing.

Source

fn lower( &self, value: ToolResult, policy: DegradePolicy, report: &mut TranslationReport, ) -> Result<Self::Wire, LowerError>

IR to wire.

§Errors

LowerError::FeatureUnavailable when the value needs something this revision cannot express and the policy authorises no rule for it. Any rule the policy does authorise is applied and recorded in report.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§