Files
n8n-mcp-server/src/tools/execution/handler.ts
rhys-041 c01a97b6fb Add run_webhook tool for executing workflows via webhooks
This commit adds a new MCP tool, run_webhook, which allows executing n8n workflows via webhooks instead of using the API directly. This implementation:

1. Creates a new RunWebhookHandler class that:
   - Takes a workflowName parameter and automatically prepends "webhook/" to create the full path
   - Uses basic authentication from environment variables
   - Makes HTTP requests to the webhook endpoints and returns the responses

2. Adds new environment variables:
   - N8N_WEBHOOK_USERNAME: Username for webhook basic authentication
   - N8N_WEBHOOK_PASSWORD: Password for webhook basic authentication

3. Updates server configuration and handlers to register and expose the new tool

4. Adds comprehensive documentation in:
   - execution-tools.md with examples and schema
   - README.md with configuration and usage information

This provides a simpler alternative to the API-based workflow execution, allowing Claude to trigger n8n webhooks directly.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-27 15:32:10 -04:00

65 lines
1.7 KiB
TypeScript

/**
* Execution Tools Handler
*
* This module handles calls to execution-related tools.
*/
import { ToolCallResult } from '../../types/index.js';
import { McpError } from '@modelcontextprotocol/sdk/types.js';
import { ErrorCode } from '../../errors/error-codes.js';
import { N8nApiError, getErrorMessage } from '../../errors/index.js';
import {
ListExecutionsHandler,
GetExecutionHandler,
DeleteExecutionHandler,
RunWebhookHandler
} from './index.js';
/**
* Handle execution tool calls
*
* @param toolName Name of the tool being called
* @param args Arguments passed to the tool
* @returns Tool call result
*/
export default async function executionHandler(
toolName: string,
args: Record<string, any>
): Promise<ToolCallResult> {
try {
// Route to the appropriate handler based on tool name
switch (toolName) {
case 'list_executions':
return await new ListExecutionsHandler().execute(args);
case 'get_execution':
return await new GetExecutionHandler().execute(args);
case 'delete_execution':
return await new DeleteExecutionHandler().execute(args);
case 'run_webhook':
return await new RunWebhookHandler().execute(args);
default:
throw new McpError(
ErrorCode.NotImplemented,
`Unknown execution tool: '${toolName}'`
);
}
} catch (error) {
// Get appropriate error message
const errorMessage = getErrorMessage(error);
return {
content: [
{
type: 'text',
text: `Error executing execution tool: ${errorMessage}`,
},
],
isError: true,
};
}
}