UNPKG

mcp-use

Version:

A utility library for integrating Model Context Protocol (MCP) with LangChain, Zod, and related tools. Provides helpers for schema conversion, event streaming, and SDK usage.

34 lines (33 loc) 1.57 kB
import { z } from 'zod'; import { logger } from '../../logging.js'; import { MCPServerTool } from './base.js'; const EnumerateServersSchema = z.object({}); export class ListMCPServersTool extends MCPServerTool { name = 'list_mcp_servers'; description = `Lists all available MCP (Model Context Protocol) servers that can be connected to, along with the tools available on each server. Use this tool to discover servers and see what functionalities they offer.`; schema = EnumerateServersSchema; constructor(manager) { super(manager); } async _call() { const serverNames = this.manager.client.getServerNames(); if (serverNames.length === 0) { return `No MCP servers are currently defined.`; } const outputLines = ['Available MCP servers:']; for (const serverName of serverNames) { const isActiveServer = serverName === this.manager.activeServer; const activeFlag = isActiveServer ? ' (ACTIVE)' : ''; outputLines.push(`- ${serverName}${activeFlag}`); try { const serverTools = this.manager.serverTools?.[serverName] ?? []; const numberOfTools = Array.isArray(serverTools) ? serverTools.length : 0; outputLines.push(`${numberOfTools} tools available for this server\n`); } catch (error) { logger.error(`Unexpected error listing tools for server '${serverName}': ${String(error)}`); } } return outputLines.join('\n'); } }