Token导航 LogoToken导航TokenDH.com
P2p Anomaly Detection Foundry Agent logo
AI代理stdio官方级别未说明来源级核验

P2p Anomaly Detection Foundry Agent

MCP Server

一款基于Azure AI Foundry的采购到支付异常检测工具,通过分析发票图像识别采购异常和合规问题。

工具数

0

提示词数

0

GitHub Stars

6

资源数

0
PythonAI代理工作流自动化

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

MSFT-Innovation-Hub-India

提供方

MSFT-Innovation-Hub-India

最后核验

2026/5/17 20:20

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

命令预览

pip install -r requirements.txt

详细介绍

P2P异常检测代理

使用Azure AI Foundry代理服务进行采购到支付异常检测的应用程序。此工具分析采购发票图像,以检测采购异常和合规问题。它展示的Azure AI Foundry的关键功能是:

  • 仅使用自然语言指令执行多步业务流程的代理服务。应用程序的代码中几乎没有业务逻辑。
  • 在多步骤业务流程中调用各种自动化工具操作。它展示了最近宣布的与MCP服务器、Azure Logic Apps的交钥匙集成。此外,它还使用a)使用gpt-4o模型对图像进行视觉推理,b)交钥匙矢量搜索,c)推理以应用业务规则并执行p2p异常检测。
  • Azure AI Foundry中的代理服务能够:

- 最重要的是,业务流程中的所有步骤都是通过以下方式执行的 一个电话 从应用程序到代理服务。代理人精心安排这些步骤 自主地,根据中的说明 自然语言。客户端应用程序本身包含 没有业务逻辑 在代码中。 - 处理应用程序状态 在不同的工具调用之间。它决定从一个工具调用的输出中提取什么作为输入传递给下一个工具。 - 编排业务流程 从头到尾,对它们进行排序,以实现业务目标。

解决方案的架构

下面显示的是解决方案的架构。客户端应用程序是一个基于python的控制台应用程序,它调用在Azure AI Foundry代理服务中运行的P2P异常检测代理。

alt text

解决方案组件

序号实体实体目的或描述
1铸造代理端到端实现P2P异常检测过程的自治代理。它通过单个API调用来编排业务流程中的所有步骤,处理不同工具调用之间的应用程序状态,并对操作进行排序,以实现业务目标。
2视觉推理工具调用使用GPT-4o的视觉功能分析输入的采购发票图像,以提取采购发票标题、发票行、供应商ID和合同ID。然后,在后续调用Logic App进行合同验证时使用这些提取的信息。
3Azure逻辑应用程序工具调用代理通过HTTP触发器调用,从发票中提取供应商ID和合同ID。通过在Azure SQL数据库上执行动态SQL查询返回匹配的合同数据,提供合同标题和行项目详细信息以与发票进行比较。
4矢量搜索工具调用检索应用于企业环境中P2P异常检测的业务规则。使用配置的矢量存储搜索上传的业务规则文档(p2p rules.txt),以查找相关的合规性和验证标准
5推理应用检索到的业务规则根据合同数据评估采购发票。确定是否存在异常或合规问题,并生成一份详细的验证报告,其中包含判断、比较和建议。
6MCP服务器工具调用将生成的报告编目并存储在Azure Blob存储中。MCP服务器实现了特定于协议的实现,以连接到Blob存储服务,执行生命周期操作,如创建容器、列出容器和上传Blob。代理使用MCP服务器将降价验证报告上传到指定的容器。

该解决方案的一些关键方面是:

  1. 只有 从客户端应用程序调用Azure AI Foundry中的代理。其余步骤由代理服务自主执行。下面的代码片段显示了客户端应用程序将用户提供的发票图像和用户指令传递给Azure AI Foundry中的代理。
  2. 请注意,代码中没有任何业务逻辑,也没有编写代码来执行任何工具操作。这些是由代理人自己自主完成的。

