n8n-nodes-mcp

repository·main·Indexed 25 days ago

https://github.com/nerding-io/n8n-nodes-mcp

A community node for n8n that enables workflows and AI agents to interact with Model Context Protocol (MCP) servers. It supports accessing external tools, resources, and prompts via STDIO, HTTP Streamable, and legacy SSE transports. Key operations include executing tools, retrieving prompts, and listing available resources. Version 0.1.37.

Tokens
2K
Snippets
3
Records
10
Agent score
85%

What's inside n8n-nodes-mcp

  1. Install the n8n-nodes-mcp community node

    main

    To use the MCP Client node in n8n, follow the standard n8n community nodes installation guide.

    Important: If you intend to use the MCP Client node as a tool within n8n AI Agents, you MUST set the following environment variable to true: N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE=true

  2. Fix MCP Client Tool Schema Truncation

    main

    The listTools operation in the MCP Client node previously suffered from schema truncation because it converted the original JSON Schema from the MCP server into a Zod object and then back to JSON Schema using zodToJsonSchema. This process was lossy, removing nested structures, enums, and constraints.

    To fix this, ensure the listTools operation preserves the original JSON Schema from the MCP server by passing it through directly without Zod conversion.

    // Use the original JSON Schema from the MCP server directly
    const aiTools = tools.map((tool: any) => ({
        name: tool.name,
        description: tool.description || `Execute the ${tool.name} tool`,
        schema: tool.inputSchema || {type: 'object', properties: {}, additionalProperties: false},
        func: async (params) => { /* ... */ }
    }));
  3. Enable MCP Client as a Tool in AI Agents

    main

    To allow n8n AI Agents to use the MCP Client node as a tool, you must enable community package tool usage via environment variables.

    Setup by Platform:

    • Docker: Add N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE=true to your docker-compose.yml environment section.
    • Bash/Zsh: export N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE=true before starting n8n.
    • Desktop App: Create a .env file in the n8n directory containing N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE=true.
    • Mac/Linux (Permanent): Add export N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE=true to your ~/.zshrc or ~/.bash_profile.
    environment:
      - N8N_COMMUNITY_PACKAGES_ALLOW_TOOL_USAGE=true
  4. Configure Server-Sent Events (SSE) Transport (Deprecated)

    main

    SSE transport is available for legacy compatibility but is deprecated and will not receive further updates. Use HTTP Streamable for new projects.

    Credential Fields:

    • SSE URL: The URL of the SSE endpoint (default: http://localhost:3001/sse).
    • Messages Post Endpoint: Optional custom endpoint for posting messages if different from the SSE URL.
    • Additional Headers: Optional headers to send with requests, formatted as name:value, one per line.
  5. Configure HTTP Streamable Transport (Recommended)

    main

    HTTP Streamable is the recommended transport method for new implementations. It provides better efficiency and flexibility than the deprecated SSE transport.

    Credential Fields:

    • HTTP Streamable URL: The HTTP endpoint that supports streaming responses (e.g., http://localhost:3001/stream).
    • Additional Headers: Optional headers to send with requests, formatted as name:value, one per line.
  6. Pass Environment Variables to MCP Servers

    main

    When using Command-line Based Transport (STDIO), you can provide environment variables to the MCP server in two ways:

    1. Credentials UI: Add variables directly in the n8n credentials configuration using NAME=VALUE format. This is ideal for individual setups and secure storage.
    2. Docker Environment Variables: In Docker deployments, prefix your variables with MCP_. These will be automatically passed to the MCP servers when executed.

    Example Docker Configuration:

    services:
      n8n:
        image: n8nio/n8n
        environment:
          - MCP_BRAVE_API_KEY=your-api-key-here
          - MCP_OPENAI_API_KEY=your-openai-key-here
  7. Configure Command-line Based Transport (STDIO)

    main

    Use the STDIO transport to start and interact with an MCP server directly via the command line. This is useful for local servers.

    Credential Fields:

    • Command: The command to start the MCP server (e.g., npx).
    • Arguments: Optional arguments to pass to the server command (e.g., -y @modelcontextprotocol/server-brave-search).
    • Environment Variables: Variables to pass to the server in NAME=VALUE format.
  8. Verify Tool Schema Integrity with Unit Tests

    main

    When implementing or verifying the fix for schema truncation, use a unit test to ensure that complex, nested JSON Schemas are returned identically to how they were received from the MCP server. The test should assert that the schema property in the output matches the inputSchema of the mock tool, including nested properties, formats, and required fields.

    import { McpClient } from '../nodes/McpClient/McpClient.node';
    // ...other imports
    
    describe('MCP Client Tool Schema Serialization', () => {
      it('should return the full, original JSON Schema for a tool', async () => {
        const originalSchema = {
          type: 'object',
          properties: {
            queries: {
              type: 'array',
              items: {
                type: 'object',
                properties: {
                  fg_id: { type: 'string', format: 'uuid' },
                  emailExact: { type: 'string', format: 'email' },
                  // ...more fields
                },
                required: ['fg_id'],
                additionalProperties: false
              }
            }
          },
          required: ['queries'],
          additionalProperties: false
        };
    
        // Mock the MCP server/tools response
        const fakeTool = {
          name: 'fg_findPersons',
          description: 'Find persons based on an array of criteria.',
          inputSchema: originalSchema
        };
    
        // Simulate your listTools logic here
        const aiTools = [fakeTool].map(tool => ({
          name: tool.name,
          description: tool.description,
          schema: tool.inputSchema
        }));
    
        // Assert the schema is identical
        expect(aiTools[0].schema).toEqual(originalSchema);
      });
    });
  9. Use MCP Client Operations

    main

    The MCP Client node supports the following operations to interact with MCP servers:

    • Execute Tool: Execute a specific tool with provided parameters.
    • Get Prompt: Retrieve a specific prompt template.
    • List Prompts: Retrieve a list of all available prompts.
    • List Resources: Retrieve a list of all available resources from the MCP server.
    • List Tools: Retrieve a list of all available tools (including names, descriptions, and parameter schemas).
    • Read Resource: Read the content of a specific resource by its URI.
  10. Register n8n-nodes-mcp nodes and credentials

    main

    The n8n-nodes-mcp package exports a module structure required for n8n to discover and load its nodes and credential types. When integrating this package into an n8n instance, the following types are available:

    Nodes

    • mcpClient: The primary MCP Client node.

    Credential Types

    • mcpClientApi: Credentials for standard API access.
    • mcpClientSseApi: Credentials for Server-Sent Events (SSE) transport.
    • mcpClientHttpApi: Credentials for HTTP streamable transport.