MCPcopy Index your code
hub / github.com/modelcontextprotocol/registry / GetServerByName

Method GetServerByName

internal/database/postgres.go:276–340  ·  view source on GitHub ↗

GetServerByName retrieves the latest version of a server by server name

(ctx context.Context, tx pgx.Tx, serverName string, includeDeleted bool)

Source from the content-addressed store, hash-verified

274
275// GetServerByName retrieves the latest version of a server by server name
276func (db *PostgreSQL) GetServerByName(ctx context.Context, tx pgx.Tx, serverName string, includeDeleted bool) (*apiv0.ServerResponse, error) {
277 if ctx.Err() != nil {
278 return nil, ctx.Err()
279 }
280
281 // Build filter conditions
282 isLatest := true
283 filter := &ServerFilter{
284 Name: &serverName,
285 IsLatest: &isLatest,
286 IncludeDeleted: &includeDeleted,
287 }
288
289 argIndex := 1
290 whereConditions, args, _ := buildFilterConditions(filter, argIndex)
291
292 whereClause := ""
293 if len(whereConditions) > 0 {
294 whereClause = "WHERE " + strings.Join(whereConditions, " AND ")
295 }
296
297 query := fmt.Sprintf(`
298 SELECT server_name, version, status, status_changed_at, status_message, published_at, updated_at, is_latest, value
299 FROM servers
300 %s
301 ORDER BY published_at DESC
302 LIMIT 1
303 `, whereClause)
304
305 var name, version, status string
306 var statusChangedAt, publishedAt, updatedAt time.Time
307 var statusMessage *string
308 var valueJSON []byte
309
310 err := db.getExecutor(tx).QueryRow(ctx, query, args...).Scan(&name, &version, &status, &statusChangedAt, &statusMessage, &publishedAt, &updatedAt, &isLatest, &valueJSON)
311 if err != nil {
312 if errors.Is(err, pgx.ErrNoRows) {
313 return nil, ErrNotFound
314 }
315 return nil, fmt.Errorf("failed to get server by name: %w", err)
316 }
317
318 // Parse the ServerJSON from JSONB
319 var serverJSON apiv0.ServerJSON
320 if err := json.Unmarshal(valueJSON, &serverJSON); err != nil {
321 return nil, fmt.Errorf("failed to unmarshal server JSON: %w", err)
322 }
323
324 // Build ServerResponse with separated metadata
325 serverResponse := &apiv0.ServerResponse{
326 Server: serverJSON,
327 Meta: apiv0.ResponseMeta{
328 Official: &apiv0.RegistryExtensions{
329 Status: model.Status(status),
330 StatusChangedAt: statusChangedAt,
331 StatusMessage: statusMessage,
332 PublishedAt: publishedAt,
333 UpdatedAt: updatedAt,

Callers

nothing calls this directly

Calls 4

getExecutorMethod · 0.95
StatusTypeAlias · 0.92
buildFilterConditionsFunction · 0.85
QueryRowMethod · 0.80

Tested by

no test coverage detected