A ClientSession is a logical connection with an MCP server. Its methods can be used to send requests or notifications to the server. Create a session by calling [Client.Connect]. Call [ClientSession.Close] to close the connection, or await server termination with [ClientSession.Wait].
| 300 | // Call [ClientSession.Close] to close the connection, or await server |
| 301 | // termination with [ClientSession.Wait]. |
| 302 | type ClientSession struct { |
| 303 | // Ensure that onClose is called at most once. |
| 304 | // We defensively use an atomic CompareAndSwap rather than a sync.Once, in case the |
| 305 | // onClose callback triggers a re-entrant call to Close. |
| 306 | calledOnClose atomic.Bool |
| 307 | onClose func() |
| 308 | |
| 309 | conn *jsonrpc2.Connection |
| 310 | client *Client |
| 311 | keepaliveCancel context.CancelFunc |
| 312 | mcpConn Connection |
| 313 | |
| 314 | // No mutex is (currently) required to guard the session state, because it is |
| 315 | // only set synchronously during Client.Connect. |
| 316 | state clientSessionState |
| 317 | |
| 318 | // Pending URL elicitations waiting for completion notifications. |
| 319 | pendingElicitationsMu sync.Mutex |
| 320 | pendingElicitations map[string]chan struct{} |
| 321 | } |
| 322 | |
| 323 | type clientSessionState struct { |
| 324 | InitializeResult *InitializeResult |
nothing calls this directly
no outgoing calls
no test coverage detected