@vibe-kit/grok-cli
Version:
An open-source AI agent that brings the power of Grok directly into your terminal.
116 lines • 4.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mcpManager = exports.MCPConnectionManager = void 0;
const client_1 = require("./client");
const mcp_manager_1 = require("../utils/mcp-manager");
class MCPConnectionManager {
constructor() {
this.clients = new Map();
this.isInitialized = false;
}
async initialize() {
if (this.isInitialized) {
return;
}
const servers = mcp_manager_1.MCPManager.getServers();
for (const [name, server] of Object.entries(servers)) {
try {
const client = new client_1.MCPClient(server);
await client.connect();
this.clients.set(name, client);
console.log(`✅ Connected to MCP server: ${name}`);
}
catch (error) {
console.warn(`⚠️ Failed to connect to MCP server '${name}': ${error.message}`);
}
}
this.isInitialized = true;
}
getAllTools() {
const mcpTools = [];
for (const [serverName, client] of this.clients) {
const tools = client.getTools();
for (const tool of tools) {
mcpTools.push({
type: 'function',
function: {
name: `mcp__${serverName}__${tool.name}`,
description: `[MCP ${serverName}] ${tool.description}`,
parameters: tool.inputSchema || {
type: 'object',
properties: {},
required: []
}
}
});
}
}
return mcpTools;
}
getAllResources() {
const allResources = [];
for (const [serverName, client] of this.clients) {
const resources = client.getResources();
allResources.push(...resources);
}
return allResources;
}
async callTool(serverName, toolName, params) {
const client = this.clients.get(serverName);
if (!client) {
throw new Error(`MCP server '${serverName}' not connected`);
}
return await client.callTool(toolName, params);
}
async getResource(serverName, uri) {
const client = this.clients.get(serverName);
if (!client) {
throw new Error(`MCP server '${serverName}' not connected`);
}
return await client.getResource(uri);
}
getConnectedServers() {
return Array.from(this.clients.keys());
}
isServerConnected(serverName) {
return this.clients.has(serverName);
}
async disconnect() {
for (const [name, client] of this.clients) {
try {
await client.disconnect();
}
catch (error) {
console.warn(`Failed to disconnect from MCP server '${name}':`, error);
}
}
this.clients.clear();
this.isInitialized = false;
}
async reconnectServer(serverName) {
// Disconnect existing client if any
const existingClient = this.clients.get(serverName);
if (existingClient) {
await existingClient.disconnect();
this.clients.delete(serverName);
}
// Get server config and reconnect
const server = mcp_manager_1.MCPManager.getServer(serverName);
if (!server) {
throw new Error(`MCP server '${serverName}' not found in configuration`);
}
try {
const client = new client_1.MCPClient(server);
await client.connect();
this.clients.set(serverName, client);
return true;
}
catch (error) {
throw new Error(`Failed to reconnect to MCP server '${serverName}': ${error}`);
}
}
}
exports.MCPConnectionManager = MCPConnectionManager;
// Global instance
exports.mcpManager = new MCPConnectionManager();
//# sourceMappingURL=manager.js.map