-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest-wrapper.js
More file actions
335 lines (315 loc) · 8.21 KB
/
Copy pathrest-wrapper.js
File metadata and controls
335 lines (315 loc) · 8.21 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
const express = require('express');
const axios = require('axios');
/**
* REST API Wrapper for SQL Server MCP Server
*
* This wrapper converts standard REST endpoints to MCP JSON-RPC calls,
* making it easier for other applications to integrate with the MCP server.
*
* Usage:
* 1. Start MCP server in HTTP mode: MCP_TRANSPORT=http node dist/index.js
* 2. Start this REST wrapper: node rest-wrapper.js
* 3. Call REST endpoints from your application
*/
const MCP_SERVER_URL = process.env.MCP_SERVER_URL || 'http://localhost:3000/mcp';
const REST_PORT = process.env.REST_PORT || '4000';
const app = express();
app.use(express.json());
/**
* Helper function to call MCP server
*/
async function callMCPServer(method, params = {}) {
try {
const response = await axios.post(MCP_SERVER_URL, {
jsonrpc: "2.0",
id: Date.now(),
method,
params
});
return response.data.result;
} catch (error) {
console.error('MCP Server Error:', error.response?.data || error.message);
throw error;
}
}
/**
* Helper function to parse MCP text response
*/
function parseResponse(result) {
if (result && result.content && result.content[0]) {
return JSON.parse(result.content[0].text);
}
return result;
}
// ============== REST Endpoints ==============
/**
* GET /api/health
* Health check endpoint
*/
app.get('/api/health', async (req, res) => {
try {
const result = await callMCPServer('initialize', {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: {
name: "rest-wrapper",
version: "1.0.0"
}
});
res.json({
status: 'healthy',
mcpServer: MCP_SERVER_URL,
serverInfo: result.serverInfo
});
} catch (error) {
res.status(500).json({
status: 'unhealthy',
error: error.message
});
}
});
/**
* GET /api/databases
* List all databases
*/
app.get('/api/databases', async (req, res) => {
try {
const result = await callMCPServer('tools/call', {
name: 'list_databases'
});
const data = parseResponse(result);
res.json({
success: true,
count: data.length,
data
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* GET /api/tables
* List all tables
* Query params: schema (default: dbo)
*/
app.get('/api/tables', async (req, res) => {
try {
const schema = req.query.schema || 'dbo';
const result = await callMCPServer('tools/call', {
name: 'list_tables',
arguments: { schema }
});
const data = parseResponse(result);
res.json({
success: true,
schema,
count: data.length,
data
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* GET /api/tables/:tableName
* Describe a specific table
* Query params: schema (default: dbo)
*/
app.get('/api/tables/:tableName', async (req, res) => {
try {
const { tableName } = req.params;
const schema = req.query.schema || 'dbo';
const result = await callMCPServer('tools/call', {
name: 'describe_table',
arguments: { tableName, schema }
});
const data = parseResponse(result);
res.json({
success: true,
tableName,
schema,
columns: data.length,
data
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* GET /api/tables/:tableName/relationships
* Get table relationships
* Query params: schema (default: dbo)
*/
app.get('/api/tables/:tableName/relationships', async (req, res) => {
try {
const { tableName } = req.params;
const schema = req.query.schema || 'dbo';
const result = await callMCPServer('tools/call', {
name: 'get_table_relationships',
arguments: { tableName, schema }
});
const data = parseResponse(result);
res.json({
success: true,
tableName,
schema,
count: data.length,
data
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* GET /api/search
* Search tables and columns by keyword
* Query params: keyword
*/
app.get('/api/search', async (req, res) => {
try {
const { keyword } = req.query;
if (!keyword) {
return res.status(400).json({
success: false,
error: 'keyword parameter is required'
});
}
const result = await callMCPServer('tools/call', {
name: 'search_tables',
arguments: { keyword }
});
const data = parseResponse(result);
res.json({
success: true,
keyword,
count: data.length,
data
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* POST /api/query
* Execute SQL SELECT query
* Body: { sqlQuery: string }
*/
app.post('/api/query', async (req, res) => {
try {
const { sqlQuery } = req.body;
if (!sqlQuery) {
return res.status(400).json({
success: false,
error: 'sqlQuery is required in request body'
});
}
const result = await callMCPServer('tools/call', {
name: 'execute_read_query',
arguments: { sqlQuery }
});
const data = parseResponse(result);
res.json({
success: true,
rows: Array.isArray(data) ? data.length : 0,
data
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* DELETE /api/cache
* Clear metadata cache
*/
app.delete('/api/cache', async (req, res) => {
try {
const result = await callMCPServer('tools/call', {
name: 'clear_cache'
});
res.json({
success: true,
message: 'Cache cleared successfully'
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
/**
* GET /api/tools
* List all available MCP tools
*/
app.get('/api/tools', async (req, res) => {
try {
const result = await callMCPServer('tools/list');
res.json({
success: true,
count: result.tools.length,
tools: result.tools.map(tool => ({
name: tool.name,
description: tool.description,
parameters: tool.inputSchema?.properties || {}
}))
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
});
// ============== Error Handling ==============
app.use((req, res) => {
res.status(404).json({
success: false,
error: 'Endpoint not found'
});
});
app.use((err, req, res, next) => {
console.error('Error:', err);
res.status(500).json({
success: false,
error: err.message
});
});
// ============== Start Server ==============
const PORT = process.env.PORT || REST_PORT;
app.listen(PORT, () => {
console.log('╔════════════════════════════════════════╗');
console.log('║ REST API Wrapper for SQL Server MCP ║');
console.log('╠════════════════════════════════════════╣');
console.log(`║ Server running on: http://localhost:${PORT} ║`);
console.log(`║ MCP Server URL: ${MCP_SERVER_URL.padEnd(36)} ║`);
console.log('╠════════════════════════════════════════╣');
console.log('║ Available Endpoints: ║');
console.log('║ GET /api/health ║');
console.log('║ GET /api/databases ║');
console.log('║ GET /api/tables ║');
console.log('║ GET /api/tables/:tableName ║');
console.log('║ GET /api/tables/:tableName/relationships ║');
console.log('║ GET /api/search?keyword=xxx ║');
console.log('║ POST /api/query ║');
console.log('║ DELETE /api/cache ║');
console.log('║ GET /api/tools ║');
console.log('╚════════════════════════════════════════╝');
});