#
# This workflow demonstrates the InvokeMcpTool declarative action.
#
# InvokeMcpTool lets a workflow author call a tool exposed by a Model Context
# Protocol (MCP) server directly from YAML without writing any Python glue.
# It can:
#
#   - dispatch a tool call against an MCP server (with optional auth headers),
#   - store the parsed tool result in a workflow variable, and
#   - add the result to the conversation so a downstream agent can answer
#     questions based on it.
#
# This sample calls ``microsoft_docs_search`` on the public Microsoft Learn
# Docs MCP server (no authentication required) and uses a Foundry agent to
# answer a single question about Microsoft technology using the search
# results.
#
# Example inputs (Choose one or provide yours):
#   How do I configure logging in the Agent Framework?
#   Gpt-5.4-mini
#
# Workflow inputs (set by the host via ``workflow.run({...})``):
#   text:            The user's question (required).
#   requireApproval: Optional bool. When true, the MCP tool call pauses for
#                    human approval before contacting the server. Defaults
#                    to false when omitted.
#
kind: Workflow
trigger:

  kind: OnConversationStart
  id: workflow_invoke_mcp_tool_demo
  actions:

    # Capture the user's question into a local variable so the MCP tool call
    # can pass it as an argument.
    - kind: SetVariable
      id: capture_query
      variable: Local.SearchQuery
      value: =Workflow.Inputs.text

    # Invoke microsoft_docs_search on the Microsoft Learn Docs MCP server.
    # The result is parsed into Local.SearchResults and also added to the
    # conversation (via conversationId) so the agent below can answer the
    # user's question based on it.
    #
    # ``requireApproval`` reads from Workflow.Inputs so the host can toggle
    # the human-approval flow without editing this YAML. When the input is
    # absent or evaluates to a falsy value, the tool runs without pausing.
    - kind: InvokeMcpTool
      id: search_docs
      conversationId: =System.ConversationId
      serverUrl: https://learn.microsoft.com/api/mcp
      serverLabel: MicrosoftLearnDocs
      toolName: microsoft_docs_search
      requireApproval: =Workflow.Inputs.requireApproval
      arguments:
        query: =Local.SearchQuery
      output:
        autoSend: false
        result: Local.SearchResults

    # Use the agent to answer the user's question using the conversation
    # context (which now contains the MCP search results). The user's
    # question is supplied via ``input.messages`` (sourced from the workflow
    # inputs), and the prior conversation history is bound via
    # ``conversationId``.
    - kind: InvokeAzureAgent
      id: answer_question
      conversationId: =System.ConversationId
      agent:
        name: DocsAgent
      input:
        messages: =Workflow.Inputs.text
      output:
        autoSend: true
        messages: Local.AgentResponse
