File size: 4,660 Bytes
07af8f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Request, Response } from 'express';
import { ApiResponse } from '../types/index.js';

const REGISTRY_BASE_URL = 'https://registry.modelcontextprotocol.io/v0.1';

/**
 * Get all MCP servers from the official registry
 * Proxies the request to avoid CORS issues in the frontend
 * Supports cursor-based pagination
 */
export const getAllRegistryServers = async (req: Request, res: Response): Promise<void> => {
  try {
    const { cursor, limit, search } = req.query;

    // Build URL with query parameters
    const url = new URL(`${REGISTRY_BASE_URL}/servers`);
    if (cursor && typeof cursor === 'string') {
      url.searchParams.append('cursor', cursor);
    }
    if (limit && typeof limit === 'string') {
      const limitNum = parseInt(limit, 10);
      if (!isNaN(limitNum) && limitNum > 0) {
        url.searchParams.append('limit', limit);
      }
    }
    if (search && typeof search === 'string') {
      url.searchParams.append('search', search);
    }

    const response = await fetch(url.toString(), {
      headers: {
        Accept: 'application/json, application/problem+json',
      },
    });

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();

    const apiResponse: ApiResponse<typeof data> = {
      success: true,
      data: data,
    };

    res.json(apiResponse);
  } catch (error) {
    console.error('Error fetching registry servers:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to fetch registry servers';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

/**
 * Get all versions of a specific MCP server
 * Proxies the request to avoid CORS issues in the frontend
 */
export const getRegistryServerVersions = async (req: Request, res: Response): Promise<void> => {
  try {
    const { serverName } = req.params;

    if (!serverName) {
      res.status(400).json({
        success: false,
        message: 'Server name is required',
      });
      return;
    }

    // URL encode the server name
    const encodedName = encodeURIComponent(serverName);
    const response = await fetch(`${REGISTRY_BASE_URL}/servers/${encodedName}/versions`, {
      headers: {
        Accept: 'application/json, application/problem+json',
      },
    });

    if (!response.ok) {
      if (response.status === 404) {
        res.status(404).json({
          success: false,
          message: 'Server not found',
        });
        return;
      }
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();

    const apiResponse: ApiResponse<typeof data> = {
      success: true,
      data: data,
    };

    res.json(apiResponse);
  } catch (error) {
    console.error('Error fetching registry server versions:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to fetch registry server versions';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};

/**
 * Get a specific version of an MCP server
 * Proxies the request to avoid CORS issues in the frontend
 */
export const getRegistryServerVersion = async (req: Request, res: Response): Promise<void> => {
  try {
    const { serverName, version } = req.params;

    if (!serverName || !version) {
      res.status(400).json({
        success: false,
        message: 'Server name and version are required',
      });
      return;
    }

    // URL encode the server name and version
    const encodedName = encodeURIComponent(serverName);
    const encodedVersion = encodeURIComponent(version);
    const response = await fetch(
      `${REGISTRY_BASE_URL}/servers/${encodedName}/versions/${encodedVersion}`,
      {
        headers: {
          Accept: 'application/json, application/problem+json',
        },
      },
    );

    if (!response.ok) {
      if (response.status === 404) {
        res.status(404).json({
          success: false,
          message: 'Server version not found',
        });
        return;
      }
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json();

    const apiResponse: ApiResponse<typeof data> = {
      success: true,
      data: data,
    };

    res.json(apiResponse);
  } catch (error) {
    console.error('Error fetching registry server version:', error);
    const errorMessage =
      error instanceof Error ? error.message : 'Failed to fetch registry server version';
    res.status(500).json({
      success: false,
      message: errorMessage,
    });
  }
};