UNPKG

n8n

Version:

n8n Workflow Automation Tool

79 lines 3.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildVerifyMcpServerTool = buildVerifyMcpServerTool; const tool_1 = require("@n8n/agents/tool"); const api_types_1 = require("@n8n/api-types"); const zod_1 = require("zod"); const mcp_client_factory_1 = require("../json-config/mcp-client-factory"); const builder_tool_names_1 = require("./builder-tool-names"); const verifyMcpServerInputSchema = zod_1.z.object({ name: zod_1.z .string() .min(1) .max(64) .regex(/^[a-zA-Z0-9_-]+$/) .describe('The server name (used as the tool-name prefix)'), url: zod_1.z.string().min(1).describe('The MCP server endpoint URL'), transport: zod_1.z .enum(['sse', 'streamableHttp']) .default('streamableHttp') .describe('Transport type. Defaults to streamableHttp'), authentication: zod_1.z .union([api_types_1.McpAuthenticationSchemaTypes, zod_1.z.string().endsWith('McpOAuth2Api')]) .default('none') .describe('Authentication scheme'), credential: zod_1.z .string() .optional() .describe('Credential id returned by ask_credential. Required when authentication is not "none"'), connectionTimeoutMs: zod_1.z .number() .int() .min(1) .max(120_000) .optional() .describe('Connection timeout in milliseconds'), }); function buildVerifyMcpServerTool(deps) { return new tool_1.Tool(builder_tool_names_1.BUILDER_TOOLS.VERIFY_MCP_SERVER) .description('Test connectivity to an MCP server before adding it to the agent config. ' + 'Establishes a temporary connection, lists the available tools, then closes the connection. ' + 'Returns { ok: true, tools: [{ name, description }] } on success, or ' + '{ ok: false, error: string } on failure. ' + 'Call this after ask_credential (when authentication is not "none") and before patch_config.') .input(verifyMcpServerInputSchema) .handler(async (input) => { let client; try { client = await (0, mcp_client_factory_1.buildMcpClientForServer)({ name: input.name, url: input.url, transport: input.transport, authentication: input.authentication, credential: input.credential, ...(input.connectionTimeoutMs !== undefined && { connectionTimeoutMs: input.connectionTimeoutMs, }), }, deps); const tools = await client.listTools(); return { ok: true, tools: tools.map((t) => ({ name: t.name, description: t.description ?? '', })), }; } catch (error) { return { ok: false, error: error instanceof Error ? error.message : String(error), }; } finally { await client?.close().catch(() => { }); } }) .build(); } //# sourceMappingURL=verify-mcp-server.tool.js.map