@tiledesk/tiledesk-server 2.14.25 → 2.14.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@
6
6
  (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
7
7
 
8
8
 
9
+ # 2.14.26
10
+ - Added endpoints to connect with a MCP Tools and get tools list.
11
+ - Updated tybot-connectort to 2.0.44
12
+
9
13
  # 2.14.25
10
14
  - Deprecate user file upload routes in files.js and images.js
11
15
 
package/app.js CHANGED
@@ -150,6 +150,7 @@ var segment = require('./routes/segment');
150
150
  var webhook = require('./routes/webhook');
151
151
  var webhooks = require('./routes/webhooks');
152
152
  var copilot = require('./routes/copilot');
153
+ var mcp = require('./routes/mcp');
153
154
 
154
155
  var bootDataLoader = require('./services/bootDataLoader');
155
156
  var settingDataLoader = require('./services/settingDataLoader');
@@ -637,6 +638,8 @@ app.use('/:projectid/quotes', [passport.authenticate(['basic', 'jwt'], { session
637
638
 
638
639
  app.use('/:projectid/integration', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('admin', ['bot','subscription'])], integration )
639
640
 
641
+ app.use('/:projectid/mcp', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('admin', ['bot','subscription'])], mcp);
642
+
640
643
  app.use('/:projectid/kbsettings', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], kbsettings);
641
644
  app.use('/:projectid/kb/unanswered', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('admin', ['bot','subscription'])], unanswered);
642
645
  app.use('/:projectid/kb', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('admin', ['bot','subscription'])], kb);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-server",
3
3
  "description": "The Tiledesk server module",
4
- "version": "2.14.25",
4
+ "version": "2.14.26",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "pretest": "mongodb-runner start",
@@ -49,7 +49,7 @@
49
49
  "@tiledesk/tiledesk-rasa-connector": "^1.0.10",
50
50
  "@tiledesk/tiledesk-sms-connector": "^0.1.11",
51
51
  "@tiledesk/tiledesk-telegram-connector": "^0.1.14",
52
- "@tiledesk/tiledesk-tybot-connector": "^2.0.43",
52
+ "@tiledesk/tiledesk-tybot-connector": "^2.0.44",
53
53
  "@tiledesk/tiledesk-voice-twilio-connector": "^0.1.28",
54
54
  "@tiledesk/tiledesk-vxml-connector": "^0.1.89",
55
55
  "@tiledesk/tiledesk-whatsapp-connector": "1.0.22",
