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)
| 944 | // It need not be called on servers that are used for multiple concurrent connections, |
| 945 | // as with [StreamableHTTPHandler]. |
| 946 | func (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]. |