Skip to content

protocol

protocol

MCP JSON-RPC 2.0 protocol message types.

Classes

MCPRequest dataclass

MCPRequest(method: str, params: Dict[str, Any] = dict(), id: int | str = 0, jsonrpc: str = '2.0')

JSON-RPC 2.0 request message.

Functions
to_json
to_json() -> str

Serialize to JSON string.

Source code in src/openjarvis/mcp/protocol.py
def to_json(self) -> str:
    """Serialize to JSON string."""
    return json.dumps(
        {
            "jsonrpc": self.jsonrpc,
            "id": self.id,
            "method": self.method,
            "params": self.params,
        }
    )
from_json classmethod
from_json(data: str) -> MCPRequest

Deserialize from JSON string.

Source code in src/openjarvis/mcp/protocol.py
@classmethod
def from_json(cls, data: str) -> MCPRequest:
    """Deserialize from JSON string."""
    parsed = json.loads(data)
    return cls(
        method=parsed["method"],
        params=parsed.get("params", {}),
        id=parsed.get("id", 0),
        jsonrpc=parsed.get("jsonrpc", "2.0"),
    )

MCPResponse dataclass

MCPResponse(result: Any = None, error: Optional[Dict[str, Any]] = None, id: int | str = 0, jsonrpc: str = '2.0')

JSON-RPC 2.0 response message.

Functions
to_json
to_json() -> str

Serialize to JSON string.

Source code in src/openjarvis/mcp/protocol.py
def to_json(self) -> str:
    """Serialize to JSON string."""
    obj: Dict[str, Any] = {"jsonrpc": self.jsonrpc, "id": self.id}
    if self.error is not None:
        obj["error"] = self.error
    else:
        obj["result"] = self.result
    return json.dumps(obj)
from_json classmethod
from_json(data: str) -> MCPResponse

Deserialize from JSON string.

Source code in src/openjarvis/mcp/protocol.py
@classmethod
def from_json(cls, data: str) -> MCPResponse:
    """Deserialize from JSON string."""
    parsed = json.loads(data)
    return cls(
        result=parsed.get("result"),
        error=parsed.get("error"),
        id=parsed.get("id", 0),
        jsonrpc=parsed.get("jsonrpc", "2.0"),
    )
error_response classmethod
error_response(id: int | str, code: int, message: str, data: Any = None) -> MCPResponse

Create an error response.

Source code in src/openjarvis/mcp/protocol.py
@classmethod
def error_response(
    cls,
    id: int | str,
    code: int,
    message: str,
    data: Any = None,
) -> MCPResponse:
    """Create an error response."""
    error: Dict[str, Any] = {"code": code, "message": message}
    if data is not None:
        error["data"] = data
    return cls(error=error, id=id)

MCPNotification dataclass

MCPNotification(method: str, params: Dict[str, Any] = dict(), jsonrpc: str = '2.0')

JSON-RPC 2.0 notification (no id, no response expected).

Functions
to_json
to_json() -> str

Serialize to JSON string.

Source code in src/openjarvis/mcp/protocol.py
def to_json(self) -> str:
    """Serialize to JSON string."""
    return json.dumps(
        {
            "jsonrpc": self.jsonrpc,
            "method": self.method,
            "params": self.params,
        }
    )

MCPError dataclass

MCPError(code: int, message: str, data: Any = None)

Bases: Exception

MCP protocol error with JSON-RPC error code.