Files
agent-framework/dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/Workflows/DurableWorkflowOptionsExtensions.cs
T
Shyju Krishnankutty 49d69b3bf5 .NET: Expose workflows as MCP tools when hosting on Azure functions (#4768)
* Expose workflow as MCP Tool

* Expose workflow as MCP Tool

* Cleanup

* PR feedback fixes

* update changelog to include PR numner

* Improvements to error handling.

* Adding a sample project demonstrating how to setup Agents and Workflows together.

* Ensure duplicate agent registrations are properly handled.
2026-03-25 15:43:15 +00:00

58 lines
2.4 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Agents.AI.DurableTask.Workflows;
using Microsoft.Agents.AI.Workflows;
namespace Microsoft.Agents.AI.Hosting.AzureFunctions;
/// <summary>
/// Extension methods for <see cref="DurableWorkflowOptions"/> to configure Azure Functions HTTP trigger options.
/// </summary>
public static class DurableWorkflowOptionsExtensions
{
/// <summary>
/// Adds a workflow and optionally exposes a status HTTP endpoint for querying pending HITL requests.
/// </summary>
/// <param name="options">The workflow options to add the workflow to.</param>
/// <param name="workflow">The workflow instance to add.</param>
/// <param name="exposeStatusEndpoint">If <see langword="true"/>, a GET endpoint is generated at <c>workflows/{name}/status/{runId}</c>.</param>
public static void AddWorkflow(this DurableWorkflowOptions options, Workflow workflow, bool exposeStatusEndpoint)
{
ArgumentNullException.ThrowIfNull(options);
options.AddWorkflow(workflow);
if (exposeStatusEndpoint && options.ParentOptions is FunctionsDurableOptions functionsOptions)
{
functionsOptions.EnableStatusEndpoint(workflow.Name!);
}
}
/// <summary>
/// Adds a workflow and configures whether to expose a status HTTP endpoint and/or an MCP tool trigger.
/// </summary>
/// <param name="options">The workflow options to add the workflow to.</param>
/// <param name="workflow">The workflow instance to add.</param>
/// <param name="exposeStatusEndpoint">If <see langword="true"/>, a GET endpoint is generated at <c>workflows/{name}/status/{runId}</c>.</param>
/// <param name="exposeMcpToolTrigger">If <see langword="true"/>, an MCP tool trigger is generated for the workflow.</param>
public static void AddWorkflow(this DurableWorkflowOptions options, Workflow workflow, bool exposeStatusEndpoint, bool exposeMcpToolTrigger)
{
ArgumentNullException.ThrowIfNull(options);
options.AddWorkflow(workflow);
if (options.ParentOptions is FunctionsDurableOptions functionsOptions)
{
if (exposeStatusEndpoint)
{
functionsOptions.EnableStatusEndpoint(workflow.Name!);
}
if (exposeMcpToolTrigger)
{
functionsOptions.EnableMcpToolTrigger(workflow.Name!);
}
}
}
}