@webdevtoday/grok-cli
Version:
A sophisticated CLI tool for interacting with xAI Grok 4, featuring conversation history, file reference, custom commands, memory system, and genetic development workflows
184 lines โข 7.22 kB
JavaScript
;
/**
* MCP Tool Integration
* Wraps MCP server tools to integrate them into the main tool system
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.McpToolManager = exports.McpTool = void 0;
const base_1 = require("./base");
const chalk_1 = __importDefault(require("chalk"));
/**
* Dynamic MCP tool that wraps an MCP server tool
*/
class McpTool extends base_1.BaseTool {
constructor(serverName, toolName, description, inputSchema, mcpClient) {
const name = `mcp_${serverName}_${toolName}`;
const fullDescription = `${description} (via MCP:${serverName})`;
super(name, fullDescription);
this.mcpClient = mcpClient;
this.serverName = serverName;
this.toolName = toolName;
this.inputSchema = inputSchema;
}
async execute(params) {
try {
console.log(chalk_1.default.blue(`๐ง Executing MCP tool: ${this.serverName}:${this.toolName}`));
const result = await this.mcpClient.callTool(this.serverName, this.toolName, params);
return {
success: true,
output: JSON.stringify(result, null, 2)
};
}
catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
return {
success: false,
output: '',
error: `MCP tool execution failed: ${errorMsg}`
};
}
}
validateParams(params) {
// Basic validation - could be enhanced with JSON schema validation
// For now, just check if required parameters are present
if (this.inputSchema?.required) {
for (const required of this.inputSchema.required) {
if (!(required in params)) {
return false;
}
}
}
return true;
}
}
exports.McpTool = McpTool;
/**
* MCP Tool Manager - manages dynamic registration of MCP tools
*/
class McpToolManager {
constructor(toolExecutor, mcpClient) {
this.toolExecutor = toolExecutor;
this.mcpClient = mcpClient;
this.registeredTools = new Set();
// Listen for MCP client events to dynamically register/unregister tools
this.mcpClient.on('serverConnected', (serverName) => {
this.registerServerTools(serverName);
});
this.mcpClient.on('serverDisconnected', (serverName) => {
this.unregisterServerTools(serverName);
});
}
/**
* Register all tools from a connected MCP server
*/
async registerServerTools(serverName) {
try {
const server = this.mcpClient.getServer(serverName);
if (!server || server.status !== 'connected') {
return;
}
console.log(chalk_1.default.blue(`๐ฆ Registering tools from MCP server: ${serverName}`));
for (const tool of server.tools) {
const mcpTool = new McpTool(serverName, tool.name, tool.description, tool.inputSchema, this.mcpClient);
// Register the tool with the main tool registry
this.toolExecutor.getRegistry().register(mcpTool);
this.registeredTools.add(mcpTool.name);
console.log(chalk_1.default.green(` โ
Registered: ${mcpTool.name}`));
}
if (server.tools.length > 0) {
console.log(chalk_1.default.green(`๐ Registered ${server.tools.length} tools from ${serverName}`));
}
}
catch (error) {
console.error(chalk_1.default.red(`โ Failed to register tools from ${serverName}:`), error);
}
}
/**
* Unregister all tools from a disconnected MCP server
*/
async unregisterServerTools(serverName) {
try {
console.log(chalk_1.default.yellow(`๐ฆ Unregistering tools from MCP server: ${serverName}`));
// Find and unregister all tools from this server
const toolsToRemove = [];
for (const toolName of this.registeredTools) {
if (toolName.startsWith(`mcp_${serverName}_`)) {
toolsToRemove.push(toolName);
}
}
for (const toolName of toolsToRemove) {
this.toolExecutor.getRegistry().unregister(toolName);
this.registeredTools.delete(toolName);
console.log(chalk_1.default.yellow(` โ
Unregistered: ${toolName}`));
}
if (toolsToRemove.length > 0) {
console.log(chalk_1.default.yellow(`๐งน Unregistered ${toolsToRemove.length} tools from ${serverName}`));
}
}
catch (error) {
console.error(chalk_1.default.red(`โ Failed to unregister tools from ${serverName}:`), error);
}
}
/**
* Refresh all registered MCP tools
*/
async refreshAllTools() {
console.log(chalk_1.default.blue('๐ Refreshing all MCP tools...'));
// Clear all registered MCP tools
for (const toolName of this.registeredTools) {
this.toolExecutor.getRegistry().unregister(toolName);
}
this.registeredTools.clear();
// Re-register tools from all connected servers
const servers = this.mcpClient.getAllServers();
for (const server of servers) {
if (server.status === 'connected') {
await this.registerServerTools(server.name);
}
}
console.log(chalk_1.default.green('โ
MCP tools refreshed'));
}
/**
* Get statistics about registered MCP tools
*/
getStats() {
const toolsByServer = {};
for (const toolName of this.registeredTools) {
const parts = toolName.split('_');
if (parts.length >= 3 && parts[0] === 'mcp' && parts[1]) {
const serverName = parts[1];
toolsByServer[serverName] = (toolsByServer[serverName] || 0) + 1;
}
}
return {
totalTools: this.registeredTools.size,
toolsByServer,
registeredTools: Array.from(this.registeredTools).sort()
};
}
/**
* List all registered MCP tools with details
*/
listRegisteredTools() {
const tools = [];
for (const toolName of this.registeredTools) {
const tool = this.toolExecutor.getRegistry().getTool(toolName);
if (tool && tool instanceof McpTool) {
const server = this.mcpClient.getServer(tool.serverName);
tools.push({
name: tool.name,
serverName: tool.serverName,
toolName: tool.toolName,
description: tool.description,
status: server?.status || 'unknown'
});
}
}
return tools.sort((a, b) => a.name.localeCompare(b.name));
}
}
exports.McpToolManager = McpToolManager;
//# sourceMappingURL=mcp.js.map