Portal Community

Automatic MCP Registration

When an agent is initialised, the AIFunctionToolRegistrar queries all active AI Functions for that agent and tenant, then registers each one as an MCPTool:

public class AIFunctionToolRegistrar
{
    public async Task RegisterForAgentAsync(
        Guid agentId, Guid tenantId,
        MCPToolRegistry registry,
        CancellationToken ct)
    {
        // Load all active AI Functions for this agent + shared tenant functions
        var functions = await _store.GetActiveForAgentAsync(agentId, tenantId, ct);

        foreach (var fn in functions.Where(f => f.InputSchema != null))
        {
            registry.Register(new MCPTool
            {
                Schema = new ToolDefinition
                {
                    Name        = fn.Name,
                    Description = fn.Description ?? fn.Name,
                    InputSchema = JsonDocument.Parse(fn.InputSchema!)
                },
                Handler = async (input, ctx, ct2) =>
                {
                    var result = await _runtime.ExecuteAsync(fn, input, new AIFunctionContext
                    {
                        AgentId  = ctx.AgentId,
                        UserId   = ctx.UserId,
                        TenantId = ctx.TenantId
                    }, ct2);

                    return JsonSerializer.Serialize(result);
                }
            });
        }
    }
}

Creating an AI Function MCP Tool — Step by Step

1
Write the Function Code Write a JavaScript execute(input) function that performs the desired logic.
2
Define Name and Description Give the function a unique name (snake_case) and a tool description that tells the LLM when to call it.
3
Define Input Schema Write the JSON Schema for the function's input parameters. This is what the LLM uses to construct tool call arguments.
4
Assign to Agent In the agents-app, assign the AI Function to the target agent (or leave AgentId null for tenant-wide availability).
5
Test via Chat Open a chat with the agent and trigger the function. Use the Context Inspector to verify the tool was called and the result injected.

AI Function vs. C# Tool: When to Use Each

ScenarioUse AI FunctionUse C# Tool
Data formatting / calculationYes — pure logic, no I/O neededOverkill
Database queryNo — no DB access in sandboxYes
External API callNo — no network in sandboxYes
Logic that admins may want to changeYes — no deployment neededNo — requires developer
Complex multi-service orchestrationNo — too complex for sandboxYes