Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create an MCP server that can accept calls from AI agent tools like n8n?
#3
MCP server example in LA.
The code is from https://devblogs.microsoft.com/blog/micr...t-protocol
 
Code:
Copy      Help
// script "MCP_server.cs"
/*/ role exeProgram; outputPath %folders.Workspace%\exe\MCP_server; console true; nuget mcp\ModelContextProtocol; nuget mcp\Microsoft.Extensions.Hosting; /*/

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;

print.ignoreConsole = true;

//workaround for: does not find assemblies in runtimes\win\lib\net9.0
System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += (o, e) => o.LoadFromAssemblyPath(folders.ThisAppBS + $@"runtimes\win\lib\net9.0\{e.Name}.dll");

try {
    var builder = Host.CreateApplicationBuilder(args);
    builder.Logging.AddConsole();
    
    builder.Services
        .AddMcpServer()
        .WithStdioServerTransport()
        .WithToolsFromAssembly();
    
    builder.Build().Run();
}

catch (Exception ex) { print.it(ex); }

/// <summary>
///
Add your functions to this class like the example. They will be automatically used as MCP tools by MCP clients.
/// </summary>
[McpServerToolType]
public static class EchoTool {
    [
McpServerTool, Description("Echoes the message back to the client.")]
    public static string Echo(string message) => $"Hello from C#: {message}";
}

Compile. Copy full path of the exe file. Paste in Command field in MCP client settings (more info below). MCP client will run the exe.

To use in n8n, need the community MCP Client, not the built-in MCP Client. And locally installed n8n, not the cloud n8n.

n8n MCP Client node fields for testing this MCP server:
- Credentials: create credential where Command is full path of your exe file created when you compile the example script. Later just select the credential.
- Tool Name: Echo
- Tool Parameters: {"message":"test"}.


Messages In This Thread
RE: create an MCP server that can accept calls from AI agent tools like n8n? - by Gintaras - 05-13-2025, 01:54 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)