MCPcopy Index your code
hub / github.com/modelcontextprotocol/go-sdk / Connect

Method Connect

mcp/server.go:1020–1042  ·  view source on GitHub ↗

Connect connects the MCP server over the given transport and starts handling messages. It returns a connection object that may be used to terminate the connection (with [Connection.Close]), or await client termination (with [Connection.Wait]). If opts.State is non-nil, it is the initial state for

(ctx context.Context, t Transport, opts *ServerSessionOptions)

Source from the content-addressed store, hash-verified

1018//
1019// If opts.State is non-nil, it is the initial state for the server.
1020func (s *Server) Connect(ctx context.Context, t Transport, opts *ServerSessionOptions) (*ServerSession, error) {
1021 var state *ServerSessionState
1022 var onClose func()
1023 if opts != nil {
1024 state = opts.State
1025 onClose = opts.onClose
1026 }
1027
1028 s.opts.Logger.Info("server connecting")
1029 ss, err := connect(ctx, t, s, state, onClose, s.opts.Logger)
1030 if err != nil {
1031 s.opts.Logger.Error("server connect error", "error", err)
1032 return nil, err
1033 }
1034
1035 // Start keepalive before returning the session to avoid race conditions with Close.
1036 // This is safe because the spec allows sending pings before initialization (see ServerSession.handle for details).
1037 if s.opts.KeepAlive > 0 {
1038 ss.startKeepalive(ss.server.opts.KeepAlive)
1039 }
1040
1041 return ss, nil
1042}
1043
1044// TODO: (nit) move all ServerSession methods below the ServerSession declaration.
1045func (ss *ServerSession) initialized(ctx context.Context, params *InitializedParams) (Result, error) {

Callers 15

Example_lifecycleFunction · 0.95
Example_progressFunction · 0.95
Example_cancellationFunction · 0.95
RunMethod · 0.95
TestElicitationURLModeFunction · 0.95
TestEndToEndFunction · 0.95
TestMiddlewareFunction · 0.95

Calls 3

connectFunction · 0.70
ErrorMethod · 0.45
startKeepaliveMethod · 0.45

Tested by 15

Example_lifecycleFunction · 0.76
Example_progressFunction · 0.76
Example_cancellationFunction · 0.76
TestElicitationURLModeFunction · 0.76
TestEndToEndFunction · 0.76
TestMiddlewareFunction · 0.76
TestNoJSONNullFunction · 0.76