package/routes/mcp.js ADDED
@@ -0,0 +1,74 @@
1
+ let express = require('express');
2
+ let router = express.Router();
3
+ let winston = require('../config/winston');
4
+ const mcpService = require('../services/mcpService');
5
+ const Project = require('../models/project');
6
+
7
+ /**
8
+ * POST /mcp/connect
9
+ * Initializes a connection to an MCP server
10
+ * Body: { url: string, auth?: { type: 'bearer'|'api_key'|'basic', token?: string, key?: string, username?: string, password?: string } }
11
+ */
12
+ router.post('/connect', async (req, res) => {
13
+ try {
14
+ const id_project = req.projectid;
15
+ const { url, auth } = req.body;
16
+
17
+ if (!url) {
18
+ return res.status(400).send({ success: false, error: "Missing required parameter 'url'" });
19
+ }
20
+
21
+ // Build server configuration
22
+ const serverConfig = {
23
+ url: url,
24
+ projectId: id_project,
25
+ auth: auth || undefined
26
+ };
27
+
28
+ // Initialize the connection
29
+ const result = await mcpService.initializeServer(serverConfig);
30
+
31
+ res.status(200).send({
32
+ success: true,
33
+ message: 'MCP server connected successfully',
34
+ capabilities: result?.capabilities || {}
35
+ });
36
+ } catch (error) {
37
+ winston.error(`Error connecting to MCP server:`, error);
38
+ res.status(500).send({ success: false, error: error.message || 'Error connecting to MCP server' });
39
+ }
40
+ });
41
+
42
+ /**
43
+ * POST /mcp/tools
44
+ * Gets the list of tools from an MCP server
45
+ * Body: { url: string, auth?: object }
46
+ */
47
+ router.post('/tools', async (req, res) => {
48
+ try {
49
+ const id_project = req.projectid;
50
+ const { url, auth } = req.body;
51
+
52
+ if (!url) {
53
+ return res.status(400).send({ success: false, error: "Missing required parameter 'url' in body" });
54
+ }
55
+
56
+ // Build server configuration
57
+ const serverConfig = {
58
+ url: url,
59
+ projectId: id_project,
60
+ auth: auth || undefined
61
+ };
62
+
63
+ // listTools automatically initializes if necessary
64
+ const tools = await mcpService.listTools(serverConfig);
65
+
66
+ res.status(200).send(tools);
67
+ } catch (error) {
68
+ winston.error(`Error getting tools from MCP server:`, error);
69
+ res.status(500).send({ success: false, error: error.message || 'Error getting tools from MCP server' });
70
+ }
71
+ });
72
+
73
+ module.exports = router;
74
+
@@ -0,0 +1,283 @@
1
+ 'use strict';
2
+
3
+ const axios = require('axios').default;
4
+ const winston = require('../config/winston');
5
+
6
+
7
+ /**
8
+ * MCP Service - Manages connections and calls to MCP servers
9
+ * MCP (Model Context Protocol) uses JSON-RPC 2.0 for communication
10
+ */
11
+ class MCPService {
12
+
13
+ constructor() {
14
+ // Cache of connections to MCP servers
15
+ // Key: `${projectId}_${url}` or just `${url}` if projectId is not provided
16
+ // Value: { config, sessionId?, initialized, capabilities }
17
+ this.connections = new Map();
18
+ }
19
+
20
+ /**
21
+ * Parses a response in SSE (Server-Sent Events) format and extracts the JSON object
22
+ * @param {String} sseData - String with SSE format
23
+ * @returns {Object} - Parsed JSON object
24
+ */
25
+ parseSSEResponse(sseData) {
26
+ const lines = sseData.split('\n');
27
+ for (const line of lines) {
28
+ if (line.startsWith('data: ')) {
29
+ const jsonStr = line.substring(6); // Removes "data: "
30
+ try {
31
+ return JSON.parse(jsonStr);
32
+ } catch (e) {
33
+ throw new Error(`Failed to parse SSE JSON data: ${e.message}`);
34
+ }
35
+ }
36
+ }
37
+ throw new Error('No data field found in SSE response');
38
+ }
39
+
40
+ /**
41
+ * Sends a JSON-RPC request to an MCP server
42
+ * @param {String} url - MCP server URL
43
+ * @param {Object} request - JSON-RPC request
44
+ * @param {Object} auth - Authentication configuration (optional)
45
+ * @param {String} sessionId - Session ID for the MCP server (optional)
46
+ * @returns {Promise<Object>} - Server response (includes sessionId if present in headers)
47
+ */
48
+ async sendJSONRPCRequest(url, request, auth, sessionId) {
49
+ const config = {
50
+ method: 'POST',
51
+ url: url,
52
+ headers: {
53
+ 'Content-Type': 'application/json',
54
+ 'Accept': 'application/json, text/event-stream'
55
+ },
56
+ data: request,
57
+ timeout: 30000, // 30 seconds timeout
58
+ validateStatus: function (status) {
59
+ return status >= 200 && status < 500; // Accept also 4xx to capture headers
60
+ }
61
+ };
62
+
63
+ // Add session ID in mcp-session-id header if present
64
+ if (sessionId) {
65
+ config.headers['mcp-session-id'] = sessionId;
66
+ }
67
+
68
+ // Add authentication if present
69
+ if (auth) {
70
+ if (auth.type === 'bearer' && auth.token) {
71
+ config.headers['Authorization'] = `Bearer ${auth.token}`;
72
+ } else if (auth.type === 'api_key' && auth.key) {
73
+ config.headers['X-API-Key'] = auth.key;
74
+ } else if (auth.type === 'basic' && auth.username && auth.password) {
75
+ const credentials = Buffer.from(`${auth.username}:${auth.password}`).toString('base64');
76
+ config.headers['Authorization'] = `Basic ${credentials}`;
77
+ }
78
+ }
79
+
80
+ try {
81
+ const response = await axios(config);
82
+
83
+ // Capture session ID from response headers (if present)
84
+ const sessionIdFromHeader = response.headers['mcp-session-id'] ||
85
+ response.headers['x-mcp-session-id'];
86
+
87
+ // Parse response: could be SSE (text/event-stream) or direct JSON
88
+ let responseData;
89
+ if (typeof response.data === 'string') {
90
+ // SSE format: extracts JSON from "data:" line
91
+ responseData = this.parseSSEResponse(response.data);
92
+ } else {
93
+ // JSON response already parsed
94
+ responseData = response.data;
95
+ }
96
+
97
+ // Add session ID from response if present in headers
98
+ if (sessionIdFromHeader) {
99
+ responseData.sessionId = sessionIdFromHeader;
100
+ }
101
+
102
+ // Check if there's a JSON-RPC error
103
+ if (responseData.error) {
104
+ throw new Error(`MCP Error: ${responseData.error.message || 'Unknown error'} (code: ${responseData.error.code})`);
105
+ }
106
+
107
+ return responseData;
108
+ } catch (error) {
109
+ if (error.response) {
110
+ // HTTP error
111
+ const status = error.response.status;
112
+ const statusText = error.response.statusText;
113
+ const responseData = error.response.data;
114
+ const errorMessage = `HTTP Error ${status}: ${statusText}`;
115
+ const details = responseData ? ` - ${JSON.stringify(responseData)}` : '';
116
+ throw new Error(`${errorMessage}${details}`);
117
+ } else if (error.request) {
118
+ // No response received
119
+ throw new Error(`No response from MCP server: ${error.message}`);
120
+ } else {
121
+ // Request configuration error
122
+ throw new Error(`Request error: ${error.message}`);
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Generates a unique cache key based on URL and projectId
129
+ * @param {String} url - MCP server URL
130
+ * @param {String} projectId - Project ID (optional)
131
+ * @returns {String} - Unique cache key
132
+ */
133
+ getCacheKey(url, projectId) {
134
+ return projectId ? `${projectId}_${url}` : url;
135
+ }
136
+
137
+ /**
138
+ * Initializes a connection to an MCP server
139
+ * @param {Object} serverConfig - MCP server configuration { url, projectId?, auth? }
140
+ * @returns {Promise<Object>} - Initialization result
141
+ */
142
+ async initializeServer(serverConfig) {
143
+ if (!serverConfig || !serverConfig.url) {
144
+ throw new Error('Server MCP configuration is missing or invalid');
145
+ }
146
+
147
+ const serverId = this.getCacheKey(serverConfig.url, serverConfig.projectId);
148
+
149
+ try {
150
+ // Initialize session (some MCP servers require session ID)
151
+ let sessionId = null;
152
+ let initializeResponse = null;
153
+
154
+ try {
155
+ // JSON-RPC call to initialize the server
156
+ const response = await this.sendJSONRPCRequest(serverConfig.url, {
157
+ jsonrpc: '2.0',
158
+ id: Date.now(),
159
+ method: 'initialize',
160
+ params: {
161
+ protocolVersion: '2024-11-05',
162
+ capabilities: {
163
+ tools: {}
164
+ },
165
+ clientInfo: {
166
+ name: 'tiledesk-server',
167
+ version: '1.0.0'
168
+ }
169
+ }
170
+ }, serverConfig.auth, null);
171
+
172
+ // Session ID is returned in 'mcp-session-id' header from response
173
+ sessionId = response.sessionId || null;
174
+ initializeResponse = response;
175
+
176
+ if (sessionId) {
177
+ winston.debug(`MCP Server ${serverId} session initialized with ID: ${sessionId}`);
178
+ } else {
179
+ winston.debug(`MCP Server ${serverId} initialized (stateless, no session ID required)`);
180
+ }
181
+ } catch (initError) {
182
+ // If initialization fails, try anyway without session ID
183
+ // (some MCP servers don't require session ID)
184
+ winston.debug(`MCP Server ${serverId} initialization failed: ${initError.message}`);
185
+ throw initError;
186
+ }
187
+
188
+ // Save connection in cache with session ID
189
+ this.connections.set(serverId, {
190
+ config: serverConfig,
191
+ sessionId: sessionId,
192
+ initialized: true,
193
+ capabilities: initializeResponse.result?.capabilities || {}
194
+ });
195
+
196
+ winston.info(`MCP Server initialized: ${serverId}${sessionId ? ` (session: ${sessionId})` : ' (stateless)'}`);
197
+ return initializeResponse.result;
198
+ } catch (error) {
199
+ winston.error(`Error initializing MCP server ${serverId}:`, error);
200
+ throw error;
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Gets the list of available tools from an MCP server
206
+ * @param {Object} serverConfig - MCP server configuration { url, projectId?, auth? }
207
+ * @returns {Promise<Array>} - List of available tools
208
+ */
209
+ async listTools(serverConfig) {
210
+ if (!serverConfig || !serverConfig.url) {
211
+ throw new Error('Server MCP configuration is missing or invalid');
212
+ }
213
+
214
+ const serverId = this.getCacheKey(serverConfig.url, serverConfig.projectId);
215
+
216
+ try {
217
+ // Check if server is already initialized
218
+ let connection = this.connections.get(serverId);
219
+ if (!connection) {
220
+ await this.initializeServer(serverConfig);
221
+ connection = this.connections.get(serverId);
222
+ }
223
+
224
+ const sessionId = connection?.sessionId || null;
225
+
226
+ // JSON-RPC call to get the list of tools
227
+ // If server requires session ID but we don't have it, try first without
228
+ let response;
229
+ try {
230
+ response = await this.sendJSONRPCRequest(serverConfig.url, {
231
+ jsonrpc: '2.0',
232
+ id: Date.now(),
233
+ method: 'tools/list',
234
+ params: {}
235
+ }, serverConfig.auth, sessionId);
236
+ } catch (error) {
237
+ // If it fails and we have a "No valid session ID" error, try without session ID
238
+ if (sessionId && error.message && error.message.includes('No valid session ID')) {
239
+ winston.debug(`Retrying tools/list without session ID for server ${serverId}...`);
240
+ response = await this.sendJSONRPCRequest(serverConfig.url, {
241
+ jsonrpc: '2.0',
242
+ id: Date.now(),
243
+ method: 'tools/list',
244
+ params: {}
245
+ }, serverConfig.auth, null);
246
+ } else {
247
+ throw error;
248
+ }
249
+ }
250
+
251
+ const tools = response.result?.tools || [];
252
+ winston.debug(`MCP Server ${serverId} returned ${tools.length} tools`);
253
+ return tools;
254
+ } catch (error) {
255
+ winston.error(`Error listing tools from MCP server ${serverId}:`, error);
256
+ throw error;
257
+ }
258
+ }
259
+
260
+
261
+ /**
262
+ * Removes a connection from cache
263
+ * @param {String} url - MCP server URL
264
+ * @param {String} projectId - Project ID (optional)
265
+ */
266
+ removeConnection(url, projectId) {
267
+ const serverId = this.getCacheKey(url, projectId);
268
+ this.connections.delete(serverId);
269
+ }
270
+
271
+ /**
272
+ * Clears all connections from cache
273
+ */
274
+ clearConnections() {
275
+ this.connections.clear();
276
+ }
277
+ }
278
+
279
+ // Singleton instance
280
+ const mcpService = new MCPService();
281
+
282
+ module.exports = mcpService;
283
+