共用方式為


將 App Service 應用程式整合為適用於 GitHub Copilot Chat 的 MCP 伺服器 (Spring Boot)

在本教學課程中,您將瞭解如何透過模型內容通訊協定 (MCP) 公開 Spring Boot Web 應用程式的功能、將其新增為 GitHub Copilot 的工具,以及在 Copilot 聊天代理程式中使用自然語言與您的應用程式互動。

此螢幕快照顯示 Azure App Service 中裝載的 GitHub Copilot 呼叫 Todos MCP 伺服器。

如果您的 Web 應用程式已經有有用的功能,例如購物、旅館預訂或資料管理,很容易就能讓這些功能可供使用:

藉由將 MCP 伺服器新增至 Web 應用程式,您即可讓代理程式瞭解及使用應用程式在回應使用者提示時的功能。 這表示您的應用程式可以執行的任何動作,代理程式也可以執行。

  • 將 MCP 伺服器新增至 Web 應用程式。
  • 在 GitHub Copilot 聊天代理程式模式中本機測試 MCP 伺服器。
  • 將 MCP 伺服器部署至 Azure App Service,並在 GitHub Copilot Chat 中連線到它。

Prerequisites

本教學課程假設您使用的是 教學課程:使用Linux和 Azure Cosmos DB 上的 Azure App Service 建置 Java Spring Boot Web 應用程式範例。

至少,在 GitHub Codespaces 中開啟 範例應用程式 ,並執行 azd up來部署應用程式。

將 MCP 伺服器新增至 Web 應用程式

  1. 在codespace中,開啟 pom.xml 並將 spring-ai-starter-mcp-server-webmvc 套件新增至您的專案:

    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
        <version>1.0.0</version>
    </dependency>
    
  2. 開啟 src/main/java/com/microsoft/azure/appservice/examples/springbootmongodb/TodoApplication.java。 為了簡單起見,您將在這裡新增所有 MCP 伺服器程式代碼。

  3. TodoApplication.java結尾,新增下列類別。

    @Service
    class TodoListToolService {
        private final TodoItemRepository todoItemRepository;
    
        public TodoListToolService(TodoItemRepository todoItemRepository) {
            this.todoItemRepository = todoItemRepository;
        }
    
        @Tool(description = "Get a todo item by its id")
        public Optional<TodoItem> getTodoItem(String id) {
            return todoItemRepository.findById(id);
        }
    
        @Tool(description = "Get all todo items")
        public List<TodoItem> getAllTodoItems() {
            return todoItemRepository.findAll();
        }
    
        @Tool(description = "Add a new todo item")
        public String addNewTodoItem(String description, String owner) {
            TodoItem item = new TodoItem(UUID.randomUUID().toString(), description, owner);
            todoItemRepository.save(item);
            return "Todo item created";
        }
    
        @Tool(description = "Update an existing todo item")
        public String updateTodoItem(String id, String description, String owner, boolean finished) {
            if (!todoItemRepository.existsById(id)) {
                return "Todo item not found";
            }
            TodoItem item = new TodoItem(id, description, owner);
            item.setFinish(finished);
            todoItemRepository.save(item);
            return "Todo item updated";
        }
    
        @Tool(description = "Delete a todo item by its id")
        public String deleteTodoItem(String id) {
            if (!todoItemRepository.existsById(id)) {
                return "Todo item not found";
            }
            todoItemRepository.deleteById(id);
            return "Todo item deleted";
        }
    }
    

    上述程式代碼會使用下列特定屬性,讓 Spring AI 提供工具:

    • @Service:標示 TodoListToolService 為 Spring 受控服務。
    • @Tool:將方法標示為 Spring AI 中的可呼叫工具。
    • description:這些會為每個工具提供人類可讀取的描述。 它可協助呼叫的代理程序瞭解如何使用此工具。

    此程式代碼會複製現有 src/main/java/com/microsoft/azure/appservice/examples/springbootmongodb/controller/TodoListController.java的功能,這是不必要的,但為了簡單起見,您會保留它。 最佳做法是將應用程式邏輯移至服務類別,然後從 TodoListControllerTodoListToolService呼叫服務方法。

  4. TodoApplication.java 中,將下列方法新增至 TodoApplication 類別。

    @Bean
    public ToolCallbackProvider todoTools(TodoListToolService todoListToolService) {
        return MethodToolCallbackProvider.builder().toolObjects(todoListToolService).build();
    }
    

    此方法將 TodoListToolService 中的工具作為 Spring AI 的回呼來提供。 根據預設,spring-ai-starter-mcp-server-webmvc 套件中的 MCP 伺服器自動設定會自動連接這些工具回呼。 此外,根據預設,MCP 伺服器端點為 <base-url>/sse

  5. TodoApplication.java頂端,新增下列匯入。

    import java.util.List;
    import java.util.Optional;
    import java.util.UUID;
    
    import org.springframework.ai.tool.ToolCallbackProvider;
    import org.springframework.ai.tool.annotation.Tool;
    import org.springframework.ai.tool.method.MethodToolCallbackProvider;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Service;
    
    import com.microsoft.azure.appservice.examples.springbootmongodb.dao.TodoItemRepository;
    import com.microsoft.azure.appservice.examples.springbootmongodb.model.TodoItem;
    

