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

Method Run

mcp/server.go:946–973  ·  view source on GitHub ↗

Run runs the server over the given transport, which must be persistent. Run blocks until the client terminates the connection or the provided context is cancelled. If the context is cancelled, Run closes the connection. If tools have been added to the server before this call, then the server will

(ctx context.Context, t Transport)

Source from the content-addressed store, hash-verified

944// It need not be called on servers that are used for multiple concurrent connections,
945// as with [StreamableHTTPHandler].
946func (s *Server) Run(ctx context.Context, t Transport) error {
947 s.opts.Logger.Info("server run start")
948 ss, err := s.Connect(ctx, t, nil)
949 if err != nil {
950 s.opts.Logger.Error("server connect failed", "error", err)
951 return err
952 }
953
954 ssClosed := make(chan error)
955 go func() {
956 ssClosed <- ss.Wait()
957 }()
958
959 select {
960 case <-ctx.Done():
961 ss.Close()
962 <-ssClosed // wait until waiting go routine above actually completes
963 s.opts.Logger.Error("server run cancelled", "error", ctx.Err())
964 return ctx.Err()
965 case err := <-ssClosed:
966 if err != nil {
967 s.opts.Logger.Error("server session ended with error", "error", err)
968 } else {
969 s.opts.Logger.Info("server session ended")
970 }
971 return err
972 }
973}
974
975// bind implements the binder[*ServerSession] interface, so that Servers can
976// be connected using [connect].

Callers 15

mainFunction · 0.95
runServerFunction · 0.95
runCancelContextServerFunction · 0.95
mainFunction · 0.95
mainFunction · 0.95
mainFunction · 0.95
mainFunction · 0.95
mainFunction · 0.95
mainFunction · 0.95
mainFunction · 0.95
TestSplitChallengesFunction · 0.80

Calls 6

ConnectMethod · 0.95
CloseMethod · 0.65
ErrorMethod · 0.45
WaitMethod · 0.45
DoneMethod · 0.45
ErrMethod · 0.45