返回市场
MCP代理网格服务器

MCP代理网格服务器

作者:vishalmysore5 星标更新:2025-06-07

项目介绍

多服务器MCP和A2A实现

本项目展示了如何在4个不同的服务器上使用代理网格来实现MCP和A2A协议。每个服务器负责特定领域,并可以独立查询或与其他服务器组合查询。 它采用多服务器架构,使用模型上下文协议(MCP)和代理到代理(A2A)通信协议。设计用于处理各种领域,如甜品、健身房、餐厅和瑜伽服务。

实现了四个独立的服务器:

  1. 甜品服务器(端口7863)
  2. 健身房服务器(端口7864)
  3. 餐厅服务器(端口7865)
  4. 瑜伽服务器(端口7866)

启动服务器

甜品服务器

要启动甜品服务器,请运行:

mvn spring-boot:run -Pdessert 

甜品服务器的应用属性:

spring.application.name=dessert-server
server.port=7863
a2a.persistence=cache
tools4ai.properties.path=tools4ai_dessert.properties

甜品服务器将在端口7863上启动:http://localhost:7863

健身房服务器

要启动健身房服务器,请运行:

mvn spring-boot:run -Pgym 

健身房服务器的应用属性:

spring.application.name=gym-server
server.port=7864
a2a.persistence=cache
tools4ai.properties.path=tools4ai_gym.properties

健身房服务器将在端口7864上启动:http://localhost:7864

餐厅服务器

要启动餐厅服务器,请运行:

mvn spring-boot:run -Prestaurant

餐厅服务器的应用属性:

spring.application.name=restaurant-server
server.port=7865
a2a.persistence=cache
tools4ai.properties.path=tools4ai_restaurant.properties

餐厅服务器将在端口7865上启动:http://localhost:7865

瑜伽服务器

要启动瑜伽服务器,请运行:

mvn spring-boot:run -Pyoga 

瑜伽服务器的应用属性:

spring.application.name=yoga-server
server.port=7866
a2a.persistence=cache
tools4ai.properties.path=tools4ai_yoga.properties

瑜伽服务器将在端口7866上启动:http://localhost:7866

每个服务器都有自己的tools4ai属性文件,其中包含一个单独的动作包扫描路径:

# 甜品服务器
action.packages.to.scan=org.example.desert

# 健身房服务器
action.packages.to.scan=org.example.gym

# 餐厅服务器
action.packages.to.scan=org.example.restaurant

# 瑜伽服务器
action.packages.to.scan=org.example.yoga

MCP服务器和客户端示例

该服务器设计为同时作为A2A(代理到代理)和MCP(模型上下文协议)服务器工作:

  • A2A:Google的代理到代理协议,用于AI代理之间的通信
  • MCP:模型上下文协议,用于与AI模型和工具交互

Claude桌面集成 Claude配置 Claude桌面配置

使用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/

对于瑜伽服务器(端口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配置文件来配置Claude桌面客户端:

C:\Users\<yourusername>\AppData\Roaming\Claude\claude_desktop_config.json

将任意服务器添加到您的配置中。例如,添加瑜伽服务器:

{
"mcpServers": {
"yogaserver": {
"command": "java",
"args": [
"-jar",
"PATH_TO_YOUR_JAR/mcp-connector-full.jar",
"http://localhost:7866"
],
"timeout": 30000
}
}
}

您可以从这里下载mcp-connector-full.jar。 这是一个必需组件,使客户端和服务器之间能够进行MCP协议的通信。对于A2A则不需要。

Mesh客户端

AgentCatalog是一个类,允许您连接到多个服务器并在它们之间处理查询。下面的示例演示了如何使用AgentCatalog查询与甜品相关的信息。 您可以将其与AgenticMesh结合使用,这将允许您连接到多个服务器并在它们之间以流水线或并行的方式处理查询。

@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);
    }
}