package mcp import "encoding/json" // request is one JSON-RPC 2.0 call. The MCP endpoint is stateless, so there is // nothing else to track between requests. type request struct { JSONRPC string `json:"jsonrpc"` ID json.RawMessage `json:"id"` Method string `json:"method"` Params json.RawMessage `json:"params"` } // isNotification reports whether the caller omitted "id". Such requests get // no JSON-RPC response at all (MCP uses them for notifications/initialized). func (r *request) isNotification() bool { return len(r.ID) == 0 } // response is a single JSON-RPC 2.0 reply. Exactly one of Result/Error is set. type response struct { JSONRPC string `json:"jsonrpc"` ID json.RawMessage `json:"id"` Result any `json:"result,omitempty"` Error *rpcError `json:"error,omitempty"` } // idOrNull keeps the reply valid when the request id is missing or malformed: // JSON-RPC allows "id": null on errors that cannot be correlated. func idOrNull(id json.RawMessage) json.RawMessage { if len(id) == 0 { return json.RawMessage("null") } return id } func resultResponse(id json.RawMessage, result any) *response { return &response{JSONRPC: "2.0", ID: idOrNull(id), Result: result} } func errorResponse(id json.RawMessage, err *rpcError) *response { return &response{JSONRPC: "2.0", ID: idOrNull(id), Error: err} } // toolCallParams is the tools/call payload. Arguments stay raw so each tool // decodes only the fields it understands (and reports -32602 itself). type toolCallParams struct { Name string `json:"name"` Arguments json.RawMessage `json:"arguments"` }