在本機測試 MCP 伺服器

  1. 在codespace終端機中,使用 mvn spring-boot:run執行應用程式。

  2. 選取 [在瀏覽器中開啟],然後新增工作。

    保持 Spring Boot 在執行中。 您的 MCP 伺服器端點目前正在 http://localhost:8080/sse 執行。

  3. 回到程式代碼空間,開啟 [Copilot Chat],然後在提示方塊中選取 [代理程式 模式]。

  4. 選取 [ 工具] 按鈕,然後在下拉式清單中選取 [ 新增更多工具... ]。

    顯示如何在 GitHub Copilot 聊天代理程式模式中新增 MCP 伺服器的螢幕快照。

  5. 選取 [新增 MCP 伺服器]。

  6. 選取HTTP(HTTP 或 Server-Sent 事件)

  7. [輸入伺服器 URL] 中,輸入 http://localhost:8080/sse

  8. [輸入伺服器標識符] 中,輸入 todos-mcp 或任何您想要的名稱。

  9. 選取 [工作區設定]。

  10. 在新的 [Copilot Chat] 視窗中,輸入類似 「顯示待辦事項」 的指令。

  11. 根據預設,當您叫用 MCP 伺服器時,GitHub Copilot 會顯示安全性確認。 選取繼續

    此螢幕快照顯示 GitHub Copilot Chat 中 MCP 調用的預設安全性訊息。

    您現在應該會看到指出 MCP 工具呼叫成功的回應。

    顯示 GitHub Copilot Chat 視窗中 MCP 工具呼叫回應的螢幕快照。

將您的 MCP 伺服器部署至 App Service

  1. 回到 Codespace 終端機,藉由提交變更(GitHub Actions 方法)或執行 azd up (Azure Developer CLI 方法)來部署變更。

  2. 在 AZD 輸出中,尋找應用程式的 URL。 AZD 輸出中 URL 看起來像這樣:

     Deploying services (azd deploy)
    
       (✓) Done: Deploying service web
       - Endpoint: <app-url>
     
  3. 完成後,azd up開啟.vscode/mcp.json。 將 URL 變更為 <app-url>/sse

  4. 在修改過的 MCP 伺服器組態上方,選取 [ 啟動]。

    顯示如何從本機 mcp.json 檔案手動啟動 MCP 伺服器的螢幕快照。

  5. 啟動新的 GitHub Copilot 聊天視窗。 您應該能夠在 Copilot 代理程式中檢視、建立、更新和刪除工作。

安全性最佳做法

當 MCP 伺服器由由大型語言模型 (LLM) 支援的代理程式呼叫時,請注意 提示插入 式攻擊。 請考慮下列安全性最佳做法:

  • 認證與授權:以 Microsoft Entra 認證保護你的 MCP 伺服器,確保只有授權使用者或代理能存取你的工具。 請參閱透過 Microsoft Entra 驗證,從 Visual Studio Code 向 Azure App Service 發出安全的模型內容通訊協定呼叫 (機器翻譯)
  • 輸入驗證和清理:本教學課程中的範例程式代碼會省略輸入驗證和清理,以求簡單明瞭。 在生產案例中,請一律實作適當的驗證和清理,以保護您的應用程式。 針對 Spring,請參閱 Spring:驗證表單輸入
  • HTTPS: 此範例依賴 Azure App Service,其預設會強制執行 HTTPS,並提供免費的 TLS/SSL 憑證來加密傳輸中的數據。
  • 最低許可權原則:只公開使用案例所需的工具和數據。 除非必要,否則請避免公開敏感性作業。
  • 速率限制和節流:使用 API 管理 或自定義中間件來防止濫用和阻斷服務攻擊。
  • 記錄和監視:記錄存取和使用MCP端點以進行稽核和異常偵測。 監視可疑活動。
  • CORS 設定:如果您的 MCP 伺服器是從瀏覽器存取,請將跨原始來源要求限制為受信任的網域。 如需詳細資訊,請參閱 啟用CORS
  • 一般更新:讓您的相依性保持在最新狀態,以減輕已知的弱點。

更多資源