def process_anomaly_detection(self, user_prompt: str, image_path: str, verbose: bool = True) -> str:
        """
        Process the anomaly detection request with proper error handling and retry logic.
        
        Args:
            user_prompt: User's prompt for anomaly detection
            image_path: Path to the invoice image
            
        Returns:
            Response from the AI agent
            
        Raises:
            Exception: For various processing errors
        """
        try:
            # Create message content
            self._show_progress("Creating message content...", verbose)
            content_blocks = self.create_message_content(user_prompt, image_path)
            
            # Create a new thread for this conversation
            self._show_progress("Creating conversation thread...", verbose)
            thread = self.client.threads.create()
            logger.info(f"Created thread, ID: {thread.id}")
            
            # Create the message
            self._show_progress("Sending message to agent...", verbose)
            message = self.client.messages.create(
                thread_id=thread.id, 
                role="user", 
                content=content_blocks
            )
            
            # Run the agent with proper error handling
            self._show_progress("Processing with AI agent (this may take a moment)...", verbose)
            run = self.client.runs.create_and_process(
                thread_id=thread.id, 
                agent_id=self.agent.id
            )
            
            if run.status == "failed":
                error_msg = f"Agent run failed: {run.last_error}"
                logger.error(error_msg)
                raise Exception(error_msg)
            
            # Retrieve and process messages
            self._show_progress("Retrieving analysis results...", verbose)
            messages = self.client.messages.list(
                thread_id=thread.id, 
                order=ListSortOrder.ASCENDING
            )
            
            # Extract the agent's response
            agent_response = ""
            for message in messages:
                if message.role == "assistant" and message.text_messages:
                    agent_response = message.text_messages[-1].text.value
                    break
            
            if not agent_response:
                raise Exception("No response received from the agent")
            
            self._show_progress("Analysis complete!", verbose)
            logger.info("Successfully processed anomaly detection request")
            return agent_response
            
        except Exception as e:
            logger.error(f"Error processing anomaly detection: {e}")
            raise

先决条件

  • Python 3.12+
  • 已配置Azure AI代理服务
  • Azure凭据(托管身份、服务主体或交互)

在Azure AI Foundry中为代理创建项目

请参阅记录的步骤 这里 在Azure AI Foundry中创建项目。我们需要项目的连接字符串来完成以下步骤。 接下来,部署代理可以使用的gpt-4o模型(在这里的应用程序中使用)。

使用MCP工具集成创建代理

在上面创建的项目中,使用以下脚本在Azure AI Foundry中创建代理。将整个块复制并粘贴到您的bash终端中。我已经向代理提供了虚拟指令,一旦创建,我将对其进行更新。

*备注*:在此处配置MCP服务器之前,必须先运行它。在我的例子中,它托管在Azure容器应用程序中。此MCP服务器中托管的代码的GitHub Repo可用 这里

首先执行az登录,然后执行以下操作

AZURE_AI_FOUNDRY_PROJECT_ENDPOINT=""https://p2p-anomaly-detection-a-resource.services.ai.azure.com/api/projects/p2p-anomaly-detection-agent"
API_VERSION="2025-05-15-preview"
echo "Getting access token..."
AGENT_TOKEN=$(az account get-access-token --resource 'https://ai.azure.com' --query accessToken --output tsv)
echo "Token obtained, creating agent..."
curl --request POST \
  --url "$AZURE_AI_FOUNDRY_PROJECT_ENDPOINT/assistants?api-version=$API_VERSION" \
  -H "Authorization: Bearer $AGENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
      "instructions": "You are a customer support chatbot. Use the tools provided and your knowledge base to best respond to customer queries.",
      "tools": [
          {
              "type": "mcp",
              "server_label": "azure_storage_operations_mcp",
              "server_url": "https://mcp-server-az-storage-svc.wonderfulsea-77230f8f.southindia.azurecontainerapps.io/sse",
              "require_approval": "never"
          }
      ],
      "name": "p2p-anomaly-detection-agent",
      "model": "gpt-4o"
  }'

在撰写本文时,MCP工具与Azure AI Foundry中的代理服务的集成需要通过REST API调用完成。配置后,它将显示在代理上,如下所示。

alt text

