多服务器MCP和A2A的实现
此项目实现了两个单独的服务器:
- 电影服务器(端口7861)
- 库服务器(端口7862)
启动服务器
电影服务器
要启动电影服务器,请运行:
mvn spring-boot:run -Pmovies 电影服务器将在端口7861上启动:http://localhost:7861
注意:使用main类指定配置文件很重要,因为项目中有多个Spring Boot应用程序。
Movie服务器从其自己的应用程序属性开始
@SpringBootApplication
@EnableAgent
@PropertySource("classpath:application-movies.properties")
public class MoviesServer {
public static void main(String[] args) {
SpringApplication.run(MoviesServer.class, args);
}
}
此应用程序属性有自己的tools4ai属性文件,用于配置Movies服务器的操作。
tools4ai.properties.path=tools4ai_movies.properties这 tools4ai_movies.properties 文件包含特定于电影服务器的操作配置,例如:
action.packages.to.scan=example.movies.server以及中定义的所有操作 example.movies.server 包裹将被扫描并注册为工具。
package example.movies.server;
import com.t4a.annotations.Action;
import com.t4a.annotations.Agent;
import com.t4a.detect.ActionCallback;
import com.t4a.processor.AIProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Agent(groupName = "movieOperations", groupDescription = "Manage Bollywood movie operations like getting movie lists and famous dialogues")
@Slf4j
public class MoviesService {
private ActionCallback callback;
private AIProcessor processor;
public MoviesService() {
log.info("Created Movies Service");
}
@Action(description = "Get a list of popular Bollywood movies")
public String getBollywoodMovies() {
log.info("Fetching Bollywood movies list");
return "Popular Bollywood Movies: DDLJ, Sholay, Mother India, Mughal-e-Azam, PK";
}
@Action(description = "Get movies starring Shah Rukh Khan")
public String getSRKMovies() {
log.info("Fetching SRK movies");
return "SRK Movies: DDLJ, Chennai Express, Pathaan, Chak De India, My Name Is Khan";
}
@Action(description = "Get movies starring Amitabh Bachchan")
public String getAmitabhMovies() {
log.info("Fetching Amitabh Bachchan movies");
return "Amitabh Movies: Sholay, Deewar, Zanjeer, Don, Agneepath";
}
@Action(description = "Check if a given dialog is from a movie")
public String checkDialog(String dialog) {
log.info("Checking dialog: '{}'", dialog);
if (dialog.contains("Mogambo")) {
return "This is from Mr. India";
} else if (dialog.contains("Kitne aadmi the")) {
return "This is from Sholay";
} else {
return "Dialog not recognized";
}
}
@Action(description = "Get famous dialogues by Gabbar Singh")
public String gabbarSpeaks() {
log.info("Getting Gabbar's dialogues");
return "Famous Gabbar dialogues: 'Kitne aadmi the?', 'Jo dar gaya samjho mar gaya', 'Holi kab hai? Kab hai Holi?'";
}
}
库服务器
要启动库服务器,请运行:
mvn spring-boot:run -Plibrary 您还可以使用应用程序属性提供工具属性文件
spring.application.name=spring-boot
server.port=7862
a2a.persistence=cache
tools4ai.properties.path=tools4ai_library.properties如您所见,这两个服务器都有各自的tools4ai属性文件 每个文件都有一个单独的action.packages.to.scan。 因此,电影服务器从电影包中添加动作,库服务器从库包中添加操作。
action.packages.to.scan=example.movies.server库服务器将在端口7862上启动:http://localhost:7862
Movies Server Library Server Library Server
MCP服务器和客户端示例
该服务器设计为同时作为A2A(代理到代理)和MCP(模型上下文协议)服务器:
- A2A:AI代理间通信的Google代理对代理协议
- MCP:用于与AI模型和工具交互的模型上下文协议
服务器可以使用任一协议连接到任何客户端。此外,出于测试目的,此存储库中还包含了这两种协议的Java客户端实现。
Java客户端
中提供了两个Java客户端实现 src/main/java/org/example/client 包裹:
A2AClient.java-谷歌用于AI代理通信的代理到代理协议实现MCPClient.java-AI模型交互的模型上下文协议实现
您可以根据协议需求使用任一客户端。
使用curl命令进行测试
您可以使用curl命令来测试这两台服务器。每台服务器都有自己的一组服务,并在不同的端口上运行:
- 电影服务器(端口7861):通过以下方式提供与电影相关的操作
MoviesService - 库服务器(端口7862):通过以下方式提供与库相关的操作
LibraryService
获取工具列表
对于电影服务器(端口7861):
curl -H "Content-Type: application/json" `
-d '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":9}' `
http://localhost:7861/对于库服务器(端口7862):
curl -H "Content-Type: application/json" `
-d '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":9}' `
http://localhost:7862/调用工具示例
示例:调用电影相关工具(端口7861):
curl -H "Content-Type: application/json" `
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "rentMovie",
"arguments": {
"provideAllValuesInPlainEnglish": {
"name": "The Dark Knight",
"user": "John Doe"
}
}
},
"id": 25
}' `
http://localhost:7861/示例:调用与库相关的工具(端口7862):
curl -H "Content-Type: application/json" `
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "holdBook",
"arguments": {
"provideAllValuesInPlainEnglish": {
"name": "harry potter and the prisoner of azkaban",
"user": "Vishal Mysore"
}
}
},
"id": 25
}' `
http://localhost:7862/注意:确保您为要与之交互的服务器使用了正确的端口号。Movies服务器在端口7861上运行,并通过MoviesService提供与电影相关的操作,而Library服务器在端口7822上运行,通过LibraryService提供与库相关的操作。
对于a2a
curl-uhttp://localhost:7861/.well-已知/agent.json
连接到克劳德桌面
配置Claude桌面客户端 找到您的Claude配置文件:
C:\Users\\AppData\Roaming\Claude\claude_desktop_config.json将以下服务器配置添加到文件中:
"mycustomserver": {
"command": "java",
"args": [
"-jar",
"PATH_TO_YOUR_JAR/mcp-connector-full.jar",
"http://localhost:7861"
],
"timeout": 30000
}
您可以下载 mcp-connector-full.jar 来自此存储库的发布部分。 这里 下载MCP连接器 下载MCP连接器JAR。这是一个强制性组件,可实现MCP协议客户端和服务器之间的通信,而A2A则不需要。
如果上面的文件是空的,那么您可以通过这种方式添加
{
"mcpServers": {
"mycustomserver": {
"command": "java",
"args": [
"-jar",
"PATH_TO_YOUR_JAR/mcp-connector-full.jar",
"http://localhost:7862"
],
"timeout": 30000
}
}
}