MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / ClientSession

Struct ClientSession

mcp/client.go:302–321  ·  view source on GitHub ↗

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].

Source from the content-addressed store, hash-verified

300// Call [ClientSession.Close] to close the connection, or await server
301// termination with [ClientSession.Wait].
302type 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
323type clientSessionState struct {
324 InitializeResult *InitializeResult

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected