{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "tidb-link.backend.v1",
  "description": "Backend Connection messages (ADR 0109). Open a WebSocket to /api/v1/org/backend/ws with the tidb-link.backend.v1 subprotocol and an Organization API Key in the Authorization header. Each text frame carries one JSON-RPC 2.0 message; binary frames and batches are not used. The core sends hello first, and the Application Server sends nothing before it. The Application Server sends the requests session.attach, session.detach, session.send, connector.serve, connector.withdraw, tool.claim, tool.result and ping (SessionAttachRequest, SessionDetachRequest, SessionSendRequest, ConnectorServeRequest, ConnectorWithdrawRequest, ToolClaimRequest, ToolResultRequest, PingRequest); the core sends the notifications and responses in CoreMessage, including session.event and tool.call.",
  "x-subprotocol": "tidb-link.backend.v1",
  "x-path": "/api/v1/org/backend/ws",
  "x-close-codes": {
    "1001": "Heartbeat timeout: consecutive core WebSocket pings went unanswered.",
    "1002": "Protocol error detected by the WebSocket layer.",
    "1007": "A text frame was not valid UTF-8.",
    "1009": "A frame exceeded maxFrameBytes.",
    "1011": "Internal error, or the Application Server stopped reading its frames: more than 8 MiB of frames waited for it beyond the events already on the socket. A peer that stops reading may observe 1006 instead. Reconnect and re-attach with the cursor of the last session.event processed.",
    "1012": "Closed after connection.drain; open a new connection.",
    "1013": "This replica lost the connection's lease or could not confirm its Organization API Key; reconnect.",
    "4001": "The Organization API Key is missing, invalid, revoked or rotated, or its Organization is inactive.",
    "4009": "Protocol error: a binary frame, or a frame sent before hello."
  },
  "x-error-codes": {
    "-32700": "Parse error: the frame is not JSON. The response id is null.",
    "-32600": "Invalid Request: not a single JSON-RPC 2.0 request, notification or response object, or a string id longer than 256 characters (answered with id null).",
    "-32601": "Method not found.",
    "-32602": "Invalid params: the params do not match the method's definition, including unknown members, invalid Client Tool declarations and Connector or function names that do not equal their own slug.",
    "-32603": "Internal error: the core could not complete the request, the connection is closing, or this connection has more than twice maxSessions requests unanswered; retry, on a new connection if this one closes.",
    "-32000": "Refused: error.data.reason is connection_full (this connection already holds or is acquiring maxSessions attachments; open another connection), connection_draining (the core sent connection.drain; attach, send or serve on a new connection), session_not_found (no active Session of that ordinary End User in the Key's Organization), session_read_only (the Session cannot be continued, so its tools cannot be replaced) or authentication_required (the Key or its Organization is no longer valid; whichever request answers it, including session.attach, session.send, connector.serve and tool.result, the connection closes with 4001, which ends its declarations and attachments). tool.result answers it too; other tool.claim and tool.result refusals are results, not errors. session.send also answers session_not_attached (this connection holds no attachment of the Session) and every refusal of the REST message command, with the ErrorEnvelope's code as reason, its message as error.message and its details as error.data.details. A message that would start a Turn whose MCP Authorization is not usable is refused with mcp_authorization_required, and nothing is saved."
  },
  "definitions": {
    "CoreMessage": {
      "description": "A message the core sends to the Application Server.",
      "oneOf": [
        {
          "$ref": "#/definitions/Hello"
        },
        {
          "$ref": "#/definitions/ConnectionDrain"
        },
        {
          "$ref": "#/definitions/SessionEvent"
        },
        {
          "$ref": "#/definitions/ToolCall"
        },
        {
          "$ref": "#/definitions/SessionDetached"
        },
        {
          "$ref": "#/definitions/SessionAttachResponse"
        },
        {
          "$ref": "#/definitions/SessionDetachResponse"
        },
        {
          "$ref": "#/definitions/ConnectorServeResponse"
        },
        {
          "$ref": "#/definitions/ToolClaimResponse"
        },
        {
          "$ref": "#/definitions/ToolResultResponse"
        },
        {
          "$ref": "#/definitions/SessionSendResponse"
        },
        {
          "$ref": "#/definitions/PingResponse"
        },
        {
          "$ref": "#/definitions/ErrorResponse"
        }
      ]
    },
    "ApplicationServerMessage": {
      "description": "A message the Application Server sends to the core: a request (with id), a notification (without id), or a response to a core request.",
      "oneOf": [
        {
          "type": "object",
          "required": ["jsonrpc", "id", "method"],
          "properties": {
            "jsonrpc": {
              "const": "2.0"
            },
            "id": {
              "type": ["string", "number", "null"],
              "maxLength": 256
            },
            "method": {
              "type": "string"
            },
            "params": {
              "type": ["object", "array"]
            }
          }
        },
        {
          "type": "object",
          "required": ["jsonrpc", "method"],
          "not": {
            "required": ["id"]
          },
          "properties": {
            "jsonrpc": {
              "const": "2.0"
            },
            "method": {
              "type": "string"
            },
            "params": {
              "type": ["object", "array"]
            }
          }
        },
        {
          "description": "A response to a core request; the core sends none yet, so it ignores responses.",
          "type": "object",
          "required": ["jsonrpc", "id"],
          "not": {
            "required": ["method"]
          },
          "properties": {
            "jsonrpc": {
              "const": "2.0"
            },
            "id": {
              "type": ["string", "number", "null"],
              "maxLength": 256
            },
            "result": {},
            "error": {
              "type": "object"
            }
          },
          "oneOf": [
            {
              "required": ["result"]
            },
            {
              "required": ["error"]
            }
          ]
        }
      ]
    },
    "SessionAttachRequest": {
      "description": "Take or replace this connection's Session Attachment of one Session. The Session must be active, belong to the Key's Organization and be owned by the named ordinary End User; each attach is audited. An executing attachment (the default) is the Session's only one: it supersedes the executing attachment on any other connection, which receives session.detached with reason superseded, and advances the Session's generation. An observing attachment receives events only; it never supersedes, never advances generation and does not make the Session attached for Client Tool calls. A connection holds at most one attachment per Session: attaching again replaces its kind. Re-attaching executing on the connection that already holds the executing attachment keeps its generation; switching executing to observing leaves the Session without an executing attachment. Each Session this connection holds or is attaching counts toward maxSessions. The connection then receives session.event for every event of the Session after the attach's position, or after cursor when given; a re-attach of a Session this connection already holds, without cursor, instead continues the delivery it had, so events at or before the reply's cursor may still follow it. The reply precedes the events of the attachment it answers. An attachment lapses when its connection's lease ends, within 10 seconds of the core losing the connection, unless the core already ended it. Authority is checked inside the attach, before it writes; an attach can still commit just after a revocation or Organization deactivation, and it then ends with its connection within about a second.",
      "type": "object",
      "required": ["jsonrpc", "id", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"],
          "maxLength": 256
        },
        "method": {
          "const": "session.attach"
        },
        "params": {
          "type": "object",
          "required": ["userId", "sessionId"],
          "additionalProperties": false,
          "properties": {
            "userId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 255,
              "description": "The Session's ordinary End User owner."
            },
            "sessionId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 255
            },
            "kind": {
              "type": "string",
              "enum": ["executing", "observing"],
              "default": "executing"
            },
            "tools": {
              "description": "Executing attachments only: replaces the Session's whole Client Tool set, like PUT /api/v1/org/users/{userId}/sessions/{id}/tools without If-Match; the last writer wins and an identical set keeps its revision. Omit to keep the current set.",
              "$ref": "#/definitions/ClientToolDeclarations"
            },
            "cursor": {
              "description": "Resume point: the cursor of the last session.event processed for this Session. The core first replays every event after it, then continues live, with nothing missing or repeated at the handoff. Without it, delivery starts after the Session's latest event at the attach (the reply's cursor), and history is not replayed; re-attaching a Session this connection already holds, for example to replace tools, continues its delivery without gap or repeat. A cursor ahead of the Session's latest event answers -32602. Events recorded before positions existed are never replayed.",
              "type": "string",
              "pattern": "^(0|[1-9][0-9]{0,14})$"
            }
          },
          "if": {
            "required": ["kind"],
            "properties": {
              "kind": {
                "const": "observing"
              }
            }
          },
          "then": {
            "not": {
              "required": ["tools"]
            }
          }
        }
      }
    },
    "SessionDetachRequest": {
      "description": "Release this connection's attachment of the Session, if any. Answers an empty result even when the connection holds none.",
      "type": "object",
      "required": ["jsonrpc", "id", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"],
          "maxLength": 256
        },
        "method": {
          "const": "session.detach"
        },
        "params": {
          "type": "object",
          "required": ["sessionId"],
          "additionalProperties": false,
          "properties": {
            "sessionId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 255
            }
          }
        }
      }
    },
    "ToolClaimRequest": {
      "description": "Ask to execute one tool.call. A Client Tool call is granted only to the connection holding the Session's current executing attachment, while the call awaits its outcome and its claim window is open; the claim binds the call to that attachment's generation, and repeating it from the same connection and generation answers claimed: true again. A Connector call is granted only to the connection it was sent to, while that connection is open and the claim window open; the claim binds the call to that connection, and repeating it there answers claimed: true again. Execute only after claimed: true. If the answer is lost, do not execute; submit an error result instead, since the claim may have been granted.",
      "type": "object",
      "required": ["jsonrpc", "id", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"],
          "maxLength": 256
        },
        "method": {
          "const": "tool.claim"
        },
        "params": {
          "type": "object",
          "required": ["callId"],
          "additionalProperties": false,
          "properties": {
            "callId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 64
            }
          }
        }
      }
    },
    "ToolResultRequest": {
      "description": "Submit a claimed call's result: output, or error when execution failed. Any Backend Connection of the call's Organization may submit it, so a result survives a connection change. The first valid result settles the call; submitting the same result again answers accepted: true, a different one is refused. An accepted Client Tool result appears in the Session's interaction list under the input ID tool.result#<callId>, which no REST clientEventId can take or look up; a Connector call's result is recorded with the call only. For a Connector call, output or the error message is what the model receives; an error tells it the function reported an error. A Key or Organization that is no longer valid is refused with error -32000, reason authentication_required, and the connection closes with 4001; submit the result again through a connection of a valid Key. Lengths count UTF-16 code units; JSON Schema maxLength counts code points.",
      "type": "object",
      "required": ["jsonrpc", "id", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"],
          "maxLength": 256
        },
        "method": {
          "const": "tool.result"
        },
        "params": {
          "oneOf": [
            {
              "type": "object",
              "required": ["callId", "output"],
              "additionalProperties": false,
              "properties": {
                "callId": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 64
                },
                "output": {
                  "description": "The result text the model receives; serialize structured results yourself.",
                  "type": "string",
                  "maxLength": 65536
                }
              }
            },
            {
              "type": "object",
              "required": ["callId", "error"],
              "additionalProperties": false,
              "properties": {
                "callId": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 64
                },
                "error": {
                  "type": "object",
                  "required": ["message"],
                  "additionalProperties": false,
                  "properties": {
                    "message": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 4096
                    }
                  }
                }
              }
            }
          ]
        }
      }
    },
    "SessionSendRequest": {
      "description": "Submit a user message to a Session this connection holds an attachment of, of either kind. It is the REST message command POST /api/v1/org/users/{userId}/sessions/{sessionId}/events with type agent.session.input.message, submitted by this connection's Organization API Key for the End User named in that attachment's session.attach: the same validation, Key, End User and Session authority and audit, clientEventId idempotency in the same namespace as REST commands, admission refusals and Client Tool freezing. A message to an idle Session starts a Turn, which runs on the core replica that received the request; a message to a Session with an active Turn steers that Turn. A Turn's terminal events arrive before the Session releases it, so a message sent between them steers or is refused; send the next message after the agent.session.idle event that follows the Turn. The reply is the acceptance receipt, not the Turn result: follow the Turn through session.event, matching its turnId; events of the Turn can arrive before the reply. Repeating the same clientEventId and data returns the original receipt with replayed true and starts nothing; the same clientEventId with different data is refused with interaction_input_conflict. A frame over 1 MiB, the REST request body limit, answers -32602. A request after connection.drain is refused with connection_draining; send it again with the same clientEventId on a new connection.",
      "type": "object",
      "required": ["jsonrpc", "id", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"],
          "maxLength": 256
        },
        "method": {
          "const": "session.send"
        },
        "params": {
          "type": "object",
          "required": ["sessionId", "clientEventId", "data"],
          "additionalProperties": false,
          "properties": {
            "sessionId": {
              "type": "string",
              "minLength": 1,
              "maxLength": 255
            },
            "clientEventId": {
              "description": "Stable identity of this message in the Session's command namespace, shared with REST commands.",
              "type": "string",
              "pattern": "^[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}$"
            },
            "data": {
              "description": "The data of the REST message command: content (a string or text parts) or text, and optionally userFileIds, clarificationSourceTurnId and billingTag.",
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "content": {
                  "oneOf": [
                    {
                      "type": "string",
                      "minLength": 1
                    },
                    {
                      "type": "array",
                      "minItems": 1,
                      "items": {
                        "type": "object",
                        "required": ["type", "text"],
                        "additionalProperties": false,
                        "properties": {
                          "type": {
                            "const": "text"
                          },
                          "text": {
                            "type": "string",
                            "minLength": 1
                          }
                        }
                      }
                    }
                  ]
                },
                "text": {
                  "description": "Ordinary Turns require readable string text. A linked single-select clarification accepts a string or one-element string array; a linked multi-select clarification requires a non-empty string array.",
                  "oneOf": [
                    {
                      "type": "string",
                      "minLength": 1
                    },
                    {
                      "type": "array",
                      "minItems": 1,
                      "maxItems": 9,
                      "items": {
                        "type": "string",
                        "minLength": 1
                      }
                    }
                  ]
                },
                "userFileIds": {
                  "type": "array",
                  "maxItems": 20,
                  "items": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 64
                  }
                },
                "clarificationSourceTurnId": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 64
                },
                "billingTag": {
                  "description": "A Billing Tag snapshot for a Turn this message starts; null means untagged. The value is normalized to NFC and limited to 1-128 UTF-8 bytes, without leading or trailing whitespace or control or format characters.",
                  "oneOf": [
                    {
                      "type": "null"
                    },
                    {
                      "type": "object",
                      "required": ["key", "value"],
                      "additionalProperties": false,
                      "properties": {
                        "key": {
                          "type": "string",
                          "pattern": "^[a-z][a-z0-9_.-]{0,63}$"
                        },
                        "value": {
                          "type": "string"
                        }
                      }
                    }
                  ]
                }
              },
              "anyOf": [
                {
                  "required": ["content"]
                },
                {
                  "required": ["text"]
                }
              ]
            }
          }
        }
      }
    },
    "ConnectorServeRequest": {
      "description": "Declare the functions of the Organization's Connector that this connection serves (ADR 0109). The core finds the Organization's Connector with this name, or creates it, and answers its connectorId: an Organization has one Connector name space, so the same name is the same Connector. Serving the same name again on this connection replaces this connection's declaration. A connection serves the Connector while it holds a declaration and stays open; connector.withdraw or the end of the connection ends its declaration. The Connector also stores the latest declaration served. A Run whose Template references the Connector freezes the declaration most recently served among the connections serving it, or the stored one when none serves it, and the model sees each function as mcp_<name>__<function name>, with the platform-reserved async choice. Creating the Connector or changing its stored declaration is audited with this connection's Key; serving an unchanged declaration is not. A call of a served function arrives as tool.call with connector on one connection serving the Connector whose declaration of the function matches the frozen one; with none, the call is not performed and the Turn continues. A Connector with this name left from the retired HTTP callback or tunnel transports, which listing and reading Connectors do not return, is taken over in place: it keeps its connectorId, so Templates that reference it need no change, and is listed from then on. Refused with connection_draining after connection.drain, and with authentication_required when the Key or its Organization is no longer valid; the connection then closes with 4001, which ends its declarations and attachments.",
      "type": "object",
      "required": ["jsonrpc", "id", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"],
          "maxLength": 256
        },
        "method": {
          "const": "connector.serve"
        },
        "params": {
          "type": "object",
          "required": ["name", "tools"],
          "additionalProperties": false,
          "properties": {
            "name": {
              "description": "The Connector name, equal to its own slug: lowercase letters and digits joined by single underscores.",
              "type": "string",
              "minLength": 1,
              "maxLength": 29,
              "pattern": "^[a-z0-9]+(_[a-z0-9]+)*$"
            },
            "tools": {
              "description": "The functions this connection serves, replacing its earlier declaration of the Connector.",
              "$ref": "#/definitions/ConnectorFunctionDeclarations"
            }
          }
        }
      }
    },
    "ConnectorWithdrawRequest": {
      "description": "End this connection's declaration of the Connector. Answers an empty result, like SessionDetachResponse, even when the connection holds none. The Connector and its stored latest declaration stay.",
      "type": "object",
      "required": ["jsonrpc", "id", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"],
          "maxLength": 256
        },
        "method": {
          "const": "connector.withdraw"
        },
        "params": {
          "type": "object",
          "required": ["name"],
          "additionalProperties": false,
          "properties": {
            "name": {
              "description": "The Connector name.",
              "type": "string",
              "minLength": 1,
              "maxLength": 29,
              "pattern": "^[a-z0-9]+(_[a-z0-9]+)*$"
            }
          }
        }
      }
    },
    "PingRequest": {
      "description": "Liveness probe for an Application Server whose WebSocket cannot send or observe ping frames. The core answers at once without database work, never behind other requests, and the request never counts toward the unanswered-request limit. The core's ping frames still need their pongs, which WebSocket clients send automatically.",
      "type": "object",
      "required": ["jsonrpc", "id", "method"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"],
          "maxLength": 256
        },
        "method": {
          "const": "ping"
        },
        "params": {
          "type": "object",
          "additionalProperties": false,
          "maxProperties": 0
        }
      }
    },
    "ClientToolDeclarations": {
      "type": "array",
      "maxItems": 64,
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": ["function"]
          },
          "name": {
            "description": "Client function name; platform tool names, the mcp_ prefix and double underscores are reserved.",
            "type": "string",
            "pattern": "^[a-zA-Z0-9_-]{1,64}$"
          },
          "description": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4096
          },
          "parameters": {
            "type": "object",
            "additionalProperties": {}
          }
        },
        "required": ["type", "name", "description", "parameters"],
        "additionalProperties": false
      }
    },
    "ConnectorFunctionDeclarations": {
      "type": "array",
      "maxItems": 64,
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": ["function"]
          },
          "name": {
            "description": "Function name, equal to its own slug: lowercase letters and digits joined by single underscores. tool.call carries it as name.",
            "type": "string",
            "minLength": 1,
            "maxLength": 29,
            "pattern": "^[a-z0-9]+(_[a-z0-9]+)*$"
          },
          "description": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4096
          },
          "parameters": {
            "type": "object",
            "additionalProperties": {},
            "description": "A JSON Schema with type \"object\" that neither defines nor requires the platform-reserved async property."
          }
        },
        "required": ["type", "name", "description", "parameters"],
        "additionalProperties": false
      },
      "description": "Connector functions, shaped like Client Tool declarations: names are unique within the declaration."
    },
    "Hello": {
      "description": "First message on every connection. Parameters may change without a new subprotocol version.",
      "type": "object",
      "required": ["jsonrpc", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "method": {
          "const": "hello"
        },
        "params": {
          "type": "object",
          "required": [
            "connectionId",
            "maxSessions",
            "claimWindowMs",
            "heartbeatMs",
            "maxFrameBytes"
          ],
          "additionalProperties": false,
          "properties": {
            "connectionId": {
              "description": "Identity of this Backend Connection.",
              "type": "string",
              "pattern": "^bconn_[a-f0-9]{32}$"
            },
            "maxSessions": {
              "description": "Most Session Attachments this connection may hold.",
              "type": "integer",
              "minimum": 1
            },
            "claimWindowMs": {
              "description": "How long a published Client Tool call waits for its claim.",
              "type": "integer",
              "minimum": 1
            },
            "heartbeatMs": {
              "description": "Interval of the core's WebSocket ping frames. A client that cannot observe them can send the ping request when nothing has arrived for this long.",
              "type": "integer",
              "minimum": 1
            },
            "maxFrameBytes": {
              "description": "Largest frame the core accepts; a larger frame closes the connection with 1009. Frames the core sends, such as session.event, are not bounded by it.",
              "type": "integer",
              "minimum": 1
            }
          }
        }
      }
    },
    "ConnectionDrain": {
      "description": "The core will close this connection after closeAfterMs. Open a new connection, move the work there, then close this one.",
      "type": "object",
      "required": ["jsonrpc", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "method": {
          "const": "connection.drain"
        },
        "params": {
          "type": "object",
          "required": ["closeAfterMs"],
          "additionalProperties": false,
          "properties": {
            "closeAfterMs": {
              "type": "integer",
              "minimum": 0
            }
          }
        }
      }
    },
    "SessionDetached": {
      "description": "The core ended this connection's attachment of the Session: superseded by an executing attach on another connection, or the Session was deleted. Attach again to resume.",
      "type": "object",
      "required": ["jsonrpc", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "method": {
          "const": "session.detached"
        },
        "params": {
          "type": "object",
          "required": ["sessionId", "reason"],
          "additionalProperties": false,
          "properties": {
            "sessionId": {
              "type": "string"
            },
            "reason": {
              "type": "string",
              "enum": ["superseded", "session_deleted"]
            }
          }
        }
      }
    },
    "SessionEvent": {
      "description": "One event of an attached Session, for executing and observing attachments alike. Events of one Session arrive strictly in order, without gaps or repeats; events of different Sessions are unordered. While the Application Server reads too slowly the core may drop an assistant output snapshot (agent.session.turn.output_text.snapshot) that a later snapshot of the same Turn directly follows; it drops nothing else, and closes the connection with 1011 instead. A requires_action event authorizes nothing on a Backend Connection; execute Client Tool calls only from tool.call after a granted tool.claim.",
      "type": "object",
      "required": ["jsonrpc", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "method": {
          "const": "session.event"
        },
        "params": {
          "type": "object",
          "required": ["sessionId", "cursor", "event"],
          "additionalProperties": false,
          "properties": {
            "sessionId": {
              "type": "string"
            },
            "cursor": {
              "description": "Opaque position of this event; pass the last one processed as session.attach cursor to resume.",
              "type": "string"
            },
            "event": {
              "description": "The Session event envelope: type (agent.session.turn.*, agent.session.realtime.*, agent.session.template_switch.*, agent.session.idle), sessionId, turnId, createdAt and data. agent.session.idle follows the terminal events of a Turn once the Session has released it; wait for it before session.send, an audio Turn or a Template switch. A failed Turn sends agent.session.turn.failed twice: data.event turn_error with the Turn's failure in data.payload (code, message and, for some codes, details; the same payload a Turn read returns as failure), then data.event turn_finished. With code mcp_authorization_required, an MCP Authorization stopped being usable during the Turn (revoked, or refused by the server with HTTP 401 or 403) and details.registrations lists {serverId, owner, reason} as in the session.send refusal; the Turn is not resumed, so send a new message once the authorization is usable again. A File Notification arrives as agent.session.turn.item.added with data.event operation_step: its succeeded step carries data.payload.file {path, name, version, byteSize, updatedAt}, one file the Agent announced in the Session's Drive; read it pinned to version through the Session file content route or a download ticket. It asks for no reply. A File Notification the Agent could not make arrives as the failed operation_step of that Operation, whose data.payload.fileRefusal {reason, path?} says why (reason not_found, directory, local_only, uploads, reserved, skills, invalid_path, path_too_long, invalid_arguments, limit_reached, drain_failed or unavailable; local_only means a folder such as build/ that the sandbox never saves) and which path the Agent tried; the Operation in Turn reads carries the same fileRefusal.",
              "type": "object",
              "required": ["type", "sessionId", "turnId", "createdAt", "data"],
              "properties": {
                "type": {
                  "type": "string"
                },
                "sessionId": {
                  "type": "string"
                },
                "turnId": {
                  "type": ["string", "null"]
                },
                "createdAt": {
                  "type": "string"
                },
                "data": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    },
    "ToolCall": {
      "description": "One published call to execute: a Client Tool call published to the Session's executing attachment, or a Connector call published to the Serving Connection chosen for it (ADR 0109). Execute it only after tool.claim answers claimed: true; the requires_action Session event that records Client Tool calls authorizes nothing, and Connector calls have no Session event. The core sends a Client Tool call to the attachment that was executing when the call was published, and to a later executing attachment of the Session (after a reconnect or a move to another connection) while the call is still unclaimed and its claim window open; a call whose claim window has just passed, or that an earlier attachment generation claimed, may still arrive, and its claim is refused; it sends each call at most once per attachment, so deduplicate by callId. An attachment superseded moments earlier may still receive a call; its claim is refused. A call published while the Session had no executing attachment is not delivered, at once, and is never sent here. A Connector call carries connector and goes to exactly one connection serving the Connector whose declaration of the function matches the one the Run froze, preferring the connection holding the Session's executing attachment, then one on the replica running the Turn; it needs no attachment and is sent once, never offered to another connection. With no such connection it is not delivered, at once. If its Turn ends first, an unclaimed call is reported not performed and a claimed one without a result unknown.",
      "type": "object",
      "required": ["jsonrpc", "method", "params"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "method": {
          "const": "tool.call"
        },
        "params": {
          "type": "object",
          "required": [
            "callId",
            "sessionId",
            "userId",
            "turnId",
            "name",
            "arguments",
            "claimBy",
            "deadlineAt"
          ],
          "additionalProperties": false,
          "properties": {
            "callId": {
              "type": "string"
            },
            "sessionId": {
              "type": "string"
            },
            "userId": {
              "description": "The Session's End User owner, also on a Connector call.",
              "type": "string"
            },
            "turnId": {
              "type": "string"
            },
            "name": {
              "description": "A Client Tool the Session declared, or the declared name of a Connector function.",
              "type": "string"
            },
            "connector": {
              "description": "Present on a Connector call only: the Connector whose function to run, as this connection served it with connector.serve.",
              "type": "object",
              "required": ["id", "name"],
              "additionalProperties": false,
              "properties": {
                "id": {
                  "description": "The connectorId connector.serve answered.",
                  "type": "string"
                },
                "name": {
                  "description": "The Connector name.",
                  "type": "string"
                }
              }
            },
            "arguments": {
              "description": "The model's arguments, without the reserved async choice.",
              "type": "object"
            },
            "claimBy": {
              "description": "Claim before this time (claimWindowMs after publication). Unclaimed by then, the call was not performed and the model is told so.",
              "type": "string"
            },
            "deadlineAt": {
              "description": "Submit the result before this time, 60 seconds after publication. Claimed without a result by then, the model is told its effect is unknown.",
              "type": "string"
            }
          }
        }
      }
    },
    "SessionAttachResponse": {
      "description": "Result of session.attach.",
      "type": "object",
      "required": ["jsonrpc", "id", "result"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"]
        },
        "result": {
          "type": "object",
          "required": ["generation", "cursor"],
          "additionalProperties": false,
          "properties": {
            "generation": {
              "description": "The Session's attachment generation. An executing attachment owns it; an observing attachment reports it unchanged.",
              "type": "integer",
              "minimum": 0
            },
            "cursor": {
              "description": "Opaque position of the Session's latest event when the attachment took effect. Resume from the cursor of the last session.event processed, not from this one, which a replay may not have reached yet.",
              "type": "string"
            }
          }
        }
      }
    },
    "SessionDetachResponse": {
      "description": "Result of session.detach and connector.withdraw.",
      "type": "object",
      "required": ["jsonrpc", "id", "result"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"]
        },
        "result": {
          "type": "object",
          "additionalProperties": false,
          "maxProperties": 0
        }
      }
    },
    "ConnectorServeResponse": {
      "description": "Result of connector.serve.",
      "type": "object",
      "required": ["jsonrpc", "id", "result"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"]
        },
        "result": {
          "type": "object",
          "required": ["connectorId"],
          "additionalProperties": false,
          "properties": {
            "connectorId": {
              "description": "The Organization's Connector with the served name; Templates reference it as tools.mcp[].serverId.",
              "type": "string"
            }
          }
        }
      }
    },
    "ToolClaimResponse": {
      "description": "Result of tool.claim.",
      "type": "object",
      "required": ["jsonrpc", "id", "result"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"]
        },
        "result": {
          "oneOf": [
            {
              "type": "object",
              "required": ["claimed"],
              "additionalProperties": false,
              "properties": {
                "claimed": {
                  "const": true
                }
              }
            },
            {
              "type": "object",
              "required": ["claimed", "reason"],
              "additionalProperties": false,
              "properties": {
                "claimed": {
                  "const": false
                },
                "reason": {
                  "description": "call_not_found: no such call in the Key's Organization, one published without an executing attachment before the per-Session subscription was removed, or a Connector call sent to another connection. superseded: this connection does not hold the Session's current executing attachment, another attachment generation claimed the call, or this connection's lease has ended. claim_window_closed: claimBy passed without a claim; the call was not performed. call_settled: the core already decided the call's outcome, including a call published while its Session had no executing attachment, or with no connection serving the function, which is not delivered at once.",
                  "type": "string",
                  "enum": ["call_not_found", "superseded", "claim_window_closed", "call_settled"]
                }
              }
            }
          ]
        }
      }
    },
    "ToolResultResponse": {
      "description": "Result of tool.result.",
      "type": "object",
      "required": ["jsonrpc", "id", "result"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"]
        },
        "result": {
          "oneOf": [
            {
              "type": "object",
              "required": ["accepted"],
              "additionalProperties": false,
              "properties": {
                "accepted": {
                  "const": true
                }
              }
            },
            {
              "type": "object",
              "required": ["accepted", "reason"],
              "additionalProperties": false,
              "properties": {
                "accepted": {
                  "const": false
                },
                "reason": {
                  "description": "call_not_found: no such call in the Key's Organization or visible to the Key, or one published without an executing attachment before the per-Session subscription was removed. not_claimed: no connection claimed the call. call_settled: the call already has an outcome, from an earlier different result, its deadline, the end of its Turn or its publication while nobody could claim it.",
                  "type": "string",
                  "enum": ["call_not_found", "not_claimed", "call_settled"]
                }
              }
            }
          ]
        }
      }
    },
    "SessionSendResponse": {
      "description": "Result of session.send: the acceptance receipt.",
      "type": "object",
      "required": ["jsonrpc", "id", "result"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"]
        },
        "result": {
          "type": "object",
          "required": ["interaction", "replayed"],
          "additionalProperties": false,
          "properties": {
            "interaction": {
              "description": "The REST interaction receipt, as in SessionInteractionResponse of the customer OpenAPI.",
              "type": "object",
              "required": [
                "inputId",
                "sequence",
                "kind",
                "mode",
                "turnId",
                "message",
                "createdAt",
                "appliedAt",
                "rejection",
                "result"
              ],
              "properties": {
                "inputId": {
                  "type": "string"
                },
                "sequence": {
                  "type": "integer",
                  "minimum": 1
                },
                "kind": {
                  "type": "string",
                  "enum": ["message", "cancel", "retry", "client_tool_output"]
                },
                "mode": {
                  "description": "started: the message started the Turn turnId. steered: it joined the active Turn turnId. rejected: see rejection.",
                  "type": "string",
                  "enum": ["started", "steered", "cancelled", "accepted", "rejected"]
                },
                "turnId": {
                  "type": "string"
                },
                "message": {
                  "oneOf": [
                    {
                      "type": "null"
                    },
                    {
                      "type": "object",
                      "required": ["id", "role", "content", "createdAt"],
                      "properties": {
                        "id": {
                          "type": "string"
                        },
                        "role": {
                          "const": "user"
                        },
                        "content": {
                          "type": "string"
                        },
                        "createdAt": {
                          "type": "string"
                        }
                      }
                    }
                  ]
                },
                "createdAt": {
                  "type": "string"
                },
                "appliedAt": {
                  "type": ["string", "null"]
                },
                "rejection": {
                  "oneOf": [
                    {
                      "type": "null"
                    },
                    {
                      "type": "object",
                      "required": ["code"],
                      "properties": {
                        "code": {
                          "type": "string",
                          "enum": [
                            "interaction_admission_closed",
                            "interaction_target_conflict",
                            "client_tool_call_unavailable",
                            "client_tool_call_settled"
                          ]
                        }
                      }
                    }
                  ]
                },
                "result": {
                  "description": "The original execution generation's settlement.",
                  "type": "object",
                  "required": ["status", "completedAt"],
                  "properties": {
                    "status": {
                      "type": "string",
                      "enum": ["running", "succeeded", "failed", "interrupted"]
                    },
                    "completedAt": {
                      "type": ["string", "null"]
                    }
                  }
                }
              }
            },
            "replayed": {
              "description": "True when this repeats an already accepted clientEventId (REST answers 200); false for a new admission (REST 202).",
              "type": "boolean"
            }
          }
        }
      }
    },
    "PingResponse": {
      "description": "Result of ping.",
      "type": "object",
      "required": ["jsonrpc", "id", "result"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"]
        },
        "result": {
          "type": "object",
          "required": ["time"],
          "additionalProperties": false,
          "properties": {
            "time": {
              "description": "The answering replica's clock, in ISO 8601 UTC. Informational: claim windows and deadlines are judged by the database clock.",
              "type": "string"
            }
          }
        }
      }
    },
    "ErrorResponse": {
      "description": "The core's answer to a request it cannot serve.",
      "type": "object",
      "required": ["jsonrpc", "id", "error"],
      "additionalProperties": false,
      "properties": {
        "jsonrpc": {
          "const": "2.0"
        },
        "id": {
          "type": ["string", "number", "null"]
        },
        "error": {
          "type": "object",
          "required": ["code", "message"],
          "additionalProperties": false,
          "properties": {
            "code": {
              "type": "integer",
              "enum": [-32700, -32600, -32601, -32602, -32603, -32000]
            },
            "message": {
              "type": "string"
            },
            "data": {
              "description": "Present with code -32000.",
              "type": "object",
              "required": ["reason"],
              "additionalProperties": false,
              "properties": {
                "reason": {
                  "description": "Why the core refused: one of the reasons in x-error-codes -32000; for session.send, also the REST ErrorEnvelope code.",
                  "type": "string"
                },
                "details": {
                  "description": "session.send only: the REST ErrorEnvelope details, when present. With reason mcp_authorization_required, details.registrations lists each MCP Server Registration whose MCP Authorization is not usable, as {serverId, owner: organization or end_user, reason: missing, invalid, revoked, insufficient_scope or catalog_unavailable}."
                }
              }
            }
          }
        }
      }
    }
  }
}
