Skip to content

.NET: Expose workflows as MCP tools when hosting on Azure functions#4768

Open
kshyju wants to merge 8 commits intomainfrom
shkr/exposw_wf_as_tool
Open

.NET: Expose workflows as MCP tools when hosting on Azure functions#4768
kshyju wants to merge 8 commits intomainfrom
shkr/exposw_wf_as_tool

Conversation

@kshyju
Copy link
Contributor

@kshyju kshyju commented Mar 18, 2026

Motivation and Context

This PR adds support for exposing durable workflows as MCP (Model Context Protocol) tools in Microsoft.Agents.AI.Hosting.AzureFunctions. This mirrors the existing agent MCP tool support (AddAIAgent(enableMcpToolTrigger: true)) but for workflows.

When enabled, the Azure Functions host automatically registers an MCP tool trigger for the workflow at the /runtime/webhooks/mcp endpoint, allowing MCP-compatible clients to
discover and invoke the workflow as a tool.

Usage

builder.ConfigureDurableWorkflows(workflows =>
{
    workflows.AddWorkflow(myWorkflow, exposeStatusEndpoint: false, exposeMcpToolTrigger: true);
});

New Public APIs

Type Member Description
DurableWorkflowOptionsExtensions AddWorkflow(DurableWorkflowOptions, Workflow, bool exposeStatusEndpoint, bool exposeMcpToolTrigger) New overload that configures MCP tool trigger for the workflow

Implementation details

  • FunctionsDurableOptions — Tracks which workflows have MCP tool triggers enabled (internal, mirrors status endpoint pattern)
  • FunctionMetadataFactory.CreateWorkflowMcpToolTrigger — Generates function metadata using System.Text.Json.Nodes.JsonObject (AOT-safe, no manual string escaping)
  • BuiltInFunctions.RunWorkflowMcpToolAsync — Schedules an orchestration, waits for completion, validates RuntimeStatus, and returns the DurableWorkflowResult.Result
  • BuiltInFunctionExecutor — Routes the new entry point to RunWorkflowMcpToolAsync
  • DurableWorkflowsFunctionMetadataTransformer — Registers the MCP tool trigger when enabled
  • FunctionsApplicationBuilderExtensions — Adds the new entry point to the middleware filter

Sample app

Added 04_WorkflowMcpTool under dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/ with two workflows:

  • Translate — Simple string-in/string-out workflow (TranslateText → FormatOutput)
  • OrderLookup — Returns a POCO (LookupOrder → EnrichOrder → OrderSummary)

Tests

  • Unit tests (FunctionMetadataFactoryTests.cs) — 7 tests covering all factory methods, including JSON validity validation via JsonDocument.Parse()
  • Integration test (WorkflowSamplesValidation.cs) — Connects via MCP client (HttpClientTransport), lists tools, invokes both workflows, and asserts results

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings March 18, 2026 21:28
@kshyju kshyju changed the title Expose workflows as MCP tools when hosting on Azure functions. Expose workflows as MCP tools when hosting on Azure functions Mar 18, 2026
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation .NET labels Mar 18, 2026
@kshyju kshyju changed the title Expose workflows as MCP tools when hosting on Azure functions .NET: Expose workflows as MCP tools when hosting on Azure functions Mar 18, 2026
@kshyju kshyju added the azure-functions Issues and PRs related to Azure Functions label Mar 18, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Exposes durable workflows hosted in Azure Functions as MCP (Model Context Protocol) tools, enabling MCP clients to discover and invoke workflows through the Functions MCP webhook endpoint.

Changes:

  • Add MCP tool trigger registration via function metadata transformation + built-in executor support.
  • Add unit + integration tests validating MCP tool metadata and end-to-end tool invocation.
  • Add a new Azure Functions sample demonstrating two workflows exposed as MCP tools.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/Workflows/DurableWorkflowsFunctionMetadataTransformer.cs Registers MCP tool trigger functions per workflow when enabled in options.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/Workflows/DurableWorkflowOptionsExtensions.cs Adds an AddWorkflow overload to opt into status endpoint and/or MCP tool trigger.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/FunctionsDurableOptions.cs Tracks which workflows enable MCP tool triggers.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/FunctionsApplicationBuilderExtensions.cs Ensures middleware/executor registration covers MCP tool entry point.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/FunctionMetadataFactory.cs Creates function metadata for MCP tool trigger functions.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/BuiltInFunctions.cs Implements RunWorkflowMcpToolAsync to run workflows via MCP invocation.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/BuiltInFunctionExecutor.cs Dispatches MCP tool invocations to the new built-in entry point.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/CHANGELOG.md Notes the new MCP tool trigger support in Unreleased.
