Agregar endpoints HTTP /token y /status al servidor WebMCP
POST /token genera un token de registro autenticado con Bearer token. GET /status retorna estado del servidor, clientes conectados y version.
This commit is contained in:
@@ -9,8 +9,11 @@
|
||||
padding: "20px",
|
||||
inactivityTimeout: 5 * 60 * 1e3,
|
||||
// 5 minutes in milliseconds
|
||||
headless: false,
|
||||
...options
|
||||
};
|
||||
this._listeners = {};
|
||||
this._currentStatus = "disconnected";
|
||||
this.isConnected = false;
|
||||
this.isExpanded = false;
|
||||
this.socket = null;
|
||||
@@ -37,6 +40,62 @@
|
||||
this.REGISTER_PATH = "/register";
|
||||
this._init();
|
||||
}
|
||||
/**
|
||||
* Register an event listener
|
||||
* @public
|
||||
* @param {string} event - Event name
|
||||
* @param {Function} callback - Callback function
|
||||
* @returns {Function} Unsubscribe function
|
||||
*/
|
||||
on(event, callback) {
|
||||
if (!this._listeners[event]) {
|
||||
this._listeners[event] = [];
|
||||
}
|
||||
this._listeners[event].push(callback);
|
||||
return () => this.off(event, callback);
|
||||
}
|
||||
/**
|
||||
* Remove an event listener
|
||||
* @public
|
||||
* @param {string} event - Event name
|
||||
* @param {Function} callback - Callback function to remove
|
||||
*/
|
||||
off(event, callback) {
|
||||
if (!this._listeners[event]) return;
|
||||
this._listeners[event] = this._listeners[event].filter((cb) => cb !== callback);
|
||||
}
|
||||
/**
|
||||
* Emit an event to all registered listeners
|
||||
* @private
|
||||
* @param {string} event - Event name
|
||||
* @param {Object} data - Event data
|
||||
*/
|
||||
_emit(event, data) {
|
||||
if (!this._listeners[event]) return;
|
||||
this._listeners[event].forEach((cb) => {
|
||||
try {
|
||||
cb(data);
|
||||
} catch (e) {
|
||||
console.error(`Error in ${event} listener:`, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Get current connection info snapshot
|
||||
* @public
|
||||
* @returns {Object} Connection state snapshot
|
||||
*/
|
||||
getConnectionInfo() {
|
||||
return {
|
||||
isConnected: this.isConnected,
|
||||
channel: this.currentChannel,
|
||||
server: this.currentServer,
|
||||
status: this._currentStatus,
|
||||
tools: Array.from(this.availableTools.keys()),
|
||||
prompts: Array.from(this.availablePrompts.keys()),
|
||||
resources: Array.from(this.availableResources.keys())
|
||||
};
|
||||
}
|
||||
_format(s) {
|
||||
return s.replace(/[.:]/g, "_");
|
||||
}
|
||||
@@ -45,12 +104,14 @@
|
||||
* @private
|
||||
*/
|
||||
_init() {
|
||||
if (document.querySelector("[data-webmcp-widget]")) {
|
||||
console.warn("WebMCP widget already initialized on this page");
|
||||
return;
|
||||
if (!this.options.headless) {
|
||||
if (document.querySelector("[data-webmcp-widget]")) {
|
||||
console.warn("WebMCP widget already initialized on this page");
|
||||
return;
|
||||
}
|
||||
this._createWidget();
|
||||
this._setupEventListeners();
|
||||
}
|
||||
this._createWidget();
|
||||
this._setupEventListeners();
|
||||
this._resetInactivityTimer();
|
||||
this._checkStoredToken();
|
||||
}
|
||||
@@ -542,6 +603,8 @@
|
||||
* @private
|
||||
*/
|
||||
_updateStatus(status, message) {
|
||||
this._currentStatus = status;
|
||||
this._emit("statusChange", { status, message: message || status });
|
||||
const container = document.getElementById(this.elementId);
|
||||
if (!container) return;
|
||||
const statusIndicator = container.querySelector(".webmcp-status");
|
||||
@@ -892,6 +955,7 @@
|
||||
this._reconnectAttempts = 0;
|
||||
this._updateStatus("connected", `Connected to ${this.currentChannel}`);
|
||||
this._updateConnectionUI(true);
|
||||
this._emit("connected", { channel: this.currentChannel, server: this.currentServer });
|
||||
console.log("WebMCP connection established");
|
||||
this._registerItemsWithServer();
|
||||
});
|
||||
@@ -901,6 +965,7 @@
|
||||
if (event.code === 1e3) {
|
||||
this._updateStatus("disconnected", "Disconnected");
|
||||
this._updateConnectionUI(false);
|
||||
this._emit("disconnected", { code: event.code, reason: event.reason });
|
||||
return;
|
||||
}
|
||||
if (this._reconnectAttempts < this._maxReconnectAttempts && this._lastConnectionToken) {
|
||||
@@ -908,6 +973,11 @@
|
||||
const delay = this._reconnectDelay * this._reconnectAttempts;
|
||||
console.log(`Reconnecting (attempt ${this._reconnectAttempts}/${this._maxReconnectAttempts}) in ${delay}ms...`);
|
||||
this._updateStatus("connecting", `Reconectando (${this._reconnectAttempts}/${this._maxReconnectAttempts})...`);
|
||||
this._emit("reconnecting", {
|
||||
attempt: this._reconnectAttempts,
|
||||
maxAttempts: this._maxReconnectAttempts,
|
||||
delay
|
||||
});
|
||||
setTimeout(() => {
|
||||
this.connect(this._lastConnectionToken);
|
||||
}, delay);
|
||||
@@ -915,6 +985,7 @@
|
||||
}
|
||||
this._updateStatus("disconnected", "Disconnected");
|
||||
this._updateConnectionUI(false);
|
||||
this._emit("disconnected", { code: event.code, reason: event.reason });
|
||||
this.currentToken = "";
|
||||
this.currentServer = "";
|
||||
this.currentChannel = "";
|
||||
@@ -961,6 +1032,7 @@
|
||||
break;
|
||||
case "toolRegistered":
|
||||
console.log(`Tool registered with server: ${message.name}`);
|
||||
this._emit("toolRegistered", { name: message.name });
|
||||
break;
|
||||
case "promptRegistered":
|
||||
console.log(`Prompt registered with server: ${message.name}`);
|
||||
@@ -1005,6 +1077,7 @@
|
||||
this.registeredTools.delete(message.name);
|
||||
this._saveItemsToStorage();
|
||||
this._updateToolsList();
|
||||
this._emit("toolRemoved", { name: message.name });
|
||||
console.log(`Tool removed by server: ${message.name}`);
|
||||
}
|
||||
break;
|
||||
@@ -1013,6 +1086,7 @@
|
||||
this.registeredTools.clear();
|
||||
this._saveItemsToStorage();
|
||||
this._updateToolsList();
|
||||
this._emit("toolRemoved", { name: "*" });
|
||||
console.log("All tools removed by server");
|
||||
break;
|
||||
case "getClientInfo":
|
||||
@@ -1039,6 +1113,7 @@
|
||||
break;
|
||||
case "error":
|
||||
console.error(`Server error: ${message.message}`);
|
||||
this._emit("error", { message: message.message });
|
||||
break;
|
||||
default:
|
||||
console.warn(`Unknown message type: ${message.type}`);
|
||||
@@ -1062,6 +1137,7 @@
|
||||
type: "toolResponse",
|
||||
result: { content: [{ type: "text", text: `Herramienta "${name}" registrada exitosamente` }] }
|
||||
});
|
||||
this._emit("toolCreated", { name });
|
||||
console.log(`Tool created by agent: ${name}`);
|
||||
} catch (e) {
|
||||
this._sendMessage({
|
||||
|
||||
Reference in New Issue
Block a user