將AI集成到系統(tǒng)和開發(fā)工具中變得越來越普遍,其中AI Agent為用戶交互帶來了一個(gè)全新模式。例如在Amazon Q Developer等代碼輔助工具中,用戶只需給AI分配任務(wù),它就能自主規(guī)劃并完成操作。接下來將由站長(zhǎng)百科詳細(xì)為大家介紹在亞馬遜云科技上集成MCP Server與Agent的全流程。
一、MCP與Agent集成原理
MCP(Model Context Protocol)提供了Agent連接外部服務(wù)的標(biāo)準(zhǔn)方法,例如產(chǎn)品數(shù)據(jù)庫、CRM系統(tǒng)等。要實(shí)現(xiàn)連接,Agent需作為MCP Client,通過配置或代碼獲取與之交互的MCP Server列表。
MCP規(guī)范定義了兩種基礎(chǔ)傳輸方式:
1、STDIO傳輸:適用于本地進(jìn)程分叉場(chǎng)景,目前多數(shù)AI代碼輔助工具中的MCP Server采用此方式,可直接訪問開發(fā)者本地資源。
2、Server-Sent Events(SSE)傳輸:適用于網(wǎng)絡(luò)服務(wù)器場(chǎng)景,支持客戶端與服務(wù)器的狀態(tài)更新(如工具列表變化時(shí)主動(dòng)通知)。但由于有狀態(tài)協(xié)議在Serverless架構(gòu)中兼容性較差,2025年3月MCP規(guī)范更新為更簡(jiǎn)單的Streamable HTTP協(xié)議(當(dāng)前官方SDK暫未支持,后續(xù)將跟進(jìn)介紹)。
二、在亞馬遜云科技上部署MCP Server
點(diǎn)擊注冊(cè)亞馬遜云科技立享100+免費(fèi)云服務(wù)>>>
亞馬遜云科技支持通過亞馬遜云服務(wù)器、EKS、ECS等服務(wù)運(yùn)行基于SSE的MCP Server。以下以Java + Spring AI為例,演示如何實(shí)現(xiàn)一個(gè)預(yù)約領(lǐng)養(yǎng)狗狗的MCP Server(代碼來自AWS官方):
網(wǎng)絡(luò)架構(gòu)如下:
1、核心代碼實(shí)現(xiàn)
@SpringBootApplication
publicclassSchedulingApplication {publicstaticvoidmain(String[] args){
SpringApplication.run(SchedulingApplication.class, args);
}@Bean
ToolCallbackProvider serviceToolCallbackProvider(
DogAdoptionAppointmentScheduler scheduler) {
return MethodToolCallbackProvider.builder()
.toolObjects(scheduler)
.build();
}}
@Service
classDogAdoptionAppointmentScheduler {@Tool(description = “schedule an appointment to adopt a dog” +
” at the Pooch Palace dog adoption agency”)
String scheduleDogAdoptionAppointment(
@ToolParam(description = “the id of the dog”) int id,
@ToolParam(description = “the name of the dog”) String name) {
// demo scheduling response (but could use backing data / services on AWS)
var instant = Instant.now().plus(3, ChronoUnit.DAYS);
System.out.println(“confirming the appointment: ” + instant + ” for dog ” + id + ” named ” + name);
return instant.toString();
}
}
2、Maven依賴配置
<!– the BOM for Spring AI –>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-M6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!– The Spring Webflux MCP Server –>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-server-webflux-spring-boot-starter</artifactId>
</dependency>
</dependencies>
3、部署與安全配置
MCP Server可通過安全組限制訪問,僅允許VPC內(nèi)的MCP Client通信。如需公開訪問,可結(jié)合AWS API Gateway等服務(wù)配置身份驗(yàn)證。
三、集成Agent與MCP Client
完成MCP Server部署后,可通過以下兩種方式在亞馬遜云科技上運(yùn)行Agent:
1、使用Amazon Bedrock內(nèi)置Agent:直接對(duì)接MCP Server。
2、基于Spring AI構(gòu)建自定義Agent:通過REST API暴露服務(wù),以下為實(shí)現(xiàn)示例:
1、依賴配置(Maven)
<!– the BOM for Spring AI –>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0-M6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!– enable a REST server –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!– MCP Client –>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
</dependency>
<!– Amazon Bedrock Converse support in Spring AI –>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock-converse-spring-boot-starter</artifactId>
</dependency>
<!– for an automatic health check URL –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
2、配置文件(application.properties)
spring.ai.bedrock.converse.chat.options.model=amazon.nova-pro-v1:0
spring.ai.bedrock.converse.chat.enabled=true
3、REST控制器實(shí)現(xiàn)
@RestController
classConversationalController {privatefinal ChatClient chatClient;
ConversationalController(
@Value(“${scheduling-service.url}”) String schedulingUrl,
ChatClient.Builder builder) {var mcpClient = McpClient
.sync(new HttpClientSseClientTransport(url))
.build();mcpClient.initialize();
this.chatClient = builder
.defaultTools(new SyncMcpToolCallbackProvider(mcpClient))
.build();
}@PostMapping(“/inquire”)
String inquire(@RequestParam String question){
return chatClient
.prompt()
.user(question)
.call()
.content();
}
}
四、實(shí)戰(zhàn)測(cè)試
將Agent服務(wù)部署到Amazon ECS后,可通過以下命令測(cè)試:
curl -X POST –location “http://myecsserver/inquire” \
-H “Content-Type: application/x-www-form-urlencoded” \
-d ‘question=when can i schedule to adopt Prancer’
此時(shí),Agent會(huì)通過MCP Client調(diào)用Server中的預(yù)約工具,返回如“3天后可預(yù)約領(lǐng)養(yǎng)Prancer”的結(jié)果。
相關(guān)閱讀:
《如何使用MCP Typescript SDK構(gòu)建MCP客戶端》
《Amazon Bedrock+DeepSeek搭建企業(yè)知識(shí)庫圖文教程》
-
廣告合作
-
QQ群號(hào):4114653