dotnet/tests/Microsoft.Agents.AI.Hosting.AzureFunctions.UnitTests/FunctionMetadataFactoryTests.cs Adds unit tests for MCP tool trigger metadata generation.
dotnet/tests/Microsoft.Agents.AI.Hosting.AzureFunctions.IntegrationTests/WorkflowSamplesValidation.cs Adds an integration test that lists and calls MCP tools exposed by the host.
dotnet/samples/04-hosting/DurableWorkflows/README.md Lists the new MCP tool sample.
dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/04_WorkflowMcpTool/* New sample app demonstrating workflows exposed as MCP tools.
dotnet/agent-framework-dotnet.slnx Includes the new sample project in the solution.

@kshyju kshyju requested a review from Copilot March 18, 2026 22:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds opt-in support for exposing Durable Workflows hosted in Microsoft.Agents.AI.Hosting.AzureFunctions as MCP (Model Context Protocol) tools, enabling MCP clients to discover and invoke workflows via the Functions MCP webhook endpoint.

Changes:

  • Introduces workflow-level configuration (exposeMcpToolTrigger) and internal tracking to register MCP tool triggers for selected workflows.
  • Adds MCP tool trigger function metadata generation and a built-in executor path that runs a workflow and returns its serialized result.
  • Adds unit/integration coverage plus a new Azure Functions sample demonstrating MCP tool invocation for workflows.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/Workflows/DurableWorkflowsFunctionMetadataTransformer.cs Registers MCP tool-trigger functions for workflows when enabled.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/Workflows/DurableWorkflowOptionsExtensions.cs Adds AddWorkflow(..., exposeMcpToolTrigger) overload to enable MCP triggers.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/FunctionsDurableOptions.cs Tracks which workflows have MCP tool triggers enabled.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/FunctionsApplicationBuilderExtensions.cs Ensures built-in middleware/executor routing includes the MCP workflow entry point.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/FunctionMetadataFactory.cs Adds factory method to generate MCP tool trigger bindings for workflows.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/BuiltInFunctions.cs Adds RunWorkflowMcpToolAsync to schedule and await workflow completion for MCP calls.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/BuiltInFunctionExecutor.cs Routes the new MCP workflow entry point to the corresponding built-in handler.
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/CHANGELOG.md Adds an unreleased changelog entry for workflow MCP tool trigger support.
dotnet/tests/Microsoft.Agents.AI.Hosting.AzureFunctions.UnitTests/FunctionMetadataFactoryTests.cs New unit tests validating function metadata factory output (incl. MCP workflow trigger).
dotnet/tests/Microsoft.Agents.AI.Hosting.AzureFunctions.IntegrationTests/WorkflowSamplesValidation.cs Adds an integration test that connects via MCP, lists tools, and invokes workflow tools.
dotnet/samples/04-hosting/DurableWorkflows/README.md Links the new workflow MCP tool sample from the samples index.
dotnet/samples/04-hosting/DurableWorkflows/AzureFunctions/04_WorkflowMcpTool/* New Azure Functions sample app demonstrating two workflows exposed as MCP tools.
dotnet/agent-framework-dotnet.slnx Adds the new sample project to the solution.

if (registeredFunctions.Add(mcpToolFunctionName))
{
this._logger.LogRegisteringWorkflowTrigger(workflow.Key, mcpToolFunctionName, "mcpTool");
original.Add(FunctionMetadataFactory.CreateWorkflowMcpToolTrigger(workflow.Key, workflow.Value.Description));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for an agent and a workflow to have the same key value? And if so, does that mean it's possible to have a duplicate MCP function name if both enable MCP triggering?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, this fails during worker indexing (at app startup). The resulting error message is not very helpful for the user.

Error indexing method 'Functions.dafx-Joker'. Microsoft.Azure.WebJobs.Host: Method overloads are not supported. There are multiple methods with the name 'Host.Functions.dafx-Joker'."

The root cause is that we use the same prefix (dafx-) for both agents and workflows, which leads to duplicate function names. This issue also exists for HTTP triggers when an agent and a workflow share the same name.

One potential solution is to use a different prefix for workflows (e.g., dafxw-). We can address that separately once we decide on the approach. Let me follow up offline on this.

While validating this scenario, I also discovered another bug where having both workflows and agents together did not behave as expected in azure functions hosting scenario. I’ve fixed this issue and added a new sample to demonstrate the correct behavior.

@kshyju kshyju requested a review from ahmedmuhsin March 19, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

azure-functions Issues and PRs related to Azure Functions documentation Improvements or additions to documentation .NET

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants