forked from MCP-Club/mcp-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
277 lines (240 loc) · 8.58 KB
/
server.mjs
File metadata and controls
277 lines (240 loc) · 8.58 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import dotenv from 'dotenv';
import Fastify from 'fastify';
import cors from '@fastify/cors';
import { promises as fs } from 'fs';
import path from 'path';
import pkg from 'pg';
const { Pool } = pkg;
import TextEncoder from './encoder.mjs';
dotenv.config();
const fastify = Fastify({ logger: true });
const PORT = process.env.PORT || 3000;
const DATABASE_URL = `postgresql://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`;
// Create a global database pool
const pool = new Pool({
connectionString: DATABASE_URL,
max: 20, // Maximum number of clients in the pool
idleTimeoutMillis: 30000, // Close idle clients after 30 seconds
connectionTimeoutMillis: 2000, // Return an error after 2 seconds if connection could not be established
});
// Add pool error handlers
pool.on('error', (err, client) => {
console.error('Unexpected error on idle client', err);
});
pool.on('connect', () => {
console.log('Database connected successfully');
});
// Test database connection on startup
const testConnection = async () => {
try {
const client = await pool.connect();
console.log('Database connection test successful');
client.release();
} catch (err) {
console.error('Error connecting to the database:', err);
process.exit(1); // Exit if we can't connect to database
}
};
testConnection();
const { toEmbedding, toEmbeddingOpenAI } = TextEncoder();
fastify.decorate('toEmbedding', text => toEmbedding(text));
fastify.decorate('toEmbeddingOpenAI', text => toEmbeddingOpenAI(text));
// Register CORS
fastify.register(cors);
// Decorate fastify with our db pool
fastify.decorate('db', pool);
// Load registry from directories recursively
async function scanDirectory(dir, registry = [], prefix = '') {
try {
const items = await fs.readdir(dir);
for (const item of items) {
// ignore directory starting with '.' and node_modules
if (item.startsWith('.') || item === 'node_modules') {
continue;
}
const fullPath = path.join(dir, item);
const mcpJsonPath = path.join(fullPath, 'mcp.json');
const relativePath = prefix ? path.join(prefix, item) : item;
try {
const stat = await fs.stat(fullPath);
if (stat.isDirectory()) {
try {
const configData = await fs.readFile(mcpJsonPath, 'utf8');
const mcpData = JSON.parse(configData);
registry.push(mcpData);
} catch (err) {
// If no mcp.json, continue scanning subdirectories
await scanDirectory(fullPath, registry, relativePath);
}
}
} catch (err) {
continue;
}
}
return registry;
} catch (err) {
console.error(`Error scanning directory ${dir}:`, err);
return registry;
}
}
async function loadRegistry() {
try {
return await scanDirectory('./servers');
} catch (err) {
console.error('Error loading registry:', err);
return [];
}
}
// Welcome endpoint
fastify.get('/', async (request, reply) => {
return { message: 'Welcome to MCP Registry API' };
});
// Get entire registry
fastify.get('/registry', async (request, reply) => {
try {
const registry = await loadRegistry();
return registry;
} catch (err) {
reply.code(500);
return { error: 'Failed to load registry' };
}
});
// Search registry
fastify.get('/search', async (request, reply) => {
try {
const { q } = request.query;
if (!q) {
reply.code(400);
return { error: 'Search query is required' };
}
const registry = await loadRegistry();
const searchTerm = q.toLowerCase();
const results = registry.filter(mcp => {
return (
mcp.id.toLowerCase().includes(searchTerm) ||
mcp.title.toLowerCase().includes(searchTerm) ||
mcp.description.toLowerCase().includes(searchTerm) ||
(mcp.tags && mcp.tags.some(tag => tag.toLowerCase().includes(searchTerm)))
);
});
return results;
} catch (err) {
reply.code(500);
return { error: 'Search failed' };
}
});
// Get specific MCP by ID
fastify.get('/registry/:id', async (request, reply) => {
try {
const registry = await loadRegistry();
const mcp = registry.find(m => m.id === request.params.id);
if (!mcp) {
reply.code(404);
return { error: 'MCP not found' };
}
return mcp;
} catch (err) {
reply.code(500);
return { error: 'Failed to get MCP' };
}
});
// Test database connection
fastify.get('/db-test', async (request, reply) => {
try {
const { rows } = await fastify.db.query('SELECT NOW()');
return { success: true, timestamp: rows[0].now };
} catch (error) {
fastify.log.error(error);
return reply.status(500).send({ error: 'Database connection failed' });
}
});
fastify.get('/recommend', async (request, reply) => {
try {
const { description } = request.query;
if (!description) {
return reply.status(400).send({ error: 'Description is required' });
}
// encode description to vector
const embedding = await fastify.toEmbedding(description);
// Format the embedding array for PostgreSQL vector type
const vectorString = `[${Array.from(embedding).join(',')}]`;
try {
// Search for similar servers using vector similarity
const { rows } = await pool.query(`
SELECT
server_id,
title,
description,
github_url,
1 - (embedding <=> $1::vector) as similarity
FROM mcp_servers
WHERE embedding IS NOT NULL
ORDER BY embedding <=> $1::vector
LIMIT $2
`, [vectorString, 3]); // limiting to top 3 results
return rows.map(row => ({
server_id: row.server_id,
title: row.title,
description: row.description,
github_url: row.github_url,
similarity: parseFloat(row.similarity.toFixed(4))
}));
} catch (dbErr) {
fastify.log.error('Database error:', dbErr);
throw dbErr;
}
} catch (err) {
fastify.log.error(err);
return reply.status(500).send({ error: 'Recommendation failed' });
}
});
fastify.get('/recommend/v2', async (request, reply) => {
try {
const { description, limit = 10 } = request.query;
if (!description) {
return reply.status(400).send({ error: 'Description is required' });
}
const embedding = await fastify.toEmbeddingOpenAI(description);
// Format the embedding array for PostgreSQL vector type
const vectorStr = `[${embedding.join(',')}]`;
// Search for similar servers combining BM25 and vector similarity
const query = `
SELECT id,
title,
description,
github_url,
ts_rank(tsv_description, plainto_tsquery('english', $1)) AS bm25_score,
1 - (embedding_small <=> $2::vector) AS vector_score,
COALESCE(0.5 * ts_rank(tsv_description, plainto_tsquery('english', $1)), 0) +
0.5 * (1 - (embedding_small <=> $2::vector)) AS final_score
FROM mcp_servers
ORDER BY final_score DESC
LIMIT $3;
`;
const result = await pool.query(query, [description, vectorStr, limit]);
if (result.rows.length === 0) {
fastify.log.info("No results found in the database");
return [];
}
return result.rows.map(row => ({
id: row.id,
title: row.title,
description: row.description,
github_url: row.github_url,
score: row.final_score
}));
} catch (err) {
fastify.log.error(err);
return reply.status(500).send({ error: 'Recommendation failed' });
}
});
// Start the server
const start = async () => {
try {
await fastify.listen({ port: PORT, host: '0.0.0.0' });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();