多服务器MCP和A2A的实现
该项目演示了如何在4台不同的服务器上使用代理Mesh来实现MCP和A2A协议。每个服务器负责一个特定的域,可以独立查询或与其他服务器组合查询。 它具有使用模型上下文协议(MCP)和代理到代理(A2A)通信协议的多服务器架构。它旨在处理各种领域,如甜点、健身房、餐厅和瑜伽服务。
实现四个独立的服务器:
- 甜品服务器(端口7863)
- 健身房服务器(端口7864)
- 餐厅服务器(端口7865)
- 瑜伽服务器(端口7866)
启动服务器
甜品服务器
要启动Dessert服务器,请运行:
mvn spring-boot:run -Pdessert Dessert服务器的应用程序属性:
spring.application.name=dessert-server
server.port=7863
a2a.persistence=cache
tools4ai.properties.path=tools4ai_dessert.propertiesDessert服务器将在端口7863上启动:http://localhost:7863
健身房服务器
要启动Gym服务器,请运行:
mvn spring-boot:run -Pgym Gym服务器的应用程序属性:
spring.application.name=gym-server
server.port=7864
a2a.persistence=cache
tools4ai.properties.path=tools4ai_gym.propertiesGym服务器将在端口7864上启动:http://localhost:7864
餐厅服务员
要启动Restaurant服务器,请运行:
mvn spring-boot:run -PrestaurantRestaurant服务器的应用程序属性:
spring.application.name=restaurant-server
server.port=7865
a2a.persistence=cache
tools4ai.properties.path=tools4ai_restaurant.propertiesRestaurant服务器将在端口7865上启动:http://localhost:7865
瑜伽服务器
要启动Yoga服务器,请运行:
mvn spring-boot:run -Pyoga Yoga服务器的应用程序属性:
spring.application.name=yoga-server
server.port=7866
a2a.persistence=cache
tools4ai.properties.path=tools4ai_yoga.propertiesYoga服务器将在端口7866上启动:http://localhost:7866
每台服务器都有自己的tools4ai属性文件,其中包含一个单独的action.packages.to.scan:
# Dessert Server
action.packages.to.scan=org.example.desert
# Gym Server
action.packages.to.scan=org.example.gym
# Restaurant Server
action.packages.to.scan=org.example.restaurant
# Yoga Server
action.packages.to.scan=org.example.yogaMCP服务器和客户端示例
该服务器设计为同时作为A2A(代理到代理)和MCP(模型上下文协议)服务器:
- A2A:AI代理间通信的Google代理对代理协议
- MCP:用于与AI模型和工具交互的模型上下文协议
Claude Desktop Integration Claude Configuration Claude Desktop Configuration
使用curl命令进行测试
您可以使用curl命令测试所有服务器。每台服务器都有自己的一组服务,并在不同的端口上运行:
- 甜品服务器(端口7863):通过以下方式提供与甜品相关的操作
DessertService - 健身房服务器(端口7864):通过以下方式提供与健身房相关的操作
GymService - 餐厅服务器(端口7865):通过以下方式提供与餐厅相关的操作
RestaurantService - 瑜伽服务器(端口7866):通过以下方式提供瑜伽相关动作
YogaService
获取工具列表
对于甜品服务器(端口7863):
curl -H "Content-Type: application/json" `
-d '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":9}' `
http://localhost:7863/对于健身房服务器(端口7864):
curl -H "Content-Type: application/json" `
-d '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":9}' `
http://localhost:7864/对于餐厅服务器(端口7865):
curl -H "Content-Type: application/json" `
-d '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":9}' `
http://localhost:7865/对于Yoga服务器(端口7866):
curl -H "Content-Type: application/json" `
-d '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":9}' `
http://localhost:7866/调用工具示例
示例:调用与甜点相关的工具(端口7863):
curl -H "Content-Type: application/json" `
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "findIndianSweets",
"arguments": {
"provideAllValuesInPlainEnglish": {}
}
},
"id": 25
}' `
http://localhost:7863/示例:调用瑜伽相关工具(端口7866):
curl -H "Content-Type: application/json" `
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "getBeginnerPoses",
"arguments": {
"provideAllValuesInPlainEnglish": {}
}
},
"id": 25
}' `
http://localhost:7866/注意:确保您为要与之交互的服务器使用了正确的端口号。
连接到克劳德桌面
通过查找您的Claude配置文件来配置Claude桌面客户端:
C:\Users\\AppData\Roaming\Claude\claude_desktop_config.json将任何服务器添加到您的配置中。例如,要添加Yoga服务器:
{
"mcpServers": {
"yogaserver": {
"command": "java",
"args": [
"-jar",
"PATH_TO_YOUR_JAR/mcp-connector-full.jar",
"http://localhost:7866"
],
"timeout": 30000
}
}
}对于您需要执行此操作的所有4台服务器
"yogaserver": {
"command": "java",
"args": [
"-jar",
"/work/a2a-mcp-bridge/target/mcp-connector-full.jar",
"http://localhost:7864"
],
"timeout": 30000
}
,
"RestaurantServer": {
"command": "java",
"args": [
"-jar",
"/work/a2a-mcp-bridge/target/mcp-connector-full.jar",
"http://localhost:7865"
],
"timeout": 30000
}
,
"GymServer": {
"command": "java",
"args": [
"-jar",
"/work/a2a-mcp-bridge/target/mcp-connector-full.jar",
"http://localhost:7862"
],
"timeout": 30000
}
,
"DessertServer": {
"command": "java",
"args": [
"-jar",
"/work/a2a-mcp-bridge/target/mcp-connector-full.jar",
"http://localhost:7863"
],
"timeout": 30000
}您可以下载 mcp-connector-full.jar 从 这里. 这是一个强制性组件,它使您的客户端和服务器之间能够进行MCP协议的通信。对于A2A,这不是必需的。
Mesh客户端
AgentCatalog是一个类,它允许您连接到多个服务器并跨它们处理查询。下面的示例演示了如何使用AgentCatalog查询与甜点相关的信息。 您可以将其与AgentMesh结合使用,这将允许您连接到多个服务器,并以管道或并行方式处理它们之间的查询。
@Log
public class MeshClient {
public static void main(String[] args) {
AgentCatalog agentCatalog = new AgentCatalog();
agentCatalog.addAgent("http://localhost:7862/");
agentCatalog.addAgent("http://localhost:7863/");
String answer = agentCatalog.processQuery("what is the receipe for Gulab Jamun").getTextResult();
log.info("Answer: " + answer);
}
}