*备注*:创建后,承载代理的项目的连接字符串/端点以及代理ID需要存储在应用程序的.env文件中。见下文。

az_agentic_ai_service_connection_string="https://p2p-anomaly-detection-a-resource.services.ai.azure.com/api/projects/p2p-anomaly-detection-agent"
az_assistant_id = "asst_EZdyBrR1TXOk8ynapJLUvMFY"
LOG_LEVEL=INFO

代理说明

导航到Azure AI Foundry,选择上面创建的代理,并更新如下所示的说明,以执行P2P异常检测。

This is a Procure to Pay process. You will be provided with the Purchase Invoice image as input.

Note the sequence of execution you must adhere to strictly:
- Step 2 must be performed only after Step 1 is performed
- Step 3 below must be performed only after Step 1 and Step 2 are completed.
- Step 5 must be performed only after Step 1, Step 2, Step 3 and Step 4 have been performed successfully.
- Step 6 must be performed only after Step 5 is performed

Step 1: As a first step, you will extract the Contract ID and Supplier ID from the Purchase Invoice image along with all the line items from the Invoice in the form of a table.
Step 2: You will then use the function tool by passing the Contract ID and Supplier ID to retrieve the contract details.
Step 3: You will then use the file search tool to retrieve the business rules applicable to detection of anomalies in the Procure to Pay process.
Step 4: Then, apply the retrieved business rules to match the invoice line items with the contract details fetched from the system, and detect anomalies if any.
Step 5: Prepare a detailed 'p2p verification report' in markdown with the following content:
 - Verdict: Whether the Purchase Invoice complies with the Contract?
 - Purchase Invoice details: Invoice Header and Invoice Lines, in Markdown Table Format
 - Contract Details: Contract Header and Contract Lines, in Markdown Table Format
 - P2P Business Rules: The Rules that were retrieved using the File Search Tool
 - Reasoning behind the verdict:  Provide a detailed reasoning why you think the Invoice aligns with the Contract yes or no. Use a Markdown Table format to compare each item in the Invoice with the Contract and indicate the basis for your judgement. Use icons/images to embellish the report and make it easy for comprehension by Business users. They must be able to quickly give this a once over and commit the Invoice into the System
Step 6: You will use the MCP tool available with you to upload the 'p2p verification report' created in Step 5. 
 - Choose a container with name 'p2p-anomaly-detection-outcomes' to upload to. If the container with this name does not exist, create one. 
 - The name of the Report must start with the Invoice Number, appended with a hyphen and appended with a guid. E.g. Invoice001-001.md
 - Secure the name of the Report document uploaded to the Blob Storage account through the MCP Tool
Step 7: Wait till Step 6 completes, then return the content of the Markdown document that was uploaded to Blob Storage.

alt text

使用上述步骤创建代理后,可以直接在Azure AI Foundry中执行剩余的工具配置。当然,您可以使用SDK通过代码执行这些步骤,但我选择在本文中保持简单。

创建和配置Vector存储

上传 p2p-rules.txt 作为Agent的知识源来创建向量存储。此文件包含适用于采购到付款流程中异常检测的业务规则。创建后,它应该显示如下:

alt text

将Azure Logic应用程序配置为代理的工具操作

在此处配置之前,必须先创建逻辑应用程序。创建可以通过REST API调用触发的Azure Logic应用程序。Logic App应该实现一个HTTP请求触发器,并将最后一个操作作为HTTP响应。

在本文涵盖的用例中,Logic App用于根据从采购发票映像中检索到的输入合同ID和供应商ID,从Azure SQL数据库中检索合同标题和合同行信息

创建Logic应用程序

注: Logic App中的所有操作都必须配置为同步运行。 请参阅文档 这里 了解更多详情。

为了运行此示例,请创建一个类似于下面描述的逻辑应用程序。Logic App获取ContractID和SupplierID。它在Azure SQL数据库上使用这些参数动态运行T-SQL查询,并返回合约头和行信息。

HTTP触发器中使用的有效载荷的模式是:

{
    "properties": {
        "ContractID": {
            "type": "string"
        },
        "SupplierID": {
            "type": "string"
        }
    },
    "type": "object"
}

以下是Logic应用程序流的屏幕截图:

logicapp

Logic App操作应禁用异步模式。见下文:

alt text

将Logic App配置为Agent的工具操作

使用Azure AI Foundry门户将Logic App配置为P2P代理的工具操作。见下文:

alt text

设置

  1. 再进行
   pip install -r requirements.txt
  1. 配置环境
   cp .env.template .env
   # Edit .env with your Azure configuration
  1. 设置Azure身份验证

- 地方发展:使用 az login 用于交互式身份验证 - 用于生产:配置托管身份或服务主体

用法

交互模式

运行主脚本并按照提示操作:

python p2pagent.py

脚本将要求:

  • 发票图像的路径
  • 分析提示(或使用默认值)

运行输出示例:

(.venv) PS C:\Users\sansri\agentic-ai-service-samples\p2p-anomaly-detection-agent> python p2pagent.py
2025-07-04 09:04:22,149 - __main__ - INFO - Successfully initialized Azure AI Agents client
2025-07-04 09:04:22,149 - INFO - Successfully initialized Azure AI Agents client

=== P2P Anomaly Detection Agent ===
This tool analyzes invoice images for procurement anomalies.

Enter the path to your invoice image: data_files/Invoice-002.png

Enter your analysis prompt (or press Enter for default):
can you perform the procure to pay anomaly detection based on the instructions you have been provided with and give me a detailed response if this Purchase Invoice Image attached aligns with the Contract?

Processing image: C:\Users\sansri\agentic-ai-service-samples\p2p-anomaly-detection-agent\data_files\Invoice-002.png
Analyzing with Azure AI Agent...
🔄 Creating message content...
2025-07-04 09:04:46,774 - __main__ - INFO - Successfully converted image to base64: C:\Users\sansri\agentic-ai-service-samples\p2p-anomaly-detection-agent\data_files\Invoice-002.png
2025-07-04 09:04:46,774 - INFO - Successfully converted image to base64: C:\Users\sansri\agentic-ai-service-samples\p2p-anomaly-detection-agent\data_files\Invoice-002.png
2025-07-04 09:04:46,777 - __main__ - INFO - Successfully created message content blocks
2025-07-04 09:04:46,777 - INFO - Successfully created message content blocks
🔄 Creating conversation thread...
2025-07-04 09:04:47,231 - __main__ - INFO - Created thread, ID: thread_Lz8ViyvcnDMG9i9TGkgE7jq6
2025-07-04 09:04:47,231 - INFO - Created thread, ID: thread_Lz8ViyvcnDMG9i9TGkgE7jq6
🔄 Sending message to agent...
🔄 Processing with AI agent (this may take a moment)...
🔄 Retrieving analysis results...
🔄 Analysis complete!
2025-07-04 09:05:55,543 - __main__ - INFO - Successfully processed anomaly detection request
2025-07-04 09:05:55,543 - INFO - Successfully processed anomaly detection request

==================================================
ANOMALY DETECTION RESULTS
==================================================
### P2P Verification Report

#### **Verdict:**
The Purchase Invoice **does not comply** with the Contract.

---

#### **Purchase Invoice Details**

| **Attribute**        | **Value**                                                                    |
|-----------------------|------------------------------------------------------------------------------|
| **Invoice Number**    | INV001002                                                                   |
| **Contract Reference**| CON000002                                                                   |
| **Supplier ID**       | SUP0008                                                                     |
| **Total Invoice Value** | 113130.16 USD                                                              |
| **Invoice Date**      | 2023-06-15                                                                  |

---

#### **Invoice Line Items**

| **Item ID** | **Quantity** | **Unit Price (USD)** | **Total Price** | **Description**                       |
|-------------|--------------|-----------------------|-----------------|---------------------------------------|
| ITEM0040    | 116          | $136.75              | $15863.00       | Description for ITEM0040              |
| ITEM0082    | 116          | $554.62              | $64335.92       | Description for ITEM0082              |
| ITEM0011    | 36           | $398.09              | $14331.24       | Description for ITEM0011              |
| ITEM0031    | 36           | $475.00              | $17100.00       | Description for ITEM0031              |
| ITEM9999    | 10           | $150.00              | $1500.00        | Extra item not in contract            |

---

#### **Contract Details**

| **Attribute**                  | **Value**                                                                    |
|--------------------------------|------------------------------------------------------------------------------|
| **Contract ID**                | CON000002                                                                   |
| **Supplier ID**                | SUP0008                                                                     |
| **Contract Date**              | 2022-10-19                                                                  |
| **Expiration Date**            | 2023-01-07                                                                  |
| **Contract Total Amount**      | 66543.39 USD                                                                |
| **Status**                     | Expired                                                                     |
| **Currency**                   | USD                                                                         |

| **Item ID** | **Quantity** | **Unit Price (USD)** | **Total Price** | **Delivery Date** | **Description**            |
|-------------|--------------|-----------------------|-----------------|-------------------|----------------------------|
| ITEM0040    | 78           | $136.75              | $10666.50       | 2023-01-01        | Description for ITEM0040   |
| ITEM0082    | 57           | $479.87              | $27352.58       | 2022-11-26        | Description for ITEM0082   |
| ITEM0011    | 21           | $398.09              | $8359.89        | 2022-11-29        | Description for ITEM0011   |
| ITEM0031    | 47           | $429.03              | $20164.41       | 2022-12-09        | Description for ITEM0031   |

---

#### **P2P Business Rules**
- Invoice date in Purchase Invoice must be within the Contract term【6:0†source】.
- The Contract must be valid; ensure it is not expired【6:0†source】.
- Invoice Total Value should stay within the Contract value【6:0†source】.
- Items in the Invoice must be strictly from the Contract item list, with correct quantities, descriptions, and unit prices【6:0†source】.
- Minor rounding differences in amounts should be ignored【6:0†source】.

---

|------------------|-------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| ITEM0040         | Quantity: 78, Unit Price: $136.75               | Invoice quantity exceeds Contract quantity.                                                                |
|------------------|-------------------------------------------------|------------------------------------------------------------------------------------------------------------|
| ITEM0040         | Quantity: 78, Unit Price: $136.75               | Invoice quantity exceeds Contract quantity.                                                                |
| ITEM0082         | Quantity: 57, Unit Price: $479.87               | Invoice unit price differs and quantity exceeds Contract quantity.                                        |
| ITEM0011         | Quantity: 21, Unit Price: $398.09               | Invoice quantity exceeds Contract quantity.                                                                |
| ITEM0031         | Quantity: 47, Unit Price: $429.03               | Invoice quantity exceeds Contract quantity and Contract total is lower than Invoice total.                 |
| ITEM9999         | Not listed in Contract                          | Extra item not defined in Contract—anomalous.                                                             |

### Additional Observations:
- **Contract is Expired:** The Contract expired on 2023-01-07. Invoice dated 2023-06-15 falls outside the Contract's validity term.
- **Invoice exceeds Contract Value:** Contract value capped at $66543.39 USD, while Invoice totals $113130.16 USD.
- **Currency Matches:** Invoice and Contract both indicate prices in USD.

---

### Professional Guidance and Suggested Next Steps:
- The anomalies detected prevent this Invoice from fully complying with the Contract terms.
- **Recommendation:** Rectify discrepancies (quantities, unit price adjustments, or removal of extra items) and adhere to valid Contract before committing Invoice to the System.

---

### Uploading Report Content
Proceeding to upload this Markdown report into Blob Storage.
==================================================

导航到Azure门户和与MCP服务器关联的存储帐户。上传代理的P2P异常检测过程的输出。见下文。

alt text

目录标签

目录标签

PythonAI代理工作流自动化采购异常检测本地部署合规审计发票分析自动化流程

接入字段

传输方式(transport,传输协议)

stdio

鉴权方式(authType,认证方式)

token

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

stdiotoken部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

来源信息

继续浏览同类 MCP